vote_direction = 'none';

function setup_voting_events() {
  var thumbs_up = $('#thumbs_up_vote_image');
  var thumbs_down = $('#thumbs_down_vote_image');
  
  thumbs_up.onmouseover = function() {
    if (vote_direction != 'up') {
      thumbs_up.src = thumbs_up.src.replace('/large_unsaturated/', '/large/');
    }
  };
  
  thumbs_up.onmouseout = function() {
    if (vote_direction != 'up') {
      thumbs_up.src = thumbs_up.src.replace('/large/', '/large_unsaturated/');
    }
  };
  
  thumbs_down.onmouseover = function() {
    if (vote_direction != 'down') {
      thumbs_down.src = thumbs_down.src.replace('/large_unsaturated/', '/large/');
    }
  };
  
  thumbs_down.onmouseout = function() {
    if (vote_direction != 'down') {
      thumbs_down.src = thumbs_down.src.replace('/large/', '/large_unsaturated/');
    }
  };
}

function reset_vote_direction(direction) {
  vote_direction = direction;
  
  var thumbs_up = $('#thumbs_up_vote_image')[0];
  var thumbs_down = $('#thumbs_down_vote_image')[0];
  
  if (vote_direction == 'up') {
    thumbs_down.src = thumbs_down.src.replace('/large/', '/large_unsaturated/');
    thumbs_up.src = thumbs_up.src.replace('/large_unsaturated', '/large/');
  } else {
    thumbs_up.src = thumbs_up.src.replace('/large/', '/large_unsaturated/');
    thumbs_down.src = thumbs_down.src.replace('/large_unsaturated', '/large/');
  }
}

function after_vote(request) {
	console.log("Request result: ", request);
	if (request.responseText) {
    json = eval('(' + request.responseText + ')');
	} else {
		json = request;
	}
  $('#up_votes_count')[0].innerHTML = json.up;
  $('#down_votes_count')[0].innerHTML = json.down
}


var hovered_node = null;
var hovered_height = 30;

function hoverVideo(node, big_height) {
	big_height = big_height || 89;
  if (hovered_node == node) return;
  if (hovered_node) unhoverVideo(hovered_node);
  hovered_node = node;
  hovered_height = $('#' + node.id + '>.details').height();
  
  $('#' + node.id + '>.details').animate({height:big_height + 'px'}, 250);
}

function unhoverVideo(node) {
  $('#' + node.id + '>.details').animate({height:hovered_height + 'px'}, 250);
}

$(document).ready(function() {
	// Setup Ajax handler for remote forms
	$('form[data-mode=remote]').submit(function() {
		$(this).css({opacity:0.5});
		var submit_buttons = $(this).children(':submit');
		if (submit_buttons && submit_buttons.length > 0) { 
		  submit_buttons[0].disabled = true;
		}
		var loading_func = $(this).attr('data-loading');
		if (loading_func) {
			eval(loading_func);
		}
		var complete_func = $(this).attr('data-result');
		var theform = this;
		$.post(this.action, $(this).serialize(), function(result, textStatus) {
			try {
			  $(theform).children(':submit')[0].disabled = false;
			} catch(e) {}
			eval(complete_func + "(result)");
		}, 'json');
		// Abort normal form post
		return false;
	});
}); 


