$(function() {
	var rewind = function(){
		var on = $("#nav .grid_full a.on");
		if ( ! on.length ) on = $("#nav .grid_full a:first");
		on.trigger('mouseover');
	};

	$("#nav .grid_full").append("<span><em /></span>").find("a").hover(function(){
		var l = $(this).position().left;
		$("#nav .grid_full span").animate({width: $(this).width(), left: l},{queue: false, duration: 500, easing: 'easeOutExpo'});

	},rewind);
	rewind();

	var next = function(){
		scroll.call(this, -1);
	};
	var prev = function(){
		scroll.call(this,1);
	};
	var scroll = function(dir){
		var pw = $(this).parent().width();
		var w = $(this).width();
		var l = $(this).position().left;
		var step = 100;
		var max = w-pw;

		if( max <= 0 ) return false;
		var left = l+step*dir;
		if (( left < -max ) || ( left > 0 )){
			if ( dir > 0 ) left = 0;
			else left = -max;
		}

		if ( left == 0 ) $(this).parent().siblings("a.prev").hide(); else $(this).parent().siblings("a.prev").show();
		if ( left == -max ) $(this).parent().siblings("a.next").hide(); else $(this).parent().siblings("a.next").show();

		$(this).animate({left: left},{duration: 500, queue: false});
	};
});
