$(document).ready(function(){
	has_voted = $.cookie("com.hyperhappen.holding.vote");
	$('#clock').countdown({
							until: new Date(2010, 6 -1, 1, 17, 0, 0),
							format: 'ODHMS',
							layout: '<li><div class="grad"><img src="img/grad.png"></div><div class="num" id="months">{onn}</div></li>'+
									'<li><div class="grad"><img src="img/grad.png"></div><div class="num" id="days">{dnn}</div></li>'+
									'<li><div class="grad"><img src="img/grad.png"></div><div class="num" id="hours">{hnn}</div></li>'+
									'<li><div class="grad"><img src="img/grad.png"></div><div class="num" id="mins">{mnn}</div></li>'+
									'<li><div class="grad"><img src="img/grad.png"></div><div class="num" id="secs">{snn}</div></li>'
							
									
							});
						
	if (has_voted) {
		$('.image span').html('YOU\'VE VOTED');
		$('#pleasevote').html('Thanks for voting. Come back to find out whether we manage to launch the site in time.');
	}
	
	$("a.vote").bind( 'click', function(event){
		vote_id = $(this).parent().parent().attr('id');
		if ( has_voted ) {
			$('.image span').html('YOU\'VE VOTED');
		} else {
			$('.image span').html('YOU\'VE VOTED');
			$('#' + vote_id + ' .image span').html('THANKS!');
			$('#pleasevote').html('Thanks for voting. Come back to find out whether we manage to launch the site in time.');
			
			
			$.get('/vote.php', { vote_for: vote_id }, function(data) {
				//$('#result').html(data);
				if (data) {
					var response = JSON.parse(data);
					if (response.msg == 'ok') {
						
						for(i in response.results) {
							key = response.results[i].key;
							$('#'+key+' .stat').html( response.results[i].percentage + '%' );
						}
						$.cookie("com.hyperhappen.holding.vote", vote_id,  { expires: 300 });
						$('#a.vote').unbind('click');
						has_voted = true;
						
					} else {
						alert('error parsing json');
					}
				} else {
					alert('error getting data');
				}
			});
		}
		event.preventDefault();
	});
});