var banner = {
	// Multiple Rotating Banner ads object
	
	// bannerObjects is a queue-style array that will hold banners
	bannerObjects: null,
	
	// banner object constructor
	banner: function(obj, obj_name) {
		this.bannerName = obj_name;
		this.bannerObject = document.getElementById(obj);
		this.banners = [];
		this.currentBanner = 0;
		this.numberOfBanners = 0;
	},
		
	// setInterval ID
	intervalId: null,
	
	// Start function
	start: function() {
		// Check bannerObjects for NULL, if it is, the script has not been filled with data yet. Warn user.
		if (!this.bannerObjects) {
			alert('The banner code hasn\'t been initialized. Please add Banner Objects and Banners before Starting.');
			return false;
		}		
		
		this.rotate();
		
		// Check to see if script has set up an interval for rotate
		if (!this.intervalId) { this.intervalId = setInterval('banner.rotate()', 30000); }
				
		return true;	
	},
	
	// Add a banner Object
	addBannerObject: function(obj, obj_name) {
		// Test Type of object passed
		if (typeof(obj) != 'string') {
			alert('You must pass the ID of the object for addBannerObject()');
			return false;
		}
		
		// Test to see if obj even exists on the page - if so, exit - don't want nasty errors
		if (document.getElementById(obj) === null) {
			// Element isn't on page, exit
			return false;
		}
			
		// Test to see if bannerObjects is defined
		if (!this.bannerObjects) {
			// bannerObjects is NULL, create it. Then make the first element a banner object
			this.bannerObjects = [];
			this.bannerObjects[0] = new this.banner(obj, obj_name);
		} else {
			// Check to see if banner object has already been created (only if NOT the first entry)
			var len = this.bannerObjects.length;
			
			for (var ndx = 0; ndx < len; ndx++) {
				if (this.bannerObjects[ndx].bannerName == obj_name) {
					alert('The banner ' + obj_name + ' has already been created.');
					return false;
				}			
			}
		
			// If banner is non existant, create it
			this.bannerObjects[this.bannerObjects.length] = new this.banner(obj, obj_name);
		}
		
		return true;
	},
	
	// Add an image and href URL to the banner list
	addBanner: function(obj_name, href_url, img_url, alt_text) {
		// Flag for checking if banner object has been created
		var flagBeenCreated = false;
		
		// Place holder for the index of bannerObjects the banner object is found at
		var atNdx = 0;
		
		// Check to see if banner object has been created (if SO, set flag to true)
		var len = this.bannerObjects.length;
		
		for (var ndx = 0; ndx < len; ndx++) {
			if (this.bannerObjects[ndx].bannerName == obj_name) {
				flagBeenCreated = true;
				atNdx = ndx;
			}
		}
		
		if (!flagBeenCreated) {
			return false;
		} else {
			// Add the banner to the bannerObjects queue, and increment the number of banners for that banner object
			this.bannerObjects[atNdx].banners[this.bannerObjects[atNdx].numberOfBanners] = href_url + ';' + img_url + ';' + alt_text;
			this.bannerObjects[atNdx].numberOfBanners++;
		}
						
		return true;	
	},	
	
	// Rotate the banners			
	rotate: function() {
		// Scan trough banners and rotate banners
		var len = this.bannerObjects.length;
		
		for (var ndx = 0; ndx < len; ndx++) {
			// temporary string & array storage
			var tmp = "";
			var tmp_array = [];
			
			tmp = this.bannerObjects[ndx].banners[this.bannerObjects[ndx].currentBanner];
			tmp_array = tmp.split(';');
			
			this.bannerObjects[ndx].bannerObject.href = tmp_array[0];
			this.bannerObjects[ndx].bannerObject.getElementsByTagName('img')[0].src = tmp_array[1];
			this.bannerObjects[ndx].bannerObject.getElementsByTagName('img')[0].alt = tmp_array[2];
			this.bannerObjects[ndx].bannerObject.getElementsByTagName('img')[0].title = tmp_array[2];
			
			if (this.bannerObjects[ndx].currentBanner >= this.bannerObjects[ndx].numberOfBanners - 1) {
				this.bannerObjects[ndx].currentBanner = 0;
			} else {
				this.bannerObjects[ndx].currentBanner++;
			}
		}
				
		return true;
	}
};