// <SCRIPT SRC="">
// TOJAM specific methods v1.0

function removeHTMLTags(pHTML){
	var strInputCode = pHTML;
	/* 
		This line is optional, it replaces escaped brackets with real ones, 
		i.e. &lt; is replaced with < and &gt; is replaced with >
	*/	
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;
}

function dropshadowtext(pID, pLeft, pTop, pWidth, pHeight, pValue, pClassName, pZIndex)
{

	var e = window.document.getElementById(pID);
	if (e)
	{
		e.style.left=pLeft;
		e.style.top=pTop;
		e.style.width=pWidth;
		e.style.height=pHeight;
	}
	else
	{
		var body = document.getElementsByTagName("body").item(0);   // can also be document.getElementsByTagName("body")[0];
	
		dv = document.createElement('div'); // create dynamically div tag
		dv.setAttribute('id',pID);       //give id to it
		if (pClassName) { dv.className = pClassName; }
	
		//set the inner styling of the div tag 
		dv.style.position="absolute";       
		dv.style.left=pLeft;
		dv.style.top=pTop;
		dv.style.width=pWidth;
		dv.style.height=pHeight;
		dv.style.color="#222244";     // formerly #6666ff, which looked great with #222244
		if (pZIndex) { dv.style.zIndex = pZIndex; }
	
		//set the html content inside the div tag
		dv.innerHTML=removeHTMLTags(pValue);  // don't want any of the color, etc.
		dv.style.visibility = "visible";
		body.appendChild(dv);
	}
}

function dropshadow(pID, pLeft, pTop, pWidth, pHeight, pValue, pZIndex, pParent, pImage)
{
	var e = window.document.getElementById(pID);
	if (e)
	{
		e.style.left=pLeft; e.style.top=pTop; e.style.width=pWidth; e.style.height=pHeight;

		e = window.document.getElementById(pID + "g");
		e.style.left=pLeft-5; e.style.top=pTop-5; e.style.width=pWidth; e.style.height=pHeight;

		e = window.document.getElementById(pID + "b");
		if (e) { e.style.left=pLeft-5; e.style.top=pTop-5; e.style.width=pWidth; e.style.height=pHeight; }
	}
	else
	{
		var body = null;
		if (pParent) {
			body = window.document.getElementById(pParent);
		} else {
			body = document.getElementsByTagName("body").item(0);
		}

		var dv = null;
	
		// Layer 1: The Shadow
		dv = document.createElement('div');
			dv.setAttribute('id', pID);
			dv.style.position="absolute";       
			dv.style.left=pLeft;
			dv.style.top=pTop;
			dv.style.width=pWidth;
			dv.style.height=pHeight;
			dv.style.backgroundColor="#2F2F64";   // used to be black COLOR="#2B5271"
			if (pZIndex) { dv.style.zIndex = pZIndex; }
			body.appendChild(dv);
		dv = null;

		// Layer 2: The grid gradient
		dv = document.createElement('div');
			dv.setAttribute('id', pID + "g");
			dv.style.position="absolute";       
			dv.style.left=pLeft-5;
			dv.style.top=pTop-5;
			dv.style.width=pWidth;
			dv.style.height=pHeight;
			dv.style.backgroundColor="#82B5E0";
			dv.style.backgroundImage="url(../images/box_bluefade.jpg)";
			dv.style.backgroundRepeat="no-repeat";
			if (pZIndex) { dv.style.zIndex = pZIndex; }	
			body.appendChild(dv);
		dv = null;

		if (pImage) {
			// Layer 3: The TTC bus image
			dv = document.createElement('div');
				dv.setAttribute('id', pID + "b");
				dv.style.position="absolute";       
				dv.style.left=pLeft-5;
				dv.style.top=pTop-5;
				dv.style.width=pWidth;
				dv.style.height=pHeight;
				dv.style.backgroundColor="transparent";
				// dv.style.backgroundImage="url(../images/box_ttcbus.gif)";
				dv.style.backgroundImage="url(" + pImage + ")";
				dv.style.backgroundPosition="bottom right";
				dv.style.backgroundRepeat="no-repeat";
				if (pZIndex) { dv.style.zIndex = pZIndex; }	
				body.appendChild(dv);
			dv = null;
		}
	}
}


// -----------------------------------------------------------------------------------------------------------------------
// jimtext: Particular to TOJam - reposition the boxes within Main
// v2.0 Attempts to make all the spacer columns the same size (doesn't center)
var jimtext_left = 30 + 210; // navigation.asp leftnav width has an effect on this. 30spacer + 210leftbacksplash

