// ----------------------------------------------------
// Author: Xavier Sarrate ( aka ker )

// ABILBO CONCEPT, s.l.
// --------------------
// Consell de Cent, 303 1er
// 08007 Barcelona
// Tel. 93 323 56 54
 
// info@abilbo.com
// WWW.ABILBO.COM
 
// Copyright 2000 Abilbo Concept, s.l.

// Last change : 22/03/2000
// ----------------------------------------------------


// NAME:kerlayer
//
// TARGET: kerLayer object builder function
// PARAMS: 
// name(cadena): name of the contained div element
// left,top(integer,integer): upper left corner original coordinates
// width,height(integer,integer): object dimension. If the value is -1, the value of the div element contained is taken straight from the contained div
// visibility(boolean): Original visibility
// zindex(integer): z-axis position among all the other layers
// parent(cadena)[OPCIONAL]: div element container of the object's contained div... A lil'bit fuzzy, it isn't ??

function kerLayer(name,left,top,width,height,visibility,zindex,parent){  
  if(ns5up){ // ns5up es cierto si trabajamos con ie5 o ns6 (o superior)
    eval("this.layer=document.getElementById('"+name+"').style");
    this.event=this.layer;
    eval("this.imageList=document.getElementsByTagName('IMG')");
  }else
    if(ns4){
      if (parent)
        eval("this.layer=document.layers['"+parent+"'].document.layers['"+name+"']");
      else
        eval("this.layer=document.layers['"+name+"']");
      this.event=this.layer;
      eval("this.imageList=this.layer.document.images");
    }
    if(ie4){
      eval("this.layer=document.all['"+name+"'].style");
      eval("this.event=document.all['"+name+"']");
      eval("this.imageList=document.images");
    }  
  this.layer.top=top;
  this.layer.left=left;  
  this.layer.width=width;  
  this.layer.height=height;
  if (width==-1)
    if(ns5up) this.layer.width=this.event.offsetWidth;
    else this.layer.width=(ie4)?document.all[name].scrollWidth:document.layers[name].document.width
  if (height==-1)
    if(ns5up) this.layer.width=this.event.offsetHeight;
    else this.layer.height=(ie4)?document.all[name].scrollHeight:document.layers[name].document.height        
  

  this.layer.zIndex=zindex;
  this.obj=name+"KerLayer";
  eval(this.obj + "=this")
  /*
  if(ns4){
    this.layer.clip.top=0;
    this.layer.clip.left=0;
    this.layer.clip.right=width;
    this.layer.clip.bottom=height;
    this.layer.bgColor=null;
    this.layer.visibility=((visibility=="hide")||(visibility=="hidden"))?"hide":"show";
  }*/
  if(ns5up){
    this.layer.clip="rect("+0+"px "+width+"px "+height+"px "+0+"px)"
    this.layer.backgroundColor=null;
    this.layer.visibility=((visibility=="hide")||(visibility=="hidden"))?"hidden":"visible";
  }else
    if(ns4){
      this.layer.clip="rect("+0+"px "+width+"px "+height+"px "+0+"px)"
      this.layer.backgroundColor=null;
      this.layer.visibility=((visibility=="hide")||(visibility=="hidden"))?"hidden":"visible";
    }
    if(ie4){
      this.layer.clip="rect("+0+"px "+width+"px "+height+"px "+0+"px)"
      this.layer.backgroundColor=null;
      this.layer.visibility=((visibility=="hide")||(visibility=="hidden"))?"hidden":"visible";
    }

  this.trDir=null;
  this.triDir=null;
  this.trTo=null;
  this.trRec=null;
  this.Timer=null;
  this.originX=null;
  this.originY=null;
  
  this.getTop=kerLayerGetTop;
  this.getLeft=kerLayerGetLeft;
  this.getWidth=kerLayerGetWidth;
  this.getHeight=kerLayerGetHeight;
  this.getZIndex=kerLayerGetZIndex;
  this.getClipTop=kerLayerGetClipTop;
  this.getClipLeft=kerLayerGetClipLeft;
  this.getClipBottom=kerLayerGetClipBottom;
  this.getClipRight=kerLayerGetClipRight;
  this.getBgColor=kerLayerGetBgColor;
  
  this.putTop=kerLayerPutTop;
  this.putLeft=kerLayerPutLeft;
  this.putWidth=kerLayerPutWidth;
  this.putHeight=kerLayerPutHeight;
  this.putZIndex=kerLayerPutZIndex;
  this.putClipTop=kerLayerPutClipTop;
  this.putClipLeft=kerLayerPutClipLeft;
  this.putClipBottom=kerLayerPutClipBottom;
  this.putClipRight=kerLayerPutClipRight;
  this.putBgColor=kerLayerPutBgColor;

  this.show=kerLayerShow;
  this.hide=kerLayerHide;
  this.visible=kerLayerVisible;

  this.moveX=kerLayerMoveX;
  this.moveY=kerLayerMoveY;
  this.moveXY=kerLayerMoveXY;
  
  this.slideX=kerLayerSlideX;
  this.slideY=kerLayerSlideY;
  this.slideXY=kerLayerSlideXY;
  
  this.travel=kerLayerTravel;
  this.travel2=kerLayerTravel2;

  this.putClip=kerLayerPutClip;

  this.clipTo=kerLayerClipTo;
  this.clipToStop=kerLayerClipToStop;
  this.kerLayerClipToTimer=null;  
  
  this.slideTo=kerLayerSlideTo;
  this.slideToMUA=kerLayerSlideToMUA;
  this.slideToStop=kerLayerSlideToStop;
  this.kerLayerSlideToTimer=null;
  
  this.kerLayerClipToTimer=null;
  
  this.shad=kerShad;
  this.shad2=kerShad2;
  
  this.replaceImage=kerReplaceImage;
  
  this.diffMUA=0
  this.ejeMUA=0
}

