/////////////////////////////////////////////////////////////////////////
//
// DHTML API
//
// December 2001
//
// Developed by Matt Holford for @radical.media, Inc.
// Includes parts from Danny Goodman's "Dynamic HTML"
// and concepts from dansteinman.com/dynduo/
//
// Further developed February 2002 by Dan Check and
// Matt Holford for @radical.media, Inc.
//
// Last update: 20 May 2003
//
/////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////
//
// GLOBAL VARIABLES
//
/////////////////////////////////////////////////////////////////////////

// These are really all for getObject
var range = "";
var styleObj = "";
if (isIE) {
	range = "all.";
	styleObj = ".style";
}

var ORIG_ARGS;

/////////////////////////////////////////////////////////////////////////
//
// getObject: Converts object name string or object
// reference into a valid object reference
// Arguments:
// 1. Object name
// 2. Nesting level (if any)
// 3 - n. Parent layers, top to bottom
//
/////////////////////////////////////////////////////////////////////////
function getObject () {
	if (arguments.length == 0) {
		alert ("getObject received no arguments");
		return false;
	}
	var theObj = "";
	var obj = arguments [0];
	if (typeof obj != "string") theObj = obj;
	else {
		if (isNN6) {
			theObj = eval ("document.getElementById ('" + obj + "').style");
		} else if (arguments.length == 1 || isIE) {
			theObj = eval ("document." + range + obj + styleObj);		
		} else {
			var nestPhrase = "document.";
			for (i = 1; i < arguments.length; i ++) {
				nestPhrase = "document." + arguments[i] + "." + nestPhrase;
			}
			theObj = eval (nestPhrase + obj);
			if(typeof(theObj) != "object") alert(nestPhrase + obj);
		}
	}
	return theObj;
}

/////////////////////////////////////////////////////////////////////////
//
// getObjectNoStyle: Converts object name string or object
// reference into a valid object reference without 
// ".style" on the end
// Arguments:
// 1. Object name
// 2. Nesting level (if any)
// 3 - n. Parent layers, top to bottom
//
/////////////////////////////////////////////////////////////////////////
function getObjectNoStyle () {
	if (arguments.length == 0) {
		alert ("getObjectNoStyle received no arguments");
		return false;
	}
	var theObj = "";
	var obj = arguments [0];
	if (typeof obj != "string") theObj = obj;
	else {
		if (isNN6) {
			theObj = eval ("document.getElementById ('" + obj + "')");
		} else if (arguments.length == 1 || isIE) {
			theObj = eval ("document." + range + obj);		
		} else {
			var nestPhrase = "document.";
			for (i = 1; i < arguments.length; i ++) {
				nestPhrase = "document." + arguments[i] + "." + nestPhrase;
			}
			if (arguments[0] == "mediaDesc") {
				//alert(nestPhrase + obj);				
			}
			theObj = eval (nestPhrase + obj);
			if(typeof(theObj) != "object") alert(nestPhrase + obj);
		}
	}
	return theObj;
}

/////////////////////////////////////////////////////////////////////////
//
// setPosition: moves object to (x, y)
//
/////////////////////////////////////////////////////////////////////////
function setPosition(obj, x, y) {
	var theObj = getObject(obj);
	if (typeof(theObj) != "object") {
		alert(obj + ' being moved to ' + x + 'x ' + y +'y is not an object');
	} 
	//alert("setPos: " + theObj);
	if (isIE) {
		theObj.pixelLeft = x;
		theObj.pixelTop = y;
	}
	else if (isNN6) {
		theObj.left = x + 'px';
		theObj.top = y + 'px';	
	} else {
		theObj.moveTo(x, y);
	}
}

/////////////////////////////////////////////////////////////////////////
//
// shiftBy: moves object by (x, y) pixels
//
/////////////////////////////////////////////////////////////////////////
function shiftBy(obj, x, y) {
	var theObj = getObject(obj);
	if (isIE) {
		theObj.pixelLeft += x;
		theObj.pixelTop += y;	
	} else if (isNN6) {
		//alert("Need to shiftBy for div " + obj + "(" + theObj + "), currently at " + theObj.left + ", " + theObj.top);
		var left = theObj.left;
		left = left.replace(/pt/, "");
		var top = theObj.top;
		top = top.replace(/pt/, "");
		theObj.left = parseInt(left) + x + 'px';
		theObj.top = parseInt(top) + y + 'px';
	} else {
		theObj.moveBy(x, y);
	}
}

/////////////////////////////////////////////////////////////////////////
//
// setZIndex: sets Z index of the object
//
/////////////////////////////////////////////////////////////////////////
function setZIndex (obj, z) {
	var theObj = getObject (obj);
	theObj.zIndex = z;
}

