

function clipLayer(layerRef,x,y,w,h)
{
    if(ns)
    {   
        layerRef.clip.top=y;
        layerRef.clip.left=x;
        layerRef.clip.height=h;
        layerRef.clip.width=w;
        
    }else{  
        layerRef.clip = "rect("+y+"px "+(x+w)+"px "+(y+h)+"px "+x+")";

        //alert(layerRef+".clip=rect("+y+" "+w+" "+(y+h)+" "+x+")");        
    }
    
}
function getLayer(id)
{
	
    if(w3)
		return document.getElementById(id);
	if(ie)
		return document.all[id];
    if(ns)	
		return document.layers[id];

}
function getHeight(layer)
{
	return (ns) ? layer.document.height : layer.offsetHeight;
}
function getStyle(layer)
{
	return (ns) ? layer : layer.style ;
}
function makeLayer(id,bg,width,html)    //add bg to layer right away for NS$ compatibility
{
   if(w3)
    {   
        el = document.createElement('DIV');
        el.id =id;
        if (bg.src)
            el.style.backgroundImage="url("+bg.src+")";
        el.style.width = width+"px";
		
        el.style.position ="absolute";
        el.style.visibility = "hidden";
        el.innerHTML = html;
        document.body.appendChild(el);
		//alert(id+" ok");
        return el;
    }
    if(ie4)
    {
        divs="<div id=\""+id+"\" style=\"position:absolute;width:"+width+";visibility:hidden;padding:0;";
        if (bg)divs+= "background-image:url("+bg.src+");";
        divs+="\">";
        
        dive="</div>";
        str = divs+html+dive;
       // alert(str);
        document.body.insertAdjacentHTML("beforeEnd",str);
        while(!document.all[id]){};
        document.all[id].innerHTML=html;
        return document.all[id];  
    }
    if (ns)
    { 
        if (bg) bgs = " background=\""+bg.src+"\" ";
        else bgs="";
        templayer = new Layer(width);
        templayer.document.open();
        templayer.document.write("<html><head></head><body"+bgs+">"+html+"</body></html>");
        templayer.document.close();
        return templayer;	   
    }
    
}
//
//end Utilities
//-----------------------------------------------------




//----------------------------------------------------
// Sprites
//

function Sprite(name,id)
{
    /* constructor sets up id, and the layer with the id. */
    
    this.object = name;
	this.id=name;
	this.xOffset=new Array(0,0);
	this.yOffset=new Array(0,0);
	this.layer= new Array();
    temp = getLayer(id);
	this.height = getHeight(temp);
	this.layer[0] = getStyle(temp);
    
    /* associating methods to our Sprite object */ 

    this.Move = Move;
    this.iMove = iMove;
    this.hide = Spritehide;
    this.show = Spriteshow;
    this.setXY = SpritesetXY;
    
    this.stack =0;
    //this.h = this.layer.height; these don't seem to be working!
    //this.w = this.layer.width;
}
    
 
    
   function Move(xdis,ydis,ms){ // sets up start time and orginal coords
   FatherTime = (new Date()).getTime();//get starttime
   x = this.x; //get starting x coord
   y = this.y; // get starting y coord
   ox = x;
   oy = y;
  
   
   
   this.iMove(ox,oy,xdis,ydis,ms,FatherTime)//calls recursive function
   }
   

   
function iMove(ox,oy,xdis,ydis,ms,FatherTime){// has nothing to do with iMac
  
   /* just arrived from first timeout and will recursively call itself until endtime is reached */
   
   now = (new Date()).getTime(); // get now to calculate
   
  // window.alert(" x:"+cx+" y:"+cy+" x:"+xdis+" yd:"+ydis+" ms"+ms+" lt:"+lastTime+" c:"+count);
   elapsed = now - FatherTime;
   ratio = elapsed / ms;  // ratio might be integer:WATCHOUT 'sti
   if (ratio >= 1) { // if now is at or after endtime then finish up
   this.setXY(ox+xdis,oy+ydis) // draw the last frame
   this.x=ox+xdis;this.y=oy+ydis;
   return}// fuck off 'sti
   else{ 
    xdif= xdis*ratio;// how far to move sprite
    ydif= ydis*ratio;// ibid.
    nx = ox + xdif;
    ny = oy + ydif;
    this.setXY(nx,ny);
    realNow = (new Date()).getTime(); // get now to calculate;
    wastedTime = realNow - now;// calculating how much time it took to redraw layer
    timeout =  50+ wastedTime;
    expr=this.object+'.iMove('+ox+','+oy+','+xdis+','+ydis+','+ms+','+FatherTime+')';//    
    
    this.stack=setTimeout(expr,timeout);
    }
  }

  /* simple other layer manipulation functions */
  
   function Spritehide(){
     for (i=0;i<this.layer.length;i++) this.layer[i].visibility="hidden";
   }
   function Spriteshow(){
     for (i=0;i<this.layer.length;i++) this.layer[i].visibility="visible";
   }
   function SpritesetXY(x,y,v){
   if (arguments.length==0)
   {
    x= this.x;
    y= this.y;
   }
   for(spi=0;spi<this.layer.length;spi++)
   {
   this.layer[spi].left=x+this.xOffset[spi];
   this.layer[spi].top=y+this.yOffset[spi];
   
   
   }
   if (v ==1){
   for (spi=0;spi<this.layer.length;spi++) 
   		this.layer[spi].visibility="visible";
   }
   
   this.x=x;
   this.y=y;
   }
   
   
   
//
//---end Sprites
//----------------------------------------



//---------------------------------------------------
//-- environment object
//
function Environment(minX,minY){

      if (document.layers || ns6){
        this.w=self.innerWidth;
        this.h=self.innerHeight;
        this.dhtml = true;
        }
   else{ 
        if (document.all){
          this.w=document.body.clientWidth; 
          this.h=document.body.clientHeight;  
          this.dhtml = true; 
          }
         else{
         this.dhtml = false;
            return
          }
     }
  if (this.w <minX){this.w=minX}
  if (this.h <minY){this.h=minY}
  
  this.mx = this.w/2 ;
  this.my = this.h/2 ;
  }  
  
// end evironment object





// init function initializes complile-time objects
function pageOnResize()
{
    if (ns) window.location.reload();
}

// compile-time inits and variable declarations
ns = document.layers;   // 4.0 < ns < 6.0
ie  = document.all;     // for all internet explorers
ie4 = ie;               // ie4 only
w3 = document.getElementById; // ie5 and ns6 w3 consortium DOM
ns6 = false;
ie5 = false;
if (ie && w3)
    {
    ie5 = true;
    ie4 = false;
    }
else if (w3)
    ns6 = true;  