//NAME: kerLayerGetTop
//TARGET: Return the x coordinate of the object
//PARAMS: (-)
function kerLayerGetTop(){ return parseInt(this.layer.top); }

//NAME: kerLayerGetLeft
//TARGET: Return the y coordinate of the object
//PARAMS: (-)
function kerLayerGetLeft(){ return parseInt(this.layer.left); }

//NAME: kerLayerGetZIndex
//TARGET: Return the z coordinate of the object
//PARAMS: (-)
function kerLayerGetZIndex(){ return parseInt(this.layer.zIndex); }

//NAME: kerLayerGetWidth
//TARGET: Return the object width
//PARAMS: (-)
function kerLayerGetWidth(){ return parseInt(this.layer.width); }

//NAME: kerLayerGetHeight
//TARGET: Return the object height
//PARAMS: (-)
function kerLayerGetHeight(){ return parseInt(this.layer.height); }


//NAME: kerLayerGetClipTop
//TARGET: Return the object upper clipping 
//PARAMS: (-)
function kerLayerGetClipTop(){
  if(ns4) return parseInt(this.layer.clip.top);
  if(ie4){
    var clipTmp = this.layer.clip.split("rect(")[1].split(")")[0].split("px")
    return Number(clipTmp[0]);
  }
}

//NAME: kerLayerGetClipLeft
//TARGET: Return the object left clipping 
//PARAMS: (-)
function kerLayerGetClipLeft(){
  if(ns4) return parseInt(this.layer.clip.left);
  if(ie4){
    var clipTmp = this.layer.clip.split("rect(")[1].split(")")[0].split("px")
    return Number(clipTmp[3]);
  }
}

//NAME: kerLayerGetClipBottom
//TARGET: Return the object bottom clipping 
//PARAMS: (-)
function kerLayerGetClipBottom(){
  if(ns4) return parseInt(this.layer.clip.bottom);
  if(ie4){
    var clipTmp = this.layer.clip.split("rect(")[1].split(")")[0].split("px")
    return Number(clipTmp[2]);
  }
}

//NAME: kerLayerGetClipRight
//TARGET: Return the object right clipping 
//PARAMS: (-)
function kerLayerGetClipRight(){
  if(ns4) return parseInt(this.layer.clip.right);
  if(ie4){
    var clipTmp = this.layer.clip.split("rect(")[1].split(")")[0].split("px")
    return Number(clipTmp[1]);
  }
}

//NAME: kerLayerGetBgColor
//TARGET: Return the object background color
//PARAMS: (-)
function kerLayerGetBgColor(){ return (ns4)?this.layer.bgColor:this.layer.backgroundColor; }

//NAME: kerLayerPutLeft
//TARGET: Set the object x coordinate
//PARAMS:(integer)
function kerLayerPutLeft(value){ this.layer.left=value; }

//NAME: kerLayerPutTop
//TARGET: Set the object y coordinate
//PARAMS: (integer)
function kerLayerPutTop(value){ this.layer.top=value; }

//NAME: kerLayerPutZIndex
//TARGET: Set the object z coordinate
//PARAMS: (integer)
function kerLayerPutZIndex(value){ this.layer.zIndex=value; }

//NAME: kerLayerPutWidth
//TARGET: Set the object width
//PARAMS: (integer)
function kerLayerPutWidth(value){ this.layer.width=value; }

