/*
 * based on elightbo's version
 */

function roundx(max,min){
	return(Math.round(Math.random()*(max-min))+min)
}

var advslider = Class.create({
	initialize: function(delay,featureTotal,elmtName){
		this.delay = delay;
		this.featureTotal = featureTotal;
		this.arrSlideElmt = [featureTotal];
		this.paused = 0;
		this.curElmtNum = roundx(featureTotal,1);
		
		// preload all elements
		for (i = 1; i <= featureTotal; i++) {
			this.arrSlideElmt[i] = $(elmtName + i);
			this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this)); //pause on mouseover
			this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this)); //resume on mouseout
			$$(this.arrSlideElmt[i].img).each(function(img) {
				img = new Image()
			});
		}
	},
	start: function(){
		this.arrSlideElmt[this.curElmtNum].setStyle({
			display: 'block'
		});
		this.executor = new PeriodicalExecuter(function() {
			this.next(); //start slidehow
		}.bind(this), this.delay); 
	},
	next: function(){
		if (!this.paused) {
			this.update();
		}
	},
	update: function() {
		//alert(this.curElmtNum);
		new Effect.SlideUp(this.arrSlideElmt[this.curElmtNum],{duration: 0.2,queue: 'end',afterFinish:function(){
			this.checkSlide();
			new Effect.SlideDown(this.arrSlideElmt[this.curElmtNum],{ duration: 0.3,queue: 'end' });
		}.bind(this)});
	},
	checkSlide: function() {
		if (this.curElmtNum == this.featureTotal) { this.curElmtNum = 1; }
		else { this.curElmtNum ++; }
	}
});

window.onload = function(){
	/* <-- this config */
	layerpos 	= 'teaser-blinding';
	elmnprefix 	= 'rotate';
	rotateSpeed = 7;
	/* -- > */
	new Ajax.Updater({success:layerpos},ddopath+'axrequest/getslider.php',{
		onLoading:function(){
			document.getElementById(layerpos).innerHTML = "<img id=\"dldldluuueee\" src=\""+extpath+"images/icons/loader.gif\" border=\"0\" />";
		},onComplete:function(){
			featureTotal = $(layerpos).childElements().size();
			var adv = new advslider(rotateSpeed,featureTotal,elmnprefix);
			adv.start();
		}
	});
}

