﻿
function AdRotator() {

	var _debug = YAHOO.util.History.getQueryStringParameter('bnhdebug') == '1';


	//{bannerImgId:'Lb1Ad', this._imageFileNames: new Array('Evolve.png', 'TW_Banner.bmp', 'Forum.png','ez_slalom_468x80_v1.jpg'), imageURLs: new Array('http://ballofspray.com/evolve', 'http://ballofspray.com/joomla/index.php?option=com_banners&task=click&bid=23', 'http://ballofspray.com/vanillaforum/', 'http://ballofspray.com/joomla/index.php?option=com_banners&task=click&bid=24')}
	this.Init = function(args) {
		this.AD_ROTATE_MIN = 3000;	//If displayInterval is set below this constant, then ads will not be rotated.
		
		this._imageFolder = 'images';
		if (args.imageFolder != undefined) this._imageFolder = args.imageFolder;

		//this._imageFileNames is required
		if (args.bannerImgId == undefined) {
			if (this._debug) alert('bannerImgId cannot be null');
			return;
		}
		else
		{
			this._bannerImgId = args.bannerImgId;
		}
		
		//this._imageFileNames is required
		if (args.imageFileNames == undefined) {
			if (this._debug) alert('this._imageFileNames cannot be null');
			return;
		}
		else if (! YAHOO.lang.isArray(args.imageFileNames)) {
			if (this._debug) alert('this._imageFileNames must be an Array');
		}
		else {
			this._imageFileNames = args.imageFileNames;
			this._rnd = Math.round(Math.random() * (this._imageFileNames.length - 1));
			this._numberOfImages = this._imageFileNames.length;
		}
		
		//imageURLs is required
		if (args.imageURLs == undefined) {
			if (this._debug) alert('imageURLs cannot be null');
			return;
		}
		else if (! YAHOO.lang.isArray(args.imageURLs)) {
			if (this._debug) alert('imageURLs must be an Array');
		}
		else {
			this._imageURLs = args.imageURLs;
		}
		
		//displayInterval is required
		if (args.displayInterval == undefined) {
			this._displayInterval = 0;
		}
		else {
			if (!YAHOO.lang.isNumber(args.displayInterval)) {
				if (this._debug) alert('displayInterval must be a number');
				this._displayInterval = 0;
				return;
			}
			else {
				this._displayInterval = args.displayInterval;
			}
		}
		
		this._sPath = window.location.pathname;
		this._sPage = this._sPath.substring(this._sPath.lastIndexOf('/') + 1);

		this._bannerAdImages = new Array(this._numberOfImages);

		//Only rotate ads if displayInterval is reasonable
		if (this._displayInterval >= this.AD_ROTATE_MIN) {
			this.InitialiseBannerAdRotator();
			BnhAdRotatorChangeImage(this);
		}
		
		this.startBanner();
	}
	
	this.startBanner = function(message) {
		var bannerAd = document.getElementById(this._bannerImgId);

		//the first banner image is created in the fly. If ok to rotate ads, the rest are preloaded in InitialiseBannerRotator()
		bannerAd.src = ' ' + this._imageFolder + '/' + this._imageFileNames[this._rnd];
		bannerAd.style.visibility = 'visible';

		if (this._sPage == undefined) {
			this._sPath = window.location.pathname;
			this._sPage = this._sPath.substring(this._sPath.lastIndexOf('/') + 1);
		}
		var b = pageTracker._trackEvent(this._imageFileNames[this._rnd],this._imageFileNames[this._rnd]+":"+this._sPage+':'+this._bannerImgId+':view');
		if (_debug) alert(b);
		//document.getElementById('msg').innerHTML = this._rnd + ':'+this._bannerImgId+this._imageFileNames[this._rnd]+':view<br>' + document.getElementById('msg').innerHTML;		
	}

	this.InitialiseBannerAdRotator = function() {
	
		
		for (i = 0; i < this._numberOfImages; i++) {
			this._bannerAdImages[i] = new Image();
			this._bannerAdImages[i].src = ' ' + this._imageFolder + '/' + this._imageFileNames[i];
		}
		
	}
	
	
	this.ChangePage = function() {
		window.open(this._imageURLs[this._rnd],'_blank');
		//document.getElementById('msg').innerHTML = this._rnd+':'+this._bannerImgId+this._imageURLs[this._rnd]+':click<br>' + document.getElementById('msg').innerHTML;
		//alert(pageTracker._trackEvent('category', 'action'));
		if (this._sPage == undefined) {
			this._sPath = window.location.pathname;
			this._sPage = this._sPath.substring(this._sPath.lastIndexOf('/') + 1);
		}
		var b = pageTracker._trackEvent(this._imageFileNames[this._rnd],this._imageFileNames[this._rnd]+":"+this._sPage+':'+this._bannerImgId+':click');
		if (_debug) alert(b);
		//document.getElementById('msg').innerHTML += this._rnd+':'+this._bannerImgId+this._imageFileNames[this._rnd]+':click<br>';
	}		
		
}

//Function to change the src of the Banner Ad image
function BnhAdRotatorChangeImage(obj) {
	//If can't rotate ads, don't bother incrementing the pointer.
	//document.getElementById('msg').innerHTML += 'BnhAdRotatorChangeImage updating img: '+ obj._bannerImgId + ' every '+ obj._displayInterval +'<br>';
	if (obj._displayInterval >= obj.AD_ROTATE_MIN) {	

	//document.getElementById('msg').innerHTML += '_rnd = '+obj._rnd+'<br>';

		
		obj._rnd += 1;
	
		if (obj._rnd == obj._numberOfImages) {
			obj._rnd = 0;
		}
	}
	document.getElementById(obj._bannerImgId).src = obj._bannerAdImages[obj._rnd].src;
	_timerObject = setTimeout(function(){BnhAdRotatorChangeImage(obj);}, obj._displayInterval);

	if (obj._sPage == undefined) {
		obj._sPath = window.location.pathname;
		obj._sPage = obj._sPath.substring(obj._sPath.lastIndexOf('/') + 1);
	}
	var b = pageTracker._trackEvent(obj._imageFileNames[obj._rnd],obj._imageFileNames[obj._rnd]+":"+obj._sPage+':'+obj._bannerImgId+':view');
	if (obj._debug) alert(b);
	//document.getElementById('msg').innerHTML = obj._rnd +':'+this._bannerImgId+obj._imageFileNames[obj._rnd]+':view<br>' + document.getElementById('msg').innerHTML;

}
