var re_try_time = 1000;
var NS = (navigator.appName == "Netscape");
var IE = (navigator.appName == "Microsoft Internet Explorer");
var main_tout = null; // main timeout id


function show(layer) {
  if (NS && document[layer] || IE && document.all[layer]) {  // html code has been read ...          
    if (NS) document[layer].visibility = "show";
    else  document.all[layer].style.visibility = "visible"; 
  } 
  else setTimeout("show("+layer+")", re_try_time); 
}

function hide(layer) {
  if (NS && document[layer] || IE && document.all[layer])  // html code has been read ...      
   if (NS) document[layer].visibility = "hide";
   else  document.all[layer].style.visibility = "hidden";
  else setTimeout("hide("+layer+")", re_try_time); 
}    

function moveBy(layer, x, y) {          
  if (NS && document[layer] || IE && document.all[layer]) // html code has been read ...
   if (NS) {
       document[layer].left = document[layer].left+x; 
       document[layer].top  = document[layer].top+y; 
   }    
   else {
       document.all[layer].style.pixelLeft = document.all[layer].style.pixelLeft+x;
       document.all[layer].style.pixelTop  = document.all[layer].style.pixelTop+y;
   }   
  else setTimeout("moveBy("+layer+","+x+","+y+")", re_try_time);       
}       


function moveTo(layer, x, y) {          
  if (NS && document[layer] || IE && document.all[layer]) // html code has been read ...
   if (NS) {
       document[layer].left = x; 
       document[layer].top  = y; 
   }    
   else {
       document.all[layer].style.pixelLeft = x;
       document.all[layer].style.pixelTop  = y;
   }        
  else setTimeout("move("+layer+","+x+","+y+")", re_try_time);        
}         

function getLeft(layer) {          
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].left;         
   else  data = document.all[layer].style.pixelLeft;
  } 
  else setTimeout("getLeft("+layer+")", re_try_time);       
  return data;
}         
   
   
function getTop(layer) {          
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].top;         
   else  data = document.all[layer].style.pixelTop;
  } 
  else setTimeout("getTop("+layer+")", re_try_time);       
  return data;
}         

function getHeight(layer) {          
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
    data = (IE)?document.all[layer].clientHeight:document.layers[layer].document.height;
  } 
  else setTimeout("getHeight("+layer+")", re_try_time);       
  return data;
} 
        

function getWidth(layer) {          
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
    data = (IE)?document.all[layer].clientWidth:document.layers[layer].document.width;
  } 
  else setTimeout("getWidth("+layer+")", re_try_time);       
  return data;
}         


function resizeBy(layer, x, y) {          
  if (NS && document[layer] || IE && document.all[layer]) // html code has been read ...
   if (NS) { // ?
       document[layer].document.width = document[layer].document.width+x; 
       document[layer].document.height  = document[layer].document.height+y; 
   }    
   else {      // ok
       document.all[layer].style.width = parseInt(document.all[layer].style.width)+x;
       document.all[layer].style.height  = parseInt(document.all[layer].style.height)+y;
   }   
  else setTimeout("resizeBy("+layer+","+x+","+y+")", re_try_time);       
}       

function resizeTo(layer, x, y) {          
  if (NS && document[layer] || IE && document.all[layer]) // html code has been read ...
   if (NS) { // ?
       document[layer].document.width = x; 
       document[layer].document.height  = y; 
   }    
   else {      // ok
       document.all[layer].style.width = x;
       document.all[layer].style.height  = y;
   }   
  else setTimeout("resizeTo("+layer+","+x+","+y+")", re_try_time);       
}   


function getClipX0(layer) {                                                              
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].clip.left;        
   else  data = document.all[layer].style.clip.match(/\d+/g)[3];
  } 
  else setTimeout("getClipX0("+layer+")", re_try_time);       
  return parseInt(data);
}     

function getClipY0(layer) {                                                              
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].clip.top;        
   else  data = document.all[layer].style.clip.match(/\d+/g)[0];
  } 
  else setTimeout("getClipY0("+layer+")", re_try_time);       
  return parseInt(data);
}     

function getClipX1(layer) {                                                              
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].clip.right;        
   else  data = document.all[layer].style.clip.match(/\d+/g)[1];
  } 
  else setTimeout("getClipX1("+layer+")", re_try_time);       
  return parseInt(data);
}                 

