// dhtml library by rod morelos (rod@dhtmlcentral.com) www20.brinkster.com/viewsrc 
// keep these two lines and you're free to use this code

// last updated: 02.18.2003

// note: "web standards" compliant browsers only!
if (!document.getElementById) window.location.href = 'http://www.webstandards.org/upgrade/';

// typesavers
var d = document;

// utilities

// standards - all pixels please
function px(n)
{
	if (isNaN(n)) return n;
	else return Math.round(n) + 'px';
}

// random number generator
function rand(minn,maxn)
{
	return (Math.round(Math.random() * (maxn-minn) ) + minn);
}

/*
this allows you to test if a value is truly undefined, as opposed to false or null.
according to the JS 1.5 spec, null and undefined are considered equal). in most cases, the oft-used conditional if (!value) will return the same result for all three, which is not a clear indication if a value is undefined or not.
*/
function isnull(value)
{
	var u;
	return value == u;
} 

// browser object; browser sniffer
function browserobject()
{
	this.ver = navigator.appVersion.toLowerCase();
	this.agent = navigator.userAgent.toLowerCase();
	this.dom = d.getElementById ? 1 : 0;
	this.op7 = (this.dom && this.agent.indexOf('opera 7') > -1 || this.agent.indexOf('opera/7') > -1) ? 1 : 0;
	this.ie5 = (this.dom && this.agent.indexOf('msie 5') > -1) ? 1 : 0;
	this.ie5mac = (this.dom && this.ie5 && this.agent.indexOf('mac') > -1) ? 1 : 0;
	this.ie6 = (this.dom && this.agent.indexOf('msie 6') > -1 ) ? 1 : 0;
	this.moz = (this.dom && this.agent.indexOf('mozilla') > -1 && this.agent.indexOf('gecko') > -1  ) ? 1 : 0;
	this.ie = (this.ie5 || this.ie6 || this.ie5mac);
	this.bw = (this.ie || this.moz || this.op7);
	return this;
}
var bw = new browserobject();

// page object; document/page functions
function pageobject()
{
	this.mousex = 0;
	this.mousey = 0;
	this.layerx = 0;
	this.layery = 0; 
	return this;
}
var opage = new pageobject();

// get page scroll left
pageobject.prototype.scrollx = function()
{
	var sx = 0;
	if (d.documentElement && d.documentElement.scrollLeft) sx = d.documentElement.scrollLeft;
	else if (d.body && d.body.scrollLeft) sx = d.body.scrollLeft;
	else if (window.scrollX) sx = window.scrollX;
	return sx;
};

// get page scroll top
pageobject.prototype.scrolly = function()
{
	var sy = 0;
	if (d.documentElement && d.documentElement.scrollTop) sy = d.documentElement.scrollTop;
	else if (d.body && d.body.scrollTop) sy = d.body.scrollTop;
	else if (window.scrollY) sy = window.scrollY;
	return sy;
};

// get page width
pageobject.prototype.width = function()
{
	var w = 0;
	if (d.documentElement && d.documentElement.clientWidth) w = d.documentElement.clientWidth;
	else if (d.body && d.body.clientWidth) w = d.body.clientWidth;
	else if (window.innerWidth) w = window.innerWidth;
	return w + this.scrollx();
};

// get page height
pageobject.prototype.height = function()
{
	var h = 0;
	if (d.documentElement && d.documentElement.clientHeight) h = d.documentElement.clientHeight;
	else if (d.body && d.body.clientHeight) h = d.body.clientHeight;
	else if (window.innerHeight) h = window.innerHeight;
	return h + this.scrolly();
};

// get mouse coordinates
pageobject.prototype.mouse = function(e)
{
	if (isnull(e)) e = window.event;
	if (!isnull(e.offsetX)) this.layerx = e.offsetX;
	else this.layerx = e.layerX;
	if (!isnull(e.offsetY)) this.layery = e.offsetY;
	else this.layery = e.layerY;
	if (!isnull(e.clientX)) this.mousex = e.clientX + this.scrollx();
	if (!isnull(e.clientY)) this.mousey = e.clientY + this.scrolly();
};

// prevents default action (such as text selection)
pageobject.prototype.preventdefault = function(e)
{
	if (window.event) window.event.returnValue = false;
	else e.preventDefault();
};

// cancels event bubbling to parent elements
pageobject.prototype.cancelbubble = function(e)
{
	if (window.event) window.event.cancelBubble = true;
	else e.stopPropagation();
};

// dhtml object; makes cross-browser object
function dhtmlobject(id)
{
	this.el = d.getElementById ? d.getElementById(id) : null;
	if (isnull(this.el)) { alert(id); return false; }
	this.id = id;
	this.obj = id + 'object';
	eval(this.obj + ' = this');
	this.css = this.el.style;
	this.w = this.el.offsetWidth ? this.el.offsetWidth : 0;
	this.h = this.el.offsetHeight ? this.el.offsetHeight : 0;
	this.x = this.el.offsetLeft ? this.el.offsetLeft : 0;
	this.y = this.el.offsetTop ? this.el.offsetTop : 0;
	this.z = this.css.zIndex ? this.css.zIndex : 0;
	this.o = 100;
	this.fadetimer = 0;
	return this;
}

// update function; update width, height, left and top values
dhtmlobject.prototype.update = function()
{
	this.w = this.el.offsetWidth ? this.el.offsetWidth : parseInt(this.css.width) ? parseInt(this.css.width) : 0;
	this.h = this.el.offsetHeight ? this.el.offsetHeight : parseInt(this.css.height) ? parseInt(this.css.height) : 0;
};

