// This is a custom DHTML cross platform API structure.
// Written by Andrew Fisher
//
// Version 1.1a
// 
// All bugs, please report to Andrew Fisher (drewfisher@hotmail.com)
//
// Notes for use: Insert these API calls into the page that you want them
// available for using the code:
//		<SCRIPT LANGUAGE="javascript" SRC="pathto/dhtmlapi.js">
//		</SCRIPT>
//
// Functions and their uses are below:
//
// getObject(object) - 	takes a string or object reference and returns a true object
//			for both Netscape and IE
// shiftTo(object, xcoord, ycoord) - moves the object's top left pixel to the position
//				     denoted by xcoord and ycoord.
// shiftBy(object, deltaX, deltaY) - moves the object by the amounts specified by x and y
// resizeTo(object, x, y) - resizes the object to the dimensions given by X and Y.
// resizeBy(object, deltaW, deltaH) - resizes the object by the width and height given
//				      by deltaW and deltaH
// setZIndex(object, ZOrder) - moves the object to the specified ZIndex
// show(object) - shows the object
// hide(object) - hides the object
// getVisibility(object) - gets the visible state of the object.
// getObjectLeft(object) - returns the left position of the object.
// getObjectTop(object) - returns the top position of the object.
// getObjectWidth(object) - returns the width of an object.
// getObjectHeight(object) - returns the height of an object.
// setObjectLeft(object, value) - sets the left position of the object.
// setObjectTop(object, value) - sets the top position of the object.
// setObjectWidth(object, value) - sets the width of an object.
// setObjectHeight(object, value) - sets the height of an object.
// scrollMenuObject(object, open, speed, toWidth, toHeight, direction) 
//	scrolls out an object. - See function below for more details on parameters.


// This portion of the script generates global Variables for use
// in all areas of this script.

	var isNav, isIE;
	var coll = "";
	var styleObj = "";
	if (parseInt(navigator.appVersion) >= 4) {
		if (navigator.appName == "Netscape") {
			isNav = true;
		} else {
			isIE = true;
			coll = "all.";
			styleObj = ".style";
		}
	}

function getObject(obj) {
// This function takes in a reference to an object in the current document.
// It returns either the relevant object (for IE4 or NN4 as required) evaluated
// or else an object that was passed into it. This way you always use the getObject call even
// if the object is known to exist. There is no checking to see if the object IS real.
// The function assumes the object passed in DOES exsit.

	var theObj;
	if (typeof obj == "string") {
		theObj = eval("document." + coll + obj + styleObj);
	} else {
		theObj = obj;
	}
	return theObj;
}

// THE FOLLOWING FUNCTIONS ARE USED FOR MOVING OBJECTS AROUND THE SCREEN

function shiftTo(obj, x, y) {
// This function moves an object to coordinates denoted by X and Y for it's
// top left location.

	var theObj = getObject(obj);
	if (isNav) {
		theObj.moveTo(x,y);
	} else {
		theObj.pixelLeft = x;
		theObj.pixelTop = y;
	}
}

function shiftBy(obj, deltaX, deltaY) {
// This function moves an object by the amount denoted by deltaX and deltaY

	var theObj = getObject(obj);
	if (isNav) {
		theObj.moveBy(deltaX, deltaY);
	} else {
		theObj.pixelLeft += deltaX;
		theObj.pixelTop += deltaY;
	}
}

// THE FOLLOWING FUNCTIONS ARE USED TO RESIZE AN OBJECT

function resizeTo(obj, x, y) {
// This function resizes an object to a specific size.

	var theObj = getObject(obj);

	if (x <= 0) { x = 0; }
	if (y <= 0) { y = 0; }

	setObjectWidth(theObj, x);
	setObjectHeight(theObj, y);
}

function resizeBy(obj, deltaW, deltaH) {
// This function resizes the width of an object by deltaW and the
// height by deltaH

	var theObj = getObject(obj);
	var oldW = getObjectWidth(theObj);
	var oldH = getObjectHeight(theObj);
	if ((oldW + deltaW) <= 0 ) {
		setObjectWidth(theObj, 0);
	} else {
		setObjectWidth(theObj, (oldW + deltaW));
	}
	if ((oldH + deltaH) <= 0 ) {
		setObjectHeight(theObj, 0);
	} else {
		setObjectHeight(theObj, (oldH + deltaH));
	}
}


// THE FOLLOWING FUNCTIONS ARE USED FOR DISPLAYING AN OBJECT

function setZIndex(obj, zOrder) {
// This function sets the z-index of an object

	var theObj = getObject(obj);
	theObj.zIndex = zOrder;
}

function show(obj) {
// this function shows the object
	var theObj = getObject(obj);
	theObj.visibility = "visible";
}

function hide(obj) {
// this function hides the object
	var theObj = getObject(obj);
	theObj.visibility = "hidden";
}

// THE FOLLOWING FUNCTIONS ARE USED TO GET PROPERTIES FROM AN OBJECT

function getVisibility(obj) {
// this function returns the visibility of an object as a string

	var theObj = getObject(obj);
	return theObj.visibility;
}


function getObjectLeft(obj) {
// this function returns the coordinate of the left side of the object

	var theObj = getObject(obj);
	if (isNav) {
		return theObj.left;
	} else {
		return theObj.pixelLeft;
	}
}

function getObjectTop(obj) {
// this function returns the coordinate of the top of the object

	var theObj = getObject(obj);
	if (isNav) {
		return theObj.top;
	} else {
		return theObj.pixelTop;
	}
}

function getObjectWidth(obj) {
// this function returns the width of the object.

	var theObj = getObject(obj);
	if (isNav) {
		return theObj.clip.width;
	} else {
		return theObj.posWidth;
	}
}

