/////////////////////////////////
// File Name: mBnr.js       //
// Version: 2.1                  //
// By: Manish Kumar Namdeo     //
// http://www.mysticalm.com    //
/////////////////////////////////

// BANNER OBJECT
function Bnr(objName){
	this.obj = objName;
	this.aNodes = [];
	this.currentBnr = 0;
	this.changeOnRefresh = false;
	
};

// ADD NEW BANNER
Bnr.prototype.add = function(bnrType, bnrPath, bnrDuration, height, width, hyperlink, target) {
	this.aNodes[this.aNodes.length] = new Node(this.obj +"_"+ this.aNodes.length, bnrType, bnrPath, bnrDuration, height, width, hyperlink, target);
};

// Node object
function Node(name, bnrType, bnrPath, bnrDuration, height, width, hyperlink, target) {
	this.name = name;
	this.bnrType = bnrType;
	this.bnrPath = bnrPath;
	this.bnrDuration = bnrDuration;
	this.height = height
	this.width = width;
	this.hyperlink= hyperlink;
	this.target= target;
};

// Outputs the banner to the page
Bnr.prototype.toString = function() {
	var str = "";
	var iBnrIndex = 0;
	if(this.changeOnRefresh == true){
		// Read the cookie
		var BnrName = this.obj;
		var lastBnrIndex = readCookie(BnrName);

		if(isNaN(lastBnrIndex) == true || lastBnrIndex == null){
			iBnrIndex = 0;
		}else if(lastBnrIndex == '' || parseInt(lastBnrIndex) >= this.aNodes.length - 1){
			iBnrIndex = 0;
		}else{
			iBnrIndex = parseInt(lastBnrIndex) + 1;
		}
	
		// Set the new value
		createCookie(BnrName,iBnrIndex,7);
	}

	for (var iCtr=0; iCtr < this.aNodes.length; iCtr++){
		if(this.changeOnRefresh == true && iBnrIndex != iCtr){
			continue;
		}
		str = str + '<span name="'+this.aNodes[iCtr].name+'" '
		str = str + 'id="'+this.aNodes[iCtr].name+'" ';
		if(this.changeOnRefresh == true && iBnrIndex == iCtr){
			str = str + 'class="m_bnr_show" ';
		}else{
			str = str + 'class="m_bnr_hide" ';
		}
		str = str + 'bgcolor="#FFFCDA" ';	// CHANGE BANNER COLOR HERE
		str = str + 'align="center" ';
		str = str + 'valign="top" >\n';
			
		if ( this.aNodes[iCtr].bnrType == "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[iCtr].width+'" ';
			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" ';
			str = str + 'id="bnr_'+this.aNodes[iCtr].name+'" ';
			str = str + 'ALIGN="" ';
			str = str + 'VIEWASTEXT>';
			str = str + '<PARAM NAME=movie VALUE="'+ this.aNodes[iCtr].bnrPath + '">';
			str = str + '<PARAM NAME=quality VALUE=high>';
			str = str + '<PARAM NAME=bgcolor VALUE=#FFFCDA>';
			if (this.aNodes[iCtr].hyperlink != ""){
				str = str + '<PARAM NAME=flashvars VALUE="clickTag='+this.aNodes[iCtr].hyperlink;
				if(this.aNodes[iCtr].target != ""){
					str = str + '&clickTarget='+this.aNodes[iCtr].target;
				}
				str = str + '" />';
			}
			str = str + '<EMBED ';
			str = str + 'src="'+this.aNodes[iCtr].bnrPath+'" ';
			str = str + 'quality=high ';
//			str = str + 'bgcolor=#FFFCDA ';
			str = str + 'WIDTH="'+this.aNodes[iCtr].width+'" ';
			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" ';
			str = str + 'NAME="bnr_'+this.aNodes[iCtr].name+'" ';
			str = str + 'ALIGN="center" ';
			str = str + 'TYPE="application/x-shockwave-flash" ';
			str = str + 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" ';
			if (this.aNodes[iCtr].hyperlink != ""){
				str = str + 'FLASHVARS="clickTag='+this.aNodes[iCtr].hyperlink;
				if(this.aNodes[iCtr].target != ""){
					str = str + '&clickTarget='+this.aNodes[iCtr].target;
				}
				str = str + '" ';
			}
			
			str = str + '>';
			str = str + '</EMBED>'
			str = str + '</OBJECT>'
		}else if ( this.aNodes[iCtr].bnrType == "IMAGE" ){
			if (this.aNodes[iCtr].hyperlink != ""){
				str = str + '<a href="'+this.aNodes[iCtr].hyperlink+'" '
				if(this.aNodes[iCtr].target != ""){
					str = str + ' target="' + this.aNodes[iCtr].target + '" ';
				}
				str = str + '>';
			}
			str = str + '<img src="'+this.aNodes[iCtr].bnrPath+'" ';
			str = str + 'border="0" ';
			str = str + 'height="'+this.aNodes[iCtr].height+'" ';
			str = str + 'width="'+this.aNodes[iCtr].width+'">';
			if (this.aNodes[iCtr].hyperlink != ""){
				str = str + '</a>';
			}
		}


		str += '</span>';


	}
	return str;
};

// START THE BANNER ROTATION
Bnr.prototype.start = function(){
	if(this.changeOnRefresh == false){
		this.changeBnr();
		var thisBnrObj = this.obj;
		// CURRENT BANNER IS ALREADY INCREMENTED IN cahngeBnr() FUNCTION
		setTimeout(thisBnrObj+".start()", this.aNodes[this.currentBnr].bnrDuration * 1000);
	}
}

// CHANGE BANNER
Bnr.prototype.changeBnr = function(){
	var thisBnr;
	var prevBnr = -1;
	if (this.currentBnr < this.aNodes.length ){
		thisBnr = this.currentBnr;
		if (this.aNodes.length > 1){
			if ( thisBnr > 0 ){
				prevBnr = thisBnr - 1;
			}else{
				prevBnr = this.aNodes.length-1;
			}
		}
		if (this.currentBnr < this.aNodes.length - 1){
			this.currentBnr = this.currentBnr + 1;
		}else{
			this.currentBnr = 0;
		}
	}
	

	if (prevBnr >= 0){
		document.getElementById(this.aNodes[prevBnr].name).className = "m_bnr_hide";
	}
	document.getElementById(this.aNodes[thisBnr].name).className = "m_bnr_show";
}

// Following Cookie Code taken from http://www.quirksmode.org
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
