/////////////////////////////////
// File Name: mBannerRandom.js //
// Forked Version 2.1.3        //
//   John McBride 7 Sep 11     //
// based on:
// mBanner.js
// Version: 2.1                //
// By: Manish Kumar Namdeo     //
// http://www.mysticalm.com    //
/////////////////////////////////

// BANNER OBJECT
function Banner(objName){
	this.obj = objName;
	this.aNodes = [];
	this.currentBanner = 0;
	this.previousBanner = 0;
	this.firstCycleCount = 0;
	this.firstCycleDone = [];
}

// ADD NEW BANNER
Banner.prototype.add = function(bannerType, bannerPath, bannerDuration, height, width, hyperlink, target) {
	this.firstCycleDone[this.aNodes.length] = false;
	this.aNodes[this.aNodes.length] = new Node(this.obj +"_"+ this.aNodes.length, bannerType, bannerPath, bannerDuration, height, width, hyperlink, target);
}

// Node object
function Node(name, bannerType, bannerPath, bannerDuration, height, width, hyperlink, target) {
	this.name = name;
	this.bannerType = bannerType;
	this.bannerPath = bannerPath;
	this.bannerDuration = bannerDuration;
	this.height = height
	this.width = width;
	this.hyperlink= hyperlink;
	this.target= target;
}

// Outputs the banner to the page
Banner.prototype.toString = function() {
	var str = "";
	var start_idx = Math.floor(Math.random()*this.aNodes.length);
	for (var idx = 0 ; idx != this.aNodes.length ; idx++){
		str = str + '<span name="' + this.aNodes[idx].name + '" '
		str = str + 'id="' + this.aNodes[idx].name + '" ';
		if( idx == start_idx ){
		        this.currentBanner = idx
			str = str + 'class="m_banner_show" ';
		}else{
			str = str + 'class="m_banner_hide" ';
		}
		str = str + 'bgcolor="#FFFCDA" ';	// CHANGE BANNER COLOR HERE
		str = str + 'align="center" ';
		str = str + 'valign="top" >\n';
			
		if ( this.aNodes[idx].bannerType == "FLASH" ){
			str = str + '<OBJECT ';
			str = str + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
			str = str + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ';
			str = str + 'WIDTH="'+this.aNodes[idx].width+'" ';
			str = str + 'HEIGHT="'+this.aNodes[idx].height+'" ';
			str = str + 'id="bnr_'+this.aNodes[idx].name+'" ';
			str = str + 'ALIGN="" ';
			str = str + 'VIEWASTEXT>';
			str = str + '<PARAM NAME=movie VALUE="'+ this.aNodes[idx].bannerPath + '">';
			str = str + '<PARAM NAME=quality VALUE=high>';
			str = str + '<PARAM NAME=bgcolor VALUE=#FFFCDA>';
			if (this.aNodes[idx].hyperlink != ""){
				str = str + '<PARAM NAME=flashvars VALUE="clickTag='+this.aNodes[idx].hyperlink;
				if(this.aNodes[idx].target != ""){
					str = str + '&clickTarget='+this.aNodes[idx].target;
				}
				str = str + '" />';
			}
			str = str + '<EMBED ';
			str = str + 'src="'+this.aNodes[idx].bannerPath+'" ';
			str = str + 'quality=high ';
//			str = str + 'bgcolor=#FFFCDA ';
			str = str + 'WIDTH="'+this.aNodes[idx].width+'" ';
			str = str + 'HEIGHT="'+this.aNodes[idx].height+'" ';
			str = str + 'NAME="bnr_'+this.aNodes[idx].name+'" ';
			str = str + 'ALIGN="center" ';
			str = str + 'TYPE="application/x-shockwave-flash" ';
			str = str + 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" ';
			if (this.aNodes[idx].hyperlink != ""){
				str = str + 'FLASHVARS="clickTag='+this.aNodes[idx].hyperlink;
				if(this.aNodes[idx].target != ""){
					str = str + '&clickTarget='+this.aNodes[idx].target;
				}
				str = str + '" ';
			}
			
			str = str + '>';
			str = str + '</EMBED>'
			str = str + '</OBJECT>'
		}else if ( this.aNodes[idx].bannerType == "IMAGE" ){
			if (this.aNodes[idx].hyperlink != ""){
				str = str + '<a href="'+this.aNodes[idx].hyperlink+'" '
				if(this.aNodes[idx].target != ""){
					str = str + ' target="' + this.aNodes[idx].target + '" ';
				}
				str = str + '>';
			}
			str = str + '<img src="'+this.aNodes[idx].bannerPath+'" ';
			str = str + 'border="0" ';
			str = str + 'height="'+this.aNodes[idx].height+'" ';
			str = str + 'width="'+this.aNodes[idx].width+'">';
			if (this.aNodes[idx].hyperlink != ""){
				str = str + '</a>';
			}
		}


		str += '</span>';


	}
	return str;
}

// START THE BANNER ROTATION
Banner.prototype.start = function(){
	this.changeBanner();
	var thisBannerObj = this.obj;
	// CURRENT BANNER IS ALREADY INCREMENTED IN cahngeBanner() FUNCTION
	setTimeout(thisBannerObj+".start()", this.aNodes[this.currentBanner].bannerDuration * 1000);
}

// CHANGE BANNER
// fork 2.1.1 => banner changes are now random rather than simply sequential
Banner.prototype.changeBanner = function(){
        if ( this.aNodes.length != 0 ){
	    this.previousBanner = this.currentBanner;
	    while( this.previousBanner == this.currentBanner )
	    {
	      var rand = Math.floor(Math.random()*this.aNodes.length);
	      if( this.firstCycleCount < this.aNodes.length ){
		while( this.firstCycleDone[rand] ){  //skip any already seen banners during first cycle
	          rand = Math.floor(Math.random()*this.aNodes.length);
		}
		this.currentBanner = rand;
		this.firstCycleDone[rand] = true;
		this.firstCycleCount += 1;
	      }else{
	        this.currentBanner = rand;
	      }
	    }
            document.getElementById(this.aNodes[this.previousBanner].name).className = "m_banner_hide";
            document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
	}
}

