/***************************************************************************
	UniDom - Div Library object.
	
	This is a modified version of Thomas Brattlis 'lib_obj' this version uses
	the Point and Rectangle objects which I have developed to encapsulate
	Cartesian coordinate calculations. It's hoped that this will allow for
	more complex functionality with easier programming. 
	
	The function names have been standardised so polymorphic behaviour is
	possible. I would have made a base object to act as an interface but this
	is pointless in JavaScript. The Point object may serve as a basic guide
	to any who which to developed based on this model.
	
	Version:
		v0.1, Monday, July 08, 2002
	
	Requires:
		Rectangle v0.1
		
	Author:
		Alexander Whillas

	Copyright (C) 2002 Taylor Square Designs
***************************************************************************/

// Browsercheck (needed) ***************
function lib_bwcheck() { 
  this.ver=navigator.appVersion;
  this.agent=navigator.userAgent;
  this.dom=document.getElementById?1:0;
  this.opera5=this.agent.indexOf("Opera 5")>-1;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6;
  this.mac=this.agent.indexOf("Mac")>-1;
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
  return this;
}
bw = new lib_bwcheck() //Browsercheck object

// Document size object ********
function lib_doc_size(){
//  this.width = bw.ie && document.body.offsetWidth-20||innerWidth||0;
//  this.height = bw.ie && document.body.offsetHeight-5||innerHeight||0;
  this.width = bw.ie && document.body.offsetWidth || innerWidth || 0;
  this.height = bw.ie && document.body.offsetHeight || innerHeight || 0;
	if (bw.ns4) this.width -= 15;
  if(!this.width||!this.height) return message('Document has no width or height') 
  return this;
}

// Lib objects  ********************
function UniDom(obj, nest){
	//if(!bw.bw) 
	//	return lib_message('Old browser');
	
	nest = (!nest) ? "" : 'document.' + nest + '.';
	this.evnt = bw.dom ? document.getElementById(obj) : bw.ie4 ? document.all[obj] : bw.ns4 ? eval(nest + "document.layers." + obj) : 0;
	
	
	if(this.evnt) {
		this.css = bw.dom || bw.ie4 ? this.evnt.style : this.evnt;
		this.ref = bw.dom || bw.ie4 ? document : this.css.document;

		/* 	The following Rectangle/Points for the object and its clipping 
		border are reference objects that help in calculations. The actual
		values are stored in this.css.*. These objects should be updated when
		changing the real values. 	*/
	
		// Represent the current Div objects area.
		this.div = new Rectangle(
			parseInt(this.css.top) || this.css.pixelTop || this.evnt.offsetTop || 0,
			parseInt(this.css.left) || this.css.pixelLeft || this.evnt.offsetLeft || 0,
			this.evnt.offsetWidth || this.css.clip.width || this.ref.width || this.css.pixelWidth || 0,
			this.evnt.offsetHeight || this.css.clip.height || this.ref.height || this.css.pixelHeight || 0);

		// Representing the Clipping path/margin of the object.
		this.c = 0;
		if( (bw.dom || bw.ie4) && this.css.clip ) {
			this.c = this.css.clip;
			this.c = this.c.slice(5, this.c.length - 1); 
			this.c = this.c.split(' ');
			for(var i = 0; i < 4; i++)
				this.c[i] = parseInt(this.c[i]);
		}
	
		// Clipping area top left, or offset from the border is more acurate.
		this.clip_tl = new Point(
			this.css.clip.top || this.c[0] || 0,
			this.css.clip.left || this.c[3] || 0);
		// Clipping area bottom right, or offset from the border is more acurate.
		this.clip_br = new Point(
			this.css.clip.right || this.c[1] || this.w || 0,
			this.css.clip.bottom || this.c[2] || this.h || 0);
	
		this.obj = obj + "Object"; 
		eval(this.obj + " = this;");
		
		return this;
	} else return null;
}

UniDom.prototype.getX = function () {
	return this.div.getX();
}

UniDom.prototype.getY = function () {
	return this.div.getY();
}

// If the top most left point is equal to the given point object
UniDom.prototype.isEqualTo = function (p_point) {
	return (this.getX() == p_point.getX() && this.getY() == p_point.getY()); 
}

// If the top most left point is equal to the given point object
UniDom.prototype.setEqualTo = function (p_point) {
	this.css.left = p_point.x;
	this.css.top = p_point.y;

	return true;
}

// The distance in pixes of the top most left point to the given Point object.
UniDom.prototype.distanceTo = function (p_point) {
	with (Math) {
		return round(sqrt(pow((this.getX() - p_point.x), 2) + pow((this.getY() - p_point.y), 2)));
	}
}

// Moving object to **************
UniDom.prototype.moveIt = function(p_point) {
	this.css.left = p_point.x;
	this.css.top = p_point.y;
	this.div.moveTo(p_point);	// Update internal reference.
	return true;
}

// Moving object by ***************
UniDom.prototype.moveBy = function(p_x, p_y) {
	this.css.left = this.div.getX() + p_x;
	this.css.top = this.div.getY() + p_y;
	this.div.moveBy(p_x, p_y);	// Update internal reference.
	return true;
}

//Writing content to object ***
UniDom.prototype.writeIt = function(text, startHTML, endHTML) {
	if(bw.ns4) {
		if(!startHTML) {
			startHTML = ""; 
			endHTML = "";
		}
		this.ref.open("text/html"); 
		this.ref.write(startHTML + text + endHTML); 
		this.ref.close();
	} else 
		this.evnt.innerHTML = text;
}

//Showing object ************
UniDom.prototype.showIt = function(){this.css.visibility = "visible";}

//Hiding object **********
UniDom.prototype.hideIt = function(){this.css.visibility = "hidden";}
