var Sp_blankurl='blankpix.gif';
var Sp_totalsprites=0;
var Sp_sprite=new Array();
var Sp_spritesonscreen=0;
Sp_xoffset=0;
Sp_yoffset=0;
Sp_cxoffset=0;
Sp_cyoffset=0;

function Sp_Sprite()
{
	this.on=false;
	this.state=0;
	this.x=0;
	this.y=-1000;
	this.cx=0;
	this.cy=0;
	this.z=G_Layer_index;
	this.xmin=0;
	this.xmax=100;
	this.ymin=0;
	this.ymax=100;
	this.bounces=false;
	this.frame=0;
	this.framewidth=0;
	this.frameheight=0;
	this.animframes=0;
	this.animpos=0;
	this.anims=0;
	this.animsstart=0;
	this.animsend=0;
	this.animdir="forward";
	this.animd=1;
	this.animspd=0;
	this.animtmr=0;
	this.animrepeat=-1;
	this.crepeat=0;
	this.width=0;
	this.height=0;
	this.owidth=0;
	this.oheight=0;
	this.xdir=0;
	this.ydir=0;
	this.xydegs=-1;
	this.speed=0;
	this.xspeed=0;
	this.yspeed=0;
	this.mouse=true;
	this.onmouseover="";
	this.onmouseout="";
	this.onclickdown="";
	this.onclickup="";
	this.draggable=false;
	this.dragnormal=true;
	this.dragvert=false;
	this.draghoriz=false;
	this.collides=false;
	this.hit=null;
	this.totalhits=0;
	this.hitarray=new Array();
	this.image="";
	this.isstatic=false;
	this.hard=false;
	this.alive=true;
	this.index=G_Layer_index;

	this.targetting=null;
	this.targettingx=0;
	this.targettingy=0;

	this.hitevents=false;
	this.hitevent=new Array();

	this.following=null;
	this.followingx=0;
	this.followingy=0;
	this.isfollowing=null;
	this.followedby=new Array();
	this.beingfollowed=false

	this.setZ=Sp_setz;
	this.setDir=Sp_setdir;
	this.hasHit=Sp_hashit;
	this.setXlimits=Sp_setxlimits;
	this.setYlimits=Sp_setylimits;
	this.setSpeed=Sp_setspeed;
	this.setXYdegs=Sp_setxydegs;
	this.switchOff=Sp_switchoff;
	this.makeStatic=Sp_makestatic;
	this.makeNormal=Sp_makenormal;
	this.dragType=Sp_dragtype;
	this.setAnimationSpeed=Sp_setanimspd;
	this.setAnimationLoop=Sp_setanimloop;
	this.setAnimationRepeat=Sp_setanimrepeat;
	this.check_collide=Sp_check_collide;
	this.useHitEvents=Sp_useHitEvents;
	this.setHitEvent=Sp_setHitEvent;
	this.follow=Sp_follow;
	this.stopFollowing=Sp_stopfollow;
	this.target=Sp_target;
	this.stopTargetting=Sp_stopTargetting;
	this.setCollides=Sp_setCollides;
	if(isIE4)
	{
		document.body.insertAdjacentHTML("BeforeEnd",'<img src="" name="Sp_i'+G_Layer_index+'" style="position: absolute; left:0; top:-1000">');
		this.ob=document.images["Sp_i"+G_Layer_index].style;
		this.moveto=Sp_movetoIE;
		this.moveTo=Sp_movetoIE;
		this.moveTo_l=Sp_movetoIE_l;
		this.setImage=Sp_setimageIE;
		this.setFrame=Sp_setframeIE;
		this.setAnimation=Sp_setanimIE;
		this.resize=Sp_resizeIE;
		this.switchOn=Sp_switchonIE;
	}
	else if (isNav4)
	{
		this.ob=document.layers[G_Layer_index]=new Layer(this.width);
		document["Sdiv"+G_Layer_index]=this.ob;
		this.ob.name=G_Layer_index;
		this.ob.height=this.height;
		this.ob.clip.height=this.height;
		this.ob.left=-1000;
		this.ob.top=0;
		this.ob.zIndex=this.z;
		this.ob.visibility="show";
		this.moveto=Sp_movetoNS;
		this.moveTo=Sp_movetoNS;
		this.moveTo_l=Sp_movetoNS_l;
		this.setImage=Sp_setimageNS;
		this.setFrame=Sp_setframeNS;
		this.setAnimation=Sp_setanimNS;
		this.resize=Sp_resizeNS;
		this.switchOn=Sp_switchonNS;
	}
	else if (isNav6)
	{
		this.raw = document.createElement('IMG');
		this.raw.id = "Sp_i"+G_Layer_index;
		this.raw.style.position = "absolute";
		this.raw.style.left = 0;
		this.raw.style.top = -1000;
		document.documentElement.appendChild(this.raw);
		this.raw = document.getElementById(this.raw.id);
		this.ob  = document.getElementById(this.raw.id).style;

		this.moveto=Sp_movetoN6;
		this.moveTo=Sp_movetoN6;
		this.moveTo_l=Sp_movetoN6_l;
		this.setImage=Sp_setimageIE;
		this.setFrame=Sp_setframeN6;
		this.setAnimation=Sp_setanimN6;
		this.resize=Sp_resizeIE;
		this.switchOn=Sp_switchonN6;
	}
	else Alert ('Neznámý Prohlížeč!');
	
	G_Layers[G_Layer_index]=this;
	G_Layer_index++;
	Sp_sprite[Sp_totalsprites]=this;
	Sp_totalsprites++;
	return this
}

