//
//    Copyright (c) 2004, efigence.com. All rights reserved.
//

// =====================================================================================
// 
function Browser() {
  var b=navigator.appName;
  if (b=="Netscape") {
    this.b="ns";
  }
  else if ((b=="Opera") || (navigator.userAgent.indexOf("Opera")>0)) this.b = "opera";
  else if (b=="Microsoft Internet Explorer") {
    this.b="ie";
  }
  if (!b) alert('Unidentified browser./nThis browser is not supported,');
  this.version=navigator.appVersion;
  this.v=parseInt(this.version);
  this.ns=(this.b=="ns" && this.v>=4);
  this.ns4=(this.b=="ns" && this.v==4);
  this.ns6=(this.b=="ns" && this.v==5);
  this.ie=(this.b=="ie" && this.v>=4);
  this.ie4=(this.version.indexOf('MSIE 4')>0);
  this.ie5=(this.version.indexOf('MSIE 5')>0);
  this.ie55=(this.version.indexOf('MSIE 5.5')>0);
  this.ie6=(this.version.indexOf('MSIE 6')>0);
  this.opera=(this.b=="opera");
  this.dom=(document.createElement && document.appendChild && document.getElementsByTagName)?true:false;
  this.def=(this.ie||this.dom);
  this.mozilla = navigator.userAgent.indexOf('Mozilla') != -1 && parseInt(navigator.appVersion.substring(0,1)) >= 5;
  var ua=navigator.userAgent.toLowerCase();
  if (ua.indexOf("win")>-1) this.platform="win32";
  else if (ua.indexOf("mac")>-1) this.platform="mac";
  else this.platform="other";
}
is = new Browser();



