/* **************************	*/
/* TICKER 2 BY 1 v.1.5		*/
/* MARCO JONATHAN ROSSI		*/
/* 2007 JULY 12; 2008 FEB 19	*/
/* **************************	*/

function mjrTicker_2By1(pName) {
	/* CONSTRUCTOR FOR mjrTicker_2By1 CLASS */
	/* NAMING CONVENTION: tck{pName} */
	/* VARIABLES */
	this.name		= pName;				// NAME OF THIS
	this.data 		= new Array(); 			// data array
	this.num		= 0;					// data offset
	this.objnum		= 1;					// object offset, start at 1
	this.objnum_o	= 0;					// interval
	this.interval 	= null;				// interval on each run

	/* TICKER DATA :: DEFAULT */
	this.itemPerTime 		= 2;		// how many items to appear per time
	this.displayTime 		= 1000;	// item display per time
	this.scrollDelay 		= 50;		// time to delay per scrolling
	this.scrollAmount 	= 5;
	
	this.maxItemPerTime	= 2;		// MAXIMUM ITEM PER TIME
	
	/* TICKER ITEM */
	this.eachHeight	= 0;		// HEIGHT OF EACH ITEM
	
	
	/* GLOBAL FUNCHOLDER */
	if(!window.FUNCHLD) window.FUNCHLD = new Array();
	var thisInd = window.FUNCHLD.length;
	window.FUNCHLD[thisInd] = this;
	

	// FIRSTRUN
	this.isFirstRun = true;
	
	
	this.objToKill	= null;

	/* START TICKER */
	this.start = function () {
		/* STARTS THE TICKER */
		/* INITIAL NUMBER: 0 */
		this.num = 0;
		
		// marks of error
		/*if(this.haserror) {
			alert("Warning: there is an error in the event setup. Please contact the webmaster for details.");
		}*/
		
		/* REFERENCES TO OBJECTS */
		var objMain = document.getElementById("pnl" + this.name + "_Main");
		var objPut 	= document.getElementById("pnl" + this.name + "_Move");
		
		/* INITIAL TOP OF objPut IS 0 px */
		//objPut.style.top = "-" + this.eachHeight + "px";
		
		/* INITIAL objnum_o */
		this.objnum_o = this.objnum;
		
	
		/* PLAY THE TICKER: BUT DELAY THE INITIAL PLAYING */
		setTimeout("FUNCHLD["+thisInd+"].addItem();", this.itempertime);
	}
	
	/* ADD NEW ITEM TO THE TICKER */
	this.addItem = function () {
		var objPut = document.getElementById("pnl" + this.name + "_Move");

		/* COUNT ALL ITEMS IN THE objPut */
		var t_ct = 0;
		var t_ni = 0;
		var t_tItem = objPut.firstChild;
		if(t_tItem) {
			do {
				if(t_tItem.tagName)
					t_ni++;
			} while(t_tItem = t_tItem.nextSibling);
		}
		if(t_ni) t_ct = t_ni - 1;
		
		
		
		/* ADD THE ITEM */
		// THIS WILL PUT ALL THE TICKER UNTIL  THE TICKER CONTENT IS MORE THAN THE PARENT
		var visibleHeight = objPut.offsetHeight - objPut.offsetTop;
		if(visibleHeight < (objPut.parentNode.offsetHeight * 2)) {
			do {
				this.insertNewItem(objPut);
				var visibleHeight = objPut.offsetHeight - objPut.offsetTop;
				t_ct++;
			} while(visibleHeight < (objPut.parentNode.offsetHeight * 2) && t_ct< this.maxItemPerTime);
		}
		

		// DELAY PLAYING FIRST ON FIRSTRUN
		if(this.isFirstRun) {
			this.isFirstRun = false;
			setTimeout("FUNCHLD["+thisInd+"].addItem();", this.displayTime);
		} 
		else {
			/* PLAY THE TICKER */
			clearInterval(this.interval);
			this.interval = setInterval("FUNCHLD["+thisInd+"].playTick();", this.scrollDelay);
		}
	}
	
	/* INSERT AN ITEM */
	this.insertNewItem = function(objPut) {
		// PROCESS THE CONTENT STRING
		// MADE BY MJR: MAY 20, 2008. THIS ONE WILL REPLACE THE TEXT CONTAINING IN <!--t:name--> TO this.data[n][name].
		/*	Example: 
			contentTemp = "<B><!--t:date--></B>";  data[n] = { date: 12 }
			will result in
			"<B>12</B>",
		*/
		var t_contentString = this.contentTemp;
		var t_curData = this.data[this.num];
		for(var t_indexName in t_curData) {
			t_contentString = t_contentString.split("<!--t:" + t_indexName + "-->").join(t_curData[t_indexName]);
		}
	
		/* CREATE ITEM NODE */
		var t_temp;
		var newnode = document.createElement("LI");
		newnode.id 			= "pnl" + this.name + "_p" + this.objnum;
		newnode.className 	= this.contentClass;
		newnode.style.height	= (t_temp = this.eachHeight) ? t_temp : "";
		newnode.innerHTML 	= t_contentString;
		objPut.appendChild( newnode );
		
		/* INCREMENT THE OBJECT */
		this.objnum++;		// for the object id
		if(this.objnum>100) 
			this.objnum = 1;
			
		/* GO TO NEXT DATA */
		this.num++;			// for the data
		if(this.num >= this.data.length) 
			this.num=0;
	}


	
	/* PLAYS THE TICKER */
	this.playTick = function () {
		/* REFERENCES TO OBJECT */
		var objPut = document.getElementById("pnl" + this.name + "_Move");
		// FIND THE FIRST ITEM OF THE NEWS LIST
		var objFirstItem = objPut.firstChild;
		if(objFirstItem) {
			while(true) {
				if(objFirstItem.tagName == "LI") break;
				else if(!(objFirstItem = objFirstItem.nextSibling)) break;
			}
			// FIND THE SECOND ITEM
			var objNextItem = objFirstItem.nextSibling;
			while(true) {
				if(objNextItem.tagName == "LI") break;
				else if(!(objNextItem = objNextItem.nextSibling)) break;
			}
		}

		/* GET THE style.top */
		var objtop = parseInt(objPut.style.top);
		if(isNaN(objtop)) 
			objtop = 0;
			
			
		/* INCREMENT THE TOP: SCROLL IT */
		objtop -= this.scrollAmount;
		objPut.style.top = "" + objtop + "px";
		
		
		
		/* IF top REACHES THE height OF EACH ITEM, KILL THE OBJECT TO BE KILLED */
		var objFirstItemFullHeight = objNextItem.offsetTop;
		if(objtop <= -objFirstItemFullHeight) {
			if(objFirstItem)
				objPut.removeChild( objFirstItem );
			objPut.style.top = "0px";
			
			/* SET THE NEXT TICKER MOVE TO DELAY FOR this.displayTime */
			clearInterval(this.interval);
			setTimeout("FUNCHLD["+thisInd+"].addItem();", this.displayTime);
		}
	}
}