function Sp_useHitEvents(Sp_a)
{
	this.hitevents=Sp_a
}

function Sp_setHitEvent(Sp_a,Sp_b)
{
	this.hitevent[Sp_a.index]=Sp_b
}

function Sp_follow(Sp_ob,Sp_x,Sp_y)
{
	if(!this.following)
	{
		this.followingx=Sp_x;
		this.followingy=Sp_y;
		if(Sp_ob.alive)
		{
			this.following=Sp_ob;
			this.draggable=false;
			this.targetting=null;
			Sp_ob.followedby[Sp_ob.followedby.length]=this;
			Sp_ob.beingfollowed=true
		}
		else alert("Error!\nLayer cannot follow object (not a sprite or mouse!). Check code!")
	}
}

function Sp_stopfollow()
{
	if(this.following)
	{
		var f=false;
		for(var n=0;n<this.following.followedby.length;n++)
		{
			if(this.following.followedby[n]==this)
			{
				f=true;
				break
			}
		}
		if(f)
		{
			for(var n2=n;n2<this.following.followedby.length-1;n2++)
				this.following.followedby[n2]=this.following.followedby[n2+1];
			this.following.followedby[this.following.followedby.length-1]=null;
			this.following.followedby.length--;
			if(this.following.followedby.length==0)
				this.following.beingfollowed=false
		}
		this.following=null
	}
}

function Sp_target(Sp_a,Sp_x,Sp_y)
{
	this.targetting=Sp_a;
	if(Sp_x)
		this.targettingx=Sp_x;
	if(Sp_y)
		this.targettingy=Sp_y
}

function Sp_stopTargetting(Sp_a)
{
	this.targetting=null;
	this.targettingx=0;
	this.targettingy=0
}

function Sp_setCollides(Sp_a)
{
	this.collides=Sp_a
}

function Sp_setz(Sp_z)
{
	this.z=Sp_z;
	this.ob.zIndex=Sp_z
}

function Sp_setdir(Sp_x,Sp_y)
{
	this.xdir=Sp_x;
	this.ydir=Sp_y;
	this.xspeed=this.xdir*this.speed;
	this.yspeed=this.ydir*this.speed
}

function Sp_setxlimits(x1,x2)
{
	this.xmin=x1;
	this.xmax=x2;
	if(this.x<this.xmin)
		this.x=this.xmin;
	else if(this.x+this.width>this.xmax)
		this.x=this.xmax
}