//NAME: kerLayerPutHeight
//TARGET: Set the object height
//PARAMS: (integer)
function kerLayerPutHeight(value){ this.layer.height=value; }

//NAME: kerLayerPutClipTop
//TARGET: Set the object top clipping
//PARAMS: (integer)
function kerLayerPutClipTop(value){
  if(ns4) this.layer.clip.top=value;
  if(ie4) this.layer.clip = "rect("+value+"px "+this.getClipRight()+"px "+this.getClipBottom()+"px "+this.getClipLeft()+"px)"
}

//NAME: kerLayerPutClipLeft
//TARGET: Set the object left clipping
//PARAMS: (integer)
function kerLayerPutClipLeft(value){
  if(ns4) this.layer.clip.left=value;
  if(ie4) this.layer.clip = "rect("+this.getClipTop()+"px "+this.getClipRight()+"px "+this.getClipBottom()+"px "+value+"px)"
}

//NAME: kerLayerPutClipBottom
//TARGET: Set the object bottom clipping
//PARAMS: (integer)
function kerLayerPutClipBottom(value){
  if(ns4) this.layer.clip.bottom=value;
  if(ie4) this.layer.clip = "rect("+this.getClipTop()+"px "+this.getClipRight()+"px "+value+"px "+this.getClipLeft()+"px)"
}

//NAME: kerLayerPutClipRight
//TARGET: Set the object right clipping
//PARAMS: (integer)
function kerLayerPutClipRight(value){
  if(ns4) this.layer.clip.right=value;
  if(ie4) this.layer.clip = "rect("+this.getClipTop()+"px "+value+"px "+this.getClipBottom()+"px "+this.getClipLeft()+"px)"
}

//NAME: kerLayerPutBgColor
//TARGET: Set the object background color 
//PARAMS: (color)
function kerLayerPutBgColor(value){
  if(ns4) this.layer.bgColor=value;
  if(ie4) this.layer.backgroundColor=value;
}

//NAME: kerLayerVisible
//TARGET: Return object current visibility (boolean)
//PARAMS: (-)
function kerLayerVisible(){ return (this.layer.visibility=="hide")||(this.layer.visibility=="hidden")?FALSE:TRUE; }

//NAME: kerLayerShow
//TARGET: Show the object
//PARAMS: (-)
function kerLayerShow(){ this.layer.visibility=(ns4)?"show":"visible"; }

//NAME: kerLayerHide
//TARGET: Hide the object
//PARAMS: (-)
function kerLayerHide(value){ this.layer.visibility=(ns4)?"hide":"hidden"; }

//NAME: kerLayerMoveX
//TARGET: Set the object x coordinate
//PARAMS: (integer)
function kerLayerMoveX(value){ this.putLeft(value); }

//NAME: kerLayerMoveX
//TARGET: Set the object y coordinate
//PARAMS: (integer)
function kerLayerMoveY(value){ this.putTop(value); }

//NAME: kerLayerMoveXY
//TARGET: Set both x,y coordinates
//PARAMS: (integer,integer)
function kerLayerMoveXY(valueX,valueY){ this.putLeft(valueX);this.putTop(valueY); }

//NAME: kerLayerSlideX
//TARGET: Increase by 'value' the object x position
//PARAMS: (integer:desplazamiento)
function kerLayerSlideX(value){ this.putLeft(value+this.getLeft()); }

//NAME: kerLayerSlideY
//TARGET:  Increase by 'value' the object y position
//PARAMS: (integer:amount of deplacement)
function kerLayerSlideY(value){ this.putTop(value+this.getTop()); }

//NAME: kerLayerSlideXY
//TARGET:  Increase by 'value' both the object x and y position
//PARAMS: (integer:deplacement x, integer:deplacement y)
function kerLayerSlideXY(valueX,valueY){ this.putLeft(valueX+this.getLeft()); this.putTop(valueY+this.getTop()); }

//NAME: kerLayerSlideXY
//TARGET: Set the clipping value in all four directions
//PARAMS: (integer:upper clipping, integer:right clipping, integer: bottom clipping, integer:clipping izquierdo)
function kerLayerPutClip(top,right,bottom,left){	
  this.putClipTop(top);
  this.putClipRight(right);
  this.putClipBottom(bottom);
  this.putClipLeft(left);
}

