/*=============================
	Copyright by dragonimp
	All rights reserved.
	
  Description:class for TBanner
  
  version0.1 2004.8.26  first thought to make a class for banner
  version1.0 2004.11.24 simple function to support a banner
  version1.1 2004.12.10 basic effect in class
  version1.2 2005.2.6   setEffect to add other filters and effects
  
  Email:dragonimp@impx.net
  HomePage:http://www.impx.net
==============================
//Usage:(4 steps to run a banner)
	0. First of all, you should include this file in your source.
  1. new TBanner to initialize a new TBanner object
	2. init the base path of images, names of images, and urls for links. first as default;
  3. setEffect to play unique effection. If omitted, it will play with default effection.
	4. start with interval time.

	//example:
	var banner_example=new TBanner();
	banner_example.init("/images/rotate/",new Array("r1.gif","r2.gif","r3.gif"),new Array("url1","url2","url3"));
	//banner_example.setEffect("progid:DXImageTransform.Microsoft.GradientWipe(duration=1.5,gradientSize=0.75,motion=forward)",null)
	//banner_example.setEffect("revealTrans(Duration=2.0,Transition=1)",function(obj){obj.filters[0].Transition = Math.floor(Math.random() * 23);})
	banner_example.start(5000);//millisecond
	
==============================*/


function TBanner()
{
	//create a contianer
	//====================================	
	this.container=document.createElement("div");
	//document.body.appendChild(this.banner); //this cannot work at unstandard env.
	var contianerID="TBannerContainer_"+this.container.uniqueID;
	document.write("<div id='"+contianerID+"'></div>");
	delete this.container;
	this.container=eval(contianerID);
			
	this.image=window.document.createElement("img");
	this.link=window.document.createElement("a");
	
	this.container.appendChild(this.link);
	this.link.appendChild(this.image);	
	//====================================
	
	this.id="banner_"+this.container.uniqueID;
	eval(this.id+"=this");//set the REAL bannerid as this object

	this.container.style.filter="revealTrans(Duration=2.0,Transition=1)";
	this.container.margin=0;
	this.container.padding=0;
	
	this.image.border=0;
	this.image.style.filter=this.container.style.filter;
	this.image.alt=this.id;
	
	this.link.target="_blank";	
	
	this.index=0;
	this.delaytime=5000;
	this.timer=null;

	
	//public:

	this.init = function(basepath,images,urls)
	{
		this.imagebasepath=basepath;
		this.images=images;
		this.urls=urls;
		this.PreloadImages(images);
					
		this.switchBanner();//show first banner
	}
	
	this.start = function(delaytime)
	{	
		this.delaytime=(delaytime==null)?this.delaytime:delaytime;
		this.timer=setInterval(this.id+".play()",this.delaytime);
	}
	
	this.stop = function()
	{
		window.clearTimeout(this.timer);
		this.timer=null;
	}
	
	this.setEffect = function(filter,fnSwitch)
	{
		this.container.style.filter=filter;
		this.image.style.filter=filter;
		this.switchEffect=(fnSwitch==null)?function(){}:fnSwitch;
	}
		
	//switch to next banner and play effection
	this.play = function()
	{
		obj=this.image;//this.container doesn't work, why???
		//obj.style.visibility="hidden";
		obj.filters[0].Apply();
		this.switchBanner();
		obj.style.visibility="visible";
		obj.filters[0].Play();		
		
		this.switchEffect(obj);
	};
	
	
	//private:
	
	//default Effect Action
	this.switchEffect = function(obj)
	{
		obj.filters[0].Transition = Math.floor(Math.random() * 23);
	}
		
	//show next banner
	this.switchBanner = function()
	{
		var banner=this;
		if (banner.index<banner.urls.length)//change link
			banner.link.href=banner.urls[banner.index];
		else
		{
			if (banner.urls[0]!=null)
			{
				banner.link.href=banner.urls[0];
				banner.link.title=banner.link.href;
				banner.link.disabled=false;
				banner.link.target="_self";
			}
			else
			{
				banner.link.href="#";
				banner.link.target="_self";
				banner.link.disabled=true;
			}
		}
		banner.image.src=banner.imagebasepath+banner.images[banner.index];

		banner.index++;
		if(banner.index >= banner.images.length)banner.index=0;
	}	

	//Load images before play
	this.PreloadImages = function(images)
	{
		var preloadedimages=new Array()
		for (i=0;i<images.length;i++)
		{
			preloadedimages[i]=new Image()
			preloadedimages[i].src=images[i];
		}
		delete preloadedimages;
	}
}
