/*
 * This is a set of functions to allow manipulation of document objects that
 * are part of the DOM specified by the W3C.
 *
 * Copyright (C) 2003 by John Glorioso, Zitego Solutions, LLC
 * <jglorioso@zitego.com>. All rights reserved.
 *
 * Redistribution and use in source forms, with or without modification, are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @author John Glorioso
 * @version $Id: dhtml.js,v 1.1.1.1 2011/03/01 15:03:20 jglorioso Exp $
 */

/*
 * Returns the given object specified by name and given the document to search
 * in. If doc is not specified, then document is assumed.
 *
 * @param name The name of the object to get.
 * @param doc The document to search in.
 */
function getObject(name, doc)
{
    var obj;
    if (!doc) doc = document;

    //First look in the frames. The name would be <obj_name>?<frame_name>
    var qIndex = name.indexOf("?");
    if (qIndex > 0 && parent.frames.length)
    {
        doc = parent.frames[name.substring(qIndex+1)].document;
        name = name.substring(0, qIndex);
    }
    if ( !(obj=doc[name]) && doc.all) obj = doc.all[name];

    //Look in the forms
    if (!obj && doc.forms)
    {
        for (var i=0; i<doc.forms.length; i++)
        {
            obj = doc.forms[i][name];
            if (obj) break;
        }
    }

    //Check out netscape layers objects
    if (!obj)
    {
        for (var i=0; doc.layers && i<doc.layers.length; i++)
        {
            obj = getObject(name, doc.layers[i].document);
            if (obj) break;
        }
    }

    //Finally check getElementById
    if (!obj && document.getElementById) obj = document.getElementById(name);

    return obj;
}

/*
 * Changes the background color of the named object to the specified color.
 *
 * @param objName The name of the object to change the color of.
 * @param color The color to change to.
 */
function swapColor(objName, color)
{
    var obj = getObject(objName);
    if ( obj != void(0) ) obj.style.backgroundColor = color;
}

/**
 * Centers a dom element in the center of the screen.
 *
 * @param elem A dom element in an html page.
 */
function centerElement(elem)
{
    if (!elem) return;

    var x, y;
    if (self.innerHeight)
    {
        x = self.innerWidth;
        y = self.innerHeight;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientHeight)
        {
            x = document.documentElement.clientWidth;
            y = document.documentElement.clientHeight;
        }
        else
        {
            if (document.body)
            {
                x = document.body.clientWidth;
                y = document.body.clientHeight;
            }
        }
    }

    var top = (y / 2 - elem.offsetHeight / 2) + f_scrollTop();
	var left = x / 2 - elem.offsetWidth / 2;
	if (left <= 0) left = 10;

	elem.style.left = left + "px"
	elem.style.top = top + "px";
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function getObjectHeight(obj)
{
 if (!obj) return 0;
 else if (obj.innerHeight) return obj.innerHeight; // all except Explorer
 else if (obj.clientHeight) return obj.clientHeight; // Explorer 6 Strict Mode
 else return 0;
}

function getObjectWidth(obj)
{
 if (!obj) return 0;
 else if (obj.innerWidth) return obj.innerWidth; // all except Explorer
 else if (obj.clientWidth) return obj.clientWidth; // Explorer 6 Strict Mode
 else return 0;
}