function jimtext_resize() {
	var divs = document.getElementsByTagName("div"); 	
	// 1280 yields 1259 = 30 + 170 + 20 + 489! + 20 + 489! + 20

	var width = 449;
	var totalwidth = gBrowser.width - jimtext_left - 40; // screen - leftnav begins (30 or 280) - 40 right spacer
	var colsmax = 0, jimtext_count = 0, colsactual = 0;
	var hold = 0, toplargest = 0;
	var divs = document.getElementsByTagName("div");

	// There are 3 Cases/Styles
	// 1. 1 JimText, # of Columns irrelevant - Center it.
	// 2. 2+ JimText, 1 Column - Stagger
	// 3. 2+ JimText, 2+ Columns - Space out appropriately
	
	// Step 1: How many columns can I fit? How many columns are there?	
	colsmax = parseInt(totalwidth / (width + 20)); if (colsmax < 1) { colsmax = 1; }
	jimtext_count = 0;
	for (var i = 0; i < divs.length; i++) {
		if ((divs[i].id) && (divs[i].id.toLowerCase() == "jimtext")) { jimtext_count++; }
	}
	if (jimtext_count < colsmax) { colsactual = jimtext_count;} else { colsactual = colsmax; }
		
	// Step 3: Place jimtext boxes within the grid	
	var stagger = false;
	var left = 0, top = 95 + 15, tops = new Array();	
	for (var i = 0; i < colsmax; i ++) {
		tops[i] = top;
	}	
	
	var col = 0;
	for (var i = 0; i < divs.length; i++) {
		if ((divs[i].id) && (divs[i].id.toLowerCase() == "jimtext")) {
			divs[i].style.width = width;

			hold = parseInt((totalwidth - width) / 2); if (hold < 20) { hold = 20; }
			// Figure out LEFT
			if ((jimtext_count == 1) || (hold < 60)) {
				// Case1: Just Center
				
				left = jimtext_left + hold;
	
			} else if (colsactual == 1) {
				// Case 2: 2+ JimText, 1 Column
				hold = (totalwidth - width) / 10;
				if (hold > 40) {
					if (stagger) {
						hold = jimtext_left + totalwidth - width - hold; // right
						if (hold < jimtext_left + 20) { hold = jimtext_left + 20; }
						left = hold;
					} else {
						left = jimtext_left + hold; // left
					}				
				
				} else {								
					if (stagger) {
						hold = jimtext_left + totalwidth - width; // right
						if (hold < jimtext_left + 20) { hold = jimtext_left + 20; }
						left = hold;
					} else {
						left = jimtext_left + 20; // left
					}
				}					
			} else	{
				// Case 3: 2+ JimText, 2+ Columns - space out appropriately
				if (col == (colsmax - 1)) {					
					hold = jimtext_left + totalwidth - width; // right
					if (hold < jimtext_left + 20) { hold = jimtext_left + 20; }
					left = hold;
				} else {
					// even amount of spacers between blocks
					// startleft = 
					hold = (totalwidth - (colsactual * width)) / colsactual;
					left = jimtext_left + (hold * (col + 1)) + (width * col);
				}
				top = tops[col];
			}			
						
			divs[i].style.left = left; 
			divs[i].style.top = top;
									
			divs[i].style.visibility = "visible";

			var zIndex = 4;
			if ((divs[i].style.zIndex) && (!isNaN(divs[i].style.zIndex)) && (parseInt(divs[i].style.zIndex) > 0)) { zIndex = parseInt(divs[i].style.zIndex) - 1; }
			dropshadow("sh_body" + i, parseInt(divs[i].style.left) + 5, parseInt(divs[i].style.top) + 5, width, divs[i].offsetHeight, "&nbsp;", zIndex);

			// Update TOP
			if (jimtext_count == 1) {
				top += parseInt(divs[i].offsetHeight) + 20;
	
			} else if ((jimtext_count > 1) && (colsmax == 1)) {
				top += parseInt(divs[i].offsetHeight) + 20;
					
			} else	{
				tops[col] = tops[col] + parseInt(divs[i].offsetHeight) + 20;
			}			
			
			col++; if (col > (colsmax - 1)) { col = 0; }
			
			hold = parseInt(divs[i].style.top) + parseInt(divs[i].offsetHeight);
			if (hold > toplargest) { toplargest = hold; }
			
			stagger = !stagger;
		}
	}	
}
addResizeEvent(jimtext_resize);
addLoadEvent(jimtext_resize);
