/**
* REQUIRES: browserFuncs.js
*/


/**
* working example;
* requires div/span elements for the grip(s)
*/
var FLASH_SPEED = 120;
var RANGE_BEG = 500;
var RANGE_END = 6000;
var aFadeVals =	[	{"clr":"#555555","ltrSpc":"0px"},	{"clr":"#aaaaaa","ltrSpc":"1px"},
							{"clr":"#ffffff","ltrSpc":"2px"},	{"clr":"#eeeeee","ltrSpc":"3px"},
							{"clr":"#dddddd","ltrSpc":"4px"},	{"clr":"#cccccc","ltrSpc":"5px"},
							{"clr":"#bbbbbb","ltrSpc":"6px"},	{"clr":"#aaaaaa","ltrSpc":"7px"},
							{"clr":"#999999","ltrSpc":"8px"},	{"clr":"#888888","ltrSpc":"9px"},
							{"clr":"#777777","ltrSpc":"10px"},	{"clr":"#666666","ltrSpc":"11px"},
							{"clr":"#555555","ltrSpc":"12px"},	{"clr":"#444444","ltrSpc":"13px"},
							{"clr":"#333333","ltrSpc":"14px"},	{"clr":"#222222","ltrSpc":"15px"},
							{"clr":"#111111","ltrSpc":"16px"},	{"clr":"#000000","ltrSpc":"17px"}
						];

var aFadeValsLnk =[	{"clr":"#5C5C73","ltrSpc":"0px"},	{"clr":"#B8B8E6","ltrSpc":"1px"},
							{"clr":"#ccccff","ltrSpc":"2px"},	{"clr":"#C2C2F3","ltrSpc":"3px"},
							{"clr":"#B8B8E6","ltrSpc":"4px"},	{"clr":"#AEAED9","ltrSpc":"5px"},
							{"clr":"#A3A3CC","ltrSpc":"6px"},	{"clr":"#9999BF","ltrSpc":"7px"},
							{"clr":"#8F8FB3","ltrSpc":"8px"},	{"clr":"#8585A6","ltrSpc":"9px"},
							{"clr":"#7A7A99","ltrSpc":"10px"},	{"clr":"#70708C","ltrSpc":"11px"},
							{"clr":"#5C5C73","ltrSpc":"12px"},	{"clr":"#474759","ltrSpc":"13px"},
							{"clr":"#333340","ltrSpc":"14px"},	{"clr":"#282833","ltrSpc":"15px"},
							{"clr":"#141419","ltrSpc":"16px"},	{"clr":"#000000","ltrSpc":"17px"}
						];


