// DHTMLapi. js custom API for cross platform
// object positioning 

// Global variables

var isCSS = false;
var isNav = false;
var isIE = false;
var isW3C = false;
var endLayerRef = "";

var coll = "";
var styleObj = "";

if (document.images)
{
	// decide which DOM the users browser is
	if ( document.body && document.body.style) {
		isCSS = true;
	}

	if (document.layers) isNav = true;

	if (isCSS && document.all) {
				isIE = true;
				coll = "all.";
				styleObj = ".style";
				endLayerRef = "";
	}

	if (isCSS && document.getElementById) {
				isW3C = true;
				coll = "getElementById('";
				styleObj = ".style";
				endLayerRef = "')";
	}		 
}


// Convert object name string or object reference
// into a valid object reference
function getObject(obj)	{
	var theObj;
	if (typeof obj == "string")	{
		theObj = eval("document." + coll + obj + endLayerRef + styleObj);
	} else {
		theObj = obj;
	}
	return theObj;
}

// Positioning an object at a specific pixel coordinate
function shiftTo(obj, x, y) {
	var theObj = getObject(obj);
		if (isNav) {
			theObj.moveTo(x,y);
		} else {
			theObj.pixelLeft = x;
			theObj.pixelTop = y;
		}
}


// set the width and height of obj
function setObjectSize(elemid, w, h) {
	var theObj = document.getElementById(elemid);

	if (isNav)	{
		theObj.clip.width = w;
		theObj.clip.height = h;
	} else if (isIE) {
		theObj.style.pixelWidth = w - 20;
		theObj.style.pixelHeight = Math.round(h * 4.2);
	}
}

// Setting the Z-order of an object
function setZIndex(obj, zOrder)	{
	var theObj = getObject(obj);
	theObj.zIndex = zOrder;
}


// Setting the visibility of an object to visible
function show(obj)	{
	var theObj = getObject(obj);
	theObj.visibility = "visible";
}

// Setting the visibility of an object to hidden
function hide(obj)	{
	var theObj = getObject(obj);
	theObj.visibility = "hidden";
}

// Retrieving the x coordinate of a positionable object
function getObjectLeft(obj)	{
	var theObj = getObject(obj);
	if (isNav)	{
		return theObj.left;
	} else {
		return theObj.pixelLeft;
	}
}

// Retrieving the y coordinate of a positionable object
function getObjectTop(obj)	{
	var theObj = getObject(obj);
	if (isNav)	{
		return theObj.top;
	} else {
		return theObj.pixelTop;
	}
}


// Return rendered height of object content in pixels
function getObjHeight(obj)	{
	if (isNav)	{
		return obj.clip.height;
	} else if (isIE) {
		return obj.clientHeight;
	} //else {
	//	return getElementById('banner').clipHeight
	//	}
}

// Return rendered width of object content in pixels
function getObjWidth(obj)	{
	if (isNav)	{
		return obj.clip.width;
	} else if(isIE) {
		return obj.clientWidth;
	} //else {
	//	return document.getElementById('banner').clipWidth
		// dom
	//}
}

// Returns the available content width space on browser window
function getInsideWindowWidth() {
	if (isNav)	{
		return window.innerWidth;
	} else if (isIE) {
		return document.body.clientWidth;
	} else {
		return window.innerWidth;
	}
}

// Returns the available content height space on browser window
function getInsideWindowHeight() {
	if(typeof window.innerHeight=="number")
		return window.innerHeight;
	else if(document.documentElement&&document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if(document.body&&document.body.clientHeight)
		return document.body.clientHeight;
	return 0;
}



// put the object at the centre of window
function centerObjectById (elemid) {
  var obj = document.getElementById(elemid);
  var x = Math.round( getInsideWindowWidth()/2 - getObjWidth(obj)/2 );
  var y = Math.round( getInsideWindowHeight()/2 - getObjHeight(obj)/2 );

  shiftTo(obj, x, y);
}


function getNextSibling(startBrother)
{
	endBrother=startBrother.nextSibling;
	while(endBrother.nodeType!=1)
	{
		endBrother = endBrother.nextSibling;
	}
	return endBrother;
} 

function getFirstChild(elm)
{
	if ( !elm.childNodes.length )
	{
		return elm.firstChild;
	}

	var children = elm.childNodes.length;
	for ( var i = 0; i <= children; ++i )
	{
		if ( elm.childNodes[i].nodeType == 1 )
		{
			return elm.childNodes[i];
		}
	}
	
	return elm.firstChild;
}


