$(document).ready(function(){

	$("a.vote").click(function(e){
 		e.preventDefault();     
      original = $(this).find('img');
      var thisUrl = this.href;
      var thistrigger = $(this);
      var votescontainer = $(this).parent().prev().find('.votes');
      var numvotescontainer = $(this).parent().prev().find('.votecount');
      //var generic_container      = $(this).parent('ul.vote-controls:first');
      var generic_container      = $(this).parent().parent();
      //var generic_container = $(this);
      //var container = $(this).parent().parent().find('.votes');
     
      $.post(thisUrl, {rating: $(this).html(), next:'../../'},  function(json) {
			// format result
			json = eval('(' + json + ')');
			var result = [json.score.score];
			var numvotes = [json.score.num_votes];
			
			// output result
			//$(container).html(result);
			if ($(votescontainer).size() != 0) {
				$(votescontainer).html(result.join(''));
				$(numvotescontainer).html(numvotes.join(''));
			} else {
				$(thistrigger).html(original);
				//alert()
				$(generic_container).next().html('Thanks. Your vote has been recorded.').removeClass('report').addClass('note');
			}
     	});
	});

  $(".vote-help-trigger").click(function(e){
    $(this).next().show('fast');
    $(this).next().css('display', 'block'); //For IE8
    return false
  });

	$("body").click(function(e){
		$(".vote-help").hide('fast');
	});
  
	$('.collapse .comment').hide().before('<p class="uncollapse" title="Show comment"><span>&#9762;</span> This comment has been reported as toxic or spam.</p>');
	
	$('.uncollapse').click(function() {
		//$(this).toggle('slow');
		$(this).next().toggle('slow');
		$(this).parent().find('.vote-controls').toggle('fast');
	})
});	

