var time_to_show = 5000;
var time_to_transition = 1000;
var elements = Array();
$(function(){ 
	$("ul#tickers").liScroll();
});
$(document).ready(function() {
	initSlideAutoPromo();
});

function initSlideAutoPromo() {
	$('#slide_autopromo ul li').each(function(i) {
		elements[i] = this;
	});

	for(var i =0; i < elements.length; i++) {
		$(elements[i]).animate({opacity: 0}, (time_to_show + time_to_transition*2) * i, function() { initTimer(this,elements.length) });
	}
}

function initTimer(element,length) {
	//alert(element.id + " " + length);
	$(element).css({display: "block"})
	$(element).animate({opacity: 1}, time_to_transition).animate({opacity: 1}, time_to_show).animate({opacity: 0}, time_to_transition,  function() { $(this).css({display: "none"}) }).animate({opacity: 0}, (time_to_show + time_to_transition*2) * (length-1) - time_to_transition, function() { initTimer(this,length)});
}

function changeSlide(num) {
	num--;
	//alert("num: " + num);
	for(var i =0; i < elements.length; i++) {
		$(elements[i]).queue("fx", []);
		$(elements[i]).stop();
		if(num == i) {
			$(elements[i]).animate({opacity: 0}, time_to_transition, function() { initTimer(this,elements.length) });
		}
		else if(num > i) {
			newid = i + elements.length - num;
			$(elements[i]).animate({opacity: 0}, time_to_transition, function() { $(this).css({display: "none"}) }).animate({opacity: 0}, (time_to_show + time_to_transition*2) * newid, function() { initTimer(this,elements.length) });
		}
		else {
			newid = i - num;
			$(elements[i]).animate({opacity: 0}, time_to_transition, function() { $(this).css({display: "none"}) }).animate({opacity: 0}, (time_to_show + time_to_transition*2) * newid, function() { initTimer(this,elements.length) });
		}

	}
}