function Sp_setylimits(y1,y2)
{
	this.ymin=y1;
	this.ymax=y2;
	if(this.y<this.ymin)
		this.y=this.ymin;
	else if(this.y+this.height>this.ymax)
		this.y=this.ymax
}

function Sp_setframeIE(Sp_a)
{
	this.animpos=this.animsstart;
	this.animtmr=this.animspd;
	this.frame=Sp_a;
	this.ob.clip="rect("+(this.animpos*this.frameheight)+","+((Sp_a*this.framewidth)+this.framewidth)+","+((this.animpos+1)*this.frameheight)+","+(Sp_a*this.framewidth)+")";
	this.ob.posLeft=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.posTop=this.y+Sp_yoffset-(this.animpos*this.frameheight)
}

function Sp_setframeNS(a)
{
	this.animpos=this.animsstart;
	this.animtmr=this.animspd;
	this.frame=a;
	this.ob.clip.left=a*this.framewidth;
	this.ob.clip.right=(a*this.framewidth)+this.framewidth;
	this.ob.clip.top=this.animpos*this.frameheight;
	this.ob.clip.bottom=(this.animpos+1)*this.frameheight;
	this.ob.moveTo(this.x+Sp_xoffset-(this.frame*this.framewidth),this.y+Sp_yoffset-(this.animpos*this.frameheight))
}

function Sp_setframeN6(Sp_a)
{
	this.animpos=this.animsstart;
	this.animtmr=this.animspd;
	this.frame=Sp_a;
	this.ob.clip="rect("+(this.animpos*this.frameheight)+","+((Sp_a*this.framewidth)+this.framewidth)+","+((this.animpos+1)*this.frameheight)+","+(Sp_a*this.framewidth)+")";
	this.ob.left=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.top=this.y+Sp_yoffset-(this.animpos*this.frameheight)
}

function Sp_setanimIE(Sp_a)
{
	if(Sp_a>this.animsend)
		Sp_a=this.animsend;
	else if(Sp_a<this.animsstart)
		Sp_a=this.animsstart;
	this.animpos=Sp_a;
	this.ob.clip="rect("+(this.animpos*this.frameheight)+","+((this.frame*this.framewidth)+this.framewidth)+","+((this.animpos*this.frameheight)+this.frameheight)+","+(this.frame*this.framewidth)+")";
	this.ob.posLeft=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.posTop=this.y+Sp_yoffset-(this.animpos*this.frameheight)
}

function Sp_setanimN6(Sp_a)
{
	if(Sp_a>this.animsend)
		Sp_a=this.animsend;
	else if(Sp_a<this.animsstart)
		Sp_a=this.animsstart;
	this.animpos=Sp_a;
	this.ob.clip="rect("+(this.animpos*this.frameheight)+","+((this.frame*this.framewidth)+this.framewidth)+","+((this.animpos*this.frameheight)+this.frameheight)+","+(this.frame*this.framewidth)+")";
	this.ob.left=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.top=this.y+Sp_yoffset-(this.animpos*this.frameheight)
}

function Sp_setanimNS(Sp_a)
{
	if(Sp_a>this.animsend)
		Sp_a=this.animsend;
	else if(Sp_a<this.animsstart)
		Sp_a=this.animsstart;
	this.animpos=Sp_a;
	this.ob.clip.top=this.animpos*this.frameheight;
	this.ob.clip.left=this.frame*this.framewidth;
	this.ob.clip.right=(this.frame*this.framewidth)+this.framewidth;
	this.ob.clip.bottom=(this.animpos*this.frameheight)+this.frameheight;
	this.ob.moveTo(this.x+Sp_xoffset-(this.frame*this.framewidth),this.y+Sp_yoffset-(this.animpos*this.frameheight))
}

