﻿var L;
var R;
var C;
var lists = new Array();
var position = new Array();
var ani = 200;
var count;
var busy;
var timer = 1;




$(document).ready(function(){
	
	L = $("#promo-left ul");
	R = $("#promo-right ul");
	C = $("#promo-center ul");

	count = L.find("li").size() - 1;
	
	lists.splice(0, 0, L, C, R);
	
	position.splice(0, 0, 0, 1, 2);
	
	for (var n = 0; n < lists.length; n++){
		ul = lists[n];
		ul.prepend("<li>" + ul.find("li").eq(count).html() + "</li>");
		ul.append("<li>" + ul.find("li").eq(1).html() + "</li>");
		ul.css("width", (ul.find("li").eq(0).width() * (count + 3)) + 50 + "px");
	}
		
	busy = false;
	
	$("#promo a.next").click(function(e){
		e.preventDefault();
		for (var n = 0; n < lists.length; n++){
			slide(n, true);
		}
		busy = true;
	});
	
	$("#promo a.prev").click(function(e){
		e.preventDefault();
		for (var n = 0; n < lists.length; n++){
			slide(n, false);
		}
		busy = true;
	});
	
	$("#promo a").hover(
		function(){
			pause();
		}, function(){
			play();
		});
	
	$("#promo a.next").click();
	
	play();	
});



function slide(list, forward){
	
	if (!busy) {
		
		var step = 1;		
		if (forward) {step = -1;}
		
		var ul = lists[list];
		var div = ul.parent().parent();		

		var width = ul.find("li").eq(0).width();

		div.find("p").fadeOut(ani);
		div.find("h4").fadeOut(ani);

					
		position[list] += step;
					
		ul.animate({left: (width * (position[list] + 1) * -1) + "px"}, ani * 2, "swing", function(){
			if (position[list] < 0){
				position[list] = count;
				ul.css("left", (width * (count + 1) * -1) + "px");
			} 
			if (position[list] > count){
				position[list] = 0;
				ul.css("left", (width * -1) + "px");
			}
			
			div.find("h4").html(ul.find("li").eq(position[list] + 1).find("a").attr("title")).fadeIn(ani);
			div.find("p").html(ul.find("li").eq(position[list] + 1).find("a").attr("rel")).fadeIn(ani);
			
			busy = false;
		});
	}
}




function play() {
	timer = setInterval("$('#promo a.next').click()", 5000);
}



function pause() {
	window.clearInterval(timer );
}