var CMS_rotatingImage = {
	gsiCnt: 0,
	gsiRotateRandom: function(gsiid,gsidly)
	{   
		var ImgAry = new Array();
		if (!document.getElementById(gsiid))
		{
			return;
		}	
		gsiobj = document.getElementById(gsiid);
		if (!gsiobj)
		{
			return;
		}
		var widgetImgs = gsiobj.getElementsByTagName("IMG");
	    if(!widgetImgs)
		{
			return;
		}
		var widgetImgMaxHeight = 0;
		var widgetImgMaxWidth = 0;
		for (var i=0; i < widgetImgs.length; i++)
		{
			this.tmpImg = widgetImgs[i];				
			ImgAry[i] = this.tmpImg;
			if(this.tmpImg.height && (this.tmpImg.height >widgetImgMaxHeight))
			{
				widgetImgMaxHeight = this.tmpImg.height;
			}
			if(this.tmpImg.width && (this.tmpImg.width >widgetImgMaxWidth))
			{
				widgetImgMaxWidth = this.tmpImg.width;
			}
		}
	    CMS_rotatingImage.setSlotHeight(gsiobj,widgetImgMaxHeight);
		CMS_rotatingImage.setSlotWidth(gsiobj,widgetImgMaxWidth);
		//sets width of the div containing the rotating images to the width of the parent cms slot.
		gsiobj.style.width = gsiobj.parentNode.offsetWidth + "px";
		if(ImgAry.length ==0 || ImgAry.length ==1)
		{
			return;
		}	
		gsiobj.ary = new Array();
		gsiobj.srcAry = new Array();
		for (gsi0=0;gsi0<ImgAry.length;gsi0++)
		{
			gsiobj.ary[gsi0]=ImgAry[gsi0];
	        if (gsi0==0)
	        {
				ImgAry[gsi0].style.visibility="visible";
	        }
			else
	        {
				ImgAry[gsi0].style.visibility="hidden";
			}
		}
		
		if(!gsiobj)
		{
			return;
		}
		
		gsiobj.start=new CMS_rotatingImage.gsiAddFunctions(gsiobj,gsidly,ImgAry.length);
		this.tmp = gsiobj.ary[0];
		gsiobj.start.last=this.tmp;
		gsiobj.start.rotate();
	},
	
	gsiAddFunctions: function (gsiobj,gsidly,nImages)
	{   
		this.obj=gsiobj;
		this.ref='rotateImages' + CMS_rotatingImage.gsiCnt;
		window[this.ref]=this;
		this.last=null;
		this.first=null;
		this.cnt=null;
		this.prevcnt=0;
		this.dly=gsidly;
		this.cntImages=nImages;
		this.setTimeOut= function(gsif,gsid){  		    
			this.to=setTimeout('window.'+this.ref+'.'+gsif,gsid);
		}
		this.rotate=function(){   
			if(this.cnt==null) 
			{
				this.first=1;
				this.cnt=0; 
			} 
			this.last=this.obj.ary[this.cnt];	
			this.last.style.visibility="visible";
			if (this.cnt==0)
			{	
				if (this.first==1)
				{
					this.first=0;
				}
				else
				{
					this.prevcnt=this.cntImages-1;
					gsiobj.replaceChild(this.last,this.obj.ary[this.prevcnt]);
				}
			}
			else
			{
				this.prevcnt=this.cnt-1;
				gsiobj.replaceChild(this.last,this.obj.ary[this.prevcnt]);
			}
			this.cnt++;
				
			if(this.cntImages == this.cnt)
			{
				this.cnt=0;
			}			
			this.setTimeOut('rotate();',this.dly);
		}
		CMS_rotatingImage.gsiCnt++;
	},
	
	getRotateFrequency: function(thisSlot,fieldName)
	{        
		if(!thisSlot)
		{
			return;
		}	
		var aryNodes = thisSlot.childNodes;
		if(!aryNodes)
		{
			return;
		}	
		for (var j=0; j < aryNodes.length; j++)
		{	
		    if(aryNodes[j].nodeName == '#comment')
			{			   
		 		var tmpStr=aryNodes[j].nodeValue;
		 		if(!tmpStr)
				{
					continue;
				}
		 		if(tmpStr.indexOf(fieldName) != -1)
		 		{
		 			var pos =tmpStr.indexOf('=');	
					if(!pos){continue;}
		 			var rate = ((tmpStr.substring(pos + 1))*1000);//convert to to millisec.					  
		 			return rate;
				}
			}
		}
		return null;
	},
	
	setSlotHeight: function(gsiobj,widgetImgMaxHeight)
	{     
		var slotHeight = gsiobj.style.height;		
		if(!slotHeight || slotHeight =='')
		{
			gsiobj.style.height=widgetImgMaxHeight;
		}
	},
	
	setSlotWidth: function(gsiobj,widgetImgMaxWidth)
	{
		var slotWidth = gsiobj.style.width;
	    if(!slotWidth || slotWidth =='')
		{
			gsiobj.style.width=widgetImgMaxWidth;
		}
	},
	
	initRotation: function()
	{  
		var doc = document;
		if(!doc)
		{
			return;
		}
		if (!doc.getElementsByTagName)
		{
			return;
		}	
		var objDiv = doc.getElementsByTagName("DIV");	
		if (!objDiv)
		{
			return;
		}
		for (var i=0; i < objDiv.length; i++)
		{   
		    flag = false;
			var thisSlot = objDiv[i];			
			if(!thisSlot){return;}
			var rate = CMS_rotatingImage.getRotateFrequency(thisSlot,'rotateFrequency=');
			if(rate)
			{			   
				CMS_rotatingImage.gsiRotateRandom(thisSlot.id,rate);
			}			
		}
	}
}