function initTxtFlasher(oCont) {
	// use this to determine coords
	//oCont.onclick = function() { window.status = (event.clientX - oCont.offsetLeft) + "," + (event.clientY - oCont.offsetTop); }  

	var aItems =
		[
		{"id":"flshr00", "left":330, "top":030, "dir":"right", "lbl":"Multimedia"},
		{"id":"flshr01", "left":425, "top":440, "dir":"left", "lbl":"Website"},
		{"id":"flshr02", "left":365, "top":115, "dir":"right", "lbl":"Application"},
		{"id":"flshr03", "left":185, "top":400, "dir":"center", "lbl":"CD ROM"},
		{"id":"flshr04", "left":010, "top":070, "dir":"left", "lbl":"Web Enabled"},
		{"id":"flshr05", "left":080, "top":470, "dir":"left", "lbl":"Database"},
		{"id":"flshr06", "left":355, "top":315, "dir":"right", "lbl":"Animation"},
		{"id":"flshr07", "left":130, "top":260, "dir":"center", "lbl":"3 Dimensional"},
		{"id":"flshr08", "left":070, "top":140, "dir":"right", "lbl":"Online"},
		{"id":"flshr09", "left":015, "top":340, "dir":"left", "lbl":"Intranet"}
		];

	var oNewEl, oTmp;
	for(var i=0, iLen=aItems.length; i<iLen; i++) {
		oNewEl = document.createElement("DIV");
		oNewEl.id = aItems[i].id;
		oNewEl.style.position = "absolute";
		//oNewEl.style.backgroundColor = "red";
		oNewEl.noWrap = true;

		oNewEl.innerHTML = aItems[i].lbl;
		oNewEl.style.letterSpacing = aFadeVals[aFadeVals.length-1].ltrSpc;

		oCont.appendChild(oNewEl);
		if(document.all) oNewEl.style.pixelWidth = oNewEl.offsetWidth;// this is for IE 5.5+ that needs the width set to work properly

		new JMTxtFlasher(oNewEl, aItems[i].lbl, FLASH_SPEED, RANGE_BEG, RANGE_END, aItems[i].top, aItems[i].left, aFadeVals, aItems[i].dir).go();

//		new JMTxtFlasher(getLayer(aItems[i].id), aItems[i].lbl, FLASH_SPEED, RANGE_BEG, RANGE_END, aItems[i].top, aItems[i].left, aFadeVals, aItems[i].dir).go();
	}
/*
	oTxtFlasher00 = new JMTxtFlasher(getLayer("flshr00"), "Sporting Events",				FLASH_SPEED, RANGE_BEG, RANGE_END, 100, 300, aFadeVals, "right");
	oTxtFlasher01 = new JMTxtFlasher(getLayer("flshr01"), "Corporate Transportation",		FLASH_SPEED, RANGE_BEG, RANGE_END, 140, 150, aFadeVals, "left");
	oTxtFlasher02 = new JMTxtFlasher(getLayer("flshr02"), "Proms",							FLASH_SPEED, RANGE_BEG, RANGE_END, 150, 650, aFadeVals, "right");
	oTxtFlasher03 = new JMTxtFlasher(getLayer("flshr03"), "Out-of-state Trips",				FLASH_SPEED, RANGE_BEG, RANGE_END, 210, 250, aFadeVals, "center");
	oTxtFlasher04 = new JMTxtFlasher(getLayer("flshr04"), "City Tours",						FLASH_SPEED, RANGE_BEG, RANGE_END, 60, 10, aFadeVals, "left");
	oTxtFlasher05 = new JMTxtFlasher(getLayer("flshr05"), "Bachelor/Bachelorette Parties",	FLASH_SPEED, RANGE_BEG, RANGE_END, 450, 30, aFadeVals, "left");
	oTxtFlasher06 = new JMTxtFlasher(getLayer("flshr06"), "Weddings",						FLASH_SPEED, RANGE_BEG, RANGE_END, 350, 520, aFadeVals, "center");
	oTxtFlasher07 = new JMTxtFlasher(getLayer("flshr07"), "Sight-seeing",					FLASH_SPEED, RANGE_BEG, RANGE_END, 270, 200, aFadeVals, "center");
	oTxtFlasher08 = new JMTxtFlasher(getLayer("flshr08"), "Casino Tours",					FLASH_SPEED, RANGE_BEG, RANGE_END, 400, 300, aFadeVals, "right");
	oTxtFlasher09 = new JMTxtFlasher(getLayer("flshr09"), "Quinceaneras",					FLASH_SPEED, RANGE_BEG, RANGE_END, 320, 15, aFadeVals, "left");

	oTxtFlasher00.go();
	oTxtFlasher01.go();
	oTxtFlasher02.go();
	oTxtFlasher03.go();
	oTxtFlasher04.go();
	oTxtFlasher05.go();
	oTxtFlasher06.go();
	oTxtFlasher07.go();
	oTxtFlasher08.go();
	oTxtFlasher09.go();
	oTxtFlasher10.go();

	// Enter msg
	var sEnterTxt = (document.all)? "Enter" : "Enter -->";
	new JMTxtFlasher(getLayer("flshr10"), sEnterTxt, 50, 150, 150, 30, 30, aFadeValsLnk, "center").go();
*/
}


