// DOM extentions  v1.0
// documentation: http://www.dithered.com/javascript/dom_extensions/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)


// offsetTop and offsetLeft corrections
// note: IE5 Mac will not include page margins in calculations.
function getOffsetTop(element, deep) {
   return getOffsetProperty(element, 'Top', deep);
}

function getOffsetLeft(element, deep) {
   return getOffsetProperty(element, 'Left', deep);
}

function getOffsetProperty(element, property, deep) {
   var offsetValue = 0;
   offsetProperty = 'offset' + property;
   
   do {
      offsetValue += element[offsetProperty];
      element = element.offsetParent;
   } while (deep == true && element != document.body && element != null);
   return offsetValue;
}

/***************************************************
This section is from the NetFlix domUtilities.js
****************************************************/

/* Constants */
var AXIS_X = 0;
var AXIS_Y = 1;

/* shortcut to getElementById */
function getNode(node) {

	return document.getElementById(node);
}

function getNodeForDoc(nodeId, doc) {

	return doc.getElementById(nodeId);
}

function killNode(x) {
	//alert(x.parentNode.id);
	x.parentNode.removeChild(x);
}  

/* Destroy a node with the Death Star */
function killNodeById(nodeID) {
	var node = getNode(nodeID);
	var nodeParent = node.parentElement;
	if (typeof nodeParent != "undefined") {
		nodeParent.removeChild(node);
	} else {
		node.style.display = "none";
	}
}

/* Replace text in a node. */
function swapTextNode(parentObject, newText) {
//alert("swapping");
    parentObject.replaceChild(document.createTextNode(newText), parentObject.firstChild);
	//alert("after swapping");
}
/* Replace child node. */
function swapChildNode(parentObject, newNode) {
//alert("swapping");
    parentObject.replaceChild(newNode, parentObject.firstChild);
	//alert("after swapping");
}


/* Replace text in a node. Has more protection than the previous version and will fall back on non-DOM techniques if it can't pick out the childNodes */
function replaceInnerText(node, text) {
	//	replacement for node.innerHTML = text; which is apparently broken in Safari 1.2.4
	if (typeof node.childNodes != "undefined" && node.childNodes.length > 0) {
		var newnode = document.createTextNode(text);
		node.replaceChild(newnode, node.firstChild);
	} else {
		// fall back on the old school just in case
		node.innerHTML = text;
	}
}

/* Cross browser solution to DOM getComputedStyle() (Safari doesn't currently support this) */
function grabComputedStyle(elementObject) {

    if (document.defaultView && document.defaultView.getComputedStyle ) {
        return  document.defaultView.getComputedStyle(elementObject, null);
     } else if  (elementObject.currentStyle) {
        return elementObject.currentStyle;
    } else  {
        return null;
    }

}

function grabComputedHeight(elementObject) {

    var height = grabComputedStyle(elementObject).height;
    
    if (height != null) {
        if (height.indexOf("px") != -1)
            height = height.substring(0, height.indexOf('px'));
        if (height == "auto") {
            if (elementObject.offsetHeight)
                height = elementObject.offsetHeight;
        }
    }
    
    return height;
    
}

/* Return the target node for an event. */
function getEventTarget(evt) {
	var tgt = evt.srcElement;
	if (!tgt)
		tgt = evt.target;
	return tgt;
}


/* Clear out any text that may have been selected (called "ranges") by dragging the mouse mid-click. */

function clearRanges(event) {
	if (isMacIE) { // Mac IE is blowing up
	} else if (isSafari) { // Avoid Safari 1.3 / 2.0 bugs with window.getSelection()
		event.stopPropagation();
	} else if (document.selection) { // IE 6
		document.selection.empty();
	} else if (window.getSelection()) { // NS 6
		window.getSelection().removeAllRanges();
	} else {
		event.stopPropagation();
	}
}

