/*
  # dynamic_layer.js                        Version 4.1
  # Copyright (c) 2000-2002 Luca Borrione   
  # Created 06 July 2000                    Last Modified 06 November 2002
  # _________________
  # COPYRIGHT NOTICE:
  # Copyright (c) 2000-2002 Luca Borrione  All Rights Reserved.
  # Permission granted to use and modify free of charge this script so long as this
  # copyright notice above is maintained, modifications are documented,
  # and credit is given for any use of the script.
  # By using this code you agree to indemnify Luca Borrione from any liability that
  # might arise from its use.
  # Selling the code contained in this script without prior written consent is
  # expressly forbidden.
  # Obtain permission before redistributing this script over the Internet or
  # in any other medium.
  # In all cases copyright and header must remain intact.
  #
  # ____________
  # SCRIPT CODE:
*/

// REQUIRED: browser_check.js

var d = document;
debugging = (document.domain.match (/192.168.2.199/) || document.domain.match(/192.168.2.145/));

var curleft = 0;
var curtop = 0;

function layer (Id)
{
  if (!d.layer || !d.layer[Id]) init_layer (Id);
  if (d.layer[Id]) return d.layer[Id];
  else return null;
}

function init_layer (Id)
{
  if (!d.layer) d.layer = [];
  d.layer[Id] = new Dynamic_Layer (Id);
  if (!d.layer[Id].object)
  {
    /*
    if (debugging)
      alert ("DYNAMIC LAYER Error:\n"+
             "Impossibile risalire al layer "+ Id +".");
    */
    delete d.layer[Id];
  }
}

