// JavaScript Document
var promo_obj = [];
var promo_current_index = 0;
var promo_next_index = 1;
var promo_max_index;
var perExec;
var timeout_b = 0;
var timeOut;
var exec_time = 5;
var reexec_time = 10000;

// IMPORTED SCRIPT
Array.prototype.randomize = function() {
 var i = this.length;
 if (i == 0) return;
 while (--i) {
  var j = Math.floor(Math.random()*(i+1));
  var tmp1 = this[i];
  var tmp2 = this[j];
  this[i] = tmp2;
  this[j] = tmp1;
 }
 return this;
}

function periodicalExec(pe){
	if(timeout_b == 1){clearTimeout(timeOut);timeout_b = 0;}
	new Effect.Parallel([
		new Effect.Fade(promo_obj[promo_current_index], { sync: true }),
		new Effect.Appear(promo_obj[promo_next_index], { sync: true })
	], {duration: 1, queue: 'end'});
	$(promo_obj[promo_current_index].getAttribute('cms:promo_nav')).removeClassName('div_hover');
	$(promo_obj[promo_next_index].getAttribute('cms:promo_nav')).addClassName('div_hover');
	promo_current_index = promo_next_index;
	(promo_next_index == promo_max_index)? promo_next_index = 0:promo_next_index++;
}

function promoFind(e){
	clearTimeout(timeOut);
	perExec.stop();
	$$('.promo_find').each(function(n){
		n.down().removeClassName('div_hover');
	});
	Event.element(e).addClassName('div_hover');
	var request_index = Event.element(e).innerHTML;
	request_index--;
	if(request_index != promo_current_index){
		promo_next_index = request_index;
		new Effect.Parallel([
			new Effect.Fade(promo_obj[promo_current_index],  { sync: true }),
			new Effect.Appear(promo_obj[promo_next_index], { sync: true })
		], {duration: 0});
		promo_current_index = promo_next_index;
		(promo_next_index == promo_max_index)? promo_next_index = 0 : promo_next_index++;
	}
	timeOut = setTimeout(function(){
		perExec.stop();
		perExec = new PeriodicalExecuter(function(pe){periodicalExec(pe); }, exec_time);
		timeout_b = 1;
	},reexec_time);
}

function loadPromoHooks(){
	var promo_arr = [];
	promo_obj.each(function(n){
		n.setAttribute('cms:promo_nav', 'find_'+n.getAttribute('id'));
		var obj = Builder.node('div', { className:'promo_find'}, 
							   [Builder.node('a',{ href:'#', id:'find_'+n.getAttribute('id'), 'cms:promo_obj':(promo_obj.indexOf(n)+1).toString() }, (promo_obj.indexOf(n)+1).toString())]);
		promo_arr.unshift(obj);
	});
	promo_arr.each(function(n){
		$('promo_nav').insert(n);
	});
	$$('.promo_find').each(function(n){
		n.observe('click', promoFind);
	});
	$$('.promo_find').pop().down().addClassName('div_hover');
}

function promoLoad(){
	promo_max_index = $("promo_container").getAttribute("cms:promo_count")-1;
	$$(".promo_main_div").each(function(n){
		promo_obj.push(n);
	});
	promo_obj.each(function(n){
		n.setStyle({
			position: 'absolute', top: '0', left: '0', right: '0', bottom: '0', backgroundImage: 'url("img/promo/' + n.getAttribute('cms:promo_img') + '")'
		});
	});
	promo_obj.randomize();
	for(var x = 1; x <= promo_max_index; x++){
		new Effect.Fade(promo_obj[x], { duration: 0 });
	}
	perExec = new PeriodicalExecuter(function(pe){periodicalExec(pe); }, exec_time);
	loadPromoHooks();
}

// End of Script

Event.observe(document, "dom:loaded", promoLoad);
