// vim: set tabstop=4 shiftwidth=4 foldmethod=marker :
/**
 * Various dynamic HTML abstraction functions
 */
// {{{ getObject(htmlElementId)
/**
 * Gets an object by Id
 * This was orginally getObject() in template.js.
 * @param htmlElementId string the id of the HTML element to get
 * @return CSSHTMLObject|false
 */
function getObject(htmlElementId)
{
	if (document.getElementById) {
		return document.getElementById(htmlElementId);
	} else if (document.all && document.all(htmlElementId)) {
		document.all(htmlElementId);
	} else {
		return false;
	}
}
// }}}
// {{{ getStyleObject(htmlElementId)
/**
 * Gets the css style object
 * This was orginally getStyleObject() in template.js.
 * @param htmlElementId string the id of the HTML element to get
 * @return CSSStyleObject|false
 */
function getStyleObject(htmlElementId)
{
	var the_object = getObject(htmlElementId);
	return (the_object) ? the_object.style : false;
}
// }}}
// {{{ getIframeDocument(iframeObj)
/**
 *  Returns a document object related to a given iFrame. This can be useful
 *  for <code>
 *      var iframedoc = getIframeDocument(iframeobj);
 *      if (iframedoc) {
 *          iframedoc.location.replace(url);
 *      }
 *  </code>
 *
 * @param iframeObj the iFrame object
 * @return may be false if there is no document object for a given iframe.
 */
function getIframeDocument(iframeObj)
{
    if (!iframeObj) { return false; }
    if (iframeObj.contentDocument) {
        // For NS6
        return iframeObj.contentDocument;
    } else if (iframeObj.contentWindow) {
        // For IE5.5 and IE6
        return iframeObj.contentWindow.document;
    } else if (iframeObj.document) {
        // For IE5
        return iframeObj.document;
       }
    return false;
}
// }}}
// {{{ changeVisibility(htmlElementId, displayType)
/**
 * This will change the display type (visibility) of a HTML element.
 * This was orginally changeDiv in template.js.
 * @param htmlElementId string the id of the HTML element to change
 * @param displayType string "block", "none", etc.
 * @return boolean whether or not it was successful
 */
function changeVisibility(htmlElementId, displayType)
{
	var the_style = getStyleObject(htmlElementId);
	if (the_style != false) {
		the_style.visibility = displayType;
		return true;
	} else {
		return false;
	}
}

// included for backwards compatibility (e.g. importing.cs)
function changeDiv(htmlElementId, displayType) {
    return changeDisplay(htmlElementId, displayType);
}
// }}}
// {{{ changeDisplay(htmlElementId, displayType)
/**
 * This will change the display type (visibility) of a HTML element.
 * This was orginally changeDiv in template.js.
 * @param htmlElementId string the id of the HTML element to change
 * @param displayType string "block", "none", etc.
 * @return boolean whether or not it was successful
 */
function changeDisplay(htmlElementId, displayType)
{
	var the_style = getStyleObject(htmlElementId);
	if (the_style != false) {
		the_style.display = displayType;
		return true;
	} else {
		return false;
	}
}

// included for backwards compatibility (e.g. importing.cs)
function changeDiv(htmlElementId, displayType) {
    return changeDisplay(htmlElementId, displayType);
}
// }}}
// {{{ toggleDisplay(htmlElementId, visibleDisplayType)
/**
 * This will toggle the display type (visibility) of a HTML element between
 * specified and none.
 * @param htmlElementId string the id of the HTML element to change
 * @param displayType string "block", "inline", etc.
 * @return mixed returns false if it can't toggle, else it returns the
 *		current display type.
 */
function toggleDisplay(htmlElementId, displayType)
{
	var the_style = getStyleObject(htmlElementId);
	if (the_style == false) {
		return false;
	}
	if (the_style.display == displayType) {
		the_style.display = 'none';
	} else {
		the_style.display = displayType;
	}
	return the_style.display;
}
// }}}
// {{{ setInnerHtml(htmlElementId, html)
/**
 * Dynamically replace text.
 */
function setInnerHtml(htmlElementId, html) {
	var bodyobj = getObject(htmlElementId);
	if (bodyobj && (typeof bodyobj.innerHTML != 'undefined')) {
		bodyobj.innerHTML = html;
		return true;
	}
	return false;
}
// }}}
// {{{ ensureElementFromObject(el,again)
/**
 * IE6 fix to make sure Object prototypes are attached to HTML Elements
 */
function ensureElementFromObject(el,again)
{
    // if not IE6 return
    if(!window.attachEvent) {return;}
    if((el.getAttribute('elementFromObject') != 'true') || (again == true)){ 
        for(property in Object) { el[property] = Object[property]; }
        el.setAttribute('elementFromObject','true');
    }
}
// }}}

/**
 * Dynamically get text.
 */
function getInnerHtml(htmlElementId) {
	var bodyobj = getObject(htmlElementId);
	if (bodyobj && (typeof bodyobj.innerHTML != 'undefined')) {
		return bodyobj.innerHTML;
	}
	return '';
}



function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getInfo(obj, selector) {
    if (!obj || !selector) { return; }

	var styleValue;
	switch(selector) {
		case 'height':
			styleValue = obj.offsetHeight;
			break;
		case 'width':
			styleValue = obj.offsetWidth;
			break;
		case 'top':
			styleValue = 0;
			if (obj.offsetParent) {
				while (obj.offsetParent) {
					styleValue += obj.offsetTop;
					obj = obj.offsetParent;
				}
			}
			else if (obj.x) { styleValue += obj.y; }
			break;
		case 'left':
			styleValue = 0;
			if (obj.offsetParent) {
				while (obj.offsetParent) {
					styleValue += obj.offsetLeft;
					obj = obj.offsetParent;
				}
			}
			else if (obj.x) { styleValue += obj.x; }
			break;
		default:
			var viewCSS = (typeof document.defaultView=='function') ? document.defaultView() : document.defaultView;
			if (viewCSS && viewCSS.getComputedStyle) {
				var s = viewCSS.getComputedStyle(obj,null);
				return s && s.getPropertyValue(selector);
			}
			styleValue = obj.currentStyle && (obj.currentStyle[selector] || null) || null;
	}
	return styleValue;
}
