// SimpleAPI
// set of functions for easily manipulating layers, a simple alternative to using the DynLayer
// 19990326

// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/

// warning: these have not been test, please email me a fix if there's any problems

var topCache = new Array();

// get the style object reference
function getObject(id,nestref) {

	if (is.ns) {
	 if (is.v >= 5) {
		return document.getElementById (id);
	 } else {
	   //return (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;
	   return document.layers[id].document;
	 }
	}
	if (is.ie) return document.all[id].style;
}

// get the element object reference
function getElement(id,nestref) {
	if (is.ns) return getObject(id,nestref)
	if (is.ie) return document.all[id]
}

// get the element object reference
function getDOMElement(id) {
  if (document.getElementById)   return document.getElementById (id);
  if (document.layers) { 
     return document.layers[id]; 
  } else if (document.all) { 
    return document.all[id]; 
  }

  return null;

}

function getObjLeft (obj)
{
	var left = -1;

	if (obj)
	{
		if (obj.style && obj.style.left) 
                  left = parseInt(obj.style.left);
		if (obj.clientX) left = obj.clientX;
		if (obj.offsetLeft) left = obj.offsetLeft;
	}
	return left;
}


function getLeft (id)
{
	var obj = getDOMElement (id);
	var left = -1;
	
	if (obj)
	{
          return getObjLeft(obj);
	}
	
	return left;
}

function getViewportWidth() {

    // supported in Mozilla, Opera, and Safari
     if(window.innerWidth)
         return window.innerWidth;
	
    // supported in standards mode of IE, but not in any other mode
     if(window.document.documentElement.clientWidth)
         return document.documentElement.clientWidth;
	
    // supported in quirks mode, older versions of IE, and mac IE (anything else).
    return window.document.body.clientWidth;
}

function getViewportHeight() {

    // supported in Mozilla, Opera, and Safari
     if(window.innerHeight)
         return window.innerHeight;
	
    // supported in standards mode of IE, but not in any other mode
     if(window.document.documentElement.clientHeight)
         return document.documentElement.clientHeight;
	
    // supported in quirks mode, older versions of IE, and mac IE (anything else).
    return window.document.body.clientHeight;
}

function getWidth (id)
{
	var obj = getDOMElement (id);
	var width = -1;
	
	if (obj)
	{
		if (obj.offsetWidth) width = obj.offsetWidth;
	}
	
	return width;
}

function getHeight (id)
{
	var obj = getDOMElement (id);
	var height = -1;
	
	if (obj)
	{
		if (obj.clientHeight) 
		{
			height = obj.clientHeight;
		} else {
			if (obj.offsetHeight)
			{
				height = obj.offsetHeight;			
			} else {
				if (obj.style && obj.style.height)
				{
					height = parseInt (obj.style.height);
				}
			}
		}
	}
	
	return height;
}

function getObjTop (obj)
{
	var top = null;

	if (obj)
	{
		if (obj.style && obj.style.top) top = parseInt(obj.style.top);
		if (obj.clientY) top = obj.clientY;
		if (obj.offsetTop) top = obj.offsetTop;
	}
	
	if (top == null)
	{
		if (topCache[obj.id])
		{
			top = topCache[obj.id];
		} else {		
			top = obj.offsetTop;
			
			if (obj.parentElement != null)
			{
				top += getObjTop (obj.parentElement);
			}
			
			topCache[obj.id] = top;
		}		
	}
	
	return top;
}



function getTop (id)
{
	var obj = getDOMElement (id);
	
	return getObjTop (obj);
}


// show and hide functions
function show(obj) {
  if (is.ns && is.v < 5) obj.visibility = "show"
  else if (is.ns && is.v >= 5) obj.style.visibility = "visible"
  else if (is.ie) obj.visibility = "visible"
}
function hide(obj) {
  if (is.ns && is.v < 5) obj.visibility = "hide"
  else if (is.ns && is.v >= 5) obj.style.visibility = "hidden"
  else if (is.ie) obj.visibility = "hidden"
}

// movement functions
function getX(obj) {
	if (is.ns) return obj.css.left
	else return obj.css.pixelLeft
}
function getY(obj) {
	if (is.ns) return obj.css.top
	else return obj.css.pixelTop
}
function setX(obj,x) {
	if (is.ns) obj.css.left = x
	else obj.css.pixelLeft = x
}
function setY(obj,y) {
	if (is.ns) obj.css.top = y
	else obj.css.pixelTop = y
}
function moveTo(obj,x,y) {
	if (x!=null) setX(obj,x)
	if (y!=null)setY(obj,y)
}
function moveBy(obj,x,y) {
	setX(obj,getX(obj)+x)
	setY(obj,getY(obj)+y)
}

function resizeLayer (id) {
  var obj;

  if (is.ns && is.v < 5) {
    obj = document.layers[id];
  } else {
    obj = document.getElementById (id);
  }

  if (is.ns && is.v < 5) {
    /*alert (obj.clip.width + " " + obj.clip.height);
    alert (obj.);
    obj.clip.width = obj.width;
    obj.clip.height = obj.height;*/
    //obj.resizeTo (obj.clip.width, obj.clip.height);
    alert (obj.toSource());
    //obj.resizeBy (0,0);
  }
}

// layer write function
function layerWrite(elm,text) {  // use getElement()    
  if (is.ns && is.v < 5) {
    elm.open();
    elm.write(text);
    elm.close();
  }
  else elm.innerHTML = text
}



// background color
function setbg(obj,color) {
	if (is.ns) obj.document.bgColor = color
	else obj.backgroundColor = color
}



function setStyle (id_or_obj, name, value)
{
  var obj = null;

  //  window.status = "TYPE: " + typeof(id_or_obj);

  if (typeof(id_or_obj) == 'string') 
    obj =  getDOMElement (id_or_obj);
  else
    obj = id_or_obj;
  
  if (obj && obj.style)	
    {
      obj.style[name] = value;
    }
}


function getStyle(id_or_obj, name)
{
  var obj = null;
  if (typeof(id_or_obj) == 'string')
     obj = getDOMElement(id_or_obj);
  else
     obj = id_or_obj;

  if (obj && obj.style)
  {
     return obj.style[name];
  } else {
     return "ERROR";
  }
}
var onloadFuncs = [];
    
function registerOnloadFunc(cb) {
        onloadFuncs[onloadFuncs.length] = cb;
}        

function doOnLoad() {
        for (var i = 0; i < onloadFuncs.length; i++) {
                if ( 'function' == typeof(onloadFuncs[i]))
                        onloadFuncs[i]();
                else
                        eval(onloadFuncs[i]);
        }
}   