function getObjectHeight(obj) {
// this function returns the height of an object.

	var theObj = getObject(obj);
	if (isNav) {
		return theObj.clip.height;
	} else {
		return theObj.posHeight;
	}
}

// THE FOLLOWING FUNCTIONS ARE USED TO SET THE PROPERTIES OF AN OBJECT

function setObjectLeft(obj, value) {
// this function sets the coordinate of the left side of the object
// it is assumed the value passed in is an integer.

	var theObj = getObject(obj);
	if (isNav) {
		theObj.left = value;
	} else {
		theObj.pixelLeft = value;
	}
}

function setObjectTop(obj, value) {
// this function sets the coordinate of the top of the object
// it is assumed the value passed in is an integer.

	var theObj = getObject(obj);
	if (isNav) {
		theObj.top = value;
	} else {
		theObj.pixelTop = value;
	}
}

function setObjectWidth(obj, value) {
// this function sets the width of the object.
// It is assumed the value passed in is an integer.

	var theObj = getObject(obj);
	if (isNav) {
		theObj.clip.width = value;
	} else {
		theObj.posWidth = value;
	}
}

function setObjectHeight(obj, value) {
// this function sets the height of an object.
// It is assumed the value passed in is an integer.

	var theObj = getObject(obj);
	if (isNav) {
		theObj.clip.height = value;
	} else {
		theObj.posHeight = value;
	}
}

// THESE FUNCTIONS DO ANIMATION TASKS.

function scrollMenuObject (obj, open, speed, toWidth, toHeight, direction) {
	//window.alert("here");
// This function scolls open or closed a layer to use as a menu or just a cool
// animation effect. The variables it passes in are as follows:
//		obj - the layer to scroll
//		open - a numeric value to specify whether the menu is opening (1) or closing (-1)
//		speed - this is the number of pixels to move per millisecond
//		toWidth - this is the width the layer will expand/contract to
//		toHeight - this is the height the layer will expand/contract to.
//		direction - a string containin the direction that the menu opens. This is
// in relation to screen coordinates. Valid entries are:
//			v+ - Scrolls DOWN the screen
//			v- - Scrolls UP the screen 
//			h+ - Scrolls LEFT to RIGHT
//			h- - Scrolls RIGHT to LEFT
//			v+h+ - Scrolls DOWN the screen and Scrolls LEFT to RIGHT
//			v+h- - Scrolls DOWN the screen and Scrolls RIGHT to LEFT
//			v-h+ - Scrolls UP the screen and Scrolls LEFT to RIGHT
//			v-h- - Scrolls UP the screen and Scrolls RIGHT to LEFT
//

	//define variables
	var objName = obj;
	var theObj = getObject(obj);
	var deltaV = 0;
	var deltaT = 0;
	var deltaL = 0;
	var deltaH = 0;

	// deltaV is vert height change
	// deltaH is horiz width change
	// deltaT is top pos change
	// deltaL is left pos change

	//determine direction and assign change factors.

	//if (toHeight == getObjectHeight(theObj)) { window.alert('The heights are equal'); }
	//if (toWidth == getObjectWidth(theObj)) { window.alert('The widths are equal'); }

	if (direction.indexOf("v+") != -1){
		deltaV = speed * open;
		deltaT = 0;
	}
	if (direction.indexOf("v-") != -1){
		deltaV = speed * open;
		deltaT = speed * open * -1; //negate value

	}
	if (direction.indexOf("h+") != -1){
		deltaH = speed * open;
		deltaL = 0;
	}
	if (direction.indexOf("h-") != -1){
		deltaH = speed * open;
		deltaL = speed * open * -1; //negate value
	}
	//window.alert("deltaH:" + deltaH + " deltaV:" + deltaV + " deltaT:" + deltaT + " deltaL:" + deltaL);
	//return (0);

// perform movement for layer.

	if (( getObjectWidth(theObj) != toWidth) || (getObjectHeight(theObj) != toHeight )) {
		//layer has not got to the complete size you want, therefore resize it.
		
		if ( ( (open == 1) && ((getObjectWidth(theObj) + deltaH) > toWidth) ) || ( (open == -1) && ((getObjectWidth(theObj) + deltaH) < toWidth) ) ) {
		// The change in width will push it over the complete size you want
		// therefore restrict it if it would go over.			
			if (deltaL != 0) {
				shiftBy(theObj, ((toWidth - getObjectWidth(theObj)) * -1), 0);
				setObjectWidth(theObj, toWidth);
			} else {
				setObjectWidth(theObj, toWidth);
			}
		} else {

		// resize the layer and move it as necessary
			resizeBy(theObj, deltaH, 0);
			shiftBy(theObj, deltaL, 0);
		}

		if ( ( (open == 1) && ((getObjectHeight(theObj) + deltaV) > toHeight) ) || ( (open == -1) && ((getObjectHeight(theObj) + deltaV) < toHeight) ) ) {
		// The change in height will push it over the complete height you want
		// therefore restrict it to the real eventual height			
			if (deltaT != 0) {
				shiftBy(theObj, 0, ((toHeight - getObjectHeight(theObj)) * -1));
				setObjectHeight(theObj, toHeight);
			} else {
				setObjectHeight(theObj, toHeight);
			}
		} else {

		// resize the layer and move it as necessary.
			resizeBy(theObj, 0, deltaV);
			shiftBy(theObj, 0, deltaT);
		}

		functionString = "scrollMenuObject('" + objName + "'," + open;
		functionString = functionString + "," + speed + "," + toWidth + "," + toHeight;
		functionString = functionString + ",'" + direction + "')";
		//window.alert(functionString);
		intervalID = setTimeout(functionString, 1);
	} else {
		
		clearTimeout(intervalID);
	}
}