/////////////////////////////////////////////////////////////////////////
//
// show: sets visibilty of object to 'visible'
//
/////////////////////////////////////////////////////////////////////////
function show (obj) {
	var theObj = getObject (obj);
	theObj.visibility = "visible";
}

/////////////////////////////////////////////////////////////////////////
//
// hide: sets visibilty of object to 'hidden'
//
/////////////////////////////////////////////////////////////////////////
function hide (obj) {
	var theObj = getObject (obj);
	theObj.visibility = "hidden";
}

/////////////////////////////////////////////////////////////////////////
//
// getVisibility: returns true if object is visible
//
/////////////////////////////////////////////////////////////////////////
function getVisibility (obj) {
	var theObj = getObject (obj);
	if (theObj.visibility == "visible") return true;
	else return false;
}

/////////////////////////////////////////////////////////////////////////
//
// getX: returns X coordinate of object
//
/////////////////////////////////////////////////////////////////////////
function getX (obj) {
	// alert("getX: need to get object for '" + obj + "'");
	var theObj = getObject (obj);
	if (isNN6) {
		var xVal = theObj.left;
		valsArray = xVal.split ("p");
		return valsArray [0];
	} else if (isNN) return theObj.left;
	else return theObj.pixelLeft;
}

/////////////////////////////////////////////////////////////////////////
//
// getY: returns Y coordinate of object
//
/////////////////////////////////////////////////////////////////////////
function getY (obj) {
	// alert("getY: need to get object for '" + obj + "'");
	var theObj = getObject (obj);
	if (isNN6) {
		var xVal = theObj.top;
		valsArray = xVal.split ("p");
		return valsArray[0];
	} else if (isNN) return theObj.top;
	else return theObj.pixelTop;
}

/////////////////////////////////////////////////////////////////////////
//
// handleResize: catches Navigator resize events
// Activating code added below function definition
//
/////////////////////////////////////////////////////////////////////////
function handleResize () {
	location.reload ();
	return false;
}
if (isNN) {
	window.captureEvents (Event.RESIZE);
	window.onresize = handleResize;
}

/////////////////////////////////////////////////////////////////////////
//
// getInsideWindowWidth: Returns available width
// of browser window
//
/////////////////////////////////////////////////////////////////////////
function getInsideWindowWidth () {
	if (window.innerWidth) return window.innerWidth;
	// Else if browser supports document.all (IE 4+)
	else if (document.all) return document.body.clientWidth;
}

/////////////////////////////////////////////////////////////////////////
//
// findCenteredX: returns origin X value, given the
// passed width value
//
/////////////////////////////////////////////////////////////////////////
function findCenteredX (width) {
	origin = Math.round ((getInsideWindowWidth () - width) / 2);
	if (origin < 0) origin = 0;
	return origin;
}

/////////////////////////////////////////////////////////////////////////
//
// getLayerWidth: returns layer width value -- see
// comments for getLayerHeight
//
/////////////////////////////////////////////////////////////////////////
function getLayerWidth (layer) {
	if (!isNN4) {
		width = (isNN6) ? eval ("document.getElementById ('" + layer + "').offsetWidth") : eval ("document.all." + layer + ".scrollWidth");
	} else {
		var nestPhrase = "document.";
		for (i = 1; i < arguments.length; i ++) {
			nestPhrase += arguments[i] + ".document.";
		}
		width = eval (nestPhrase + layer + ".document.width");
	}
	return width;
}

/////////////////////////////////////////////////////////////////////////
//
// getLayerHeight: returns layer height value
// For NN < 6, can be passed a list of embedded
// layers (this order needs to be changed: right now,
// it receives the target layer, then the rest of the
// layers, starting with the outermost layer)
//
/////////////////////////////////////////////////////////////////////////
function getLayerHeight (layer) {
	if (!isNN4) {
		height = (isNN6) ? eval ("document.getElementById ('" + layer + "').offsetHeight") : eval ("document.all." + layer + ".scrollHeight");
	} else {
		var nestPhrase = "document.";
		for (i = 1; i < arguments.length; i ++) {
			nestPhrase += arguments[i] + ".document.";
		}
		height = eval (nestPhrase + layer + ".document.height");
	}
	return height;
}

/////////////////////////////////////////////////////////////////////////
//
// getVisibility: returns true if layer is visible
//
/////////////////////////////////////////////////////////////////////////
function getVisibility (layer) {
	theObj = getObject (layer);
	if (theObj.visibility == "visible" || theObj.visibility == "show") return true;
	else return false;
}

