
function submitVoteBackground(roundId) {
	var ajaxReq = new Ajax.Request("/gnargles/submit", {
		method: 'post',
		parameters: {roundid: roundId, no:1},
		onSuccess: function(transport){
			//refresha sidan, gå till ny/samma video...
			reloadPage();
		},
		onFailure: function(){  
		}
	});
}

function submitVote(VoteType, roundId) {
	
	if(VoteType == "approval") {
		if(checkHasPassedOne()) {
			var ajaxReq = new Ajax.Request("/gnargles/submit", {
				method: 'post',
				parameters: {roundid: roundId},
				onSuccess: function(transport){
					Dialogs.alert('Your vote has been submitted! You can change your vote by submitting again.');
					var response = transport.responseText.evalJSON();
					
					if(response.msg[0] != ""){
						FB.ensureInit(function() {
						     FB.Connect.showFeedDialog(response.msg[0], response.msg[1].evalJSON(), null, null, null, FB.RequireConnect.promptConnect, null, "Comment (not required)");
						});
					}
				},
				onFailure: function(){ 
					Dialogs.alert("Your vote could not be registered."); 
				}
			});
		}
		else {
			Dialogs.alert("You must select at least one <br/>skater to qualify from this round.");
		}
	}
	else if(VoteType == "majority") {
		if(checkHasWinner()) {
			var ajaxReq = new Ajax.Request("/gnargles/submit", {
				method: 'post',
				parameters: {roundid: roundId},
				onSuccess: function(transport){
					Dialogs.alert('Your vote has been submitted! You can change your vote by submitting again.');
					var response = transport.responseText.evalJSON();
					if(response.msg[0] != ""){
						FB.ensureInit(function() {
						     FB.Connect.showFeedDialog(response.msg[0], response.msg[1].evalJSON(), null, null, null, FB.RequireConnect.promptConnect, null, "Comment (not required)");
						});
					}
				},
				onFailure: function(){ 
					Dialogs.alert("Your vote could not be registered."); 
				}
			});
		}
		else {
			Dialogs.alert("You must select a winner.");
		}
	}
}

function checkHasWinner() {
	return ($$("#visitedContainer div.winBox").length == 1);
}

function checkHasPassedOne() {
	return ($$("#visitedContainer div.inBox").length > 0);
}

function checkAllVoted() {
	return ($$("#visitedContainer div.questionmarkBox").length == 0);
}

function enableApprovalVoting(voteType, roundId) {
	var newSubmitButton = 	'<a href="#"><img onclick="submitVote(\'' + voteType + '\', ' + roundId + '); return false;" style="margin-left:11px;margin-top:0px" src="/public/images/gnargle/button_submit_vote_long.jpg" /></a>';
	$('submit_vote_button').update(newSubmitButton);
}

function nextVideo() {
	if(allowed) {
		allowed = false;
		$('videoCountDown').show();
		abort = false;
		secondsLeft = 1;
		countDown();
	}
}

function countDown() {
	$('counterDigit').update(secondsLeft--);
	if(secondsLeft >= 0) {
		if(!abort) {
			setTimeout("countDown()", 1000);
		}
		else{
			$('videoCountDown').hide();
			allowed = true;
		}
	}
	else
		loadVideo();
}

function loadVideo() {
	window.location=url;
}

function abortCountDown() {
	abort = true;
	$('videoCountDown').hide();
}

function updateEntryList(voteType, roundId, updateDiv){
	var ajaxReq = new Ajax.Request("/gnargles/listentries", {
		method: 'post',
		parameters: {roundid: roundId},
		onSuccess: function(transport){
			//uppdatera lsitan med vote ikoner
			$(updateDiv).update(transport.responseText);
			
			
			//kolla om man röstat på alla bidrag
			if(checkAllVoted()) {
				//update the submit button
				var newSubmitButton = 	'<a href="#">' +
										'<img onclick="submitVote(\'' + voteType + '\', ' + roundId + '); return false;" style="margin-left:11px;margin-top:0px" src="/public/images/gnargle/button_submit_vote_long.jpg" />' +
										'</a>';
				$('submit_vote_button').update(newSubmitButton);
				
				//submit vote silently
				submitVoteBackground(roundId);
			}
			else {
				//refresha sidan, gå till ny/samma video...
				reloadPage();
			}
			
			
			/*
			//uppdatera submit knappen
			if(voteType == "majority") {
				checkAllVoted(voteType, roundId);
			}
			else if(voteType == "approval") {
				// 	enableAuditionVoting(voteType, roundId); 
				checkAllVoted();
			}
			*/
			
		},
		onFailure: function(){ 
			Dialogs.alert("Could not register the vote"); 
		}
	});
}

function reloadPage() {
	//om man just röstade på sista bidraget -> popup på nästa sida
	if(leftToVoteOn == 1 && $$("#visitedContainer div.questionmarkBox").length == 0) {
		window.location = thisurl + '?done=1#vote';
	}
	//ladda ny video om det finns en ledig
	else if($$("#visitedContainer div.questionmarkBox").length != 0) {
		nextVideo();
	}
	else {
		window.location = thisurl + '?finished=1#vote';
	}
}

function removeVote(voteType, roundId, voteId) {

	//remove the vote
       var url = '/gnargles/removevote';
       var ajax = new Ajax.Request(url,{
           method: 'post',
           parameters: {voteId: voteId},
          	onSuccess: function(transport){
	          	//uppdatera listan med bidrag
	        	updateEntryList(voteType, roundId, 'gnargleEntries');
          	},
		onFailure: function(){ 
			alert("Could not update your list."); 
		}
       });
}

function castVote(voteType, roundId, voteId) {

	var url = '/gnargles/addvote';
	var votearray = '';
	var ajax = new Ajax.Request(url,{
           method: 'post',
           parameters: {voteId: voteId},
          	onSuccess: function(transport){
	          	//uppdatera listan med bidrag
	        	updateEntryList(voteType, roundId, 'gnargleEntries');
          	},
		onFailure: function(){ 
			alert("Could not update your list."); 
		}
       });
} 
function countDownStart(){
	timeLeftSeconds = timeLeftSeconds - 1;
	var days = Math.floor(timeLeftSeconds / (60 * 60 * 24));
	var hours = Math.floor(timeLeftSeconds / (60 * 60)) % 24;
	var minutes = Math.floor(timeLeftSeconds / (60)) % 60;
	var seconds = Math.floor(timeLeftSeconds) % 60;

	//$('datecounter').update(finishDate.getTime() +":"+ currentDate.getTime());
	if(timeLeftSeconds > 0){
		$('datecounter').update(days +'<span class="gnargleCountdownDidgit">d</span> '+ hours +'<span class="gnargleCountdownDidgit">h</span> '+ minutes +'<span class="gnargleCountdownDidgit">m</span> '+ seconds +'<span class="gnargleCountdownDidgit">s</span> ');
	}else{
		$('datecounter').update('Finished! Reload the page.');
		window.location.reload();
	}
	setTimeout('countDownStart()', 1000);
}