var newResultId = 1;
$(document).ready(function() {
  $('#voteOverlay').show();
  get_new_vote();
  $('#voteOverlay').fadeOut(800);
    
  $('.voteOption').click(function(){
	  $('#voteOverlay').fadeIn();
	  $.post("vote.php?do=vote", {
	  	winner: $(this).attr("rel"),
		hash: $('#voteMain').attr("rel")
      }, function(response){
	    if (response != 'fail') setTimeout("cast_vote('"+escape(response)+"')", 600);
		else {
		  $('#voteOverlay').fadeOut();
	      get_new_vote();
		}
      });
	});
	
	$(".voteL").bind("mouseenter",function(){
      $(this).find('div.voteHelp').fadeIn();
    }).bind("mouseleave",function(){
      $(this).find('div.voteHelp').hide();
    });
	$(".voteR").bind("mouseenter",function(){
      $(this).find('div.voteHelp').fadeIn();
    }).bind("mouseleave",function(){
      $(this).find('div.voteHelp').hide();
    });
});

function get_new_vote() {
  $.post("vote.php?do=get", { }, function(response){
    var voteArr = response.split("%%");
	$('.voteL').find('img').attr('src', 'http://www.ilovemybra.com/vote/vote/' + voteArr[2]);
	$('.voteR').find('img').attr('src', 'http://www.ilovemybra.com/vote/vote/' + voteArr[3]);
	$('.voteL').find('.voteOption').attr('rel', voteArr[0] + "," + voteArr[1]);
	$('.voteR').find('.voteOption').attr('rel', voteArr[1] + "," + voteArr[0]);
	$('.voteL').find('p').html(voteArr[4]);
	$('.voteR').find('p').html(voteArr[5]);
	$('#voteMain').attr('rel', voteArr[6]);
  });
}

function cast_vote(data) {
	var response = unescape(data);
	var resultArr = response.split("%%");
	var html = '<div class="voteResults" id="voteResultNew'+newResultId+'" style="display:none;" ><div class="thumbs"><span class="'+resultArr[9]+'">'+resultArr[1]+'%</span><a href="bra.php?s='+resultArr[11]+'"><img src="http://www.ilovemybra.com/vote/vote/thumbs/'+resultArr[5]+'" width="67" height="100" /></a> <a href="bra.php?s='+resultArr[12]+'"><img src="http://www.ilovemybra.com/vote/vote/thumbs/'+resultArr[6]+'" width="67" height="100" /></a><span class="'+resultArr[10]+'">'+resultArr[2]+'%</span></div> <div class="result"> '+resultArr[0]+'<p>Left: <a href="bra.php?s='+resultArr[11]+'">'+resultArr[3]+'</a></p><p>Right: <a href="bra.php?s='+resultArr[12]+'">'+resultArr[4]+'</a></p></div></div>';
	$('#voteResultsNew').prepend(html);
	$('#voteResultNew'+newResultId).slideDown(600);
	$('#voteOverlay').fadeOut();
	get_new_vote();
	newResultId++;
}