/* How much the document's been scrolled vertically. Needed for correcting position calculations. */
function getDocumentScrollAmount() {
	if (!isSafari) {
		return document.body.scrollTop;
	} else {
		return 0;
	}
}

/* Computes the total vertical offset of a node, the sum of the enclosing objects' vertical offset. */
function getElementOffsetY(element) {
	var totalOffset = 0;
	if (element.offsetTop != null) {
        totalOffset += element.offsetTop;
		while (element.offsetParent) {
			totalOffset += element.offsetParent.offsetTop;
			element = element.offsetParent;
		}
	}
	return totalOffset;
}

function getElementMouseCoordinate(evt, curEmt) {
var xC = -1;  
    if (!evt) var evt = window.event;
    
    if (evt.offsetX) {
        xC = evt.offsetX;
    } else if (curEmt.offsetX != null) {
        eXC = curEmt.offsetX;
        xC = evt.layerX - eXC;
    } else if (evt.layerX) {
        eXC = getElementOffsetX(curEmt);
        xC = evt.layerX - eXC;
        curEmt.offsetX = eXC;
    }
    return xC;
}

/* Computes the total horizontal offset of a node, the sum of the enclosing objects' horizontal offset. */
function getElementOffsetX(element) {
    var totalOffset = 0;
    if (element.offsetLeft != null) {
        totalOffset += element.offsetLeft;
        while (element.offsetParent) {
            totalOffset += element.offsetParent.offsetLeft;
            element = element.offsetParent;
        }
    }
    return totalOffset;
}

/* Gets the position of the top of a node. */
function getNodeTop(nodeId) {
	var itemOver = document.getElementById(nodeId);
	var itemOverTop = itemOver.style.top ? (itemOver.offsetTop - stripUnits(itemOver.style.top)) : itemOver.offsetTop;
	return itemOverTop;
}


/* Moves an object vertically. Pass in either the node or the node's ID. If amount is positive, move it down. Negative? Move it up. */
function moveNode(mover, amount) {
	if (typeof mover == "string")
		mover = getNode(mover);
	
	if (mover.style.top) {
		mover.style.top = stripUnits(mover.style.top) + amount + "px";
	} else {
		mover.style.top = (amount) + "px";
	}
}

/* Debug utility; displays all of the properties of an object in a popup */
function displayObject(obj) {
	var msg = "";
	for (var prop in obj) {
		msg += prop;
		msg += ": ";
		msg += prop.value;
		msg += "\n";
	}
	window.alert(msg);
}

function isXMLReady(xmlObject) {
	return (xmlObject.readyState == 4 && xmlObject.status == 200);
}

function setTextNodeValue(node, value) {

	for(var chld = node.firstChild; chld; chld = chld.nextSibling) {
		if (chld.odeType == 3) { // text node
			chld.nodeValue=value;
			return;
		}
	}
}

function deleteAllChildNodes(cell) {
alert("deleting children of " + cell.id); 
	if ( cell.hasChildNodes() ) {
    	while ( cell.childNodes.length >= 1 ){
			alert("Id deleting=" + cell.firstChild.id);
        	cell.removeChild( cell.firstChild );       
    	} 
	}
}   

function getNodeAttributeValue(node, attributeName) {
	x=node.attributes;
	return(x.getNamedItem(attributeName).value);
}

function setNodeAttributeValue(node, attributeName, val) {
	x=node.attributes;
	x[0].setAttribute(attributeName,val);
}

function createDiv(doc, id) {
	var newDiv = doc.createElement('Div');
	newDiv.id = id;
	return(newDiv);
}
function createDivWithClass(doc, className) {
	var newDiv = doc.createElement('Div');
	newDiv.id = id;
	//newDiv.class = className;
	return(newDiv);
}

function createHiddenText(doc, id, val) {
	var newInput = doc.createElement("INPUT");
	newInput.type = 'hidden';
	newInput.id = id
	newInput.value = val;
	return newInput;
}