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

// last updated: 03.15.2003

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

// typesavers
var d = document;

// 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();

// dhtml object; object constructor
function dhtmlobject(id)
{
	this.el = d.getElementById ? d.getElementById(id) : null;
	if (!this.el) return alert(id);
	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 = this.slidetimer = 0;
	this.fading = this.sliding = false;
	return this;
}

// update object 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;
	this.x = this.el.offsetLeft ? this.el.offsetLeft : parseInt(this.css.left) ? parseInt(this.css.left) : 0;
	this.y = this.el.offsetTop ? this.el.offsetTop : parseInt(this.css.top) ? parseInt(this.css.top) : 0;
}

// move layer to
dhtmlobject.prototype.moveto = function(x, y, z)
{
	if (x != null)
	{
		this.x = x;
		this.css.left = px(x);
	}
	if (y != null)
	{
		this.y = y;
		this.css.top = px(y);
	}
	if (z != null)
	{
		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 (typeof this.css.MozOpacity != 'undefined') this.css.MozOpacity = (o / 100);
	else if (this.el.filters) this.css.filter = 'alpha(opacity = ' + o + ')';
	this.o = o;
}

// set layer width
dhtmlobject.prototype.setwidth = function(w)
{
	if (w != null)
	{
		this.w = w;
		this.css.width = px(w);
	}
}

// set layer height
dhtmlobject.prototype.setheight = function(h)
{
	if (h != null)
	{
		this.h = h;
		this.css.height = px(h);
	}
}

// set layer size (width and height)
dhtmlobject.prototype.setsize = function(w, h)
{
	this.setwidth(w);
	this.setheight(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 (s != null) this.setsize(r, b);
}

// write HTML to layer and update width and height
dhtmlobject.prototype.writehtml = function(text)
{
	if (typeof this.el.innerHTML != 'undefined')
	{
		this.el.innerHTML = text;
		this.update();
	}
	else if (d.createRange)
	{
		var r = d.createRange();
		r.selectNodeContents(this.el);
		r.deleteContents();
		var f = r.createContextualFragment(text);
		this.el.appendChild(f);
		this.update();
	}
}

// write TEXT to layer (text only, no HTML codes)
dhtmlobject.prototype.writetext = function(text, addtext)
{
	if (!addtext) while (this.el.hasChildNodes()) this.el.removeChild(this.el.firstChild);
	this.el.appendChild(d.createTextNode(text));
	this.update();
}

/*
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)
{
	opage.getcanvas();
	if (num == 1) this.moveto(0, opage.h - this.h);
	if (num == 2) this.moveto(opage.w / 2 - this.w / 2, opage.h - this.h);
	if (num == 3) this.moveto(opage.w - this.w, opage.h - this.h);
	if (num == 4) this.moveto(0, opage.h / 2 - this.h / 2);
	if (num == 5) this.moveto(opage.w / 2 - this.w / 2, opage.h / 2 - this.h / 2);
	if (num == 6) this.moveto(opage.w - this.w, opage.h / 2 - this.h / 2);
	if (num == 7) this.moveto(0, 0);
	if (num == 8) this.moveto(opage.w / 2 - this.w / 2, 0);
	if (num == 9) this.moveto(opage.w - this.w, 0);
}

// measure the left position of an element, relative to the page
dhtmlobject.prototype.getpageleft = function(elm)
{
	var x = 0;
	var el = this.el;
	while (el.offsetParent && typeof el.offsetLeft != 'undefined')
	{
		x += el.offsetLeft;
		el = el.offsetParent;
	}
	return x;
}

// measure the top position of an element, relative to the page
dhtmlobject.prototype.getpagetop = function(elm)
{
	var y = 0;
	var el = this.el;
	while (el.offsetParent && typeof el.offsetTop != 'undefined')
	{
		y += el.offsetTop;
		el = el.offsetParent;
	}
	return y;
}

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

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

// slide animation
dhtmlobject.prototype.slideto = function(endx, endy, speed, fn)
{
	clearTimeout(this.slidetimer);
	if (!fn) fn = 0;
	this.slide(endx, endy, speed, fn);
}

dhtmlobject.prototype.slide = function(endx, endy, speed, fn)
{
	var x = endx - this.x;
	var y = endy - this.y;
	var m = Math.sqrt(x * x + y * y);
	var v = m / 10;
	speed++;
	if (v > speed) v = speed;
	else if (v < 1) v = 1;
	if (Math.abs(this.x - endx) < v && Math.abs(this.y - endy) < v)
	{
		this.sliding = false;
		clearTimeout(this.slidetimer);
		this.moveto(endx, endy);
		if (fn) eval(fn);
	}
	else
	{
		this.sliding = true;
		this.moveby(x / m * v, y / m * v);
		this.slidetimer = setTimeout(this.obj + '.slide(' + endx + ', ' + endy + ', ' + speed + ', "' + fn + '");', 10);
	}
}

// checks if the mouse pointer is over the element
dhtmlobject.prototype.onmouseover = function(e)
{
	events.init(e);
	var mx = events.mousex;
	var my = events.mousey;
	this.update();
	if (mx >= this.getpageleft() && mx <= this.getpageleft() + this.w && my >= this.getpagetop() && my <= this.getpagetop() + this.h) return true;
	else return false;	
}

// checks if the mouse pointer is over the element
dhtmlobject.prototype.ismouseover = function(e)
{
	events.init(e);
	if (events.target.id == this.id) return true;
	else return false;
}

// page object; document/page functions
function pageobject()
{
	this.scrollx = 0;
	this.scrolly = 0;
	this.width = 0;
	this.height = 0;
	this.w = 0;
	this.h = 0;
	return this;
}
var opage = new pageobject();

// get page width and height (the viewable area)
pageobject.prototype.getcanvas = function()
{
	// scroll left offset
	if (d.documentElement && d.documentElement.scrollLeft) this.scrollx = d.documentElement.scrollLeft;
	else if (d.body && d.body.scrollLeft) this.scrollx = d.body.scrollLeft;
	else if (window.scrollX) this.scrollx = window.scrollX;
	else this.scrollx = 0;
	// scroll top offset
	if (d.documentElement && d.documentElement.scrollTop) this.scrolly = d.documentElement.scrollTop;
	else if (d.body && d.body.scrollTop) this.scrolly = d.body.scrollTop;
	else if (window.scrollY) this.scrolly = window.scrollY;
	else this.scrolly = 0;
	// actual width
	if (d.documentElement && d.documentElement.clientWidth) this.width = d.documentElement.clientWidth;
	else if (d.body && d.body.clientWidth) this.width = d.body.clientWidth;
	else if (window.innerWidth) this.width = window.innerWidth;
	else this.width = 0;
	// actual height
	if (d.documentElement && d.documentElement.clientHeight) this.height = d.documentElement.clientHeight;
	else if (d.body && d.body.clientHeight) this.height = d.body.clientHeight;
	else if (window.innerHeight) this.height = window.innerHeight;
	else this.height = 0;
	// width + document scroll left offset
	this.w = this.width + this.scrollx;
	// height + document scroll top offset
	this.h = this.height + this.scrolly;
}

// event object;
function eventobject()
{
	this.mousex = 0;
	this.mousey = 0;
	this.layerx = 0;
	this.layery = 0;
	return this;
}
var events = new eventobject();

// fills the event object with the real event properties
eventobject.prototype.init = function(e)
{
	opage.getcanvas();
	if (window.event && !e) e = window.event;
	// page coordinates
	if (typeof e.clientX != 'undefined') this.mousex = e.clientX + opage.scrollx;
	if (typeof e.clientY != 'undefined') this.mousey = e.clientY + opage.scrolly;
	// element coordinates
	if (typeof e.offsetX != 'undefined') this.layerx = e.offsetX;
	else if (typeof e.layerX != 'undefined') this.layerx = e.layerX;
	if (typeof e.offsetY != 'undefined') this.layery = e.offsetY;
	else if (typeof e.layerY != 'undefined') this.layery = e.layerY;
	// others
	this.type = e.type ;
	this.target = window.event ? e.srcElement : e.target;
	if (this.target.nodeType == 3 || this.target.tagName.toLowerCase() == 'img') this.target = this.target.parentNode;
	return this;
}

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

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

// 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;
	}
}

// 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;
	}
}

// utilities; miscellaneous

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

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