$(document).ready(function(){
/*
	function updateCounter(){
		$('#counter strong').load('/race/vtotal/');
		//window.setTimeout(updateCounter(), 3000);
	}
	updateCounter();
*/
	
	
	/** INITIAL STATE **/


	//Vote Counter on Homepage
	

	//Initialize the sign up form
	$('#signup input[name=email]').val('Enter your email').blur(function(){
		if( $(this).val() == '')
			$(this).val('Enter your email');
	}).click(function(){
		if( $(this).val() == 'Enter your email' )
			$(this).val('');
	});
	
	//Hover state for website images
	$('div.innerShadow').hover(function(){
		$(this).addClass('innerShadowHover');
	},function(){
		$(this).removeClass('innerShadowHover');
	});
	
	//Set inline background-position for left and right slide results div
	$('.slideLeftResults').css( { backgroundPosition:'0px 0px' } );
	$('.slideRightResults').css( { backgroundPosition:'0px 0px' } );
	
	//Enable state select box in header navigation
	$('select[name=state]').change(function(){
		if( $(this).val() != '' ){
			switch( $(this).val() ){
				case 'ALL':
					window.location = base_url + 'races';
					break;
				case 'PRESIDENTIAL':
					window.location = base_url + 'races/show/471';
					break;
				default:
					window.location = base_url + 'races/state/' + $(this).val();
			}
		}
	});
	
	//Enable external links to ensure validation
	$('a[rel=external]').attr('target','_blank');
	
	
	/** ENABLE VOTING **/
	
	//Callback function to display results of votes for current race
	function displayVoteResults( response, status ){
		
		//Get percentages of votes
		var candidate_a_votes = response.candidate_a;
		var candidate_b_votes = response.candidate_b;
		var total_votes = response.total;
		var candidate_a_percent = Math.floor( ( candidate_a_votes / total_votes ) * 100 );
		var candidate_b_percent = Math.ceil( ( candidate_b_votes / total_votes ) * 100 );
        
		//Update candidate slides
		$('a.checkbox img').remove();
		
		var candidate_a_name = $('.slideLeft a.checkbox').text();
		var candidate_b_name = $('.slideRight a.checkbox').text();
		
		$('a.checkbox').remove();
		
		//Is this a randomly selected race?
		var isRandom = false;
		if( $('ul#navigation li.first a').attr('href') == '/races/chance' )
			isRandom = true;
		
		//Create html for links
		var linkHtml;
		if( isRandom ){
			linkHtml = '<a href="/races/chance" title="Next Race">Next Race</a>';
		} else {
			var race_id = parseInt(response.race_id);
			var next_id;
			if( race_id == 471 ){
				next_id = 1;
			} else {
				next_id = race_id + 1;
			}
			linkHtml = '<a href="/races/show/' + next_id + '" title="Next Race">Next Race</a>';
		}
		
		$('.slideLeft').append('<h1>' + candidate_a_name + '</h1><p class="results">(' + candidate_a_percent + '% / ' + candidate_a_votes + ' votes ) ' + linkHtml + '</p>');
		$('.slideRight').append('<h1>' + candidate_b_name + '</h1><p class="results">(' + candidate_b_percent + '% / ' + candidate_b_votes + ' votes ) ' + linkHtml + '</p>');
		
		if(candidate_a_percent > candidate_b_percent){
			$('.slideLeftResults').css('backgroundImage','url(' + base_url + 'imgs/resultsWinner.gif)');
		}
		if(candidate_a_percent < candidate_b_percent){
			$('.slideRightResults').css('backgroundImage','url(' + base_url + 'imgs/resultsWinner.gif)');
		}
		
		$('.slideLeftResults').animate( { backgroundPosition:'(0 ' + candidate_a_percent + '%)' }, 'slow');
		$('.slideRightResults').animate( { backgroundPosition:'(0 ' + candidate_b_percent + '%)' }, 'slow');
	}
	
	//If Javascript is enabled, disable the hyperlinks for the Click to Vote links. Then enable hover states. Then set click handler.
	$('a.checkbox').attr('href','javascript:void(0);').hover(
		function(){
			$('img',this).attr('src',base_url + 'imgs/voteHover.png');
		},
		function(){
			$('img',this).attr('src',base_url + 'imgs/voteNormal.png');
		}
	).click(
		function(){
			
			var race_id = $(this).parent().parent().parent().attr('id');
			var candidate_uri = $(this).attr('rel');
			$.getJSON( base_url + 'races/vote/' + race_id + '/candidate/' + candidate_uri, {}, displayVoteResults );
			
		}
	);
	
});