var OpacityManager=new function(){
	var TimerID;
	var Config={
		Objects:		new Array(),
		StepQt:			20,
		StepTime:		20,
		ObjectTime:		0,
		StartZIndex:	1000
	}
	var Steps=new Array();
	var CurrentIndex=0;
	
	function SetElementOpacity(oElem, nOpacity){
		var p = GetOpacityProperty();
		if (p=='filter'){
			nOpacity *= 100;
			var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;
			if (oAlpha)
				oAlpha.opacity = nOpacity;
			else
				oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
		}
		else if (p) {
			oElem.style[p] = nOpacity;
		}
	}

	function GetOpacityProperty(){
		var p;
		if (typeof document.body.style.opacity == 'string') p = 'opacity';
		else if (typeof document.body.style.MozOpacity == 'string') p =  'MozOpacity';
		else if (typeof document.body.style.KhtmlOpacity == 'string') p =  'KhtmlOpacity';
		else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p =  'filter';
		return (GetOpacityProperty = new Function("return '"+p+"';"))();
		//return p;
	}

	this.SetConfig=function(NewConfig){
		for(var Key in NewConfig){
			switch(typeof Config[Key]){
				case 'undefined':
					break;
				case 'int':
					Config[Key]=parseInt(NewConfig[Key]);
					break;
				default:
					Config[Key]=NewConfig[Key];
					break;
			}
		}
	}
	
	this.Next=function(){
		return this.Change(CurrentIndex+1);
	}
	
	this.Previous=function(){
		return this.Change(CurrentIndex-1);
	}

	this.Change=function(Index){
		clearTimeout(TimerID);
		
		Index=Math.abs(parseInt(Index)%Config.Objects.length);
		CurrentIndex=Index;
		
		Start=0;
		Finish=1;
		for(var i=0;i<Config.StepQt;i++){
			Steps[i]=Finish-(Finish-Start)*(0.5*(1+Math.cos(Math.PI*(i+1)/Config.StepQt)));
		}
		this.Animate(0);
		return false;
	}
	
	this.Animate=function(i){
		i=Math.abs(parseInt(i));
		if(i==Steps.length && Config.ObjectTime){
			TimerID=setTimeout('OpacityManager.Change('+(CurrentIndex+1)+')',Config.ObjectTime);
		}
		if(i>Steps.length-1)
			return;
		SetElementOpacity(Config.Objects[CurrentIndex],Steps[i]);
		ZIndex=Config.StartZIndex;
		if(!i){
			for(i=0;i<Config.Objects.length;i++){
				Z=parseInt(Config.Objects[i].style.zIndex);
				ZIndex=Z>ZIndex?Z:ZIndex;
			}
			Config.Objects[CurrentIndex].style.zIndex=ZIndex+1;
		}
		TimerID=setTimeout('OpacityManager.Animate('+(i+1)+')',Config.StepTime);
	}
	
	this.OnLoad=function(){
		if(Config.ObjectTime)
			TimerID=setTimeout('OpacityManager.Next()',Config.ObjectTime);
	}
}
