/*

Requires global.js


*/
var divobjs3_data = null; //required to avoid triggering stric warning in Firebug at global.js

function init_divobjs3()
{
  	 if(document.getElementsByTagName) create_dom_objects();	 
}

var theObjs = {};

function create_dom_objects()
{
 	var divElements = document.getElementsByTagName('div');
	
   	for(var i=0, j=divElements.length; i<j; i++)
   	{
  		if( divElements[i].id )
  		{
  			theObjs[divElements[i].id] = new dom_object(divElements[i]);			
		}
   	}
}

function dom_object(obj)
{
	this.el = obj;
	this.id = obj.id;
}


dom_object.prototype.setClass = function(cl)
{
		/* Leaves existing class, if it exists, and adds new class name */
	if( typeof(cl) != 'string')
	{
		return;
	}	
	
	var class_name = this.el.className;
	
	cl = cl.trim();
	
	if( !class_name )
	{
		this.el.className = cl;
	}
	else
	{
		var classes = class_name.split(' ');
		var class_exists = 0;
		
		for(var i=0,j=classes.length; i<j; i++)
		{
			var class_name = classes[i];
			class_name = class_name.trim();

			if( class_name == cl )
			{
				class_exists = 1;
			}
		}
		
		if( !class_exists )
		{
			this.el.className += ' ' + cl;
		}
	}
}

dom_object.prototype.swapClass = function(old_class, new_class)
{
	var class_name = this.getClass();
	var updated_class = '';
	
	if( !this.el.className )
	{	
	    return;
	}
	
	var classes = this.el.className.split(' ');
	
	for(var i=0; i<classes.length; i++)
	{
		if( classes[i] == old_class )
		{
			classes[i] = new_class
		}
		
		updated_class  += (updated_class) ? ' ' + classes[i] :  classes[i];
	}

	this.el.className = updated_class;
}

dom_object.prototype.getClass = function(cl)
{
	return this.el.className;
}

/* 							Position
 * 
 * getX and getY return positions relative to an absolutely positioned parent.
 * To get position relative to body if a parent element is absolutely positioned 
 * use getAbsoluteX and getAbsoluteY
 */

dom_object.prototype.setX = function(x)
{
  	this.el.style.left = parseInt(x) + 'px';
}

dom_object.prototype.setY = function(x)
{
	
	this.el.style.top = parseInt(x) + 'px';
}

dom_object.prototype.getX = function()
{
    return this.el.offsetLeft;                          
}

dom_object.prototype.getY = function()
{
	return this.el.offsetTop;
  
}

dom_object.prototype.getAbsoluteX = function()
{
	var el = this.el;
    var x = el.offsetLeft;     
    var parent = el.offsetParent;   
     
    while (parent != null)
    {                              
        x += parent.offsetLeft;     
        parent = parent.offsetParent;  
    }
    
    return x;                     
}

