ROTATE_BANNERS = {
	groups: [],
	options: {
		delay: 6
	},
	init: function(options){
		// Load options
		if(options){
			for(var i in this.options){
				if(this.options.hasOwnProperty(i)){
					if(typeof options[i] != "undefined")
						this.options[i] = options[i];
				}
			}
		}
		
		// Find banner groups
		var tmp1,tmp2, elms = this.getElementsByClassName("rotating_banners");
		for(var i=0, len=elms.length; i<len; i++){
			tmp1 = {"elm": elms[i], "banners": []};
			tmp2 = this.getElementsByClassName("rotating_banner", elms[i]);
			
			// Find banners in the group
			for(var j=0, jlen=tmp2.length; j<jlen; j++){
				tmp1.banners.push({"banner": tmp2[j]})
			}
			this.groups.push(tmp1);
		}
		
		// Start rotation
		for(var i=0, len = this.groups.length; i<len; i++){
			this.groups[i].rotator = new this.Rotator(this.groups[i], this.options.delay, this)
		}
	},
	getElementsByClassName: function(cl, elm) {
		elm = elm || document.body;
		var retnode = [],
			myclass = new RegExp('\\b'+cl+'\\b'),
			elem = elm.getElementsByTagName('*'),
			classes;
		for (var i = 0, len=elem.length; i < len; i++){
			classes = elem[i].className;
			if (myclass.test(classes)) retnode.push(elem[i]);
		}
		return retnode;
	},
	setOpacity: function(elm, opacity){
		elm.style.opacity = opacity;
		elm.style.filter = 'alpha(opacity=' + opacity*100 + ')';
	}
}

ROTATE_BANNERS.Rotator = function(group, delay, parent){
	this.group = group;
	this.delay = delay;
	this.parent = parent;
	this.timer = false;
	this.current = 0;
	this.iterator=0;
	this.paused = false;
	
	var that = this;
	this.group.elm.onmouseover = function(){
		that.paused = true;
	}

	this.group.elm.onmouseout = function(){
		that.paused = false;
	}
	
	if(this.group.banners.length<=1)
		return;
	this.run();
}

ROTATE_BANNERS.Rotator.prototype.run = function(){
	var that = this;
	this.timer = window.setTimeout(function(){
		if(!that.paused)
			that.change();
		else
			that.run();
	},this.delay*1000);
}

ROTATE_BANNERS.Rotator.prototype.change = function(){
	var that = this;
	this.next = this.current+1;
	if(this.next>=this.group.banners.length){
		this.next = 0;
	}
//	ROTATE_BANNERS.fadeOut(this.group.banners[this.current].banner)
//	ROTATE_BANNERS.fadeIn(this.group.banners[this.next].banner)
	new ROTATE_BANNERS.FadeInOut(
		this.group.banners[this.current].banner, 
		this.group.banners[this.next].banner,
		{afterFinish: function(){
			that.run();
		}})
	this.current = this.next;
}

ROTATE_BANNERS.FadeInOut = function(elm1, elm2, options){
	this.options = {
		start: 0,
		end: 1,
		duration: 2.5,
		afterFinish : null
	}
	if(options){
		if(typeof options.duration != "undefined")
			this.options.duration = options.duration;
		if(typeof options.afterFinish == "function")
			this.options.afterFinish = options.afterFinish;
	}
	
	this.elm1 = elm1;
	this.elm2 = elm2;
	this.current = 0;
	this.delay = 50;
	this.step = Math.abs(this.options.end-this.options.start)/(this.options.duration*1000/this.delay); 
	this.timer = false;
	ROTATE_BANNERS.setOpacity(elm1, 1-this.current);
	ROTATE_BANNERS.setOpacity(elm2, this.current);
	this.elm1.style.display = 'block';
	this.elm2.style.display = 'block';
	this.fade();
}

ROTATE_BANNERS.FadeInOut.prototype.fade = function(){
	var that = this;
	this.timer = window.setInterval(function(){
		that.current += that.step;
		if(that.current>=that.options.end){
			that.current = that.options.end;
			ROTATE_BANNERS.setOpacity(that.elm1, 1-that.current);
			ROTATE_BANNERS.setOpacity(that.elm2, that.current);
			window.clearInterval(that.timer);
			that.elm1.style.display = 'none';
			if(that.options.afterFinish)
				that.options.afterFinish();
		}else{
			ROTATE_BANNERS.setOpacity(that.elm1, 1-that.current);
			ROTATE_BANNERS.setOpacity(that.elm2, that.current);	
		}
	}, this.delay)
}

ROTATE_BANNERS.init();