//NAME: kerLayerClipTo
//TARGET: Sequentially change the object clipping 
//PARAMS: (integer:upper clipping, integer:right clipping, integer:bottom clipping, integer:left clipping, real:frequency, integer:rate, cadena(OPTIONAL):Action to perform at the end of the sequence)
function kerLayerClipTo(top,right,bottom,left,seconds,rate,action){
  
  var actualRight=this.getClipRight();  
  var actualTop=this.getClipTop();
  var actualBottom=this.getClipBottom();
  var actualLeft=this.getClipLeft();
   
  var diffRight=right-actualRight;
  var diffTop=top-actualTop;
  var diffBottom=bottom-actualBottom;  
  var diffLeft=left-actualLeft; 
  
  var rightDir=(diffRight<0)?-1:1;  
  var topDir=(diffTop<0)?-1:1;
  var bottomDir=(diffBottom<0)?-1:1;
  var leftDir=(diffLeft<0)?-1:1;  
    
  clearTimeout(this.kerLayerClipToTimer);
  this.kerLayerClipToTimer=null;    
    
  diffRight=((Math.abs(diffRight)>rate)&&(diffRight!=0))?rate*rightDir:diffRight;
  diffTop=((Math.abs(diffTop)>rate)&&(diffTop!=0))?rate*topDir:diffTop;
  diffBottom=((Math.abs(diffBottom)>rate)&&(diffBottom!=0))?rate*bottomDir:diffBottom;
  diffLeft=((Math.abs(diffLeft)>rate)&&(diffLeft!=0))?rate*leftDir:diffLeft;    
  
  if((diffLeft!=0)||(diffTop!=0)||(diffBottom!=0)||(diffRight!=0)){
    this.putClip(actualTop+diffTop,actualRight+diffRight,actualBottom+diffBottom,actualLeft+diffLeft);   
    this.kerLayerClipToTimer=setTimeout(this.obj+".clipTo("+top+","+right+","+bottom+","+left+","+seconds+","+rate+",'"+action+"')",seconds*1000);
  }else{
    clearTimeout(this.kerLayerClipToTimer);
    this.kerLayerClipToTimer=null;
    if(action!='undefined') eval(action);
  }  
}

//NAME: kerLayerClipTo
//TARGET: Stop a "kerLayerClipTo" sequence
//PARAMS: (-)
function kerLayerClipToStop(){ clearTimeout(kerLayerClipToTimer); }

//NAME: kerLayerSlideTo
//TARGET: sequentially move an object to a given position
//PARAMS: (integer:coordinate x, integer:coordinate y, real:frequency, integer:rate, cadena(OPTIONAL):Action to perform at the end of the sequence )
function kerLayerSlideTo(left,top,seconds,rate,action){
  var actualTop=this.getTop();
  var actualLeft=this.getLeft();
      
  var diffTop=top-actualTop;
  var diffLeft=left-actualLeft;

    
  var toDoTop=Math.round(Math.abs(diffTop)/rate);
  var toDoLeft=Math.round(Math.abs(diffLeft)/rate);

  var topDir=(diffTop<0)?-1:1;
  var leftDir=(diffLeft<0)?-1:1;  

  diffTop=(Math.abs(diffTop)>rate)?rate*topDir:diffTop;
  diffLeft=(Math.abs(diffLeft)>rate)?rate*leftDir:diffLeft;    
  
  if((diffTop!=(top-actualTop))&&(diffLeft!=(left-actualLeft))){
    if(toDoTop<toDoLeft) diffTop=Math.round(toDoTop/(toDoLeft/rate))*topDir;
    else if(toDoTop>toDoLeft) diffLeft=Math.round(toDoLeft/(toDoTop/rate))*leftDir;
  }
  if((diffLeft!=0)||(diffTop!=0)){

    this.kerLayerSlideToTimer=setTimeout(this.obj+".slideTo("+left+","+top+","+seconds+","+rate+",'"+action+"')",seconds*1000);
  }else{
    clearTimeout(this.kerLayerSlideToTimer);
    this.kerLayerSlideToTimer=null;
    if(action!='undefined') eval(action);
  }  
}

//NAME: kerLayerSlideToStop
//TARGET: Stop a "kerLayerSlideTo" sequence
//PARAMS: (-)
function kerLayerSlideToStop(){ clearTimeout(this.kerLayerSlideToTimer); }

function kerLayerTravel2(){
    var jump=(ns4)?10:20;		
	
    if ( this.trRec > 0 )
    {
    	var howMuch = (this.trRec > jump)?jump:this.trRec;
        if (( this.trDir=="up")||(this.trDir=="down"))
            this.slideY(this.triDir*howMuch);
        else
            this.slideX(this.triDir*howMuch);
            
        this.trRec-=howMuch;
        return false;
    }
    else{

    	if ( this.trTo == "out"){
            this.hide();
            this.putLeft(this.originX);
            this.putTop(this.originY);
        }
        clearTimeout(this.Timer);
        this.Timer=null;
    }
    return false;
}


