var init_posx = 0;
var rank_width = 59;

$(document).ready(function(){
	
	//document.onmousedown = set_init;
	
	$('.rank_inner').not('.voted').mouseover(function(){
		//alert(init_posx);
		
		$(this).parent().addClass('selected');
	    rank_width = $.css(this, "width");
	    //alert('made it');
	    document.onmouseover = set_init;
		document.onmousemove = setrank;
		//alert(init_posx);
	});
	
	$('.rank_inner').not('.voted').mouseout(function(){
		$(this).parent().removeClass('selected');
		$(this).css('width', rank_width+'px');
		document.onmousemove = null;
		document.onmouseover = null;
	});
	
	$('.rank_inner').not('.voted').click(function(){
		var width = $.css(this, 'width');
		
		$.post('/_includes/vote.php', {link_id: $(this).parent().parent().find('span.vote_id').html(), width: width},function(txt){
			if(txt!='success'){
				alert(txt);
			}
			else{
				$('.selected .rank_inner').addClass('voted');
				$('.selected .rank_inner').unmouseover().unmouseout().unclick();
				$('.selected').after('<span class="vote_button">voted</span>');
				$('.selected').parent().find('.vote_button').fadeIn('slow');
				$('.selected').removeClass('selected');
			}
		});
		
		$(this).parent().removeClass('select');
	});
	
	
	$('.vote_button').click(function(){
		var vote_id = $(this).parent().find('.vote_id').html();
	});
	
	
});


$(document).ready(function(){
  init();
});


function init(){
  document.onmousedown = set_init;
  $('.rank').mousedown(function(){
    document.onselectstart = function(){return false}
    $(this).addClass('selected');
    rank_width = $.css(this, "width");

	$(this).after('<div id="percent">'+Math.round(rank_width*100/220)+'%</div>');
	$('#percent').css('left', rank_width + 'px');
	document.onmousemove = setrank;
  });
  
  $(document.body).mouseup(function(){
    //clearInterval(rank_int);
    document.onselectstart = function(){return true}
    document.onmousemove = null;
    $('.rank').filter('.selected').removeClass('selected');
    $('#percent').fadeOut('quick');
    window.setTimeout("$('#percent').remove()", 1000);
  });

}

function setrank(e){
  if(!e) var e = window.event;
	//alert(rank_width);
	if(e.pageX){
	  pos_x = e.pageX;
	}
	else if (e.clientX)
	{
		pos_x = e.clientX + document.body.scrollLeft;
	}
	  
  pos_x = init_posx - pos_x;
  pos_x = Math.round(rank_width-(pos_x*2));
  if(pos_x<5) pos_x = 5;
  else if(pos_x>59) pos_x = 59;
  
  //alert(pos_x);

  $('.rank_outer').filter('.selected').find('.rank_inner').css('width', pos_x+'px');
}

function pos_x(e)
{
	if(!e) var e = window.event;
	
	if(e.pageX){
	  posx = e.pageX;
	}
	else if (e.clientX)
	{
		posx = e.clientX + document.body.scrollLeft;
	}
	return posx;
}

function set_init(e)
{
	if(!e) var e = window.event;
	
	if(e.pageX){
	  init_posx = e.pageX;
	}
	else if (e.clientX)
	{
		init_posx = e.clientX + document.body.scrollLeft;
	}
}

function posy()
{
	var posy = 0;
	var e = window.event;
	if (e.pageY)
	{
		posy = e.pageY;
	}
	else if (e.clientY)
	{
		posy = e.clientY + document.body.scrollTop;
	}
	
	return posy;
}

