	/*
		Author: Sam Tsvilik
		Project: Ad Rotator
		Rev: 1.0
		Date: 02/16/2004
		Company: x10ded.com
		Contact: sam@x10ded.com
		Copyright© 2004 x10ded.com | All Rights Reserved!
		
		Notes: 		adRotator() uses a workaround technique to achieve timely rotation of the ad's.
		Background: After initial setTimeout call to getNext() function, setTimeout within that function
					looses awareness of itself as an instance and can not be called from within itself.
		Solution: 	You must pass the name of the instance of adRotator to getNext() function, so it would remember
					how to call itself from the outside.
		
	*/
	
	//Single entry object
	function adEntry(img, url) {
		this.imgSrc = img;
		this.url = url;
	}
	
	//Ad rotator object
	function adRotator() {
		//------------- Global Variables ---------------//
		this.url = ""; //Property that returns current url for the banner
		this.defaultImg = "/images/spacer.gif"; //Property that sets default image and returns current image source
		this.delay = 4000; //default rotation delay in miliseconds
		this.getNext = rotate; //Function that swaps an image and link url
		this.addEntry = add; //Method thad appends an entry to adRotator 
		this.adImg = document.imgBanner; //Reference to an IMG object in the document
		this.timer = null; //Property that returns current timeout id to stop rotation						
		this.startRotation = startRt;
		this.stopRotation = stopRt;
				
		//------------- Local Variables ---------------//
		var adList = new Array();	//Create array of links and banners			
		var limit = adList.length; 	//Init limit size
		var position = 0;			//Init location
		/*
			instance_name - Name of the variable instance of adRotator() object
			Ex:
				var ar = new adRotator();
				ar.getNext('ar');
		*/		
		function rotate(instance_name) {									
			if(limit > 0) {
				if(position >= limit) {
					position = 0;
					this.adImg.src = adList[position].imgSrc;
					this.url = adList[position].url;
					this.imgSrc = adList[position].imgSrc;
					position++;
				} else {
					this.adImg.src = adList[position].imgSrc;
					this.url = adList[position].url;
					this.imgSrc = adList[position].imgSrc;
					position++;
				}
				this.timer = setTimeout(instance_name+".getNext('" + instance_name + "')", this.delay);
			}
		}
		
		function add(img, url) {
			//Pre-Load Image to cache			
			var imgCache = new Image();
			imgCache.src = 	img;
			//Create single element array
			var tEntry = new Array(1);
			tEntry[0] = new adEntry(img, url);
			//Append to internal array
			adList = adList.concat(tEntry);
			//Resest limit
			limit = adList.length;
			this.adImg.src = this.defaultImg; //Set default image
		}
		
		function startRt(instance_name) {
			this.timer = setTimeout(instance_name+".getNext('" + instance_name + "')", this.delay);
		}
		function stopRt() {
			clearTimeout(this.timer);
		}
	}
