/*
TO USE THIS API:
 * Make sure all the layers you want to initialize end with "Div" (e.g. "#layer1Div {...}")
 * Make sure "layerInit()" runs onLoad.
 * Each layer you specify will be initialized as a custom layer object, with its natural name minus the "Div" (e.g. "layer1")
 * Refer to this layer from now on as simply "layer1".
Example call:
	hide(layer1);
Example function use:
	function toggle(someLayer) {
		if (someLayer.style.visibility == "hidden") show(someLayer);
		else hide(someLayer);
	}
*/

/* GRAB BROWSER */
var isIE = (navigator.appName.indexOf("Explorer") >= 0) ? isIE = true : isIE = false;


/* INITIALIZATION */
function layerInit() {
	var grossLayers = new Array();
	var netLayers = new Array();
	var magicLayers = new Array();

	if (!isIE) if (document.layers.length > 0) grossLayers = document.layers;
	if (isIE) grossLayers = document.getElementsByTagName("DIV");
	netLayers = getNetLayers(grossLayers);

	for(var i=0;i<netLayers.length;i++) {
		magicLayers[i] = new getObj(netLayers[i]);
		var currId = magicLayers[i].name;
		eval(currId + " = magicLayers[i]");
	}
}
// Extract relevant layers from all layers on the page, grabs nested layers in NN
function getNetLayers(localGrossLayers) {
	var localNetLayers = new Array();
	for (var i=0; i<localGrossLayers.length; i++) {
		currLayer = localGrossLayers[i];
		if (currLayer.id.indexOf("Div") > 0) {
			localNetLayers[localNetLayers.length] = currLayer;
			if (!isIE && currLayer.document.layers.length > 0) {
				for (var j=0; j<currLayer.document.layers.length; j++) {
					localNetLayers[localNetLayers.length] = currLayer.document.layers[j];
				}
			}
		}
	}
	return localNetLayers;
}
function getObj(thisLayer) {
	this.root = thisLayer;
	this.id = thisLayer.id;
	this.name = this.id.substring(0,this.id.lastIndexOf("Div"));
	this.style = (isIE) ? this.root.style : this.root;
	this.x = getX(this);
	this.y = getY(this);
	this.isVisible = ((this.style.visibility=="hidden") || (this.style.visibility=="hide")) ? false : true;  
	this.relX = (isIE) ? this.root.offsetLeft : this.root.pageX;
	this.relY = (isIE) ? this.root.offsetTop : this.root.pageY;
	this.width = (isIE) ? this.root.offsetWidth : this.root.document.width;
	this.height = (isIE) ? this.root.offsetHeight : this.root.document.height;
	if ((isIE && this.root.currentStyle) || !isIE) {
		this.clipTop = (isIE) ? parseInt(this.root.currentStyle.clipTop) : this.root.clip.top;
		this.clipRight = (isIE) ? parseInt(this.root.currentStyle.clipRight) : this.root.clip.right;
		this.clipBottom = (isIE) ? parseInt(this.root.currentStyle.clipBottom) : this.root.clip.bottom;
		this.clipLeft = (isIE) ? parseInt(this.root.currentStyle.clipLeft) : this.root.clip.left;
	}
	return this;
}


/* CENTER A LAYER */
function horizCenter(thisLayer) {
	var w = thisLayer.width;
	var sw = (isIE) ? document.body.clientWidth : window.innerWidth-16;
	var x = (sw/2) - (w/2);
	moveTo(thisLayer,x,getY(thisLayer));
}
function vertCenter(thisLayer) {
	var h = thisLayer.height;
	var sh = (isIE) ? document.body.clientHeight : window.innerHeight-14;
	var y = (sh/2) - (h/2);
	moveTo(thisLayer,getX(thisLayer),y);
}


/* CHANGE BACKGROUND COLOR */
function changeBG(thisLayer,newColor) {
	if (isIE) thisLayer.style.backgroundColor = newColor;
	else thisLayer.style.document.bgColor = newColor;
}


/* WRITE TO A LAYER */
function writeTo(thisLayer,content) {
	if (isIE) {
		thisLayer.root.innerHTML = content;
	} else {
		tableOpen = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=" +  thisLayer.width + "HEIGHT=" + thisLayer.height + "><TR VALIGN=TOP><TD>";
		tableClose = "</TD></TR></TABLE>";
		thisLayer.root.document.open();
		thisLayer.root.document.write(tableOpen + content + tableClose);
		thisLayer.root.document.close();
	}
}


/* CHANGE LAYER'S Z-INDEX */
function setZIndex(thisLayer,num) {
	thisLayer.style.zIndex = num;
}


/* GET X & Y COORDS */
function getX(thisLayer) {
	var x = (isIE) ? thisLayer.root.offsetLeft : thisLayer.root.left;
	return x;
}
function getY(thisLayer) {
	var y = (isIE) ? thisLayer.root.offsetTop : thisLayer.root.top;
	return y;
}


/* SHOW/HIDE */
function show(thisLayer) {
	thisLayer.style.visibility = "visible";
	thisLayer.isVisible = true;
}

function hide(thisLayer) {
	thisLayer.style.visibility = "hidden";
	thisLayer.isVisible = false;
}