function Dynamic_Layer (Id)
{
  this.id = Id;
  this.object = get_object (Id);
  if (!this.object) return;

  this.css    = (is.nn4) ? this.object : this.object.style;
  this.x      = get_absolute_x (this.object);
  this.y      = get_absolute_y (this.object);
  this.left   = get_relative_x (this.object);
  this.top    = get_relative_y (this.object);
  this.width  = get_width (this.object);
  this.height = get_height (this.object);
  this.zIndex = this.css.zIndex;
  
  this.show     = layer_show;
  this.hide     = layer_hide;
  this.resizeTo = layer_resizeTo;
  this.moveBy   = layer_moveBy;
  this.moveTo   = layer_moveTo;
  this.write    = layer_write;
  this.zOrder   = layer_zOrder;
  this.align    = layer_align;
  this.reset    = layer_reset;
  this.getCSS   = layer_getCSS;
  
  this.unset   = layer_unset;
  this.none    = layer_none;

  /* __________
     FUNCTIONS: */
     
  function layer_unset()
  {
    delete d.layer[this.id];
  }   
     
  function layer_show()
  {
    /*
    if( is.ie || is.nn>=5 ){ this.object.style.visibility="visible"; }
    else if( is.nn ){ this.object.visibility="show"; }
    */
    if( is.ie || is.nn>=5 ){ this.object.style.visibility="inherit"; }
    else if( is.nn ){ this.object.visibility="inherit"; }
  }

  function layer_none()
  {
    if( is.ie || is.nn>=5 ){ this.object.style.visibility="none"; }
    else if( is.nn ){ this.object.visibility="none"; }
  }
  
  function layer_hide()
  {
    if( is.ie || is.nn>=5 ){ this.object.style.visibility="hidden"; }
    else if( is.nn ){ this.object.visibility="hide"; }
  }
  

  function layer_moveTo (temp_x, temp_y, Id_ref)
  {
    if (Id_ref)
    {
      var ref = get_object (Id_ref);
      if (!ref)
      {
        if (debugging)
          alert ("DYNAMIC LAYER Error:\n"+
                 "Impossibile risalire all'elemento di riferimento "+ Id_ref +".");
        return;
      }
	  refId_x = get_absolute_x (ref);
	  refId_y = get_absolute_y (ref);
      temp_x += refId_x;
      temp_y += refId_y;
    }
    relative_x = temp_x-this.x;
    relative_y = temp_y-this.y;

    this.moveBy (relative_x, relative_y);
  }

  
  function layer_moveBy( x,y )
  {
    this.x += x;
    this.y += y;
    this.left += x;
    this.top += y;
    if (is.nn)
    {
      this.css.left = this.left;
      this.css.top  = this.top;
    }
    else // DOM2
    {
      this.css.pixelLeft = this.left;
      this.css.pixelTop  = this.top;
    }
  }

  function layer_resizeTo( Width, Height )
  {
    // alert( this.width +"||"+ this.height );
    // return;
    if( Width ){ this.width = Width; }
    if( Height ){ this.height = Height; }
    if( is.ie )
    {
      with( this )
      {
        //Show.properties( css );
        css.pixelWidth = Width;
        css.pixelHeight = Height;
        css.clip = "rect(0,"+ Width +","+ Height +",0)";
        //Show.properties( css );
      }
    }
    else if( is.nn )
    {
      if( is.nn>=5 )
      {
        with( this )
        {
          css.width = Width;
          css.height = Height;
          css.clip = "rect(0,"+ Width +","+ Height +",0)";
        }
      }
      else
      {
        with( this )
        {
          css.clip.width = width;
          css.clip.height = height;
        }
      }
    }
  }
  
  function layer_zOrder (index,ref)
  {
    /*
      ##/
      #
      # Uso: 
      # 1: layer[id1].zOrder(1)
      #    Imposta ad 1 lo z-index del layer id1.
      #
      # 2: layer[id1].zOrder("+/-1)
      #    Aumenta o diminuisce di 1 lo z-index del layer id1.
      #
      # 3: layer[id1].zOrder("+/-1", id2)
      #    Imposta lo z-index del layer id1 a + o - 1 rispetto allo z-index del layer id2.
    ##\
    */
    /*
    if (ref) { index = eval(d.layer[ref].zIndex+index); }
    var initial = index.toString().charAt(0);
    if (initial=="+" || initial=="-") { index = eval(this.zIndex+index); }
    */
    this.zIndex = index;
    this.css.zIndex = index;
  }
   
  function layer_write( What )
  {
    if( is.ie || is.nn>=5 ){ this.object.innerHTML = What; }
    else if( is.nn && is.nn<5 )
    {
      this.css.document.write( What );
      this.css.document.close();
      // Show.properties( this.css );
    }
  }
  
  function layer_reset()
  {
    this.x      = get_absolute_x( this.object );
    this.y      = get_absolute_y( this.object );
    this.left   = get_relative_x( this.object );
    this.top    = get_relative_y( this.object );
    this.width  = get_width( this.object );
    this.height = get_height( this.object );
  }
  
  function layer_align( Align )
  {
    var x;
    if (Align.toLowerCase() == "center")
    {
      var innerWidth = (is.ie) ? document.body.clientWidth : window.innerWidth;
      x = Math.round( (innerWidth - this.width)/2 );
      this.moveTo( x, 0 );
    }
  }
  
  function layer_getCSS( Prop )
  {
    if( is.nn4 )
        return( this.object[Prop] );
    else if( is.nn )
      return( document.defaultView.getComputedStyle(document.getElementById(this.id),null).getPropertyValue(Prop) );
    else if( is.ie4 )
    {
      if( this.object.style[Prop] ){ return( this.object.style[Prop] ); }
      var styles = this.object.document.styleSheets;
      var style;
      var ret = new Array();
      var cssText = "";
      reg = new Array();
      reg.id        = (this.object.id)        ? new RegExp( this.object.id+"{" )           : null;
      reg.className = (this.object.className) ? new RegExp( this.object.className+"{" )    : null;
      reg.tagName   = (this.object.tagName)   ? new RegExp( this.object.tagName+"{" )      : null;
      for( var s=0; s<styles.length; s++ )
      {
        style = styles[s];
        cssText = style.cssText.replace( /\s*/g, "" );
        if( reg.id && reg.id.test(cssText) )
          ret.id = extract_CSS( cssText, this.object.id, Prop );
        if( reg.className && reg.className.test(cssText) )
          ret.className = extract_CSS( cssText, this.object.className, Prop );
        if( reg.tagName && reg.tagName.test(cssText) )
          ret.tagName = extract_CSS( cssText, this.object.tagName, Prop );
      }
      if( ret.id ){ return( ret.id ); }
      if( ret.className ){ return( ret.className ); }
      if( ret.tagName ){ return( tagName ); }
      return( null );
    }
    else if( is.ie )
      return( this.object.currentStyle[Prop] );
  }
  
  function extract_CSS( CSSText, Selector, Prop )
  {
    var value;
    var rules = CSSText.substring( CSSText.indexOf(Selector+"{")+Selector.length+1 );
    rules = rules.substring( 0,rules.indexOf("}") );
    rules = rules.toUpperCase();
    if( rules.indexOf(Prop.toUpperCase())!=-1 )
    {
      value = rules.substring( rules.indexOf(Prop.toUpperCase())+Prop.length+1 ).toLowerCase();
      if( value.indexOf(";")!=-1 )
        value = value.substring( 0,value.indexOf(";") ).toLowerCase();
    }
    return( value );
  }
    
}


  /* __________________
     OBJECTS SEARCHING: */  
  function get_object (Id)
  {
    curleft = 0;
    curtop = 0;
    if (document.getElementById)
      return document.getElementById(Id);
    else if (document.all)
      return document.all[Id];
    else if (document.layers)
      return nn4_scanLayers (Id);
  }
  
  function nn4_scanLayers (Id, Layer)
  {
    var layers = (Layer) ? Layer.document.layers : document.layers;
    if (layers[Id]) return layers[Id]; // Added
    var layer, r, i;
    for (i=0; i<layers.length; i++)
    {
      layer = layers[i];
      if (layer.document.layers[Id])
        return (layer.document.layers[Id]);
      else if (layer.document.images[Id])
        return (layer.document.images[Id]);
      else if (layer.document.anchors[Id])
        return (layer.document.anchors[Id]);
      else if (layer.document.layers.length>0 )
        r = nn4_scanLayers (Id, layer);
      curleft += layer.pageX;
      curtop += layer.pageY;
      if (r) return r;
    }
    return r;
  }
  
  /* ___________________
     GETTING PROPERTIES: */
     
  function get_absolute_x (obj)
  {
  	if (document.getElementById || document.all)
  	{
  		while (obj.offsetParent)
  		{
  			curleft += obj.offsetLeft;
  			obj = obj.offsetParent;
  		}
  	}
    else if (is.nn4)
    {
      if (typeof obj.pageX == 'undefined') curleft += obj.x;
      else curleft = obj.pageX;
    }
    return curleft;    
  }
  
  function get_absolute_y (obj)
  {
  	if (document.getElementById || document.all)
  	{
  		while (obj.offsetParent)
  		{
  			curtop += obj.offsetTop;
  			obj = obj.offsetParent;
  		}
  	}
    else if (is.nn4)
    {
      if (typeof obj.pageY == 'undefined') curtop += obj.y;
      else curtop = obj.pageY;
    }
    return curtop;
    
  }
    
  function get_relative_x( Object )
  {
    if (is.nn4){ return( Object.left ); }
    if (is.ie4 && Object.style.clipLeft){ return( Object.style.clipLeft ); }
    else // DOM
      return( Object.offsetLeft );
  }
  
  function get_relative_y( Object )
  {
    if( is.nn && is.nn<5 ){ return( Object.top ); }
    if( is.ie4 && Object.style.clipLeft ){ return( Object.style.clipTop ); }
    else{ return( Object.offsetTop ); }
    return( null );
  }
  
  function get_width( obj )
  {
    if (is.ie4 && is.win)
    {
      if (obj.style.pixelWidth){ return(obj.style.pixelWidth); }
      else{ return(obj.offsetWidth); }
    }
    else if (is.ie4){ return(obj.style.pixelWidth); }
    else if (is.ie >= 5 && is.win)
    {
      var clipRight = obj.currentStyle.clipRight;
      clipRight = (clipRight!="auto") ? clipRight.substring(0,clipRight.lastIndexOf("px")) : null;
      var offsetWidth = obj.offsetWidth;
      if (clipRight!=null){ return( Math.min( Math.round(clipRight), offsetWidth) ); }
      else{ return(offsetWidth); }
    }
    else if (is.nn4){ return( obj.clip.width ); }
    else // DOM
      return( obj.offsetWidth );
  }
  
  function get_height( obj )
  {
    if (is.ie4 && is.win)
    {
      if (obj.style.pixelHeight){ return(obj.style.pixelHeight); }
      else{ return(obj.offsetHeight); }
    }
    else if (is.ie4){ return(obj.style.pixelHeight); }
    else if (is.ie >= 5 && is.win)
    {
      var clipBottom = obj.currentStyle.clipBottom;
      clipBottom = (clipBottom!="auto") ? clipBottom.substring(0,clipBottom.lastIndexOf("px")) : null;
      var offsetHeight = obj.offsetHeight;
      if (clipBottom!=null){ return( Math.min( Math.round(clipBottom), offsetHeight) ); }
      else{ return(offsetHeight); }
    }
    else if (is.nn4){ return( obj.clip.height ); }
    else // DOM
      return( obj.offsetHeight );
  }