// Global variables
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;

// Initialize upon load to let all browsers establish content objects
function InitDomApi()
{
	if (document.images) {
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isNN4 = (document.layers) ? true : false;
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
}

function FindObject(id, doc)
{
	var p, i, obj;
	if (typeof id == 'string') {
		doc = (!doc) ? document : doc;
		if ((p = id.indexOf('?')) > 0 && parent.frames.length) {
			doc = parent.frames[id.substring(p + 1)].document;
			id = id.substring(0, p);
		}
		if (!(obj = doc[id]) && doc.all) {
			obj = doc.all[id];
		}
		for (i = 0; !obj && i < doc.forms.length; i++) {
			obj = doc.forms[i][id];
		}
		for (i = 0; !obj && isNN4 && i < doc.layers.length; i++) {
			obj = FindObject(id, doc.layers[i].document);
		}
		if (!obj && doc.getElementById) {
			obj = doc.getElementById(id);
		}
	}
	else {
		obj = id;
	}
	return obj;
}

function MoveLayer (topObj, popupObj)
{
	args = arguments;
	frame_width = getInsideWindowWidth();
	frame_height = getInsideWindowHeight();
	main_pos = getElementPosition(topObj);
	main_pos.width = getObjectWidth(topObj);
	main_pos.height = args.length > 2 && 0 != parseInt(args[2]) ? parseInt(args[2]) : getObjectHeight(topObj);
	if (window.pageXOffset >= 0) {
		left_scroll = window.pageXOffset;
	}
	else {
		left_scroll = document.body.scrollLeft;
		main_pos.left += document.body.scrollLeft;
//					top += document.body.scrollTop;
	}
	if (main_pos.left + main_pos.width > frame_width + left_scroll) {
		shiftTo(popupObj, (frame_width - main_pos.width + left_scroll), (main_pos.top + main_pos.height));
	}
	else {
		shiftTo(popupObj, main_pos.left, (main_pos.top + main_pos.height));
	}
}

function getElementPosition(obj) {
	var offsetTrail = FindObject(obj);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft, top:offsetTop};
}

// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y)
{
	var theObj = FindObject(obj);
	if (theObj) {
		if (isCSS) {
			// equalize incorrect numeric value type
			var units = (typeof theObj.style.left == 'string') ? 'px' : 0;
			theObj.style.left = x + units;
			theObj.style.top = y + units;
		}
		else if (isNN4) {
			theObj.moveTo(x,y)
		}
	}
}

// Move an object by x and/or y pixels
function shiftBy(obj, deltaX, deltaY)
{
	var theObj = FindObject(obj);
	if (theObj) {
		if (isCSS) {
			// equalize incorrect numeric value type
			var units = (typeof theObj.style.left == 'string') ? 'px' : 0;
			theObj.style.left = getObjectLeft(obj) + deltaX + units;
			theObj.style.top = getObjectTop(obj) + deltaY + units;
		}
		else if (isNN4) {
			theObj.moveBy(deltaX, deltaY);
		}
	}
}

// Set the background color of an object
function setClass(obj, className)
{
	var theObj = FindObject(obj);
	if (theObj) {
		theObj.className = className;
	}
}

// Set the z-order of an object
function setZIndex(obj, zOrder)
{
	var theObj = FindObject(obj);
	if (theObj) {
		theObj.style.zIndex = zOrder;
	}
}

// Set the background color of an object
function setBGColor(obj, color)
{
	var theObj = FindObject(obj);
	if (theObj) {
		if (isNN4) {
			theObj.bgColor = color;
		}
		else if (isCSS) {
			theObj.style.backgroundColor = color;
		}
	}
}

// Set the visibility of an object to visible
function show(obj)
{
	var theObj = FindObject(obj);
	if (theObj) {
		theObj.style.visibility = 'visible';
	}
	return theObj;
}

// Set the visibility of an object to hidden
function hide(obj)
{
	var theObj = FindObject(obj);
	if (theObj) {
		theObj.style.visibility = 'hidden';
	}
	return theObj;
}

// Retrieve the x coordinate of a positionable object
function getObjectLeft(obj)
{
	var elem = FindObject(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, '');
		result = cssDecl.getPropertyValue('left');
	}
	else if (elem.currentStyle) {
		result = elem.currentStyle.left;
	}
	else if (elem.style) {
		result = elem.style.left;
	}
	else if (isNN4) {
		result = elem.left;
	}
	return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function getObjectTop(obj)
{
	var elem = FindObject(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, '');
		result = cssDecl.getPropertyValue('top');
	}
	else if (elem.currentStyle) {
		result = elem.currentStyle.top;
	}
	else if (elem.style) {
		result = elem.style.top;
	}
	else if (isNN4) {
		result = elem.top;
	}
	return parseInt(result);
}

// Retrieve the rendered width of an element
function getObjectWidth(obj)
{
	var elem = FindObject(obj);
	var result = 0;
	if (elem.offsetWidth) {
		result = elem.offsetWidth;
	}
	else if (elem.clip && elem.clip.width) {
		result = elem.clip.width;
	}
	else if (elem.style && elem.style.pixelWidth) {
		result = elem.style.pixelWidth;
	}
	return parseInt(result);
}

// Retrieve the rendered height of an element
function getObjectHeight(obj)
{
	var elem = FindObject(obj);
	var result = 0;
	if (elem.offsetHeight) {
		result = elem.offsetHeight;
	}
	else if (elem.clip && elem.clip.height) {
		result = elem.clip.height;
	}
	else if (elem.style && elem.style.pixelHeight) {
		result = elem.style.pixelHeight;
	}
	return parseInt(result);
}

// Return the available content width space in browser window
function getInsideWindowWidth( )
{
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (isIE6CSS) {
		// measure the html element's clientWidth
		return document.body.parentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 0;
}

// Return the available content height space in browser window
function getInsideWindowHeight( )
{
	if (window.innerHeight) {
		return window.innerHeight;
	}
	else if (isIE6CSS) {
		// measure the html element's clientHeight
		return document.body.parentElement.clientHeight;
	}
	else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
}

var eventUtils = {
	add: function(obj, eventType, target, capture)
	{
		capture = capture || false;
		if (obj.addEventListener) obj.addEventListener(eventType, target, capture);
		else if (obj.attachEvent) obj.attachEvent('on' + eventType, target);
	}, 
	
	remove: function(obj, eventType, target, capture)
	{
		capture = capture || false;
		if (obj.removeEventListener) obj.removeEventListener(eventType, target, capture);
		else if (obj.detachEvent) obj.detachEvent('on' + eventType, target);
	}, 
	
	DOMit: function(e)
	{ 
		e = e ? e : window.event;
		e.tgt = e.srcElement? e.srcElement : e.target;
		if (!e.preventDefault) e.preventDefault = function () { return false; }
		if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
		return e;
	}
}