// move layer to
dhtmlobject.prototype.moveto = function(x,y,z)
{
	if (!isnull(x))
	{
		this.x = x;
		this.css.left = px(this.x);
	}
	if (!isnull(y))
	{
		this.y = y;
		this.css.top = px(this.y);
	}
	if (!isnull(z))
	{
		this.z = z;
		this.css.zIndex = z;
	};
};

// move layer by
dhtmlobject.prototype.moveby = function(x,y,z)
{
	this.moveto(this.x + x, this.y + y, this.z + z);
};

// set layer visibility property: 0 = hidden; 1 = visible
dhtmlobject.prototype.visibility = function(v)
{
	this.css.visibility = v ? 'visible' : 'hidden';
};


// set layer opacity
dhtmlobject.prototype.setopacity = function(o)
{
	if (!isnull(this.css.MozOpacity)) this.css.MozOpacity = (o / 100);
	else if (this.el.filters) this.css.filter = 'alpha(opacity='+o+')';
	this.o = o;
};

// get layer opacity
dhtmlobject.prototype.getopacity = function()
{
	if (!isnull(this.css.MozOpacity)) return (this.css.MozOpacity * 100);
	else if (this.el.filters && this.el.filters.alpha) return this.el.filters.alpha.opacity;
	else return this.o;
};

// set size (width and height)
dhtmlobject.prototype.setsize = function(w,h)
{
	if (!isnull(w))
	{
		this.w = w;
		this.css.width = px(w);
	}
	if (!isnull(h))
	{
		this.h = h;
		this.css.height = px(h);
	}
};

// clip layer to
dhtmlobject.prototype.clipto = function(t,r,b,l,s)
{
	this.css.clip = 'rect('+px(t)+','+px(r)+','+px(b)+','+px(l)+')';
	if (!isnull(s)) this.setsize(r,b);
};

// set background color
dhtmlobject.prototype.bg = function(color)
{
	this.css.backgroundColor = color;
}

// write HTML to layer and update width and height
dhtmlobject.prototype.writehtml = function(text)
{
	if (!isnull(this.el.innerHTML))
	{
		this.el.innerHTML = text;
		this.update();
	}
};

// deletes any content/child node in the element
dhtmlobject.prototype.deletecontent = function()
{
	while (this.el.hasChildNodes()) this.el.removeChild(this.el.firstChild);
};

// write TEXT to layer (text only, no HTML codes)
dhtmlobject.prototype.writetext = function(text,addtext)
{
	if (!addtext) this.deletecontent(); // remove any content
	this.el.appendChild(d.createTextNode(text)); // write text
	this.update(); // update width and height
};

/*
align layer relative to the page (use your keyboard's numeric keypad as reference)
7-8-9 = 7 is top left, 8 is top center and 9 is top right
4-5-6 = 4 is left center, 5 is center of page and 6 is right center
1-2-3 = 1 is bottom left, 2 is bottom center and 3 is bottom right
*/
dhtmlobject.prototype.alignto = function(num)
{
	if (num == 1) this.moveto(0, opage.height() - this.h);
	if (num == 2) this.moveto(opage.width() / 2 - this.w / 2, opage.height() - this.h);
	if (num == 3) this.moveto(opage.width() - this.w, opage.height() - this.h);
	if (num == 4) this.moveto(0, opage.height() / 2 - this.h / 2);
	if (num == 5) this.moveto(opage.width() / 2 - this.w / 2, opage.height() / 2-this.h / 2);
	if (num == 6) this.moveto(opage.width() - this.w, opage.height() / 2 - this.h / 2);
	if (num == 7) this.moveto(0,0);
	if (num == 8) this.moveto(opage.width() / 2 - this.w / 2, 0);
	if (num == 9) this.moveto(opage.width() - this.w, 0);
};

// fade animation functions
dhtmlobject.prototype.fadeto = function(target,step,time)
{
	clearTimeout(this.fadetimer);
	this.fade(target,step,time);
};


dhtmlobject.prototype.fade = function(target,step,time)
{
	if (target > this.o) this.setopacity(this.o + step);
	else this.setopacity(this.o - step);
	if (Math.abs(target - this.o) > step)
	{
		this.fadetimer = setTimeout(this.obj + '.fade('+target+','+step+','+time+')',time);
	}
	else
	{
		this.setopacity(target);
		clearTimeout(this.fadetimer);
	}
};

// checks if the mouse pointer is over the element
dhtmlobject.prototype.onmouseover = function(e)
{
	opage.mouse(e);
	var mx = opage.mousex;
	var my = opage.mousey;
	this.update();
	var el = this.el;
	while (el.parentNode.nodeName.toLowerCase() != 'body')
	{
		el = el.parentNode;
		mx = mx - el.offsetLeft;
		my = my - el.offsetTop;
	}
	if (mx >= this.x && mx <= this.x + this.w && my >= this.y && my <= this.y + this.h) return true;
	else return false;	
};

// checks if the mouse pointer is over the element
dhtmlobject.prototype.ismouseover = function(e)
{
	var el = window.event ? event.srcElement : e.target;
	if (el.nodeType == 3 || el.tagName.toLowerCase() == 'img') el = el.parentNode;
	if (el.id == this.id) return true;
	else return false;
};

// attach event to an element
function addevent(el,event,fn,capture)
{
	if (el.addEventListener)
	{
		el.addEventListener(event,fn,capture);
		return true;
	}
	else if (el.attachEvent)
	{
		var ae = el.attachEvent('on'+event,fn);
		return ae;
	}
	else
	{
		alert('handler could not be attached');
	}
} 

// detach event from an element
function removeevent(el,event,fn,capture)
{
	if (el.removeEventListener)
	{
		el.removeEventListener(event,fn,capture);
		return true;
	}
	else if (el.detachEvent)
	{
		var re = el.detachEvent('on'+event,fn);
		return re;
	}
	else
	{
		alert('handler could not be removed');
	}
}