/*

Requires global.js


*/
init_func = 'init_divobjs';

if(typeof(init_funcs) == 'undefined') {
	init_funcs = null;
} 
else {
	init_funcs[init_funcs.length] = init_func;
}


var testPos = timer = false;
var timerOn = hrestPos = allLinks = numberOfDivElements = 0;
var p = timerOff = 1;
var end = objsLoaded = false;
var opera = ( navigator.userAgent.indexOf('Opera')!=-1 && navigator.userAgent.indexOf('Opera 7')==-1)
var konq = ( navigator.userAgent.indexOf('Konqueror')!=-1 );
var ie5 = (navigator.userAgent.indexOf('Opera')==-1 && navigator.userAgent.indexOf('MSIE 5.')!=-1 );

var theObjs = new Array();

function init_divobjs()
{

  	 if(document.getElementsByTagName) create_dom_objects();
	
}

function create_dom_objects()
{
   	
   	theObjs = new Array();
 	divElements = document.getElementsByTagName('div');
  	numberOfDivElements = divElements.length;
	
   	for(i=0; i < numberOfDivElements;i++)
   	{
  		if(divElements[i].id)
  		{
  			var obj=divElements[i];
  			theObjs[obj.id] = new dom_object(obj);
			
		}
   	}
}

function dom_object(obj)
{
	
	this.actual = obj;
   	this.hide = domHide;
   	this.show = domShow;
	this.show_display = domDisplay;
   	this.hide_display = domDisplayNone;
	this.getVisibility = domGetVisibility;
	this.getPosition = domGetPosition;
	this.setPosition = domSetPosition;
	this.getX = domGetX;
	this.getY = domGetY;
	this.setX=domSetX;
   	this.setY=domSetY;
	this.setWidth = domSetWidth;
	this.setHeight= domSetHeight;
	this.getWidth = domGetWidth;
	this.getHeight = domGetHeight;
	this.resize = domResize;
	this.getDisplay = getDisplay;
   	this.xmoveBy=xdomMoveBy;
   	this.ymoveBy=ydomMoveBy;
   	this.mover = domMover;
   	this.setBgCol = domSetBackgroundCol;
	this.getBgCol = domGetBackgroundCol;
	this.setBgImg = domSetBackgroundImg;
	this.getBgImg = domGetBackgroundImg;
	this.setBorderWidth = domSetBorderWidth;
	this.getBorderWidth = domGetBorderWidth;
	
	this.setBorderStyle = domSetBorderStyle;
	this.setBorderColor = domSetBorderColor;
  	this.objNothing = doNothing();    /* 1st version of konqueror needs this to call the other
                                                  methods - for some reason */
	this.setZindex = domSetZindex;  
	this.getZindex = domGetZindex;  
	this.setClass = domSetClass;
	this.getClass = domGetClass;
	this.swapClass = domSwapClass;
	this.getPadding = domGetPadding;
	this.centreInViewport = centreInViewport;
	this.storeMarginVals = storeMarginVals;
	this.restoreMarginVals = restoreMarginVals;
	this.removeMargins = removeMargins;
	this.restored = 1;
	this.cloned = 0;
	this.snapshots = [];
	this.calls=0;
	this.fillViewport = fillViewport;
	this.get_coordinates = get_coordinates;
}


function domSetX(x)
{
  	this.actual.style.left = parseInt(x) + 'px';
}

function domSetY(x)
{
	this.actual.style.top = parseInt(x) + 'px';
}

function domGetX()
{
    
    var el = this.actual;
    var x = el.offsetLeft;     
    var parent = el.offsetParent;    
    while (parent != null)
    {                              
        x += parent.offsetLeft;     
        parent = parent.offsetParent;  
    }
    
    return x;                          
}