/* MOVING */
function moveTo(thisLayer,x,y) {
	thisLayer.style.left = x;
	thisLayer.style.top = y;
}
function moveBy(thisLayer,dx,dy) {
	var currentX;
	var currentY;
	currentX = getX(thisLayer);
	currentY = getY(thisLayer);
	thisLayer.style.left = currentX + dx;
	thisLayer.style.top = currentY + dy;
}


/* SLIDING */
var slideTimeout;										// initialize slideTimeout
function slide(thisLayer,x,y,I,D) {
	if (slideTimeout) clearTimeout(slideTimeout);
	OPTIMAL_INCREMENT = (I) ? I : 10					// aim to increment by this number of pixels
	DELAY = (D) ? D : 17;								// repeat loops this many milliseconds apart
	currX = getX(thisLayer); currY = getY(thisLayer);	// current position of layer
	dirX = x - currX; dirY = y - currY;					// direction layer must travel (+ or -)
	distX = Math.abs(dirX); distY = Math.abs(dirY);		// distance layer must travel
	slideProg = 0;										// number of steps taken so far
	/* figure out number of steps slide will execute in */
	if (distX > distY) {
		if (distX > OPTIMAL_INCREMENT) slideSteps = Math.round(distX / OPTIMAL_INCREMENT);
		else slideSteps = 1;
	} else {
		if (distY > OPTIMAL_INCREMENT) slideSteps = Math.round(distY / OPTIMAL_INCREMENT);
		else slideSteps = 1;
	}
	incX = dirX / slideSteps; incY = dirY / slideSteps;	// actual increment values (+ or -)
	slideStep(thisLayer);								// start the stepping process
}
function slideStep(thisLayer) {
	/* do this as long as the layer isn't at its destination yet */
	if (slideProg < slideSteps) {
		newX = currX + incX; newY = currY + incY;		// the next position
		moveTo(thisLayer,newX,newY);					// move the layer to the new position
		currX = newX; currY = newY;						// update the current position
		slideProg++;									// increment the number of steps taken so far
		/* execute this function again, after DELAY milliseconds */
		slideTimeout = setTimeout("slideStep(" + thisLayer.name + ")",DELAY);
	}
}


/* CLIPPING */
function clipTo(thisLayer,t,r,b,l) {
	if (isIE) {
		thisLayer.style.clip = eval("'rect(" + t + " " + r + " " + b + " " + l + ")'");
	} else {
		thisLayer.style.clip.top = t;
		thisLayer.style.clip.right = r;
		thisLayer.style.clip.bottom = b;
		thisLayer.style.clip.left = l;
	}
}
var clipTimeout;										// initialize clipTimeout
function clipInit(thisLayer,r,b,I,D) {
	if (clipTimeout != "") clearTimeout(clipTimeout);	// kill the timeout, to be neat
	OPTIMAL_INCREMENT = (I) ? I : 10					// aim to increment by this number of pixels
	DELAY = (D) ? D : 17;								// repeat loops this many milliseconds apart
	currR = thisLayer.clipRight; currB = thisLayer.clipBottom;
	if (r == null) r = thisLayer.clipRight; 
	if (b == null) b = thisLayer.clipBottom;
	t = thisLayer.clipTop; l = thisLayer.clipLeft;
	dirR = r - currR; dirB = b - currB;
	distR = Math.abs(dirR); distB = Math.abs(dirB);
	clipProg = 0;
	if (distR > distB) {
		if (distR > OPTIMAL_INCREMENT) clipSteps = Math.round(distR / OPTIMAL_INCREMENT);
		else clipSteps = 1;
	} else {
		if (distB > OPTIMAL_INCREMENT) clipSteps = Math.round(distB / OPTIMAL_INCREMENT);
		else clipSteps = 1;
	}
	incR = dirR / clipSteps; incB = dirB / clipSteps;	// num. to clip layer right by each step (+ or -)
	clipStep(thisLayer);	// start the stepping process
}
function clipStep(thisLayer) {
	/* do this as long as the layer isn't at its destination yet */
	if (clipProg < clipSteps) {
		newR = currR + incR;		// the next right position
		newB = currB + incB;		// the next bottom position
		clipTo(thisLayer,t,newR,newB,l);
		currR = newR;				// update the current right position
		currB = newB;				// update the current bottom position
		clipProg++;					// increment the progress
		/* execute this function again, after DELAY milliseconds */
		clipTimeout = setTimeout("clipStep(" + thisLayer.name + ")",DELAY);
	}
}


/* CHECK POSITION */
function checkPos(thisLayer) {
	alert("x coord is " + getX(thisLayer) + ", y coord is " + getY(thisLayer));
}


/* SCROLLING LAYERS */
// call this function onMouseOver of the up arrow
function scrollUp(outerLayer,contentLayer) {
	var I = 5;
	var D = 50;
	newX = getX(contentLayer);
	newY = 0;
	slide(contentLayer,newX,newY,I,D);
}
// call this function onMouseOver of the down arrow
function scrollDn(outerLayer,contentLayer) {
	var I = 5;
	var D = 50;
	var clipHeight = outerLayer.clipBottom - outerLayer.clipTop;
	newX = getX(contentLayer);
	newY = clipHeight - contentLayer.height;
	slide(contentLayer,newX,newY,I,D);
}
// call this function onMouseOut of both arrows to stop the scrolling action
function stopScroll() {
	clearTimeout(slideTimeout);
}