
function loadVotes(id) { // REFRESH THE VOTES FOR A PARTICULAR PERSON
	$.ajax({
		url: "/inc/ajax/mtv_fan_favorite_votes.php",
		data: { fanFavId: id },
		cache: false,
		type: 'POST',
		beforeSend: function(){
			$("#numvotes").html("<img src='/img/load_votes.gif'/>");
		},
		success: function(data) {
			$("#numvotes").html(data);
		}
	});
	
	// UPDATE THE VOTES FOR THE LEADER
	$.ajax({
		url: "/inc/ajax/mtv_fan_favorite_voting_leader.php",
		cache: false,
		dataType: 'script',
		type: 'POST'
	});
}

function loadFavorite(id) { //Refresh the profile of a given person
	$.ajax({
		url: "/inc/ajax/mtv_fan_favorite.php",
		data: { fanFavId: id },
		cache: false,
		dataType: 'script',
		type: 'POST',
		beforeSend: function(){
			$("#favArea .fanPhoto").replaceWith("<img src='/img/load_votes_white.gif' class='loader' />");
		}
	});
	
	// Calculates the number of votes for a given id
	loadVotes(id);
	
	// Assign the hidden value in the voting 'module' so when they vote, the appropriate person gets credited
	$('#votePopover input[name=cast_id]').val(id);
	
}

function flashEmail() {

	$("#votePopover input[name=email]").animate({
		opacity: 0.4
	}, 200, "swing", function() {
		$("#votePopover input[name=email]").animate({
		opacity: 1.0
		}, 200);
		
		$("#votePopover input[name=email]").addClass('error');
		
	});
	
}


$(document).ready(function() {


	//ADD EVENT LISTENER FOR LOADING PROFILES
	$('#fanScroller .pic img').click(function() {
		loadFavorite($(this).attr('class'));	
	});
	
	$('#fanScroller .pic').css({ position: "relative", top: "59px" });
	$('#fanScroller .up').css({ visibility: "hidden" });
	$('#fanScroller .down').css({ visibility: "hidden" });

	/* FAN FAVORITE "SLIDER" */
	/*
	$("#fanScroller .up").click(function() {
		$("#fanScroller .container").animate({ top: "-=100px" }, 1);
		$("#fanScroller .container .pic:last-child").prependTo("#fanScroller .container");
		$("#fanScroller .container").animate({ top: "+=100px" }, 400);
	});
	$("#fanScroller .down").click(function() {	  
		$("#fanScroller .container").animate({ top: "+=100px" }, 1);
		$("#fanScroller .container .pic:first-child").appendTo("#fanScroller .container");
		$("#fanScroller .container").animate({ top: "-=100px" }, 400);
	});
	*/
	
	/* VOTING FORM */	
	
	//ADD AN AJAX LISTENER TO THE VOTING FORM
	$('#votePopover form').submit(function() {
		$.ajax({
			url: "/inc/ajax/mtv_fan_favorite_vote.php",
			data: $(this).serializeArray(),
			cache: false,
			dataType: 'script',
			type: 'POST',
			beforeSend: function(){
				$('#votePopover .voteLoader').show();
				$('#votePopover iframe').remove();
			},
			success: function() {
				$('#votePopover .voteLoader').hide();
			}
		});
		return false;
	});
	
	
	//REMOVE THE ERROR CLASS WHEN SOMEONE LCICKS THE EMAIL BOX
	$("#votePopover input[name=email]").click(function() {
		$(this).removeClass('error');
	});
	
	$('.favFavModule .vote').click(function(){
		$('#votePopover').fadeIn();
	});
	
	$('#votePopover .back').click(function() {
		$('#votePopover').fadeOut();
	});
	
	
});