/**
* constructor
*/
function JMTxtFlasher(grip, sTxt, iFlashSpeed, iRngBeg, iRngEnd, iTop, iLeft, aVals, sDir) {
	this.type = "(C)2001-2006 Joe McKeown";
	this.grip = grip;
	this.sTxt = sTxt;
	this.iFlashSpeed = parseInt(iFlashSpeed);	// in milliseconds
	this.iRngBeg = parseInt(iRngBeg);			// in milliseconds
	this.iRngEnd = parseInt(iRngEnd);			// in milliseconds
	this.iTopOrig = parseInt(iTop);
	this.iLeftOrig = parseInt(iLeft);
	this.aVals = aVals;
	this.sDir = sDir;

	this.grip.JMTxtFlasher = this;
	this.iFlashIdx = this.aVals.length-1;// NOTE: starts at the last value in the array
	this.sGoFunc = "getLayer('" + this.grip.id + "').JMTxtFlasher.go()";
	this.iDelay = 0;
	this.iCurrTop = this.iTopOrig;
	this.iCurrLeft = this.iLeftOrig;

	/**
	* initialization methods
	*/
	this.init = function() {
		this.setBrowserDiffs();
		this.setHTML();
		this.setInitStyles();
	}

	this.setBrowserDiffs = function() {
		if(document.all) {
			this.oStyle = this.grip.style;
			this.sVizOn = "visible";
			this.sVizOff = "hidden";
		}
		else if(document.layers) {
			this.oStyle = this.grip;
			this.sVizOn = "show";
			this.sVizOff = "hide";
		}
	}

	this.setHTML = function() {
		if(document.all) {
			this.grip.innerHTML = this.sTxt;
		}
		else if(document.layers) {
			var s = "<span id=x style='position:absolute;font-family:Verdana;font-size:10px;font-weight:bold;visibility:show;color:" + this.aVals[this.iFlashIdx]["clr"] + ";'>" + this.formatNsTxt();      
			s += "</span>";
			this.grip.document.write(s);
			this.grip.document.close();
		}
	}

	this.setInitStyles = function() {
		this.oStyle.top = this.iCurrTop;
		this.oStyle.left = this.iCurrLeft;
		this.oStyle.textAlign = this.sDir;
	}

	// emulates letter-spacing CSS in IE for NS
	this.formatNsTxt = function() {
		var sSp = "";// for spacer
		var sRet = "";

		for(var j=0; j < this.iFlashIdx; j+=3) {
			sSp += "&nbsp;";
		}

		for(var i=0; i < this.sTxt.length; i++) {
			sRet += this.sTxt.charAt(i) + sSp;
		}

		return sRet;
	}

	this.setFlashVals = function() {
		this.oStyle.visibility = this.sVizOn;

		// note different techniques depending upon browser
		if(document.layers) {
			this.setHTML();
			return;
		}
		
		if("undefined" != typeof(this.aVals[this.iFlashIdx]["clr"])) {
			this.oStyle.color = this.aVals[this.iFlashIdx]["clr"];
		}
		if("undefined" != typeof(this.aVals[this.iFlashIdx]["ltrSpc"])) {
			this.oStyle.letterSpacing = this.aVals[this.iFlashIdx]["ltrSpc"];
		}
	}

	this.init();

	/**
	* methods not needed for initialization  
	*/
	this.go = function() {
		// continue with flash processing
		if(this.iFlashIdx < this.aVals.length) {
			this.setFlashVals();
			this.iDelay = this.iFlashSpeed;
			++this.iFlashIdx;
		}
		// or start over after a random delay
		else {
			this.iFlashIdx = 0;
			this.iDelay = this.getDelay();
			this.oStyle.visibility = "visible";
		}
//if(!confirm(1)) return;
		setTimeout(this.sGoFunc, this.iDelay);
	
	}

	this.getDelay= function() {
		return ((this.iRngEnd - this.iRngBeg) * Math.random()) + this.iRngBeg;
	}
}

function doJMTxtFlasher(gripId, sTxt, iRngBeg, iRngEnd, iValIdx) {
	var oEl = getLayer(gripId);
	var sGoFunc = "";
	var iDelay;

	if(iValIdx < aFadeVals.length) {
			var s = "<div nowrap style='xposition:absolute;color:" + aFadeVals[iValIdx]["clr"] + ";width:100%;'>" + sTxt;      
			s += "</div>";
			oEl.document.write(s);
			oEl.document.close();

			iDelay = FLASH_SPEED;
			sGoFunc = "doJMTxtFlasher('" + gripId + "','" + sTxt + "'," + iRngBeg + "," + iRngEnd + "," + (iValIdx+1) + ")";
	}
	else {
		iDelay = getDelay(iRngBeg,iRngEnd);
		sGoFunc = "doJMTxtFlasher('" + gripId + "','" + sTxt + "'," + iRngBeg + "," + iRngEnd + "," + 0 + ")";
	}

	setTimeout(sGoFunc, iDelay);
}

function getDelay(iBeg, iEnd) {
	return ((iEnd - iBeg) * Math.random()) + iBeg;
}