function CustomLayer(layerId) {
  
  if (layerId != null) {
    if (typeof(layerId) == "object") {
      this.node = layerId;
    } else {
      this.node = document.getElementById(layerId);
    }
    if (!this.node) {
      alert("can not find div with id = '" + layerId + "'");
    }
    this.node.efi_customLayer = this;
    this.x = 0;
    this.y = 0;
  }
  
  CustomLayer.prototype.setClip = function(x,y,x1,y1){
    if (is.ns) {
      this.node.style.clip = "rect("+y+"px "+x1+"px "+y1+"px "+x+"px)";
    } else if (is.ie) {
      this.node.style.clip = "rect("+y+"px "+x1+"px "+y1+"px "+x+"px)";
    }
  }
  // returns apropriate clipping value
  CustomLayer.prototype.getClip = function(which) {
    var clipv;
    if (this.node.style.clip == null || this.node.style.clip == "" ) {
      alert( "Layer " + this.name + " nie ma ustawionego clippingu" );
      return;
    }
    if (is.ns) {
      clipv = this.node.style.clip.split("rect(")[1].split("px)")[0].split("px, ");
    } else if (is.ie) {
      clipv = this.node.style.clip.split("rect(")[1].split(")")[0].split("px");
    }
    if (which=="t") {
      if (clipv[0]) {
        return new Number(clipv[0]);
      } else {
        return null;
      }
    }
    if (which=="r") {
      if (clipv[1]) {
        return new Number(clipv[1]);
      } else {
        return null;
      }
    }
    if (which=="b") {
      if (clipv[2]) {
        return new Number(clipv[2]);
      } else {
        return null;
      }
    }
    if (which=="l") {
      if (clipv[3]) {
        return new Number(clipv[3]);
      } else {
        return null;
      }
    }
  }
  CustomLayer.prototype.setDragArea = function(x, y, w, h){
    this.dragX = x;
    this.dragY = y;
    this.dragW = w;
    this.dragH = h;
  }

  CustomLayer.prototype.isMouseOverDraggable = function(x, y){
    if (this.dragX == null || this.dragX == NaN) {
      this.setDragArea(this.getClip("l"), this.getClip("t"), this.getClip("r"), this.getClip("b"));
    }  
    efi_debug("x: " + x + " y: " + y + " -- getX: " + this.getX() + ", dragX: " + this.dragX + ", getY(): " + this.getY() + ", dragY: " + this.dragY + " | dragW: " + this.dragW + ", dragH: " + this.dragH);
    
    if (x >= this.getX() + this.dragX
     && x <= this.getX() + this.dragX + this.dragW
     && y >= this.getY() + this.dragY
     && y <= this.getY() + this.dragY + this.dragH) {
      return true;
    }
  }

  CustomLayer.prototype.move = function(x, y) {
    this.x = x;
    this.y = y;
    this.node.style["left"] = x + "px";
    this.node.style["top"] = y + "px";
  }
  
  CustomLayer.prototype.setX = function(x) {
    this.x = x;
    this.move(this.x, this.y);
  }
  
  CustomLayer.prototype.getX = function(){
    var el = this.node;
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null) {
      ol += el.offsetLeft;
    }
    return ol;
  }
  CustomLayer.prototype.setY = function(y) {
    this.y = y;
    this.move(this.x, this.y);
  }
  CustomLayer.prototype.getY = function() {
    var el = this.node;
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null) {
      ot += el.offsetTop;
    }
    return ot;
  }
  
  CustomLayer.prototype.setW = function(w) {
    this.node.style.width = w;
  }
  
  CustomLayer.prototype.getW = function(){
    return this.node.offsetWidth;
  }
  
  CustomLayer.prototype.setH = function(h) {
    this.node.style.height = h;
  }  
  
  CustomLayer.prototype.getH = function(){
    return this.node.offsetHeight;
  }
  
  CustomLayer.prototype.show = function() {
    this.node.style.visibility = "visible";
    return;
    if (is.ns4) {
      this.node.style.visibility = "show";
    } else {
      this.node.visibility = "visible";
    }
  }
  CustomLayer.prototype.hide = function() {
    this.node.style.visibility = "hidden";
    return;    
    if (is.ns4) {
      this.node.style.visibility = "hide";
    } else {
      this.node.visibility = "hidden";
    }
  }
  CustomLayer.prototype.setContent = function(content) {
    if (content instanceof Array) {
      this.node.innerHTML = content.join("");
    } else {
      this.node.innerHTML = content;
    }
  }

  CustomLayer.prototype.setDraggable = function() {
    if (this.draggable) {
      return;
    }
    this.draggable = true;
    var x = this.getX();
    if (x == null || x == NaN) {
      alert("unknown position of the div: '" + this.node.id + "'");
      return;
    } 
    var clipl = this.getClip("l");
    if (clipl == null || clipl == NaN) {
      alert("unknown getClip('l') of the div: '" + this.node.id + "'");
      return;
    } 
    if (!document.efi_draggable) {
      document.efi_draggable = new Array();
      
      document.onmousedown = function(e) {
        var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft;
        var y = (is.ns)? e.pageY : event.y+document.body.scrollTop;
        for(var indx in document.efi_draggable) {
          var customLayer = document.efi_draggable[indx];
          efi_debug("checking dragging, customLayer: " + customLayer);
          if (customLayer.isMouseOverDraggable(x, y)) {
            efi_debug("dragging ON");
            document.efi_currentDragging = customLayer;
            document.efi_currentDragging_mouseOffsetX = x - customLayer.getX()
            document.efi_currentDragging_mouseOffsetY = y - customLayer.getY()
            return false;
          }
        }
        return true;  
      }
      document.onmousemove = function(e) {
        if (document.efi_currentDragging == null) return false;
        var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
        var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
        var customLayer = document.efi_currentDragging;
        customLayer.move(x - document.efi_currentDragging_mouseOffsetX, y - document.efi_currentDragging_mouseOffsetY);
        return false;  
      }
      
      document.onmouseup = function(e) {
        var customLayer = (is.ns) ? e.target.efi_customLayer : event.srcElement.efi_customLayer;
        efi_debug("dragging OFF");
        document.efi_currentDragging = null;
      }
    }
    document.efi_draggable.push(this);
  }
}


function efi_debug( txt ) {
  return;
  var towrite = txt;
  if (towrite instanceof Array) {
    towrite = efi_array2string(txt);
  }
  if (towrite instanceof Object) {
    towrite = efi_object2string(txt);
  }
  document.system.debug.value = document.system.debug.value + "\n> " + towrite;
  return null;
}

function efi_initDebug() {
  return;
  document.write("<form name=\"system\"><textarea name=\"debug\" rows=\"10\" cols=\"100\"></textarea></form>");
}

function efi_array2string(txt) {
  towrite = "Array[";
  for (var i in txt) {
    if (i != 0) {
      towrite = towrite + ", ";
    }
    towrite = towrite + txt[i];
  }
  towrite = towrite + "]";
}


function efi_append2array(arr1, arr2) {
  for (var i in arr2) {
    arr1.push(arr2[i]);
  }
}

function efi_object2string(txt) {
  towrite = "Object[";
  var j = 0;
  for (var i in txt) {
    if (j++ != 0) {
      towrite = towrite + ", ";
    }
    towrite = i + "=" + towrite + txt[i];
  }
  towrite = towrite + "]";  
}

function efi_getOffsetLeft(el) {
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null)
    ol += el.offsetLeft;
  return ol;
}

function efi_getOffsetTop(el) {
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null) {
    ot += el.offsetTop;
  }
  return ot;
}