function Sp_movetoIE(Sp_x,Sp_y)
{
	if(Sp_x>this.xmax+this.width)
		Sp_x=this.xmax;
	if(Sp_y>this.ymax+this.height)
		Sp_y=this.ymax;
	if(Sp_x<this.xmin)
		Sp_x=this.xmin;
	if(Sp_y<this.ymin)
		Sp_y=this.ymin;
	this.ob.posTop=Sp_y+Sp_yoffset-(this.animpos*this.frameheight);
	this.ob.posLeft=Sp_x+Sp_xoffset-(this.frame*this.framewidth);
	this.x=Sp_x;
	this.y=Sp_y;
	if(this.collides)
		this.check_collide()
}

function Sp_movetoNS(Sp_x,Sp_y)
{
	if(Sp_x>this.xmax+this.width)
		Sp_x=this.xmax;
	if(Sp_y>this.ymax+this.height)
		Sp_y=this.ymax;
	if(Sp_x<this.xmin)
		Sp_x=this.xmin;
	if(Sp_y<this.ymin)
		Sp_y=this.ymin;
	this.ob.moveTo(Sp_x+Sp_xoffset-(this.frame*this.framewidth),Sp_y+Sp_yoffset-(this.animpos*this.frameheight));
	this.x=Sp_x;
	this.y=Sp_y;
	if(this.collides)
		this.check_collide()
}

function Sp_movetoN6(Sp_x,Sp_y)
{
	if(Sp_x>this.xmax+this.width)
		Sp_x=this.xmax;
	if(Sp_y>this.ymax+this.height)
		Sp_y=this.ymax;
	if(Sp_x<this.xmin)
		Sp_x=this.xmin;
	if(Sp_y<this.ymin)
		Sp_y=this.ymin;
	this.ob.top=Sp_y+Sp_yoffset-(this.animpos*this.frameheight);
	this.ob.left=Sp_x+Sp_xoffset-(this.frame*this.framewidth);
	this.x=Sp_x;
	this.y=Sp_y;
	if(this.collides)
		this.check_collide()
}

function Sp_movetoIE_l()
{
	this.ob.posLeft=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.posTop=this.y+Sp_yoffset-(this.animpos*this.frameheight);
	if(this.collides)
		this.check_collide()
}

function Sp_movetoNS_l()
{
	this.ob.moveTo(this.x+Sp_xoffset-(this.frame*this.framewidth),this.y+Sp_yoffset-(this.animpos*this.frameheight));
	if(this.collides)
		this.check_collide()
}

function Sp_movetoN6_l()
{
	this.ob.left=this.x+Sp_xoffset-(this.frame*this.framewidth);
	this.ob.top=this.y+Sp_yoffset-(this.animpos*this.frameheight);
	if(this.collides)
		this.check_collide()
}

function Sp_check_collide()
{
	var sx2=null,sy2=null,sx=this.x,sy=this.y,x=0;
	this.hit=null;
	this.totalhits=0;
	for(x=0;x<Sp_totalsprites;x++)
	{
		sp_b=Sp_sprite[x];
		if(sp_b.on)
		{
			sx2=sp_b.x;
			sy2=sp_b.y;
			if(!((sp_b==this)||(sx>sx2+sp_b.width)||(sx+this.width<sx2)||(sy>sy2+sp_b.height)||(sy+this.height<sy2)))
			{
				this.hit=this.hitarray[this.totalhits++]=sp_b;
				if(this.hitevents&&this.hitevent[sp_b.index])
					eval(this.hitevent[sp_b.index]);
			}
		}
	}
}

function Sp_hashit(ob)
{
	for(var n=0;n<this.totalhits;n++)
	{
		if(this.hitarray[n]==ob)
			return true
	}
	return false
}

function Sp_setanimspd(Sp_a,Sp_b)
{
	Sp_b=Sp_b.toLowerCase();
	this.crepeat=0;
	this.animspd=Sp_a;
	this.animtmr=Sp_a;
	if(Sp_b=="forward")
		this.animd=1;
	else
		this.animd=-1
}

function Sp_setanimloop(Sp_a,Sp_b)
{
	if(Sp_a>=this.anims)
		Sp_a=this.anims-1;
	else if(Sp_a<0)
		Sp_a=0;
	if(Sp_b>=this.anims)
		Sp_b=this.anims-1;
	if(Sp_b<Sp_a)
		Sp_b=Sp_a;
	this.animsstart=Sp_a;
	this.animsend=Sp_b
}

