var VoteMatch = Class.create();
VoteMatch.prototype = {

	initialize: function(element) {
		this.element = $(element);
		this.options = {
			AjaxUrl: "/resource/application/service/vote-match.php"
		};
		
		this.loadNextMatch();
	},
	
	loadNextMatch: function() {
		var output = new Ajax.Request(this.options.AjaxUrl, {
			parameters: { 
				Action: "GetLatestMatchInformation"
			},
			method: "Get",
			asynchronous: false,
			onSuccess: (function(transport) {
				var response = transport.responseText.evalJSON();
				this.addMatchToDom(response);
  		}).bind(this)
		});
	},
	
	addMatchToDom: function(response) {
		
		//Add ref name
		var h3 = document.createElement("h3");
		h3.innerHTML = "<a href=\"#\">" + response.ref.FirstName.Formatted + " " + response.ref.LastName.Formatted + "<\/a>";
		this.element.appendChild(h3);
		
		//Add Strap line
		var p = document.createElement("p");
		p.className = "rate-now";
		p.innerHTML = "Give '" + response.ref.FirstName.Formatted + "' a rating of 1 to 5:";
		this.element.appendChild(p);
		
		//Add Strap line
		var p = document.createElement("p");
		p.className = "star";
		p.innerHTML = "<span>RATE NOW!:</span>";
		this.element.appendChild(p);
		
		//Add Voting stars		
		var mainVoteDiv = document.createElement("div");
		mainVoteDiv.id = "main-vote";
		this.element.appendChild(mainVoteDiv);
		var starRatingUl = document.createElement("ul");
		starRatingUl.className = "star-rating";
		[1,2,3,4,5].each((function(rating) {
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.className="star-" + rating;
			a.innerHTML = "Rate this ref " + rating;
			a.href = "#";
			Element.extend(a);
			a.observe("click", (function(event) {
				this.vote(response.match.Id.Data, rating);
				return false;
			}).bind(this));
			li.appendChild(a);
			starRatingUl.appendChild(li);	
		}).bind(this));
		mainVoteDiv.appendChild(starRatingUl);
		this.element.appendChild(mainVoteDiv);
		
		//Show Date
		var p = document.createElement("p");
		p.className = "date";
		p.innerHTML = response.match.MatchDate.Formatted.split(" - ", 1);
		this.element.appendChild(p);
		
		//Show Time
		var p = document.createElement("p");
		p.className = "time";
		dateArray = response.match.MatchDate.Formatted.split(" - ");
		p.innerHTML = dateArray[1];
		this.element.appendChild(p);

		//Show home team
		var homeTeamDiv = document.createElement("div");
		homeTeamDiv.className = "team"
		var a = document.createElement("a");
		Element.extend(a);
		a.observe("click", function() {
			window.location = "/match/view.php?MatchId=" + response.match.Id.Formatted;
		});
		a.title = "Check out the " + response.match.HomeTeam.Formatted;
		var img = document.createElement("img");
		img.title = response.match.HomeTeam.Formatted;
		img.alt = response.match.HomeTeam.Formatted;
		img.src = "/resource/image/flags/" + response.match.HomeTeam.Formatted.replace(" ", "-").replace(" ", "-") + ".png";
		img.height = 74;
		img.width = 68;
		a.appendChild(img);
		var span = document.createElement("span");
		span.innerHTML = response.match.HomeTeam.Formatted;
		a.appendChild(span);
		
		homeTeamDiv.appendChild(a);
		this.element.appendChild(homeTeamDiv);
		
		//add vs
		var p = document.createElement("p");
		p.className = "vs";
		p.innerHTML = "<span><strong>Vs.<\/strong><\/span>";
		this.element.appendChild(p);
		
		//Show Away team
		var awayTeamDiv = document.createElement("div");
		awayTeamDiv.className = "team"
		var a = document.createElement("a");
		Element.extend(a);
		a.observe("click", function() {
			window.location = "/match/view.php?MatchId=" + response.match.Id.Formatted;
		});
		a.title = "Check out the " + response.match.AwayTeam.Formatted;
		var img = document.createElement("img");
		img.title = response.match.AwayTeam.Formatted;
		img.alt = response.match.AwayTeam.Formatted;
		img.src = "/resource/image/flags/" + response.match.AwayTeam.Formatted.replace(" ", "-").replace(" ", "-") + ".png";
		img.height = 74;
		img.width = 68;
		a.appendChild(img);
		var span = document.createElement("span");
		span.innerHTML = response.match.AwayTeam.Formatted;
		a.appendChild(span);
		
		awayTeamDiv.appendChild(a);
		this.element.appendChild(awayTeamDiv);
		
		this.createTitles();
	},
	
	vote: function(matchId, rating) {
		var output = new Ajax.Request(this.options.AjaxUrl, {
			parameters: { 
				Action: "Vote",
				MatchId: matchId,
				Rating: rating
			},
			asynchronous: false,
			onSuccess: (function(transport) {
				var response = transport.responseText.evalJSON();
				if (response.success) {
					this.showMatchRefSummary(matchId, rating);
				} else {
					this.showInvalidVoteSummary(matchId, response.reasonCode, response.reason);
					//alert(response.reasonCode + ": " + response.reason);
				}
  		}).bind(this)
		});
	},
	
	showMatchRefSummary: function(matchId, rating) {
		var output = new Ajax.Request(this.options.AjaxUrl, {
			parameters: { 
				Action: "GetMatchRefSummary",
				MatchId: matchId,
				Rating: rating
			},
			asynchronous: false,
			onSuccess: (function(transport) {
				$("main-vote-popup").innerHTML = transport.responseText;
				$("main-vote-popup").show();
	 		}).bind(this)
		});
	},
	
	showInvalidVoteSummary: function(matchId, reasonCode, reason) {
		var output = new Ajax.Request(this.options.AjaxUrl, {
			parameters: { 
				Action: "ShowInvalidVote",
				MatchId: matchId,
				ReasonCode: reasonCode,
				Reason: reason
			},
			asynchronous: false,
			onSuccess: (function(transport) {
				$("main-vote-popup").innerHTML = transport.responseText;
				$("main-vote-popup").show();
	 		}).bind(this)
		});
	},
	
	createTitles: function() {
		sIFR.replaceElement(named({sSelector:"#main-content #current-match h3", sFlashSrc:"/resource/sifr/vagroundedbt.swf", sColor:"#474747", sLinkColor:"#474747", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"#main-content #current-match p.rate-now", sFlashSrc:"/resource/sifr/vagroundedbt.swf", sColor:"#ab1818", sLinkColor:"#474747", sWmode:"transparent", sHoverColor:"#cc0000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"#main-content #current-match p.date", sFlashSrc:"/resource/sifr/vagroundedbt.swf", sColor:"#474747", sLinkColor:"#474747", sWmode:"transparent", sHoverColor:"#cc0000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"#main-content #current-match p.time", sFlashSrc:"/resource/sifr/vagroundedbt.swf", sColor:"#474747", sLinkColor:"#474747", sWmode:"transparent", sHoverColor:"#cc0000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
	}
}