/*  JSBR - JavaScript Banner Rotation, version 1.0.0
 *  (c) 2010 by Philipp Laube
 *--------------------------------------------------------------------------*/

var JSBR = {
	Version: '1.0.0',
	arrS: new Array(),
	zIhi: 101,
	zIlo: 101,
	durB: 5000,
	durT: 1000,
	loop: false,
	
	isUndefined: function(o)
	{
		return typeof object === "undefined";
	},
	
	fadeIn: function(id,dur)
	{
		if(this.isUndefined(dur)){dur=1000;}
		this.fade(id,$(id).style.opacity,1.0, dur/10);
	},

	fadeOut: function(id,dur)
	{
		if(this.isUndefined(dur)){dur=1000;}
		this.fade(id,$(id).style.opacity,0.0, dur/10);
	},

	fade: function(id, curval, destval, delay)
	{
		curval = parseFloat(curval);
		destval = parseFloat(destval);
		delay = parseInt(delay/10);
		if(curval<destval)
		{
			curval+=0.01;
			if(destval-curval<0.01){curval=destval;}
			this.setOpacityById(id,curval);
			setTimeout("JSBR.fade('"+id+"',"+curval+","+destval+","+delay+")", delay);
		}
		if(curval>destval)
		{
			curval-=0.01;
			if(curval-destval<0.01){curval=destval;}
			this.setOpacityById(id,curval);
			setTimeout("JSBR.fade('"+id+"',"+curval+","+destval+","+delay+")", delay);
		}
	},

	setOpacityById: function(id,level)
	{
		this.setOpacity($(id),level);
	},
	
	setOpacity: function(el,level)
	{
		el.style.opacity=level;
		el.style.MozOpacity=level;
		el.style.KhtmlOpacity=level;
		el.style.filter="alpha(opacity="+level*100+")";
	},
	
	setZIndex: function(el,value)
	{
		el.style.zIndex=parseInt(value);
	},
	
	adaptZIndex: function(a,j)
	{
		for(i=0;i<a.length;i++)
		{
			this.setZIndex(a[i],this.zIlo);
		}
		this.setZIndex(a[j],this.zIhi);
	},
	
	changeBanner: function(k,i,d,t)
	{
		var el=this.arrS[k];
		var arrEl=el.banner;
		
			var cur=(i-1<0)?arrEl.length-1:i-1;
			this.fadeOut(arrEl[cur].id,t);
			this.adaptZIndex(arrEl,i);
			this.fadeIn(arrEl[i].id,t);
			i=(i+1<arrEl.length)?i+1:0;
			if(i!=0 || this.loop)
			{
				setTimeout("JSBR.changeBanner("+k+","+i+","+d+","+t+")",d);
			}
	},
	
	play: function()
	{
		for(i=0;i<this.arrS.length;i++)
		{
			this.changeBanner(i,0,this.durB,this.durT);
		}
	},
	
	init: function()
	{
		var jsbrs=$$("div.jsbr");
		for(i=0;i<jsbrs.length;i++)
		{
			var v=jsbrs[i];
			
			var objV={width:parseInt(v.scrollWidth),height:parseInt(v.scrollheight),banner:null};
			
			this.setZIndex(v,this.zIlo);
			var arrI=new Array();
			if(v.hasChildNodes())
			{
				var c=v.firstChild;
				while(c!=null)
				{
					if((c.nodeName.toLowerCase()=='a' 
						&& c.hasChildNodes()
						&& (c.firstChild.nodeName.toLowerCase()=='img'
							|| a.nextSibling.nodeName.toLowerCase()=='img' ))
						|| (c.nodeName.toLowerCase()=='img' && !c.hasChildNodes()))
					{		
							this.setZIndex(c,this.zIhi++);
							c.id="ssimg_"+this.arrS.length+"_"+arrI.length;
							arrI.push(c);
							this.setOpacity(c,0);
					}
					
					c=c.nextSibling;
				}
				this.zIhi--;
			}
			
			if(arrI.length<1)
			{
				v.parentNode.removeChild(v);
			}else{
				objV.banner=arrI;
				this.arrS.push(objV);	
			}
		}
	}
};


Event.observe(document, 'dom:loaded', function(){ JSBR.init(); });
Event.observe(window, 'load', function(){ JSBR.play(); });
