//  ___________
// |           |____________________________________________________
// |                                                                |
// |    core browser sniffer                                        |
// |________________________________________________________________|
//
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;
mac=(navigator.userAgent.toLowerCase().indexOf('mac'))!=-1;
safari=(navigator.userAgent.toLowerCase().indexOf('safari'))!=-1;
macIE=(ie&&mac);
if (ie && w3)
    {
    ie5 = true;
    ie4 = false;
    }
else if (w3)
    ns6 = true;  
	
//  ___________
// |           |____________________________________________________
// |                                                                |
// |    Fix IE PNG bug                                              |
// |________________________________________________________________|
function fixPNG(imgObj)
{   

	imgObj.onload=function rien(){};
	if (ie&&!mac)
	{
		w=imgObj.width;h=imgObj.height;
		imgObj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ imgObj.src+  "',sizingMethod='scale')";
		imgObj.src="images/clear.gif";
		imgObj.style.width=w;
		imgObj.style.height=h;
	}
	imgObj.style.visibility="inherit";
	
}
//  ___________
// |           |____________________________________________________
// |                                                                |
// |    core mouseover maker                                        |
// |    supply image object, src on [,and off if different		    |
// |    from <img> ] execute from onload in <img> tag               |
// |________________________________________________________________|
//
cmo_counter=0;
i_mover = new Array();
i_mout  = new Array();
function createMouseOver(imageObject,moverSrc)
{
		if(imageObject.name)
			imageObject.id = imageObject.name
		else if (imaheObject.id){
			imageObject.name = imageObject.id;
		}
		imageObject.index = cmo_counter;
		imageObject.selected = false;
		i_mover[cmo_counter]=new Image();i_mover[cmo_counter].src=moverSrc;
		i_mout[cmo_counter]=new Image();i_mout[cmo_counter].src=imageObject.src;
		imageObject.onmouseover=function cmo_over(){this.src=i_mover[this.index].src}
		imageObject.onmouseout=function cmo_out(){if(!this.selected)this.src=i_mout[this.index].src}
		cmo_counter++;
		//alert(cmo_counter)
		//alert(imageObject.index+" "+i_mover[imageObject.index].src+" "+i_mout[imageObject.index].src+" ");
		imageObject.onload="";
}	

//  ____________________________
// |                            |________________________________________________________
// |                                                                                     |
// |   color handling functions                                                          |
// |_____________________________________________________________________________________|
//
HEX_TABLE = new Array("00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F","10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F","20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F","60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F","80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F","90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F","A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF","B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF","C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF","D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF","E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF","F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF");
DEC_TABLE = new Array();
DEC_TABLE['0'] = 0;DEC_TABLE['1'] = 1;DEC_TABLE['2'] = 2;DEC_TABLE['3'] = 3;DEC_TABLE['4'] = 4;DEC_TABLE['5'] = 5;DEC_TABLE['6'] = 6;DEC_TABLE['7'] = 7;DEC_TABLE['8'] = 8;DEC_TABLE['9'] = 9;DEC_TABLE['A'] = 10;DEC_TABLE['B'] = 11;DEC_TABLE['C'] = 12;DEC_TABLE['D'] = 13;DEC_TABLE['E'] = 14;DEC_TABLE['F'] = 15;

function Color(r,g,b)
{
	this.r=r;
	this.g=g;
	this.b=b;
	//this.a=255;
}
function getHexColor(r,g,b)
{
	//alert(r+" "+g+" "+b);
	return '#'+HEX_TABLE[r]+HEX_TABLE[g]+HEX_TABLE[b];
}
function getRGB(hex)
{	
	hex = hex.toUpperCase()
	r = DEC_TABLE[hex.substr(1,1)]*16+DEC_TABLE[hex.substr(2,1)];
	b = DEC_TABLE[hex.substr(3,1)]*16+DEC_TABLE[hex.substr(4,1)];
	g = DEC_TABLE[hex.substr(5,1)]*16+DEC_TABLE[hex.substr(6,1)];
	return new Color(r,g,b);
}

