$(document).ready(function(){	
	$(".video_title_link").click(function(){
		var clicked_url = $(this).attr("title");
		var clicked_photo = $(this).attr("alt");
		
		$("#video_list a").removeClass('selected');
		$(this).addClass('selected');
		
		$("#video_preview_photo").attr({
			href: clicked_url
		})
		$("#video_preview_photo img").attr({ 
          src: clicked_photo,
          title: clicked_url,
          alt: clicked_url
        });
		return false;
	});
	
	$("#cast_vote").click(function(){
		if($.cookie('machiavelli'+$('input[name=poll_id]').val()) != 'voted')
		{
			if($('input[name=answer_id]:checked').val() != undefined)
			{
				$.cookie('machiavelli'+$('input[name=poll_id]').val(), 'voted', { expires: 7 });
				
				$.post("/cast_vote.php?mode=ajax",{
					answer_id: $('input[name=answer_id]:checked').val(),
					poll_id: $('input[name=poll_id]').val()
					},
					function(percentages){
						for(var i in percentages)
						{
							$("#answer_id"+i+" .numbers").text(percentages[i] +"%");
							$("#answer_id"+i+" .votes").css({ width:  Math.round(percentages[i]) + "px"});
						}
					}, 'json');
			}
		}
		return false;
	});
	
	$("#vote").click(function(){
		if($.cookie('machiavelli'+$('input[name=poll_id]').val()) != 'voted')
			$.cookie('machiavelli'+$('input[name=poll_id]').val(), 'voted', { expires: 7 });
		//else
		//	alert($.cookie('machiavelli'));
		return true;
	});
	/* $("#cast_vote").ajaxStart(function(){
		
	}); */
	
	$("#cast_vote").ajaxStop(function(){
		$("#home_poll").hide();
		$("#home_poll_results").show();
		//alert($.cookie('machiavelli'));
	});
})