// 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;
}
// }}}
// {{{ getInnerHtml(htmlElementId)
/**
 * Dynamically get text.
 */
function getInnerHtml(htmlElementId) {
	var bodyobj = getObject(htmlElementId);
	if (bodyobj && (typeof bodyobj.innerHTML != 'undefined')) {
		return bodyobj.innerHTML;
	}
	return '';
}
// }}}
// {{{ createAnimatedEllipses(id, write)
/**
 * Creates a ... which animates (0-1-2-3 dots).
 * @param id    unique ID for this ellipses
 * @param write if true, document.write the ellipses, otherwise return the HTML.
 */
function createAnimatedEllipses(id, write) {
    var html = '<span id="ellip-' + id + '">...</span>';
    if (write) document.write(html);
    animateEllipses(id);
    if (!write) return html;
}

function animateEllipses(id) {
    var ellipObj = getObject('ellip-' + id);
    if (ellipObj && ellipObj.style && ellipObj.style.display != 'none') {
        var curLen = ellipObj.innerHTML.length;
        var newLen = (curLen + 1) % 4;
        var newDots = '';
        for (var i = 0; i < newLen; i++) {
            newDots += '.';
        }
        //alert("new: " + newDots);
        ellipObj.innerHTML = newDots;
    }
    setTimeout('animateEllipses("' + id + '")', 250);
    
}
// }}}
// {{{ injectInnerHtml(tagId,htmlToInject)
/**
 * Non-flickering way of setting innerHTML on an element.
 * @param tag id of an elem (string) or the elem itself
 * @todo optimize by generating offline and using a replace node
 * Taken from ajax.js (we should consolidate)
 * NOTE: If you call this on an object by ref, you will lose the handle (it gets replaced).
 */
function injectInnerHtml(tag, htmlToInject)
{
    var tag_obj = tag;
    if (typeof tag == 'string') {
        tag_obj = getObject(tag);
    }
	if (!tag_obj) {
		alert('No such element: '+tag);
		return false;
	}
    var temp_obj = tag_obj.cloneNode(false);
    temp_obj.innerHTML = htmlToInject;
    tag_obj.parentNode.replaceChild(temp_obj,tag_obj);
    return true;
}
// }}}
