var scrollBox;
var scrollBoxTimeout;

$(document).ready(function(){
		var curIndex = 0;
		var xselltotalBoxes = $(".xsellbox").size();
		var xsellWidth = (xselltotalBoxes*200);
		var xsellHeight = (xselltotalBoxes*160);
		
		scrollBox = function(multiplier,timer) {
			if (xselltotalBoxes<=2) return;
			
			if (multiplier == undefined) multiplier = 1;
			
			curIndex += multiplier;
					
			if (curIndex<0) curIndex = 0;
			if (curIndex+2>xselltotalBoxes) {
				curIndex -= 1;
				$("#XcrossSellScroll .xsellbox:first").insertAfter($("#XcrossSellScroll .xsellbox:last"));
				$("#XcrossSellScroll").css("top", (-curIndex+1)*160);
			}
			
			$("#XcrossSellScroll").animate(
				{top: -curIndex*160},
				250,
				"swing"
			,function(){
				if (timer != undefined) scrollBoxTimeout = setTimeout("scrollBox(1,4000)",4000);
			});
		}

		scrollBoxTimeout = setTimeout("scrollBox(1,2000)",4000);
		
		$("#go_right").data("dir",1);
		$("#go_left").data("dir",-1);
		
		$("#go_up").data("dir",-1);
		$("#go_down").data("dir",1);
				
		$("#crossSellScroll,#XcrossSellScroll").show();
		$("#crossSellScroll").width(xsellWidth+"px");
		$("#XcrossSellScroll").height(xsellHeight+"px");
		$("#XcrossSellDiv").height(Math.min(xsellHeight,320)+"px");
		
	    $(".scroll_links").click(function(){
			var multiplier = $(this).data("dir");
			
			curIndex += multiplier;
			
			if (curIndex<0) curIndex = 0;
			if (curIndex+4>xselltotalBoxes) curIndex = xselltotalBoxes - 4;
			
			$("#crossSellScroll").animate(
				{left: -curIndex*200},
				500,
				"swing"
			);
		});

	    $(".xscroll_links").click(function(){
			var multiplier = $(this).data("dir");
			scrollBox(multiplier);
			clearTimeout(scrollBoxTimeout);
			scrollBoxTimeout = setTimeout("scrollBox(1,4000)",20000);
		});
		
		$(".scroll_links img").hover(function(){
				var hsrc = $(this).attr("src").replace("images/arrow","images/harrow");
				$(this).attr("src",hsrc);
			},function(){
				var rsrc = $(this).attr("src").replace("images/harrow","images/arrow");
				$(this).attr("src",rsrc);
			}
		);
		
		$(".xscroll_links img").hover(function(){
				var hsrc = $(this).attr("src").replace("images/arrow","images/harrow");
				$(this).attr("src",hsrc);
			},function(){
				var rsrc = $(this).attr("src").replace("images/harrow","images/arrow");
				$(this).attr("src",rsrc);
			}
		);
	 });