function getClipY1(layer) {                                                              
  var data=0;      
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) data = document[layer].clip.bottom;        
   else  data = document.all[layer].style.clip.match(/\d+/g)[2];
  } 
  else setTimeout("getClipY1("+layer+")", re_try_time);       
  return parseInt(data); 
}     

function setClip(layer, x0, y0, x1, y1) {                                                              
  if (NS && document[layer] || IE && document.all[layer]) { // html code has been read ... 
   if (NS) {
     document[layer].clip.left   = x0;   
     document[layer].clip.top    = y0;   
     document[layer].clip.right  = x1;     
     document[layer].clip.bottom = y1;  
   }      
   else {
    document.all[layer].style.clip="Rect("+y0+"px "+x1+"px "+y1+"px "+x0+"px)"
   }
   if (MAC && IE) {
     moveBy(layer, -1, -1); // to prevent MAC & IE sleeping !!!
     moveBy(layer, 1, 1);   
   }  
  } 
  else setTimeout("setClip("+layer+","+x0+","+y0+","+x1+","+y1+")", re_try_time);       
}     


function scrollH(layer, offset) {
  if (NS && document[layer] || IE && document.all[layer]) {// html code has been read ...        
    moveBy(layer, -offset, 0);      
    setClip(layer, (getClipX0(layer)+offset), getClipY0(layer), (getClipX1(layer)+offset), getClipY1(layer)); 
     if (main_tout!=null) clearTimeout(main_tout);
  } 
  else main_tout=setTimeout("scrollH("+layer+","+offset+")", re_try_time); 
}                                                  

function scrollV(layer, offset) {
  if (NS && document[layer] || IE && document.all[layer]) {// html code has been read ...        
    moveBy(layer, 0, -offset);      
    setClip(layer, getClipX0(layer), getClipY0(layer)+offset, getClipX1(layer), getClipY1(layer)+offset); 
    if (main_tout!=null) clearTimeout(main_tout);
  } 
  else main_tout=setTimeout("scrollV("+layer+","+offset+")", re_try_time); 
}                                                  


function setBody(layer, body) {
  if (NS && document[layer] || IE && document.all[layer]) // html code has been read ...        
   if (NS) {             
   	document[layer].document.write(body);
     	document[layer].document.close();
   }
   else {
     	document.all[layer].innerHTML = body;
   }
  else setTimeout("setBody("+layer+","+body+")", re_try_time); 
}                                                  

function setZindex(layer, index) {
;//
}                                                  

/* Scroll Object */

