/********************************************************************************

	Detection Variables

********************************************************************************/
var isNS4 = false;
var isNS5 = false;
var isNS6 = false;
var isIE4 = false;
var isIE5 = false;
var isIE6 = false;
var isMac = false;
var isWin = false;

var appName = navigator.appName;
var version = navigator.appVersion;
var userAgent = navigator.userAgent.toLowerCase()

var isNS = ( appName == "Netscape" );
isNS4 = isNS && version.indexOf("4.")!=-1;
isNS5 = isNS && version.indexOf("5.")==0;
isNS6 = ( isNS5 || (this.ns && version.indexOf("6.")!=-1) );

var isIE = ( appName == "Microsoft Internet Explorer" );
isIE4 = isIE && version.indexOf("MSIE 4.")!=-1;
isIE5 = isIE && version.indexOf("MSIE 5.")!=-1;
isIE6 = isIE && version.indexOf("MSIE 6.")!=-1;

isMac = ( userAgent.indexOf( "mac" ) != -1 )
isWin = ( userAgent.indexOf( "windows" ) != -1 )

/********************************************************************************

	Thing Object


	Initialization
		Thing.init()                  -- looks through document & gets all divs ending in "Div"
	                                     adds that to the Thing.all array without the "Div"

	Thing.setGlobals()                 -- create global variables with the same name as thing.name
	
	Member Variables
		myThing.name                   -- "myThing"
		myThing.id                     -- "myThingDiv"
		myThing.parentDoc              -- the parent document 
		myThing.div                    -- the actual html object
		myThing.style                  -- the style of the object
		myThing.position               -- a Point with the thing's position
		myThing.size                   -- a Point with the thing's size

********************************************************************************/
Thing.all = new Array();
function Thing( name, parentDoc ){
	this.name = name
	this.id = name + "Div";
	this.parentDoc = parentDoc;
	this.div = ( isNS4 ) ? parentDoc.layers[this.id] : document.all[this.id];
	this.style = ( isNS4 ) ? this.div : this.div.style;
	this.position = this.getPosition();
	this.size = this.getSize();
	if( ! Thing.isLoaded ) Thing.init();
}


Thing.init = function( doc ){
	this.isLoaded = true;
	if (isNS4){
		if( doc == null ) 
			doc = document;
		for( var id in doc.layers ){
			var name = id.slice(0, -3);
			if( id == name + "Div" ){
				Thing.all[name] = new Thing( name, doc );
				Thing.init( doc.layers[id].document );
			}
		}
		return true;
	}
	else if(isNS5){
		document.all = document.getElementsByTagName("*");
	}
	if( document.all ){
		if (isIE4) {
			var allD = document.all.tags("DIV");
		}
		else {
			var allD = document.getElementsByTagName("DIV");
		}
		for( var i=0; i<allD.length; i++ ){
			var id = allD[i].id;
			var name = id.slice(0, -3);
			if( id == name + "Div" ){
				Thing.all[name] = new Thing( name );
			}
		}
		return true;
	}
	return false;
}
Thing.setGlobals = function(){
	for(name in Thing.all)
		eval( name + " = Thing.all." + name );
}

/********************************************************************************

	Whats the Point Constructor
		p = new Point(x,y)
		
	Member Variables
		p.x                            -- a number
		p.y                            -- another number
	Member Functions
		r = p.add( q )                 -- add the coordinates of p & q to get r
		r = p.sub( q )                 -- subtract the coordinates of q from p to get r
		r = p.mult( m )                -- scale a point by a number m
	
		p.toString()                   -- returns a string that looks like "(x,y)", so
		alert( p )                     -- will alert "(x,y)"

********************************************************************************/
function Point(x,y){
	this.x = x;
	this.y = y;
}
Point.prototype.add = function( that ){
	return new Point( this.x + that.x, this.y + that.y );
}
Point.prototype.sub = function( that ){
	return new Point( this.x - that.x, this.y - that.y );
}
Point.prototype.mult = function( mult ){
	return new Point( mult * this.x, mult * this.y);
}
Point.prototype.perp = function(){
	return new Point( - this.y, this.x )
}
Point.prototype.distTo = function( that ){
	var dx = this.x - that.x;
	var dy = this.y - that.y;
	if( dx == 0 ) return Math.abs(dy);
	if( dy == 0 ) return Math.abs(dx);
	return Math.sqrt( dx*dx + dy*dy );
}
Point.prototype.size = function(){
	var p = new Point(0,0);
	return this.distTo( p )
}
Point.prototype.toString = function(){
	return '(' + this.x + ', ' + this.y + ')';
}
Point.isPoint = function(what){
	if( typeof(what) != "object" ) return false;
	if( what.constructor == Point ) return true;
	return false;
}
Point.prototype.isInRect = function(p1, p2){
	return ( (this.x > p1.x) && (this.x < p2.x) && (this.y > p1.y) && (this.y < p2.y) );
}
/********************************************************************************

	Screen Information
	
	Point          Thing.screenSize()       -- returns a the width and height of the screen
	Point          Thing.scrollSize()       -- returns a the horiz and vert scroll 

	Point          Point.wrtPage()          -- converts screen coordinates to page coordinates
	
********************************************************************************/

if( isNS4 || isNS5 )
	Thing.screenSize = function(){
		return new Point( window.innerWidth, window.innerHeight );
	}
else
	Thing.screenSize = function(){
		return new Point( document.body.clientWidth, document.body.clientHeight );
	}
	

if( isNS4 || isNS5 )
	Thing.scrollSize = function(){
		return new Point( window.pageXOffset, window.pageYOffset );	
	}
