/**
 * CSS auxiliar per i TD background
 * Scriverlo diretamente nella pagina HTML che fa l'include!
 * (altrimente non funziona con Netscape)
 */

/*
DynLayer.CSS_RELATIVE="relativeStyle";
var d=window.document;
d.write("<STYLE TYPE='text/css'>");
d.write("."+DynLayer.CSS_RELATIVE+"{position:relative;}");
d.writeln("</STYLE>");
*/


/**
 * window, id, body: required
 * left, top, width: optional
 * TODO: se body e una IMG, no funziona con Netscape
 * SOLUZIONE: body="" e dopo layer.setBody('<IMG...>')
 * TODO: con explorer, los metodos getX y getY retornan 0 (relativo en vez de abs)
 *       Se soluciona haciendo un moveTo (con moveBy no funciona!)
 * Solucion: guardamos left y top (las funciones getX y getY no llaman a style)
 * IMPORTANTE: si debe chiamare dentro de <head> (non dentro del <body>)!
 */
function DynLayer(window, id, body, left, top, width, cssFile){
  var d;
  this.window=window;
  this.id    =id;
  this.body  =body;
  // _left y _top se usan ara evitar el error anteriormente mencionado
  this._left =left;
  this._top  =top;
  d=window.document;
  // style sheet del layer
  d.writeln("<STYLE TYPE='text/css'>");
  // ATENCION: sobreescribe los tags existentes en anteriores CSS
  if (cssFile){
    d.writeln("@import url('"+cssFile+"');");
  }
  d.write("."+id+"{position:absolute;");  // Nota: con '#id' non funziona (td backgrounds)
  if (left) 
    d.write("left:"+left+";");
  if (top) 
    d.write("top:"+top+";");
  if (width) 
    d.write("width:"+width+";");
  d.writeln("}");
  d.writeln("</STYLE>");
  return this;
}

DynLayer.prototype.show = function(){
  this.style.visibility=DynLayer._varShow;
}
DynLayer.prototype.hide = function(){
  this.style.visibility=DynLayer._varHide;
}
DynLayer.prototype.showHide = function(){
  if (this.style.visibility=DynLayer._varShow)
    this.style.visibility=DynLayer._varHide;
  else // visibility:hide
    this.style.visibility=DynLayer._varShow;
}
DynLayer.prototype.isVisible = function(){
  return (this.style.visibility==DynLayer._varShow);
}
DynLayer.prototype.setStackingOrder = function(order){
  this.style.zIndex=order;
}
DynLayer.prototype.getStackingOrder = function(){
  return this.style.zIndex;
}

// Guardamos contexto de sesion del layer
DynLayer.prototype.setContext = function(session){
  this._session=session;
}
// Retorna el contexto
DynLayer.prototype.getContext = function(){
  return this._session;
}