function Scroll(layer, delay, offset, loop) {
	
    this.layer = layer;
    this.loop = (loop)?true:false;
    
    this.x0 = getClipX0(layer);     // position and dimension...
    this.y0 = getClipY0(layer);
    this.x1 = getClipX1(layer);
    this.y1 = getClipY1(layer);
    this.X0 = getClipX0(this.layer);
    this.X1 = getClipX1(this.layer);
    this.Y0 = getClipY0(this.layer);
    this.Y1 = getClipY1(this.layer);
    this.x = getLeft(layer);
    this.y = getTop(layer);
    this.dimx = (IE)?document.all[layer].clientWidth:document.layers[layer].document.width;
    this.dimy = (IE)?document.all[layer].clientHeight:document.layers[layer].document.height;
    //alert(this.dimx+" "+this.dimy);
    
    this.delay = delay;           // scrolling parameters
    this.offset = offset; 
    
    this.direction = null;
    this.scroll_type = null;
    this.timeout_id = null; 
    
    this.scrolling = scrolling;   // methods
    this.start = start; 	
    this.stop = stop; 	
    this.reset = reset; 	
    this.needed=(this.dimy>Math.abs(this.Y1-this.Y0) || this.dimx>Math.abs(this.X1-this.X0));    
    this.name = layer + "Var"; // in questo modo posso sapere con che nome e' stato istanziato l'oggetto per poterne richiamare i metodi con setInterval o setTimeout
    eval(this.name + " = this"); //      esempio:   this.timer = setInterval(this.name + ".step()", interval);
}	                                 


    
function start(dir, delay) {

/* direction: 0 ^      -
              1 >      +
              2 v      +
              3 <      -
*/  
              
 // alert(this.dimy+" "+this.Y1);
 if(this.dimy>this.Y1 || this.dimx>this.X1) {
  if(delay) this.delay=delay; // set new delay if specified
  this.direction = dir;
  this.offset = (dir==1||dir==2)?Math.abs(this.offset):-Math.abs(this.offset); // set offset sign according to direction (RIGHT, DOWN : positive; UP, LEFT : negative)
  this.scroll_type = (dir==0||dir==2)?'scrollV':'scrollH'; // set function name according to direction (UP/DOWN, LEFT/RIGHT)
  var doscroll = true;
  
  this.x0 = getClipX0(this.layer);
  this.x1 = getClipX1(this.layer);
  this.y0 = getClipY0(this.layer);
  this.y1 = getClipY1(this.layer);

  switch (this.direction) {
  	case 0:  
  	         if (this.y0 + this.offset <= 0) doscroll=false;
                 break;
  	case 2:  
  	         if (this.y1 + this.offset >= this.dimy) doscroll=false;
  	         break;
  	case 1:  
  	         if (this.x1 + this.offset  >= this.dimx) doscroll=false;
  	         break;
  	case 3:  
  	         if (this.x0 + this.offset  <= 0) doscroll=false;
  	         break;                           
  }	
  
  if (doscroll) {              
   if (this.scroll_type=='scrollV') scrollV(this.layer,this.offset); // perform the scroll!
   else scrollH(this.layer,this.offset); 
   this.timeout_id=setTimeout(this.name+".scrolling()",this.delay); // scrolling!
  }
  else {              
     switch (this.direction) {
  	case 0:  scrollV(this.layer, -this.y0);
                 break;
                 
  	case 2:  scrollV(this.layer, (this.dimy-this.y1));
                 break;
                 
  	case 1:  scrollH(this.layer, (this.dimx-this.x1));
  	         break;                                   
  	         
  	case 3:  scrollH(this.layer, -this.x0);
  	         break;                           
  	
        }  
     if (this.loop) { 
          moveTo(this.layer,this.x,this.y);   	
          setClip(this.layer, this.X0, this.Y0, this.X1, this.Y1); 
          this.timeout_id=setTimeout(this.name+".scrolling()",this.delay); // scrolling!
     }  
    	
  }
 }
} 

function scrolling () {
  var doscroll = true;	

  this.x0 = getClipX0(this.layer);
  this.x1 = getClipX1(this.layer);
  this.y0 = getClipY0(this.layer);
  this.y1 = getClipY1(this.layer);
  
  switch (this.direction) {
  	case 0:  
  	         if (this.y0 + this.offset <= 0) doscroll=false;
                 break;
  	case 2:  
  	         if (this.y1 + this.offset >= this.dimy) doscroll=false;
  	         break;
  	case 1:  
  	         if (this.x1 + this.offset  >= this.dimx) doscroll=false;
  	         break;
  	case 3:  
  	         if (this.x0 + this.offset  <= 0) doscroll=false;
  	         break;                           
  }	
  
  if (doscroll) {              
   if (this.scroll_type=='scrollV') scrollV(this.layer,this.offset); // perform the scroll!
   else scrollH(this.layer,this.offset); 
   this.timeout_id=setTimeout(this.name+".scrolling()",this.delay); // scrolling!
  }
  else {    
     switch (this.direction) {
  	case 0:  scrollV(this.layer, -this.y0);
                 break;
                 
  	case 2:  scrollV(this.layer, (this.dimy-this.y1));
                 break;
                 
  	case 1:  scrollH(this.layer, (this.dimx-this.x1));
  	         break;                                   
  	         
  	case 3:  scrollH(this.layer, -this.x0);
  	         break;                           
    }
    if (this.loop) { 
     	  moveTo(this.layer,this.x,this.y);   	
          setClip(this.layer, this.X0, this.Y0, this.X1, this.Y1); 
     	  this.timeout_id=setTimeout(this.name+".scrolling()",this.delay); // scrolling!
     }  	
  }

}	

function stop() {
  if (this.timeout_id!=null) {
     clearTimeout(this.timeout_id);
     this.timeout_id=null;
     setTimeout("clearTimeout("+this.name+".timeout_id)", 10);
  }	
}	           

function reset() {
   this.stop();
   moveTo(this.layer,this.x,this.y);   	
   setClip(this.layer, this.X0, this.Y0, this.X1, this.Y1); 
}	
                         