var show_why_vote = false;

var post_vote = function(){

  var the_ID = $j('this').attr('id');
  
  $j('#'+the_ID).unbind("click");
  
  if (votes_left == 0) {
  
    show_buy_more_votes();
  
  } else {
  
    $j.ajax({
  
	   type: "post",
	   url: vote_url,
	   dataType: "json",
	   success: function(data){
	   
	     if (data.result == 'success'){
	     
	       $j('#white_flash').css({width: $j(window).width(), height: $j(window).height()}).fadeIn('fast', function(){
	       
	         $j('#what_do_you_think').fadeOut('fast', function(){
	           $j('#vote_again').fadeIn('fast');
	         });
	         if (data.votes_remaining < 1){
	           var votes_left = '0';
	           var vote_noun = 'votes';
	         } else {
	           var votes_left = data.votes_remaining;
	           var vote_noun = data.votes_remaining == 1 ? 'Vote' : 'Votes';
	         }
	         
	
	         $j('#to_spend').empty().html(votes_left);
	         $j('#vote_noun').empty().html(vote_noun);
	       
	         $j('#mini_top_fans h4').show();
	         $j('#top_3_fans').empty();
	       
	         $j.each(data.fans, function(i, item){
	           if (i === 3){
	           return;
	           
	           } else {
	         
	           var noun = item.votes_cast == 1 ? 'time' : 'times';
	
	           $j('li#clone_this_li').clone()
	             .attr({id:  'mini_fan_' + i})
	             .empty()
	             .html('<p><a href="'+ item.profile_url +'"><img src="'+ item.icon +'" class="top_fan" />'+ item.name +'</a> voted '+ item.votes_cast +' '+ noun +'!</p>')
	             .appendTo('ul#top_3_fans');
	           }
                
             if (current_user.monthly_votes > 1) {
               current_user.monthly_votes -= 1;
             } else {
               current_user.purchased_votes -= 1;
             }
 
	           
             //menus: #votes_for_this_set, #ajax-votes-left, #ajax-votes-left-sub, #ajax_monthly_votes_left, #ajax_purchased_votes_left
		         $j('#ajax-votes-left').empty().html(votes_left);
		         $j('#top_nav_votes_noun').empty().html(vote_noun);
		         $j('#top_nav_votes_noun_sub').empty().html(vote_noun);
		         $j('#ajax-votes-left-sub').empty().html(votes_left);
		         $j('#ajax_monthly_votes_left').empty().html(current_user.monthly_votes);
		         $j('#ajax_purchased_votes_left').empty().html(current_user.purchased_votes);
	           
	         });
	         	       
	       });
	     
	     } else if (data.result == 'fail') {
         $j('#red_flash_msg').html('<h1>There was an application failure. Please try again or contact support.</h1>').fadeIn();
	     } else {
	       show_buy_more_votes();
	     }
	     
       $j('#white_flash').fadeOut('fast');

	   }
	  });
  
  }
  
  $j('#'+the_ID).bind("click", post_vote);

}

var show_why_vote = function(){
  var height = $j('#more_from_artist').outerHeight();
  var left = $j('#more_from_artist').position()['left'];
  var top = $j('#more_from_artist').position()['top'];

  $j('#why_vote_content').css({left: left, top: top - 10}).slideDown('slow');
  show_why_vote = true;
}

var hide_why_vote = function(){
  $j('#why_vote_content').slideUp('slow');
  show_why_vote = false;
}

$j(document).ready(function(){

  $j('#disabled_vote_button').click(function() {window.location.href = '/join';});
  $j('#vote_button').click(post_vote);
  $j('#vote_button_2').click(post_vote);
  $j('.why_vote').click(show_why_vote);
  $j('#close_why_vote').click(hide_why_vote);

});

$j(window).bind('resize', function() {

  if (show_why_vote == true) {
    hide_why_vote();
  }
    
});

$j(window).scroll(function(){
  $j('#white_flash').css({left: $j(document).scrollLeft(), top: $j(document).scrollTop()});
});