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) );
}

var isNS4 = false;
var isNS5 = false;
var isIE4 = false;

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 (document.layers){
		isNS4 = true;
		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( !document.all && document.getElementsByTagName("*") ){
		isNS5 = true;
		document.all = document.getElementsByTagName("*");
	}
	if( document.all ){
		if (!document.getElementsByTagName) isIE4 = true;
		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 );
}

Thing.prototype.getPosition = function(){
	if( isNS4 ){
		return new Point( 
			parseInt(this.style.left), 
			parseInt(this.style.top) 
		);
	}
	else if ( isNS5 ){
		return new Point( 
			parseInt(this.div.offsetLeft) - parseInt(this.div.parentNode.offsetLeft),
			parseInt(this.div.offsetTop) - parseInt(this.div.parentNode.offsetTop)
		);
	}
	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;
}

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();
}

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) {
		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;
}

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 );
}

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 + ")";
	}
}

var IMAGE_OFF_SUFFIX = "_off.gif"
var IMAGE_ON_SUFFIX = "_on.gif"

var flip = new Array();
function Flip( imagesName, imagesSrc )
{
	this.docimages = getDocimages(imagesName);
	this.imagesOff = new Image( this.docimages.width, this.docimages.height )
	this.imagesOffsrc = this.imagesOff.src = this.docimages.src;
	
	this.imagesOn = new Image ( this.docimages.width, this.docimages.height )
	if ( imagesSrc )
		this.imagesOnsrc = this.imagesOn.src = imagesSrc
	else 
		this.imagesOnsrc = this.imagesOn.src = this.docimages.src.replace( IMAGE_OFF_SUFFIX, IMAGE_ON_SUFFIX )
}

Flip.prototype.on = function(){	
	this.docimages.src = this.imagesOn.src;
	if ( this.status )
		window.status = this.status;
	return true;
}

Flip.prototype.off = function(){
	this.docimages.src = this.imagesOff.src
	window.status = "";
	return true;
}

function getDocimages(name, d){
	d = ( d == null ) ? document : d; 
	var images = d.images[name];
	if (images) return images; 
	
	if ( ! document.layers ) return null; 
	
	for ( var i=0; i < d.layers.length; i++ ) 
		if ( d.layers[i].id ){
			images = getDocimages( name, d.layers[i].document ) 
				if (images) return images; 
		}
	return null; 
}

function getAllimages( d ){
	d = ( d == null ) ? document : d; 
	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 )  
}

function mouseover(name){
	if (flip[name])
		flip[name].on()
}

function mouseout(name){
	if (flip[name])
		flip[name].off()
}

function init(){

	Thing.init();
	Thing.setGlobals();
	getAllimages();
}

onload = init

