/****************************************
KDElement
DHTML core library
turns divs and spans into objects and gives them properties and
methods that make them "manipulatible"
*****************************************/

// Browser detection and handling
var isNav = false;
var isNav4 = false;
var isIE = true;
var isIE4 = false;
var isStandard = true;
var docHeight = null;
var docWidth = null;
var isWin = false;
var isMac = false;
var isLinux = false;
var isOther = false;

// OS detection
thisAgent = navigator.userAgent.toLowerCase()
if (thisAgent.indexOf("win") != -1)
{
	isWin=true;
}
else if (thisAgent.indexOf("95") != -1)
{
	isWin=true;
}
else if (thisAgent.indexOf("98") != -1)
{
	isWin=true;
}
else if (thisAgent.indexOf("nt") != -1)
{
	isWin=true;
}
else if (thisAgent.indexOf("mac") != -1)
{
	isMac=true;
}
else if (thisAgent.indexOf("linux") != -1)
{
	isLinux=true;
}
else
{
	isOther = true;
}

// Standards Compliant code fork...
if (document.getElementById)
{
	isStandard = true;

	//All IE code fork...
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		isIE = true;
	}

	//All Nav code fork...
	if (navigator.appName.indexOf("Netscape") != -1)
	{
		isNav = true;
	}
}
// IE 4 code fork...
else if (document.all)
{
	isIE = true;
	isIE4 = true;
}
// Nav 4.x code fork...
else if (document.layers)
{
	isNav = true;
	isNav4 = true;
}
//Other browser code fork...
else
{
	alert("Your browser is not supported.  Please upgrade to Internet Explorer 4.x or greater, Netscape Navigator 4.x or greater, or a browser supporting the W3C Standard.");
	window.close();
}

//Nav4 resize fix: To use, call resizeFix() IN <BODY> TAG like this: <body onResize="resizeFix();">
function resizeFix()
{
	if(isNav4)
	{
		location.reload();
	}
}

/** 
 * Constructor:
 * pass object a DIV name and it returns an object with various properties
 */
function KDElement(obj) 
{
	if(isNav4)
	{
		this.name = obj;
		this.elem = document.layers[obj];
		this.css = this.elem;
		this.id = this.elem.id;
	}
	else if(isIE4)
	{
		this.name = obj;
		this.css = document.all[obj].style;
		this.elem = document.all[obj];
		this.id = this.elem.id;
	}
	else if(isStandard)
	{
		this.name = obj;
		this.css = document.getElementById(obj).style
		this.elem = document.getElementById(obj)
		this.id = this.elem.id;
	}

	if(isIE4 || isStandard)
	{
		if(this.css.top == "")
		{
			this.css.top = this.elem.offsetTop + "px";
		}
		if(this.css.left == "")
		{
			this.css.left = this.elem.offsetLeft + "px";
		}
		if(this.css.width == "")
		{
			this.css.width = this.elem.offsetWidth + "px";
		}
		if(this.css.height == "")
		{
			this.css.height = this.elem.offsetHeight + "px";
		}
	}
}

// move layer to x, y position
KDElement.prototype.moveTo = function(x,y) 
{
	this.css.left = x;
	this.css.top = y;
}

// move the layer by X, Y pixels
	//parses pixel value strings
pixelParser = function(string)
{
	if(isNav4)
	{
		return string;
	}
	else
	{
		value = eval(string.substring(0,string.indexOf("px")));
		return value;
	}
}

KDElement.prototype.moveBy = function(dX, dY)
{
	this.css.left = pixelParser(this.css.left) + dX;
	this.css.top = pixelParser(this.css.top) + dY;
}

//getX and getY methods
KDElement.prototype.getX = function()
{
	return pixelParser(this.css.left);
}

KDElement.prototype.getY = function()
{
	return pixelParser(this.css.top);
}

// Set the Z-order for the layer
KDElement.prototype.setZIndex = function(zIndex)
{
	this.css.zIndex = zIndex;
}

// Get the Z-order for the layer
KDElement.prototype.getZIndex = function(zIndex)
{
	return this.css.zIndex;
}

// show the DIV
KDElement.prototype.show = function()
{
	this.css.visibility = (isNav4) ? "visible" : "visible";
}

// hide the DIV
KDElement.prototype.hide = function()
{
	this.css.visibility = (isNav4) ? "hidden" : "hidden";
}

// set the background color
KDElement.prototype.setBGColor = function(color)
{
	if (isNav4)
	{
		this.css.bgColor = color;
	}
	else
	{
		this.css.backgroundColor = color;
	}
}

// get the background color
KDElement.prototype.getBGColor = function()
{
	if(isNav4)
	{
		return this.css.bgColor;
	}
	else
	{
		return this.css.backgroundColor;
	}
}

// dynamically write a DIV
KDElement.prototype.writeDIV = function(html)
{
	if(isNav4)
	{
		with(document.layers[this.id].document)
		{
			open();
			write(html);
			close();
		}
	}
	else
	{
		this.elem.innerHTML = html
	}

}

//Aligns divs within a window
KDElement.prototype.align = function(alignment, scrollHoriz, scrollVert)
{
	if(scrollHoriz == null)
	{
		scrollHoriz = 0;
	}
	if(scrollVert == null)
	{
		scrollVert = 0;
	}
	if(isNav)
	{
		docHeight = window.innerHeight;
		docWidth = window.innerWidth;
	}	
	else
	{
		docHeight = document.body.clientHeight;
		docWidth = document.body.clientWidth;
	}

	var x = 0;
	var y = 0;

	switch(alignment)
	{
		case "topLeft":
		// top-left -- don't do anything, x and y = 0
		break;
		case "topMiddle":
		// top-middle, y=0
		x = Math.round(docWidth/2 - pixelParser(this.css.width)/2);
		break;
		case "topRight":
		// top-right, y=0
		x = docWidth - pixelParser(this.css.width);
		break;
		case "middleLeft":
		// middle-left, x=0
		y = Math.round(docHeight/2 - pixelParser(this.css.height)/2);
		break;
		case "middleCenter":
		// middle-center
		x = Math.round(docWidth/2 - pixelParser(this.css.width)/2);
		y = Math.round(docHeight/2 - pixelParser(this.css.height)/2);
		break;
		case "middleRight":
		// middle-right
		x = docWidth - pixelParser(this.css.width);
		y = Math.round(docHeight/2 - pixelParser(this.css.height)/2);
		break;
		case "bottomLeft":
		// bottom-left, x=0
		y = docHeight - pixelParser(this.css.height);
		break;
		case "bottomCenter":
		// bottom-center
		x = Math.round(docWidth/2 - pixelParser(this.css.width)/2);
		y = docHeight - pixelParser(this.css.height);
		break;
		case "bottomRight":
		// bottom-right
		x = docWidth - pixelParser(this.css.width);
		y = docHeight - pixelParser(this.css.height);
		break;
		default:
		// user has inappropriately called the function
		alert("Please specify allignment.");
		return;
		break;
	}

	x += scrollHoriz;
	y += scrollVert;
	this.moveTo(x,y);
}