//NAME: kerLayerTravel
//TARGET: Make the object enter or exit from screen
//PARAMS: (string:sense("out"/"in"), string:direction("left"/"up"/"right"/"down"), real:frequency)
function kerLayerTravel(to, dir, speed){ 
    width=(ns4)?window.innerWidth:document.body.clientWidth;
    height=(ns4)?window.innerHeight:document.body.clientHeight;
    
    var ad=0;
    this.action=0
    //if (add) ad=add;
    if (action) this.action=action;
    if (this.Timer)
        return;
    this.trDir = dir;
    this.trTo = to;
    
    this.originX = this.getLeft();
    this.originY = this.getTop();

    switch(dir){
        case "left" : this.triDir = -1;
                      this.trRec = this.getLeft()+this.getWidth()-ad;break;
        case "up"   : this.triDir = -1;
                      this.trRec = this.getTop()+this.getHeight()-ad;break;
        case "right": this.triDir = 1;
                      this.trRec = width -ad;break;
        case "down" : this.triDir = 1;
                      this.trRec = height -ad - this.getTop();break;
    }
    
    this.show();
    switch(to){
    	case "out":break;
    	case "in": this.triDir*=-1;
    	if ((dir=="left")||(dir=="right"))
    	               this.slideX(this.trRec*this.triDir*-1);
    	           else
    	               this.slideY(this.trRec*this.triDir*-1);
    	           break;
    }
    // Launch the traveler
    eval("this.Timer = setInterval('"+this.obj+".travel2()',speed)");


}


function kerShad2(){
    	this.hide(); 
    	this.putClip(this.originX,this.getClipRight(),this.originY,this.getClipLeft());
}

//NAME: kerShad
//TARGET: Sequentally show or hide the object by clipping
//PARAMS: (boolean:Desired visibility, chain: clipping sequence direccion (up/down), real:frequency, integer:rate )
function kerShad(visibility, direction, seconds, rate){
    
    if (visibility=="hide"){
    	this.originX = this.getClipTop();
        this.originY = this.getClipBottom();
        if (direction=="down")
            this.clipTo(this.getClipBottom(),this.getClipRight(),this.getClipBottom(),this.getClipLeft(), seconds, rate,this.obj+".shad2()");
        else //up
            this.clipTo(this.getClipTop(),this.getClipRight(),this.getClipTop(),this.getClipLeft(), seconds, rate,this.obj+".shad2()");
    }
    else{
    	this.originX = this.getClipTop();
        this.originY = this.getClipBottom();    
        if ( direction=="up"){
            this.putClip(this.getClipTop(),this.getClipRight(),this.getClipTop(),this.getClipLeft());
            this.show();
            this.clipTo(this.getClipTop(), this.getClipRight(), this.originY, this.getClipLeft(),seconds, rate,"");
        }
        else{//down
            this.putClip(this.getClipBottom(),this.getClipRight(),this.getClipBottom(),this.getClipLeft());
            this.show();
            this.clipTo(this.originX, this.getClipRight(), this.getClipBottom(), this.getClipLeft(),seconds, rate,"");
        }
    }
}

//NAME: kerReplaceImage
//TARGET: Change the content of an image inside the object 
//PARAMS: (chain:NAME of the image, chain:path of the new graphic file)
function kerReplaceImage(cual,url){ eval("this.imageList."+cual+".src=url"); }


//NAME: kerLayerSlideToMUA
//TARGET: Sequentally move the object to another position ( with acceleration )
//PARAMS: (integer:coordinate x, integer:coordinate y, real:frequency, integer:rate, cadena(OPTIONAL):Action to perform at the end of the sequence)
function kerLayerSlideToMUA(_x,_y,_seconds,_iRate, _action){
	
  var iCur = new Array()
  var iDiff= new Array()
  var iToDo= new Array()
  
  iCur[0]=this.getLeft();
  iCur[1]=this.getTop();
  
  iDiff[0]= _x - iCur[0];
  iDiff[1]= _y - iCur[1];
  
  iToDo[0]= Math.round(iDiff[0]/_iRate)
  iToDo[1]= Math.round(iDiff[1]/_iRate)
  

  if (iToDo[0]!=0 || iToDo[1]!=0){
    this.slideXY(iToDo[0],iToDo[1])
    this.kerLayerSlideToTimer=setTimeout(this.obj+".slideToMUA("+_x+","+_y+","+_seconds+","+_iRate+",'"+_action+"')",_seconds*1000);    
  }
  else{
    clearTimeout(this.kerLayerSlideToTimer);
    this.kerLayerSlideToTimer=null
    if(_action!='undefined') eval(_action)
  }
}
