function ShowHideArrows(totalwidth)
{
	if ($("#sale").children(":first").children("div:nth-child(odd)").length <= 4)
	{
		$("#arrowleft").hide();
		$("#arrowright").hide();
	}
	else
	{
		if (parseInt($("#sale").children(":first").css("left")) == 0)
			$("#arrowleft").hide();
		else
			$("#arrowleft").show();
		
		if (parseInt($("#sale").children(":first").css("left")) == totalwidth)
			$("#arrowright").hide();
		else
			$("#arrowright").show();
	}
}

$(document).ready(function() {
	var leftintervalid = 0, rightintervalid = 0;
	var child = $("#sale").children(":first");
	if (child.length)
	{
		var childrencount = $(child).children("div").length;
		var offsetleft = $("#sale").offset().left;
		var offsettop = $("#sale").offset().top;
		var width = $("#sale").width();
		
		var diff = 0;
		if (childrencount < 16)
			diff = Math.ceil(childrencount*1.2)
		else
			diff = Math.ceil(childrencount*1.06)
		
		width -= diff;
		
		var totalwidth = 0;
		$(child).children("div").each(function() {
			totalwidth += $(this).width();
		});
		
		ShowHideArrows(totalwidth)
		
		$("#sale").bind("mousemove", {child:child}, function(evt) {
			var x = evt.pageX - offsetleft;
			var y = evt.pageY - offsettop;
			var child = evt.data.child;
			if (x > width - 50)
			{
				if (!leftintervalid)
				{
					clearInterval(rightintervalid);
					leftintervalid = setInterval(function() {
						var left = parseInt( child.css("left") );
						left -= 5;
						if (left < -totalwidth + width)
							left = -totalwidth + width;
						if (left > 0)
							left = 0;
						child.css("left", left+"px");
						
						ShowHideArrows(-totalwidth + width);
					}, 10);
				}
			}
			else if (x < 50)
			{
				if (!rightintervalid)
				{
					clearInterval(leftintervalid);
					rightintervalid = setInterval(function() {
						var left = parseInt( child.css("left") );
						left += 5;
						if (left > 0)
							left = 0;
						child.css("left", left+"px");
						
						ShowHideArrows(-totalwidth + width);
					}, 10);
				}
			}
			else
			{
				clearInterval(rightintervalid);
				clearInterval(leftintervalid);
				leftintervalid = 0;
				rightintervalid = 0;
			}
		});
		$("#sale").bind("mouseleave", null, function(evt) {
			clearInterval(rightintervalid);
			clearInterval(leftintervalid);
			leftintervalid = 0;
			rightintervalid = 0;
		});
	}
});