var Promos = {current: 1, num:5, shown:0};
var Globals = {shortTime: 4000, longTime: 10000};

function switchContent(el){
	$clear(timer);
	if(Promos.current != el){
		//Promos.current = el;
		changeContent(el);
		startTimer(Globals.longTime);
	}
}

function changeContent(el){
	//setup tween
	newEl = $("content"+el.toString());
	currentEl = $("content"+Promos.current);
	newEl.set('tween',{duration: 1000});
	newEl.fade(0.999);
	if(newEl != currentEl){
		currentEl.fade(0);
	}
	document.getElementById("control"+Promos.current).className='';
	Promos.current = el;
	document.getElementById("control"+Promos.current).className='activo';
}

function page(dir){
	$clear(timer);
	next = (Promos.current + dir)%Promos.num;
	
	if(next == 0)
		next = Promos.num;
		
	changeContent(next);
	startTimer(Globals.shortTime);
}

var addCount = function(){this.shown++; $clear(timer); changeContent((this.current%this.num)+1); startTimer(Globals.shortTime);};


function selectRandom(){
	var j = Math.floor(((Math.random()*10)%Promos.num)+1);
	var randomPromo = j;
	changeContent(randomPromo);
	$('loader').fade(0);
}

function startTimer(time){
	if(Promos.shown < Promos.num)
		timer = addCount.periodical(time, Promos); //Will add the number of seconds at the Site.
	else
		$clear(timer);
}

function initFade(){
	for(i=1;i<=Promos.num;i++){
		newEl = $("content"+i);
		newEl.fade(0);
	}
}

function showStyles(obj){
	alert($(obj).getStyle('margin'));
}

function initControls(){
	var controlDivs = $('container_wrapper').getElements('div.controls');
	for (var i in controlDivs)
	{
		controlDivs[i].innerHTML = $('controls').innerHTML;
	}
}


function stop(){
	$clear(timer);
}

window.addEvent('domready', function(){
	
	initNavTopDesc();
	
	initControls();
	initFade();
	
	setTimeout(selectRandom, 200);
	startTimer(Globals.shortTime);
});