else
	Thing.scrollSize = function(){
		return new Point( document.body.scrollLeft, document.body.scrollTop );
	}
	
Point.prototype.wrtPage = function(){
		return this.add( Thing.scrollSize() )
}

/********************************************************************************

	Style Initialization

********************************************************************************/
Thing.prototype.getPosition = function(){
	if( isNS4 ){
		return new Point( 
			parseInt(this.style.left), 
			parseInt(this.style.top) 
		);
	}
	else {
		return new Point(
			parseInt(this.div.offsetLeft),
			parseInt(this.div.offsetTop)
		);
	}
	return null;
}
Thing.prototype.getSize = function(){
	if (this.size) {
		if (isNS4) {
			this.size.x = parseInt(this.div.clip.width);
			this.size.y = parseInt(this.div.clip.height);
		} else {
			this.size.x = parseInt(this.div.offsetWidth);
			this.size.y = parseInt(this.div.offsetHeight);
		}
	} else {
		if (isNS4) {
			return new Point(
				parseInt(this.div.clip.width),
				parseInt(this.div.clip.height)
			);
		} else {
			return new Point(
				parseInt(this.div.offsetWidth),
				parseInt(this.div.offsetHeight)
			);
		}
	}
	return null;
}

/********************************************************************************

	Position Manipulation
	
		myThing.setPosition()          -- updates the html object to match the position
		                                  called by moveTo, moveBy, etc
		myThing.moveTo(p)              -- move to point
		myThing.moveTo(x,y)            -- move to specified location
		myThing.moveBy(p)              -- move by point values
		myThing.moveBy(x,y)            -- move by specified increments

********************************************************************************/
Thing.prototype.setPosition = function(){
	this.style.left = this.position.x;
	this.style.top = this.position.y;
}
Thing.prototype.moveTo = function(x,y){
	if( Point.isPoint(x) ){
		this.position = x;
	}
	else{
		if( x ) this.position.x = x;
		if( y ) this.position.y = y;
	}
	this.setPosition();
}
Thing.prototype.moveBy = function(dx,dy){
	if( Point.isPoint(dx) ){
		this.position = this.position.add(dx);
	}
	else{
		if( dx ) this.position.x += dx;
		if( dy ) this.position.y += dy;
	}
	this.setPosition();
}

/********************************************************************************

	Visibility Manipulation
	
		myThing.show()                 -- show the div
		myThing.hide()                 -- hide the div
		myThing.isVisible()            -- return true if visible, false if not
		myThing.toggle()               -- hide if visible, show if not
		myThing.write()				   -- write some new html to div
		
********************************************************************************/
Thing.prototype.show = function(){
	this.style.visibility = "visible";
}
Thing.prototype.hide = function(){
	this.style.visibility = "hidden";
}
Thing.prototype.isVisible = function(){
	return ( this.style.visibility.indexOf("d") == -1 );
}
Thing.prototype.toggle = function(){
	if( this.isVisible() ) return this.hide();
	return this.show();
}
Thing.prototype.write = function(meat){
	if (isNS4) {
		tableOpen = '<table border=0 cellpadding=0 cellspacing=0 width='+ this.size.x +'><tr valign="top"><td>';
		tableClose = '</td></tr></table>';
		this.div.document.open();
		this.div.document.write(meat);
		this.div.document.close();
	} else {
		this.div.innerHTML = meat;
	}
}
Thing.prototype.setBackground = function(color){
	if( isNS4 ) this.div.document.bgColor=color;
	else this.style.backgroundColor = color;
}

/********************************************************************************

	Screen Information
	
	Thing.screenSize() returns a Point for the width and height of the screen
	Thing.scrollSize() returns a Point for the horiz and vert scroll 

********************************************************************************/
Thing.screenSize = function(){
	if( isNS4 || isNS5 ) return new Point( window.innerWidth, window.innerHeight );
	else return new Point( document.body.clientWidth, document.body.clientHeight );
}
Thing.scrollSize = function(){
	if( isNS4 || isNS5 ) return new Point( window.pageXOffset, window.pageYOffset );	
	else return new Point( document.body.scrollLeft, document.body.scrollTop );
}

/*********************************************************************************

	Clip Methods
	
*********************************************************************************/
Thing.prototype.clipTo = function(top, right, bottom, left){
	if( isNS4 ){
		this.style.clip.top = top; 
		this.style.clip.right = right; 
		this.style.clip.bottom = bottom; 
		this.style.clip.left = left; 
	}
	else{
		this.style.clip = "rect(" + top + ", " +  right + ", " + bottom + ", " + left + ")";
	}
}

/*********************************************************************************

	Get Kids Method
	
*********************************************************************************/
Thing.prototype.getKids = function() {
	this.kids = new Array();
	if (isNS4){
		i = 0;
		for( var id in this.div.document.layers ){
			var name = id.slice(0, -3);
			if( id == name + "Div" ){
				eval( "name = Thing.all." + name );
				this.kids[i] = name;
				i++;
			}
		}
	} else {
		allKids = (isIE4) ? this.div.all.tags("DIV") : this.div.getElementsByTagName("DIV");
		for (i=0; i<allKids.length; i++) {
			name = allKids[i].id.slice(0, -3);
			eval( "name = Thing.all." + name );
			this.kids[i] = name;
		}
	}
	return this.kids;
}

/********************************************************************************

	Resize fix for netscape

********************************************************************************/
function resize(){
		if ( scrW != window.innerWidth || scrH != window.innerHeight )
			location.reload()
}
if ( isNS4 ){
	scrW = window.innerWidth;
	scrH = window.innerHeight;
	onresize=resize;
}