/////////////////////////////////////////////////////////////////////////
//
// changeFile: change the HTML of a document
//
/////////////////////////////////////////////////////////////////////////
function changeFile (widgetName, docBase, entry) {
	divObj = eval("widgets['" +  widgetName + "']");
	docFinal = 'loadtext.asp?file=' + docBase + '&entry=' + entry;
	setPosition(divObj,0,0);
	if (isIE) {
		divObj = getObjectNoStyle(widgetName);
		divObj.innerHTML = '<table><tr><td><p class="descriptionType">Loading...</p></td></tr></table>';
		divIFrame = getObjectNoStyle("invisiFrame");
		divIFrame.src = docFinal;
		setTimeout('divObj.innerHTML = window.invisiFrame.document.body.innerHTML', 1000);
	} else if (isNN6) {
		divObj = getObjectNoStyle(widgetName);
		divObj.innerHTML = '<table><tr><td><p class="descriptionType">Loading...</p></td></tr></table>';
		divIFrame = getObjectNoStyle("invisiFrame");
		divIFrame.src = docFinal;
		innerChild = divIFrame;
		setTimeout('divObj.innerHTML = innerChild.contentDocument.body.innerHTML', 1000);
	} else {
		divObj.src = docFinal;
	}
	loadHandler(entry);
}

/////////////////////////////////////////////////////////////////////////
//
// scrollDown: scrolls pane down, given object and
// parent layer
//
/////////////////////////////////////////////////////////////////////////
var DEBUG_LOOP = 0;
function scrollDown(obj, parent) {
	widgetScroll = eval("widgets['" + obj + "']");
	widgetParent = eval("widgets['" + parent + "']");
	
	if (getY(widgetScroll) >= SCROLL_WINDOW [1] - eval("getLayerHeight ('" + obj + "','" + parent + "')") - 30) 
		shiftBy(widgetScroll, 0, -TEXT_SCROLL_AMOUNT);
}

/////////////////////////////////////////////////////////////////////////
//
// scrollUp: scrolls pane down, given object name
// N.b.: this is set to scroll up at 1.5x the amount
// it scrolls down, assuming you want to get to the
// beginning faster
//
/////////////////////////////////////////////////////////////////////////
function scrollUp(obj) {
	var thisScrollAmount = 1.5 * TEXT_SCROLL_AMOUNT;
	widgetScroll = eval("widgets['" + obj + "']");
	var textY = getY(widgetScroll);
	if (textY < 0) {
		if (Math.abs(textY) >= thisScrollAmount) {
			shiftBy(widgetScroll, 0, thisScrollAmount);
		} else {
			setPosition(widgetScroll, getX (widgetScroll), 0);
		}
	}
	//window.status = "( " + getX (widgetScroll) + ", " + getY (widgetScroll) + " ): " + (SCROLL_WINDOW [1] - eval ("getLayerHeight ('" + obj + "','" + parent + "')"));
}

/////////////////////////////////////////////////////////////////////////
//
// mouseDelayer: Do this on a mouseDown event
//  Will continue to scroll at an increment you specify in setTimeout
//  for as long as the mouse remains down
//  The first eval argument calls the function specified in the mouseDown event
//  in the main document.  The format for the function is: (function,arg1,arg2...)
//  so this can be used for any delay on a function. 
//  Utilizes global vars ORIG_ARGS to know what args to pass
//  and IS_MOUSE_DOWN to know whether or not the mouse is depressed
//
/////////////////////////////////////////////////////////////////////////
function mouseDelayer() {
	var myArgs;
	myArgs = argToArg(arguments, 2, arguments.length);
	eval(arguments[0] + '(' + myArgs + ')');
	if(window.document.IS_MOUSE_DOWN == true) {
		window.document.ORIG_ARGS = argToArg(arguments, 1, arguments.length);
		setTimeout('eval("mouseDelayer(" + window.document.ORIG_ARGS + ")")', TEXT_SCROLL_DELAY);
	}
}

/////////////////////////////////////////////////////////////////////////
//
// argToArg: given an arguments array, returns it as a
//  string.  Arguments passed are: args array,
//  first element to pull (use 1 for arguments[0]) and
//  the last element to pull (use arguments.length to
//  continue to the end).
//
/////////////////////////////////////////////////////////////////////////
function argToArg(argArray, argBegin, argEnd) {
	if (argEnd < argBegin) {
		strReturn = 'ERROR calling argToArg: you are asking for ' + argEnd - argBegin + ' value(s)'; 
	} else {
		strReturn = '';
		for (var i = argBegin - 1; i < argEnd; i++) {
		strReturn +='"' + argArray[i] + '"';
		if (i != argEnd - 1) strReturn += ',';
		}
	}
	return strReturn;
}