//  ___________
// |           |____________________________________________________
// |                                                                |
// |   core cross-browser interfaces                               |
// |________________________________________________________________|
//
function clipLayer(layerRef,x,y,w,h)
{	if (typeof layerRef=='string')
		layerRef=getStyle(layerRef);
    if(ns)
    {   
        layerRef.clip.top=y;
        layerRef.clip.left=x;
        layerRef.clip.height=h;
        layerRef.clip.width=w;
        
    }else{  
		//alert("rect("+y+"px "+(x+w)+"px "+(y+h)+"px "+x+")");
        layerRef.clip = "rect("+y+"px "+(x+w)+"px "+(y+h)+"px "+x+")";      
    }
    
}
function getLayer(id)
{
    if(w3)
        return document.getElementById(id);
    if(ie)  
        return document.all[id];
    if(ns)
        return document.layers[id];
}
function getHeight(id)
{
	 if (typeof id == 'object')  
		obj = id;
	 else
		obj = getLayer(id);
	 if(w3||ie)
        return obj.offsetHeight;
     if(ns)
        return obj.document.height;	
}
function getWidth(id)
{    
	 if (typeof id == 'object')  
		obj = id;
	 else
		obj = getLayer(id);
	 if(w3||ie)
        return obj.offsetWidth;
     if(ns)
        return obj.document.width;	
}
function getStyle(id)
{  
//alert(id)
    if (typeof id == 'object')  
		obj = id;
	else
		obj = getLayer(id);
	
	if(w3||ie)
		return obj.style;
    if(ns) 
		return obj;
 //alert(id)
}
function makeLayer(id,width,html,bg)    //add bg to layer right away for NS$ compatibility
{ 
   if(w3)
    {   
        el = document.createElement('DIV');
        el.id =id;
        if (bg)
            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);
        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;
        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;	   
    }
    
}


//  ___________
// |           |____________________________________________________
// |                                                                |
// |   screen dimension object                                      |
// |________________________________________________________________|
//
function ScreenMetrics(minx,miny)
    {
	   
         if (ns||ns6){
          this.w=(self.innerWidth>minx) ? self.innerWidth : minx;
          this.h=(self.innerHeight>miny) ? self.innerHeight : miny;      
       }
       if (ie){
           this.w=(document.body.clientWidth>minx) ? document.body.clientWidth : minx;
           this.h=(document.body.clientHeight>miny) ? document.body.clientHeight : miny;          
       }
       this.mx = Math.round(this.w/2) ;
       this.my = Math.round(this.h/2) ;
    }
	

//  ___________
// |           |____________________________________________________
// |                                                                |
// |     Sprite object                                              |
// |________________________________________________________________|
//
function Sprite(name,id)
{
    this.id=name;
	this.content = id;
	this.isMoving= false;
	this.x=0;this.y=0;
	this.xOffset=new Array(0,0);
	this.yOffset=new Array(0,0);
	this.layerNode = new Array();
	this.layerNode[0] = getLayer(id);
	this.layer= new Array();
    this.layer[0]=getStyle(getLayer(id))
	this.clipH = getHeight(id);
	this.clipW = getWidth(id);
    this.Move = SpriteMove;
    this.iMove = recurSpriteMove;
    this.hide = Spritehide;
    this.show = Spriteshow;
    this.setXY = setXY;
    this.stack =0;

}  
function SpriteMove(xdis,ydis,ms)
 { 
	  if (this.isMoving==true)
	  	return;
	  this.isMoving=true;
	  this.iMove(this.x,this.y,xdis,ydis,ms,(new Date()).getTime())//calls recursive function
 }   
function recurSpriteMove(ox,oy,xdis,ydis,ms,FatherTime)
{ 
   now = (new Date()).getTime(); 
   ratio = (now - FatherTime)/ms
   if (ratio >= 1)
   {
	   this.setXY(ox+xdis,oy+ydis)
	   this.x=ox+xdis;this.y=oy+ydis;
	   this.isMoving=false;
	   return
   }
   else{  
    this.setXY(ox +xdis*ratio,oy +ydis*ratio);
    wastedTime = (new Date()).getTime() - now;
    this.stack=setTimeout(this.id+'.iMove('+ox+','+oy+','+xdis+','+ydis+','+ms+','+FatherTime+')',20+ wastedTime);
   }
}
function Spritehide(){
  	for (i=0;i<this.layer.length;i++){ 
	this.layer[i].visibility="hidden";
	this.layer[i].display="none";
	}
	this.visible= false;
}
function Spriteshow(){
  	for (i=0;i<this.layer.length;i++){ this.layer[i].visibility="visible";
	this.layer[i].display="block";
	}
	this.visible = true;
	this.clipW = getWidth(this.content);
	this.clipH = getHeight(this.content);
}  
function setXY(x,y,visBOOL)
{   
     if (arguments.length==0)
	   {
		    x= this.x;
		    y= this.y;
	   }  
	   else{
		   this.x=x;
		   this.y=y;
	   }
	   for(i=0;i<this.layer.length;i++)
	   {
	   	this.layer[i].left=x+this.xOffset[i];
	   	this.layer[i].top=y+this.yOffset[i];
	   }
	   if (visBOOL)
	   		this.show();
	   

}