// JavaScript
//Description : This Code is responsible for CrossFade the images



MSCrossFade=function(containerId,objName)
{
	this.containerId=containerId;
	this.obj=objName;
	this.galleryId = 'MSCrossFadeImages'; 
	this.galleryImages;
	this.currentImage; 
	this.previousImage;
	this.preInitTimer;
	this.width="775px";
	this.height="229px";
	this.delay=3000; //Delay in milisec , 1 sec=1000 milisec
	this.createFrameWork();
};

MSCrossFade.prototype={
	getObj:function(objId)
	{
		return (document.getElementById?document.getElementById(objId):document.all[objId]);
	},
	start:function()
	{
		this.preInit();
		this.fadeInit();
	},
	addImage:function(ImageURL)
	{
		var li=document.createElement('li');
		li.style.cssText='display: block;';
		
		var img=document.createElement('img');
		
		
		img.setAttribute('width',this.width);
		img.setAttribute('height',this.height);
		
		img.setAttribute('border','0');
		img.setAttribute('alt','');
		
		img.setAttribute('src',ImageURL);
		
		li.appendChild(img);
		this.getObj(this.galleryId).appendChild(li);
		
	},
	createFrameWork:function()
	{
		var ul=document.createElement("ul");
		ul.setAttribute('id',this.galleryId);
		ul.style.cssText='position: relative; width:'+this.width+'; height:'+this.height+'; margin:0; padding:0;';
		this.getObj(this.containerId).appendChild(ul);
	},
	preInit:function()
	{
		if ((document.getElementById)&&(typeof document.getElementById(this.galleryId) !='undefined'))
		{
			this.getObj(this.galleryId).style.visibility = "hidden";
			if(typeof this.preInitTimer != 'undefined') 
			{
				clearTimeout(this.preInitTimer); 
			}
		} 
		else 
		{
			this.preInitTimer = setTimeout(this.obj+".preInit()",2);
		}
		
	},
	fader:function(imageNumber,opacity)
	{
		var obj=this.galleryImages[imageNumber];
		if (obj.style) {
			if (obj.style.MozOpacity!=null) {  
				/* Mozilla's pre-CSS3 proprietary rule */
				obj.style.MozOpacity = (opacity/100) - .001;
			} else if (obj.style.opacity!=null) {
				/* CSS3 compatible */
				obj.style.opacity = (opacity/100) - .001;
			} else if (obj.style.filter!=null) {
				/* IE's proprietary filter */
				obj.style.filter = "alpha(opacity="+opacity+")";
			}
		}
	},
	fadeInit:function()
	{
		if(document.getElementById) 
		{
			
			this.preInit();
			
			this.galleryImages = new Array;
			var node = this.getObj(this.galleryId).firstChild;
			
			while (node) 
			{
				if (node.nodeType==1) 
				{
					this.galleryImages.push(node);
				}
				node = node.nextSibling;
			}
			
			for(i=0;i<this.galleryImages.length;i++) 
			{
				
				this.galleryImages[i].style.position='absolute';
				this.galleryImages[i].style.top=0;
				this.galleryImages[i].style.zIndex=0;
				
				this.fader(i,0);
			}
			
			this.getObj(this.galleryId).style.visibility = 'visible';
			
			this.currentImage=0;
			this.previousImage=this.galleryImages.length-1;
			opacity=100;
			this.fader(this.currentImage,100);
		
			window.setTimeout(this.obj+".crossfade(100)", 1000);
		}
	},
	crossfade:function(opacity)
	{
		if (opacity < 100) 
		{
			
			this.fader(this.currentImage,opacity);
			
			opacity += 10;
			window.setTimeout(this.obj+".crossfade("+opacity+")", 30);
		} 
		else 
		{
			
			this.fader(this.previousImage,0);
			
			this.previousImage=this.currentImage;
			this.currentImage+=1;
			
			if (this.currentImage>=this.galleryImages.length) 
			{
				
				this.currentImage=0;
			}
			
			this.galleryImages[this.previousImage].style.zIndex = 0;
			this.galleryImages[this.currentImage].style.zIndex = 100;
		
			opacity=0;
			window.setTimeout(this.obj+".crossfade("+opacity+")", this.delay);
		}
	}
};