dom_object.prototype.getAbsoluteY = function()
{
	var el = this.el;
    var y = el.offsetTop;
    var parent = el.offsetParent;
    
    while (parent != null)
    {
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    
    return y;
}

/* *************************  Dimensions  *********************** */
 
dom_object.prototype.setHeight = function(h)
{
	if( typeof(h) == 'number' )
	{
		this.el.style.height = h + 'px';
	}
}

dom_object.prototype.setWidth = function(w)
{
	if( typeof(w) == 'number' )
	{
		this.el.style.width = w + 'px';
	}
}

dom_object.prototype.getHeight = function(h)
{
	return this.el.offsetHeight;
}

dom_object.prototype.getWidth = function(w)
{
	return this.el.offsetWidth;
}

dom_object.prototype.resize = function(x,y)
{
	this.setWidth(x);
	this.setHeight(y);
}

dom_object.prototype.get_coordinates = function(absolute_pos,client_width)
{
	// added 1st Jan 2007
	// wed April 8th 2009. added optional args absolute_pos and client_only
	// absolute pos is taken from root element - html or body el
	// client_only refers to client_width rather than width including borders
	var el_left = absolute_pos ? this.getAbsoluteX() : this.getX(); // left coordinate of element when drag is stopped on mouseup
	var el_top  = absolute_pos ? this.getAbsoluteY() : this.getY(); // right coordinate of element when drag is stopped on mouseup
	var el_w = client_width ? this.el.clientWidth : this.getWidth();
	var el_h = client_width ? this.el.clientHeight : this.getHeight();
	var el_right = el_left + el_w;
	var el_bottom = el_top + el_h;
	return {top: el_top, right: el_right, bottom: el_bottom, left: el_left} 
}

/* ******************* Visibility/Display ********************************** */

dom_object.prototype.hide = function()
{
 	this.el.style.visibility = "hidden";
 	return true;
}

dom_object.prototype.getVisibility = function()
{
	return this.getCSS('visibility');
}

dom_object.prototype.show = function()
{
 	this.el.style.visibility = "visible";
}

dom_object.prototype.display = function()
{
 	this.el.style.display = 'block';
 	return true;
}

dom_object.prototype.getDisplay = function()
{
	 return	this.getCSS('display');
}

dom_object.prototype.vanish = function()
{
 	this.el.style.display = 'none';
 	return true;
}

/* ******************* Shortcuts to some DOM methods ************/

dom_object.prototype.setAtt = function(att,val)
{
	this.el.setAttribute(att,val);
}

dom_object.prototype.appChild = function(el)
{
	this.el.appendChild(el);
}

dom_object.prototype.appText = function(str)
{
	var textNode = document.createTextNode(str);
	this.el.appendChild(textNode);
}

dom_object.prototype.appHTML = function(str)
{
	this.el.innerHTML = str;
}

/* **************************** CSS stuff ************************* */

function setCSS(el, prop, val)
{
	el = getEl(el);
	
	if( !el )
	{
		return;
	}

	if(typeof(el.style[prop]) != 'undefined')
	{
		el.style[prop] = val;
	}
}

function getCSS(el, propNS)
{
	/**
		var el = getEl(el);
	var d = document.defaultView.getComputedStyle(el, '');

		return;
	s = (document.defaultView.getComputedStyle(el, '') ) ? document.defaultView.getComputedStyle(el,'').getPropertyValue(propNS) : "";
	alert(s);
	**/
}

dom_object.prototype.setCSS = function(prop,val)
{
	if(typeof(this.el.style[prop]) != 'undefined')
	{
		this.el.style[prop] = val;
	}
	
}

dom_object.prototype.getCSS = function(prop)
{
	if(typeof(this.el.style[prop]) != 'undefined')
	{
		return this.el.style[prop];
	}
	
	return null;
	
}

dom_object.prototype.getBackgroundCol = function()
{
  	 return this.getCSS('backgroundColor'); //backgroundColor doesn't work in Opera 5
}

dom_object.prototype.setBackgroundCol = function(col)
{
  	 this.setCSS('backgroundColor', col);
}

dom_object.prototype.getBackgroundImg = function()
{

  	 return this.getCSS('backgroundColor');
}

dom_object.prototype.setBackgroundImg = function(i)
{	
	 if( i.indexOf('url(') != -1 ) 
	 {
	 	this.setCSS('backgroundImage', i);
	 }
	 else 
	 {
	 	this.setCSS('backgroundImage', 'url(' + i + ')');
	 }
	 
}

dom_object.prototype.setPositioning = function(pos)
{
	this.setCSS('position', pos);
}

dom_object.prototype.getPositioning = function(pos)
{
	return this.getCSS('position');
}

dom_object.prototype.getZindex = function()
{
	return this.setCSS('zIndex');
}

dom_object.prototype.setZindex = function(z)
{
	this.setCSS('zIndex', z);
}





/*********************************** CHANGELOG ***************************************
*
*	[Mar 20 2004]	Added recursive offsetLeft and offsetTop check to getX() and getY()
*	[May 07 2004]	Added function getStyleById
*	[Aug 17 2005]   Return val from getX() getY() getHieght() getWidth() no longer string including 'px'  but integer
*	[Aug 17 2005]   Removed variable naemd 'px' that contained string 'px'.  This was originally part of trickery  for Opera6 and below as it prefered int val to px
*	[Aug 17 2005]   Moved function getStyleById() into global.js
*   [Apr 02 2009]   Added move(), move_up(),move_down(),move_left(),move_right(),move_stop(),do_move()
***************************************************************************************/
	
