/********************************************************************************

	Detection Variables

********************************************************************************/
var isNS4 = false;
var isNS5 = 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;
}

/********************************************************************************

	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 );
}


/********************************************************************************

	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;
}
/********************************************************************************
 *
 *	imageFlip.js
 *	
 *	usage : 
 *		Change OFF_SUFFIX and ON_SUFFIX to match image names.
 *		
 *		For each image which is to be flipped, document.onload should call
 *			flip.myFlipName = new Flip( myImageName, myImageSource )
 *		To turn image on, call 
 *			flip.myFlipName.on()
 *		To turn image off, call
 *			flip.myFlipName.off()
 *
 ********************************************************************************/

var IMAGE_OFF_SUFFIX = "_off.gif"
var IMAGE_ON_SUFFIX = "_on.gif"

var flip = new Array();
function Flip( imgName, imgSrc )
{
	this.docImg = getDocImg(imgName);
	this.imgOff = new Image( this.docImg.width, this.docImg.height )
	this.imgOffsrc = this.imgOff.src = this.docImg.src;
	
	this.imgOn = new Image ( this.docImg.width, this.docImg.height )
	if ( imgSrc )
		this.imgOnsrc = this.imgOn.src = imgSrc
	else 
		this.imgOnsrc = this.imgOn.src = this.docImg.src.replace( IMAGE_OFF_SUFFIX, IMAGE_ON_SUFFIX )
}

Flip.prototype.on = function(){	
	this.docImg.src = this.imgOn.src;
	//if ( this.status )
	//	window.status = this.status;
	return true;
}

Flip.prototype.off = function(){
	this.docImg.src = this.imgOff.src
	//window.status = "";
	return true;
}

//  function getDocImg( name )
//  - searches recursively through document.layers for the named image
//  - returns the named element in document.images
function getDocImg(name, d){
	d = ( d == null ) ? document : d; //set d to be the document if empty
	var img = d.images[name];
	if (img) return img; //found it
	
	if ( ! document.layers ) return null; //in ie, we die here
	
	for ( var i=0; i < d.layers.length; i++ ) 
		if ( d.layers[i].id ){
			img = getDocImg( name, d.layers[i].document )  //recursive call
				if (img) return img; //found it
		}
	return null; //did not find it
}

// function getAllImages( d )
//  - searches through document for images
//  - recurses through layers
function getAllImages( d ){
	d = ( d == null ) ? document : d; //set d to be the document if empty
	for ( var i = 0; i < d.images.length; i++ ){
		var src = d.images[i].src
		if ( d.images[i].name ){
			if ( src.indexOf( IMAGE_OFF_SUFFIX ) != -1 ){
				flip[ d.images[i].name ] = new Flip( d.images[i].name )
			}
		}
	}
	if ( !document.layers ) return;
	for ( var i=0; i < d.layers.length; i++ ) 
		getAllImages( d.layers[i].document )  //recursive call
}

//  function mouseover(name)
//	- turn on flip[name] if it exists
function mouseover(name){
	if (flip[name])
		flip[name].on()
}

//  function mouseover(name)
//	- turn off flip[name] if it exists
function mouseout(name){
	if (flip[name])
		flip[name].off()
}