/*
************************************************************************************************
T.Heinze 05/23/03
Banner/Image Rotator JS object
************************************************************************************************
USAGE SAMPLE:
//interval in seconds, image path, width, height,0=iteration or 1= random
 objBannerRot = new Banner( 2, "banners",443, 58, 0 );
 
 // path to image, url, target window, and title message
 objBannerRot.addImg( "website.jpg", "/fw/main/default.asp?DocID=1", null, "Attack" );
 objBannerRot.addImg( "wireless.jpg", "/fw/main/default.asp?DocID=1", null, "Attack" );
 //write image and link html code and start slide show
 objBannerRot.output();
 //or, in order to attach to an existing image:
 objBannerRot.output(ImageName);
 
************************************************************************************************
*/

function Banner(refreshTime, path, width, height, random){
	this.objName = "slideShow" + (Banner.count++);
	eval(this.objName + "=this");

	if (!refreshTime) this.refreshTime = 5000; else this.refreshTime = refreshTime*1000;
	if (!width) this.width = 460; else this.width = width;
	if (!height) this.height = 70; else this.height = height;
	if (random == null) this.random = 1; else this.random = random;
	this.path = path;
	this.imgs = [];
	this.filters='';
	if(random==1) {this.currentImg=null} else {this.currentImg = 0};
	this.mySize = 0;

	this.addImg = function(src, href, target, title) {
		var tempImage = new Object();  //use new Image() for preloading, can be slow though
		tempImage.src = src;
		this.imgs[this.mySize] = new Object();
		var img = this.imgs[this.mySize];
		if (typeof(this.path) == "string") if(this.path.indexOf("/")==this.path.length-1) var sep=""; else var sep="/";
		if (typeof(this.path) == "undefined" || this.path == null || this.path=="") img.src=src; else img.src = this.path+sep+src;
		if (typeof(target) == "undefined" || target == null) img.target = "_self"; else img.target = target;
		img.href = href;
		img.title = title;
		this.mySize++;
	}

	this.addFilter = function( filter ) {
		if (! this.img.filters ) return;
		this.filters += " " + filter;
	}

	this.addURL = function( sURL ){
		this.finalURL=sURL;
	}
	
	this.commandFilters = function( method ) {
		var f, i = 0;
		while( f = this.img.filters[i++] ) {
			f[method]();
		}
	}
	
	this.link = function(){
		var	img = this.imgs[this.currentImg];
		if ((typeof(img.href) == "undefined")||(!img.href)||( img.href=="")){return;}
			
		if (img.target == "_self"){
			location.href = img.href;
		}
		else if (img.target == "_blank" || img.target == "_new"){
			open(img.href,this.objName + "Win");
		}
		else top.frames[img.target].location.href = img.href;
	}

	this.randomImg = function(){
		var n;
		do { n = Math.floor(Math.random() * (this.mySize)); } 
		while(n == this.currentImg);
		this.currentImg = n;
	}

	this.showStatus = function(){
		var img = this.imgs[this.currentImg];
		if (img.title) status = img.title;
		else status = img.href;
	}

	this.output = function(sImageName){
		if (this.mySize > 1){
			if (this.currentImg == null) this.randomImg();
			if (this.currentImg >= this.mySize) this.currentImg = this.mySize - 1;
			if(!sImageName){
				var tempCode = "";
				tempCode += '<a href="javascript:'+this.objName+'.link();" ';
				tempCode += 'title="'+this.imgs[this.currentImg].title + '" ';
				tempCode += ' onMouseOver="' + this.objName + '.showStatus(); return true"';
				tempCode += ' onMouseOut="status=\'\';return true">';
				tempCode += '<img align=absmiddle src="' + this.imgs[this.currentImg].src + '" width="' + this.width;
				tempCode += '" name="' + this.objName + 'Img" height="' + this.height + '" ';
				tempCode += 'alt="'+this.imgs[this.currentImg].title + '" ';
				tempCode += 'title="'+this.imgs[this.currentImg].title + '" ';
				tempCode += 'border="0" ></a>';
				document.write(tempCode);
				this.img=document.images[this.objName+ 'Img'];
				setTimeout(this.objName+'.newImg()',this.refreshTime)
			}else{
				if(document.images[sImageName]){
					this.img=document.images[sImageName];
					setTimeout(this.objName+'.newImg()',this.refreshTime);
				}else{
					//maybe not loaded yet, so try again in 500ms
					setTimeout(this.objName+'.output("'+sImageName+'")',500);
				};
			}
		} else {
			document.write("#Error: At least 2 banners must be defined.");
		}

	}

	this.newImg = function(){
		if (!this.random){	
			this.currentImg++;
			if (this.currentImg >= this.mySize){
			   this.currentImg = 0;
			   if(this.finalURL) if (typeof(this.finalURL) == "string") window.location.href=this.finalURL;
			}
		}
		else {
			this.randomImg();
		}
		this.nextImg();
	}

	this.nextImg = function(){
		this.img.style.filter = this.filters;
		if ( this.img.filters ) this.commandFilters( 'apply' );
		this.img.src = this.imgs[this.currentImg].src;
		this.img.title = this.imgs[this.currentImg].title;
		if ( this.img.filters ) this.commandFilters( 'play' );
		setTimeout(this.objName+'.newImg()',this.refreshTime)
	}
}

Banner.count = 0;