function Sp_setimageIE(img,w,h,w2,h2)
{
	var i=document.images["Sp_i"+this.index];
	i.src=img;
	i.width=w*w2;
	i.height=h*h2;
	this.frames=w2;
	this.framewidth=w;
	this.frameheight=h;
	this.anims=h2;
	this.animsend=h2-1;
	this.animsstart=0;
	this.ob.clip="rect(0,0,0,0)";
	this.width=w;
	this.height=h;
	this.image=img
}

function Sp_setimageNS(img,w,h,w2,h2)
{
	this.ob.document.open();
	this.ob.document.write("<img src='"+img+"' width="+(w*w2)+" height="+(h*h2)+">");
	this.ob.document.close();
	this.framewidth=w;
	this.frameheight=h;
	this.frames=w2;
	this.anims=h2;
	this.animsend=h2-1;
	this.animsstart=0;
	this.ob.clip.top=0;
	this.ob.clip.bottom=0;
	this.ob.clip.left=0;
	this.ob.clip.right=0;
	this.width=w;
	this.height=h;
	this.image=img
}

function Sp_resizeIE(x,y)
{
	this.width=x;
	this.height=y;
	this.framewidth=x;
	this.frameheight=y;
	this.ob.width=this.width*this.frames;
	this.ob.height=this.height*this.anims;
	this.setAnimation(this.animpos)
}

function Sp_resizeNS(x,y)
{
	this.width=x;
	this.height=y;
	this.framewidth=x;
	this.frameheight=y;
	this.ob.document.open();
	this.ob.document.write("<img src='"+this.image+"' width="+(this.width*this.frames)+" height="+(this.height*this.anims)+">");
	this.ob.document.close();
	this.setAnimation(this.animpos)
}

function Sp_setspeed(spd)
{
	this.speed=spd;
	this.xspeed=this.xdir*spd;
	this.yspeed=this.ydir*spd
}

function Sp_setxydegs(Sp_d)
{
	var d=Math.floor(Sp_d);
	if(d>719||d<-359)
		d=0;
	else if(d>359)
		d-=360;
	else if(d<0)
		d+=360;
	this.xydegs=d;
	this.xdir=G_sin[d];
	this.ydir=G_cos[d];

	this.xspeed=this.xdir*this.speed;
	this.yspeed=this.ydir*this.speed
}

function Sp_switchoff()
{
	this.on=false;
	this.ob.top=-1000
}

function Sp_switchonIE()
{
	this.on=true;
	this.ob.posTop=this.y+Sp_yoffset-(this.animpos*this.frameheight);
	this.ob.posLeft=this.x+Sp_xoffset-(this.frame*this.framewidth)
}

function Sp_switchonNS()
{
	this.on=true;
	this.ob.moveTo(this.x+Sp_xoffset-(this.frame*this.framewidth),this.y+Sp_yoffset-(this.animpos*this.frameheight))
}

function Sp_switchonN6()
{
	this.on=true;
	this.ob.top=this.y+Sp_yoffset-(this.animpos*this.frameheight);
	this.ob.left=this.x+Sp_xoffset-(this.frame*this.framewidth)
}

function Sp_makestatic()
{
	this.on=false;
	this.isstatic=true;
	this.xdir=0;
	this.ydir=0;
	this.speed=0
}

function Sp_makenormal()
{
	this.isstatic=false
}

function Sp_setanimrepeat(x)
{
	if(x<-1)x=-1;
	this.animrepeat=x;
	this.crepeat=0
}

function Sp_dragtype(Sp_a)
{
	this.dragnormal=false;
	this.dragvert=false;
	this.draghoriz=false;
	if(Sp_a==0)
		this.dragnormal=true;
	else if(Sp_a==1)
		this.dragvert=true;
	else if(Sp_a==2)
		this.draghoriz=true
}
