/* This notice must be untouched at all times.

sprite.js    v.1.0

The latest version is available at
http://www.molooga.de

Created 06.06.2006 by H.Matthias (Web: http://www.molooga.de )
Last modified: 08.06.2006

JavaScript cross browser sprite library.
Provides methods to
- create sprites
- get and set position
- get and set size
- change sprite images
- check sprites collision 

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 sprite(name,img){
	this.spriteName=name;
	this.posX=0;
	this.posY=0;
			
	this.name = function(){
		return this.spriteName;
	};
		
	this.home = function(){
		this.hide();
		this.moveTo(0,0);	
	}	
	
	this.show = function(){
		if (ng5) this.spriteObject.style.visibility = "visible";
		else if (ns4) this.spriteObject.visibility = "show";
		else if (ie4) this.spriteObject.style.visibility ="visible";
	};
	
	this.hide = function(){
		if (ng5) this.spriteObject.style.visibility = "hidden";
		else if (ns4) this.spriteObject.visibility = "hide";
		else if (ie4) this.spriteObject.style.visibility ="hidden";
	};
	
	this.getWidth = function(){
		return Number(this.spriteObject.firstChild.width);
	};
	
	this.getHeight = function(){
		return Number(this.spriteObject.firstChild.height);
	};
	
	this.setWidth = function(width){
		this.spriteObject.firstChild.width=width;
	};	
	
	this.setHeight = function(Height){
		this.spriteObject.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.spriteObject.zIndex;
			else return this.spriteObject.style.zIndex;
	}
	
	this.setZIndex =function(z){
		if(ns4) this.spriteObject.zIndex=z;
			else this.spriteObject.style.zIndex=z;
	}
	
	this.moveTo = function(x,y){
		if(ns4) this.spriteObject.moveTo(x,y);
		else{
			this.spriteObject.style.left=x;
			this.spriteObject.style.top=y;
		}
		this.posX=x;
		this.posY=y;
	}
	
	
	this.moveBy =function(x,y){
		this.moveTo(this.posX+x,this.posY+y);
	}
		
	this.setImage = function(img){
		this.hide();
		var ni=document.createElement("img");
		ni.src=img;
		if(this.spriteObject.childNodes.length>0){
			this.spriteObject.replaceChild(ni,this.spriteObject.firstChild);
		}else{
			this.spriteObject.appendChild(ni);
		}
		//while(ni.complete!=true);
		this.spriteWidth=ni.width;
		this.spriteHeight=ni.height;
		this.show();
	};
	
	this.getImage = function(){
		return this.spriteObject.firstChild.src;
	};
	
	this.zoom = function(zoom){
		this.setWidth(Math.floor(this.spriteWidth*zoom+0.5));
		this.setHeight(Math.floor(this.spriteHeight*zoom+0.5));
	};
	
	this.collision = function(sprite){
		var x1=this.getX()
		var y1=this.getY()
		var w1=this.getWidth()
		var h1=this.getHeight()
		var x2=sprite.getX()
		var y2=sprite.getY()
		var w2=sprite.getWidth()
		var h2=sprite.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);
	}
		
	this.transparency = function(v){
		v=(typeof(v)=="undefined")?50:100-v;
 		fValue="Alpha(opacity="+v+")";
 		oValue=""+v/100;
  	this.spriteObject.style.filter=fValue;
  	this.spriteObject.style.MozOpacity=oValue;
  	this.spriteObject.style.KhtmlOpacity=oValue;
  	this.spriteObject.style.opacity=oValue;
	}	
	
	t=(ns4)?'<layer name="'+this.spriteName+'">':'<div id="'+this.spriteName+'" style="position:absolute; visibility:visible;">';
	t+=(ns4)?'</layer>':'</div>';
	document.writeln(t);
	this.spriteObject=document.getElementById(this.spriteName);
	this.setImage(img);
}		