function domGetY()
{
    var el = this.actual;
    var y = el.offsetTop;
    var parent = el.offsetParent;
    while (parent != null)
    {
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return y;
}

function domSetHeight(h)
{
	if( typeof(h) == 'number' )
	{
		this.actual.style.height = h + 'px';
	}
	else
	{
		this.actual.style.height = h;
	}
}

function domSetWidth(w)
{
	if( typeof(w) == 'number' )
	{
		this.actual.style.width = w + 'px';
	}
	else
	{
		this.actual.style.width = w;
	}
}

function domGetHeight(h)
{
	return this.actual.offsetHeight;
}

function domGetWidth(w)
{
	return this.actual.offsetWidth;
}

function domResize(x,y)
{
	this.actual.style.width = x + 'px';
	this.actual.style.height = y + 'px';
}

function xdomMoveBy(x)
{
  	var leftPos = this.getX();
  	this.actual.style.left = leftPos + x + 'px';
}

function ydomMoveBy(x)
{
  	var topPos = this.getY();
  	this.actual.style.top = topPos + x + 'px';
}

function centreInViewport()
{
	// needs to be an absolutely positioned div
	if(!this.restored) return;
	//alert(this.actual.style.position);
	var w = window.innerWidth - this.getWidth(); //getWidth returns width including width of padding and borders
	var h = window.innerHeight - this.getHeight();
	var scroll_top = (document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;
	var scroll_left = (document.documentElement && document.documentElement.scrollLeft)?document.documentElement.scrollLeft:document.body.scrollLeft;
	this.storeMarginVals();
	this.removeMargins();
	this.setX( w/2 + scroll_left);
	this.setY( h/2  + scroll_top);
	this.counter++;
	this.restored = 0;
	
}
function storeMarginVals() //takeSnapshot()
{

	
	this.leftMargin 	= getStyleById(this.actual.id,'marginLeft','margin-left');
	this.rightMargin 	= getStyleById(this.actual.id,'marginRight','margin-right');
	this.topMargin 		= getStyleById(this.actual.id,'marginTop','margin-top');
	this.bottomMargin 	= getStyleById(this.actual.id,'marginBottom','margin-bottom');

	 //this.getX() returns offsetLeft - margin + left. Not appropriate here
	this.currentTop 	= getStyleById(this.actual.id,'top','top');
	this.currentLeft 	= getStyleById(this.actual.id,'left','left');
	this.currentWidth	= getStyleById(this.actual.id,'width','width');
	
	this.currentHeight	= getStyleById(this.actual.id,'height','height');

	
	//if(!this.cloned) cloned_el = this.actual.cloneNode(true);
	
		
	this.cloned = 1;
	this.calls++
	//padding
	
}
function restoreMarginVals()
{

	
	
	if(this.leftMargin) 	this.actual.style.marginLeft 	= this.leftMargin;
	if(this.rightMargin)	this.actual.style.marginRight 	= this.rightMargin;
	if(this.topMargin)	this.actual.style.marginTop 	= this.topMargin;
	if(this.bottomMargin)   this.actual.style.marginBottom 	= this.bottomMargin;
	
	this.setX(parseInt(this.currentLeft));
	this.setY(parseInt(this.currentTop));
	this.setWidth(this.currentWidth);
	this.setHeight(this.currentHeight)
	
	
	
	this.restored = 1;
	
	
}

function removeMargins()
{

		this.actual.style.marginLeft 	=  '0px';
		this.actual.style.marginRight 	=  '0px';
		this.actual.style.marginTop 	=  '0px';
		this.actual.style.marginBottom 	=  '0px';
}


function fillViewport()
{
	
	
	var bordersX = parseInt(getStyleById(this.actual.id,'borderLeftWidth','border-left-width'))
	+ parseInt(getStyleById(this.actual.id,'borderRightWidth','border-right-width'));
	
	var bordersY = parseInt(getStyleById(this.actual.id,'borderTopWidth','border-top-width')) 
	+ parseInt(getStyleById(this.actual.id,'borderBottomWidth','border-bottom-width'));

	var paddingX = parseInt(getStyleById(this.actual.id,'paddingLeft','padding-left'))
	+ parseInt(getStyleById(this.actual.id,'paddingRight','padding-right'));

	var paddingY = parseInt(getStyleById(this.actual.id,'paddingTop','padding-top'))
	+ parseInt(getStyleById(this.actual.id,'paddingBottom','padding-bottom'));

	this.storeMarginVals();
	this.removeMargins();
	var windowWidth = window.innerWidth ? window.innerWidth - 16 : document.body.offsetWidth -20;
	var windowHeight = window.innerHeight? window.innerHeight : document.body.offsetHeight;
	//document.body.offsetHeight in IE5 returns the viewport height irrespective of tall the page is 
	if(browser.ie5) bordersX = bordersY = paddingX = paddingY = 0; //IE5 adds borders and padding inside div
	if( windowWidth && windowHeight )
	{
	
		this.setWidth( windowWidth - ( parseInt(bordersX) + parseInt(paddingX) ) ) ;
		this.setHeight( windowHeight - ( parseInt(bordersY) + parseInt(paddingY) ) );
		this.setX(0);
		this.setY(0);
		if(arguments[0]) this.appHTML(arguments[0]);
	}

}

function domMover(c,x,y,z,a,d,v)
{
  	direction = c;
  	divName = x;
  	amount = parseInt(y);
  	speed = z;
  	restPos = parseInt(a);
  	depth = d;
  	followOn = v;

  	var lessThanOrGreaterThan = (amount>0)?"<":">";


  	if(direction == "hoz") 
	{
   	 	pos = this.getX();
    		testPos = pos + lessThanOrGreaterThan + restPos;

    		if(eval(testPos))
    		{

     			
      			
			this.xmoveBy(y);
     			
      			timer = setTimeout("theObjs[divName].mover(direction,divName,amount,speed,restPos,depth,followOn)",speed);

      			
    		}
    		else
    		{

      			if(timer)clearTimeout(timer);

			this.setX(restPos);
  			hrestPos = restPos //shoved in here to grab x coord for tooltips for Konq
 			
      			flag = 0;
      			floatFlag = 0;
     			if(followOn && followOn != "over" && flag == 0) eval(followOn + "()");
     			if(followOn == "over" && flag == 0){end=true;}
      		}
   	}

  	if(direction == "vert")
  	{

    		pos = this.getY();
		
    		testPos = pos + lessThanOrGreaterThan + restPos;


    		vrestPos=restPos;

    		if(eval(testPos))

    		{

      			this.actual.style.zIndex = depth;
			this.ymoveBy(y);
      			timer = setTimeout("theObjs[divName].mover(direction,divName,amount,speed,restPos,depth,followOn)",speed);

    		}
    		else
    		{

      			if(timer)clearTimeout(timer);
			this.setY(restPos);
     			flag=0;
      			floatFlag = 0;
			
     			if(followOn && followOn != "over" && flag == 0) eval(followOn + "()");
      			if(followOn == "over" && flag == 0)end=true;

    		}
  	}
}


function doNothing()
{
	//this is required by Konqueror to call the other methods
	//from within an object constructor function
	//it also plays the role of javascript:void(0)
}

function domHide()
{
 	this.actual.style.visibility = "hidden";
 	return true;
}

   //IE/Dom hide element
   
function domGetVisibility()
{
	return this.actual.style.visibility;
}

function domShow()
{
 	this.actual.style.visibility = "visible";
 	return true;
}

function domDisplay()
{
 	this.actual.style.display = 'block';
 	return true;
}

function getDisplay()
{
	 return	this.actual.style.display;
}
function domDisplayNone()
{
 	this.actual.style.display = 'none';
 	return true;
}


function domGetBackgroundCol()
{
  	 return this.actual.style.backgroundColor; //backgroundColor doesn't work in Opera 5
}
function domSetBackgroundCol(col)
{
  	 this.actual.style.backgroundColor = col; //backgroundColor doesn't work in Opera 5
}
function domGetBackgroundImg()
{

  	 return this.actual.style.backgroundImage; //backgroundColor doesn't work in Opera 5
}
function domSetBackgroundImg(i)
{	
	 var img;
	 if(i.indexOf('url(') != -1) img = i;
	 else img = 'url(' + i + ')'
	 
  	 this.actual.style.backgroundImage = img; //backgroundColor doesn't work in Opera 5
}
function domSetBorderStyle(bStyle)
{
	 this.actual.style.borderStyle = bStyle; //backgroundColor doesn't work in Opera 5
}
function domSetBorderWidth(bWidth)
{
	 this.actual.style.borderWidth = bWidth; //backgroundColor doesn't work in Opera 5
}
function domGetBorderWidth(whichSide)
{
	if(whichSide=='left')whichSide='Left'; //enable 'left' and 'right' etc to be passed
	if(whichSide=='right')whichSide='Right';
	if(whichSide=='top')whichSide='Top';
	if(whichSide=='bottom')whichSide='Bottom';
	
	//Konqueror does not return value for style.border or style.borderLeft. it does return style.borderLeftWidth
	
	return eval('this.actual.style.border' + whichSide + 'Width'); 
}
function domSetBorderColor(bCol)
{
	 this.actual.style.borderColor = bCol;
}

function domGetPadding(whichSide)
{
	if(whichSide=='left') whichSide='Left'; //enable 'left' and 'right' etc to be passed
	if(whichSide=='right') whichSide='Right';
	if(whichSide=='top') whichSide='Top';
	if(whichSide=='bottom') whichSide='Bottom';
	
	return eval('this.actual.style.padding' + whichSide); //backgroundColor doesn't work in Opera 5
}

function domSetClass(cl)
{
	var class_name = this.actual.className;
	cl = cl.trim();
	
	if( !class_name )
	{
		this.actual.className = cl;
	}
	else
	{
		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.actual.className += ' ' + cl;
		}
	}
}

function domSwapClass(old_class,new_class)
{
	var class_name = this.actual.className;
	
	if( !class_name || (class_name == old_class) )
	{	// if no class set add new class
		this.actual.className = new_class;
	}
	else
	{
	
		var reg_exp;
		
		old_class_reg_exp = new RegExp("\\b" + old_class + "\\b");
		new_class_reg_exp = new RegExp("\\b" + new_class + "\\b");
		
		if( !new_class_reg_exp.test(class_name) && !old_class_reg_exp.test(class_name) )
		{
			this.actual.className += ' ' + new_class;
			return;
		}
		
		if( new_class_reg_exp.test(class_name) ) // if new_class already in className
		{
			if( !old_class_reg_exp.test(class_name) )
			{
				return; // new_class already set as className
			}
			else
			{
				class_name = class_name.replace(old_class,'');
			}
		}
		
		if( old_class_reg_exp.test(class_name) )
		{
			class_name = class_name.replace(old_class_reg_exp,new_class);
		}
	
		this.actual.className = class_name;
	}
	
/*
		classes = class_name.split(' ');
		class_recombined = '';
		var class_exists = 0;
		
		for(var i=0,j=classes.length;i<j;i++)
		{
			var class_instance = classes[i];
			
			class_instance = class_instance.trim();
			
			if( class_instance == new_class )
			{
				continue;
			}
			
			if( class_instance == old_class  )
			{
				class_instance = new_class;
			}
			
		
				
			class_recombined = class_recombined + ' ' + class_instance;
		}

		this.actual.className = class_recombined.trim();
	}
	*/
}

function domGetClass(cl)
{
	return this.actual.className;
}

function domSetPosition(pos)
{
	this.actual.style.position = pos;
}

function domGetPosition(pos)
{
	return this.actual.style.position;
}

function domGetZindex()
{
	return this.actual.style.zIndex;
}

function domSetZindex(z)
{
	
	this.actual.style.zIndex = z;
}

function animate(c,x,y,z,a,d,v)
{

  	direction = c;
  	divName = x;
  	amount = y;
  	speed = z;
  	restPos = a;
  	depth = d;
  	followOn = v;
  	theObjs[divName].mover(direction,divName,amount,speed,restPos,depth,followOn);
}
// ============== PROTOTYPES

dom_object.prototype.setAtt = function(att,val)
{
	this.actual.setAttribute(att,val);
}
dom_object.prototype.appChild = function(el)
{
	this.actual.appendChild(el);
}
dom_object.prototype.appTextNode = function(str)
{
	var textNode = document.createTextNode(str);
	this.actual.appendChild(textNode);
}
dom_object.prototype.appHTML = function(str)
{
	this.actual.innerHTML = str;
}

function get_coordinates()
{
	// added 1st Jan 2007
	el_left = this.getX(); // left coordinate of element when drag is stopped on mouseup
	el_top = this.getY(); // right coordinate of element when drag is stopped on mouseup
	el_w = this.getWidth();
	el_h = this.getHeight();
	el_right = el_left + el_w;
	el_bottom = el_top + el_h;
	return {top: el_top, right: el_right, bottom: el_bottom, left: el_left} 
}

// =====================

/*********************************** 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
*
***************************************************************************************/
	