/////////////////NETSCAPE////////////////
if (navigator.appName.indexOf("Netscape")!=-1){
  // static attribs
  DynLayer._varShow = "show";
  DynLayer._varHide = "hide";
  // TODO: complete events!
  DynLayer._eventMasks = {
    onabort:Event.ABORT, onblur:Event.BLUR, onchange:Event.CHANGE,
    onclick:Event.CLICK, ondblclick:Event.DBCLICK, ondragdrop:Event.DRAGDROP,
    onmouseover:Event.MOUSEOVER, onmouseout:Event.MOUSEOUT,
    onload:Event.LOAD, onunload:Event.UNLOAD, onresize:Event.RESIZE
  }
  // Si autoscrive nel document
  DynLayer.prototype.output = function(){
    var d=this.window.document;
    d.writeln("<DIV ID='"+this.id+"' CLASS='"+this.id+"'>");
    d.writeln(this.body);
    d.writeln("</DIV>");
    // Referenza element e style
    this.element=this.window.document[this.id];
    this.style  =this.window.document[this.id];
  }
  // specifics methods (differents with Explorer)
  DynLayer.prototype.getElement = function(layer){
    return layer.window.document[layer.id];
  }
  DynLayer.prototype.getStyle = function(layer){
    return layer.window.document[layer.id];
  }
  DynLayer.prototype.setBody = function(){
    var body="";
    for(var i=0; i<arguments.length; i++){
      body+=arguments[i]+"\n";
    }
    this.element.document.open();
    this.element.document.write(body);
    this.element.document.close();
  }
  DynLayer.prototype.setBgColor = function(color){
    this.style.bgColor=color;
  }
  DynLayer.prototype.setBgImage = function(img){
    this.style.background.src=img;
  }
  DynLayer.prototype.moveTo = function(x, y){
    this.style.moveTo(x, y);
  }
  DynLayer.prototype.moveBy = function(x, y){
    this.style.moveBy(x, y);
  }
  DynLayer.prototype.getX = function(){
    return this.style.left;
  }
  DynLayer.prototype.getY = function(){
    return this.style.top;
  }
  // Events
  // Si captureEvents interceptamos todos los eventos de asociados a eventName
  // Ej: si el layer contiene una tabla y captureEvents es cierto, para cada 
  // mouseout de los diferentes TDs se dispararia el handler
  DynLayer.prototype.addEventHandler = function(eventName, handler, captureEvents){
    if (captureEvents)
      this.element.captureEvents(DynLayer._eventMasks[eventName]);
    var layer=this;
    this.element[eventName]=function(event) {
      return handler(layer, event.type, event.x, event.y, 
                     event.which, event.which, 
                     ((event.modifiers & Event.SHIFT_MASK) != 0), 
                     ((event.modifiers & Event.CTRL_MASK)  != 0), 
                     ((event.modifiers & Event.ALT_MASK)   != 0));
    }
  }
  // Si captureEvents se liberan todos los eventos asociados a eventName
  DynLayer.prototype.removeEventHandler = function(eventName, captureEvents){
    if (captureEvents)
      this.element.releaseEvents(DynLayer._eventMasks[eventName]);
    delete this.element[eventName];
  }
  // Simula l'evento hover (non supportato per Nescape)
  DynLayer.prototype.hover = function(hoverColor) {
    var link, str;
    // Create a new, child layer by copying the link in this one and changing the background and text colors.
    //link = this.document.links[0];
    link = this.element.links[0];
    str = '<a href="' + link.href + '"';
    if (link.target)
      str += ' target= "' + link.target + '"'
    str += '><font color="' + hoverColor + '">' + link.text + '</font></a>';

    this.hoverLayer = new DynLayer(layer.clip.width, this);
    this.hoverLayer.document.open();
    this.hoverLayer.document.write(str);
    this.hoverLayer.document.close();
    this.hoverLayer.bgColor = hoverBgColor;

    // Show the hover layer initially.
    this.hoverLayer.show();
    // Set up event capturing on this layer to toggle visibilitly of the new layer.
    this.onmouseover = function (){ this.hoverLayer.show(); };
    this.onmouseout  = function (){ this.hoverLayer.hide(); };
  }
}
/////////////////MICROSOFT////////////////
if (navigator.appName.indexOf("Microsoft")!=-1){
  // static attribs
  DynLayer._varShow = "visible";
  DynLayer._varHide = "hidden";
  // specifics methods (differents with Netscape)
  // Si autoscrive nel document
  DynLayer.prototype.output = function(){
    var d=this.window.document;
    d.writeln("<DIV ID='"+this.id+"' CLASS='"+this.id+"'>");
    d.writeln(this.body);
    d.writeln("</DIV>");
    // Referenza element e style
    this.element=this.window.document.all[this.id];
    this.style  =this.element.style;
  }
  DynLayer.prototype.getElement = function(layer){
    return layer.window.document.all[layer.id];
  }
  DynLayer.prototype.getStyle = function(layer){
    return layer.window.document.all[layer.id].style;
  }
  DynLayer.prototype.setBody = function(){
    var body="";
    for(var i=0; i<arguments.length; i++){
      body+=arguments[i]+"\n";
    }
  this.element.innerHTML = body;
  }
  DynLayer.prototype.setBgColor = function(color){
    this.style.backgroundColor=color;
  }
  DynLayer.prototype.setBgImage = function(img){
    this.style.backgroundImage.src=img;
  }
  DynLayer.prototype.moveTo = function(x, y){
    this.style.pixelLeft = x;
    this.style.pixelTop  = y;
  }
  DynLayer.prototype.moveBy = function(x, y){
    this.style.pixelLeft += x;
    this.style.pixelTop  += y;
  }
  // TODO: error con pixelLeft
  DynLayer.prototype.getX = function(){
    return this._left;
    //return this.style.pixelLeft;
  }
  // TODO: error con pixelTop
  DynLayer.prototype.getY = function(){
    return this._top;
    //return this.style.pixelTop;
  }
  // Events
  // TODO: cancelBubble. captureEvents no tiene efecto. SIEMPRE se capturan 
  // todos los eventos asociados a eventName
  // El parametro se usa por compatibilidad con la signatura para Netscape
  DynLayer.prototype.addEventHandler = function(eventName, handler, captureEvents){
    var layer=this;
    this.element[eventName]=function() {
      var e = layer.window.event;
      e.cancelBubble=false;
      return handler(layer, e.type, e.x, e.y, 
                     e.button, e.keyCode,
                     e.shiftKey, e.ctrlKey, e.altKey);
    }
  }
  // captureEvents no tiene efecto. SIEMPRE se capturan 
  // todos los eventos asociados a eventName
  // El parametro se usa por compatibilidad con la signatura para Netscape
  DynLayer.prototype.removeEventHandler = function(eventName, captureEvents){
    delete this.element[eventName];
  }
  // L'evento hover e supportato diretamente per Explorer mediante CSS
  DynLayer.prototype.hover = function(hoverColor) {
    return true;
  }
}

