/* This notice must be untouched at all times.

layer.js    v.1.0

The latest version is available at
http://www.molooga.de

Created 08.06.2006 by H.Matthias (Web: http://www.molooga.de )
Last modified: 23.06.2006

JavaScript cross browser layer library.
Provides methods to
- create, move layers
- hide and show layers
- get and set position
- get and set background color or image

This program is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License
at http://www.gnu.org/copyleft/gpl.html for more details.
*/

var ns4 = (document.layers) ? true:false;
var ie4 = (document.all) ? true:false;
var ng5 = (document.getElementById) ? true:false;

function myLayer(name, cnt){
	this.layerName=name;
	this.posX=0;
	this.posY=0;
			
	this.name = function(){
		return this.layerName;
	};
		
	this.home = function(){
		this.hide();
		this.moveTo(0,0);	
	}	
	
	this.setClass = function(className){
		this.layerObject.className = className;
	}	
	
	this.show = function(){
		if (ng5) this.layerObject.style.visibility = "visible";
		else if (ns4) this.layerObject.visibility = "show";
		else if (ie4) this.layerObject.style.visibility ="visible";
	};
	
	this.hide = function(){
		if (ng5) this.layerObject.style.visibility = "hidden";
		else if (ns4) this.layerObject.visibility = "hide";
		else if (ie4) this.layerObject.style.visibility ="hidden";
	};
	
	/*
	this.getWidth = function(){
		return Number(this.layerObject.firstChild.width);
	};
	
	this.getHeight = function(){
		return Number(this.layerObject.firstChild.height);
	};
	
	this.setWidth = function(width){
		this.layerObject.firstChild.width=width;
	};	
	
	this.setHeight = function(Height){
		this.layerObject.firstChild.height=Height;
	};
	*/
	
	this.getX = function(){
		return this.posX;
	};	
	
	this.getY = function(){
		return this.posY;
	};
	
	this.setX = function(xpos){
		this.moveTo(xpos,this.posY);
	};	
	
	this.setY = function(ypos){
		this.moveTo(this.posX,ypos);
	};
	
	this.getZIndex =function(){
		if(ns4) return this.layerObject.zIndex;
			else return this.layerObject.style.zIndex;
	}
	
	this.setZIndex =function(z){
		if(ns4) this.layerObject.zIndex=z;
			else this.layerObject.style.zIndex=z;
	}
	
	this.moveTo = function(x,y){
		if(ns4) this.layerObject.moveTo(x,y);
		else{
			this.layerObject.style.left=x;
			this.layerObject.style.top=y;
		}
		this.posX=x;
		this.posY=y;
	}
	
	
	this.moveBy =function(x,y){
		this.moveTo(this.posX+x,this.posY+y);
	}
		
	this.setContent = function(cnt){
		this.hide();
		if(this.layerObject.childNodes.length>0){
			this.layerObject.replaceChild(cnt,this.layerObject.firstChild);
		}else{
			this.layerObject.appendChild(cnt);
		}
		/*
		//while(ni.complete!=true);
		this.layerWidth=ni.width;
		this.layerHeight=ni.height;
		*/
		this.show();
	};
	
	this.getContent = function(){
		return this.layerObject.firstChild;
	};
	
	/*
	this.zoom = function(zoom){
		this.setWidth(Math.floor(this.layerWidth*zoom+0.5));
		this.setHeight(Math.floor(this.layerHeight*zoom+0.5));
	};
	
	this.collision = function(layer){
		var x1=this.getX()
		var y1=this.getY()
		var w1=this.getWidth()
		var h1=this.getHeight()
		var x2=layer.getX()
		var y2=layer.getY()
		var w2=layer.getWidth()
		var h2=layer.getHeight()
		
		if((x1>=x2)&&(x1<(x2+w2))&&(y1>=y2)&&(y1<(y2+h2))) return true;
		if((x2>=x1)&&(x2<(x1+w1))&&(y2>=y1)&&(y2<(y1+h1))) return true;
		if((x2>=x1)&&(x2<(x1+w1))&&((y2+h2)>y1)&&(y2<=y1)) return true;
		if((x1>=x2)&&(x1<(x2+w2))&&((y1+h1)>y2)&&(y1<=y2)) return true;
		
		return false;
	};		
	*/
	
	this.left = function(){
		this.setX(0);
	}
	
	this.right = function(){
		this.setX(getWindowWidth()-this.getWidth());
	}
	
	this.center = function(){
		this.setX((getWindowWidth()-this.getWidth())/2);
	}
		
	this.top = function(){
		this.setY(0);
	}
	
	this.bottom = function(){
		this.setY(getWindowHeight()-this.getHeight());
	}
	
	this.middle = function(){
		this.setY((getWindowHeight()-this.getHeight())/2);
	}
		
	
	t=(ns4)?'<layer name="'+this.layerName+'">':'<div id="'+this.layerName+'" style="position:absolute; visibility:visible;">';
	t+=cnt;
	t+=(ns4)?'</layer>':'</div>';
	document.writeln(t);
	this.layerObject=document.getElementById(this.layerName);
	//this.setContent(cnt);
}		
