//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//
//extended by James Wanga 2006 www.thedigitalself.com
//-added maximize, restore and close window functions
//-converted the window to an object for easier instantiation
//
//TODO: Have windows move to top on focus anywhere.
//TODO: browser checks
//TODO: Add resize capability
//TODO: Add minimize function that floats just a bar and sticks a bar to any edge
//*****************************************************************************

// Determine browser and version.


function dhtmlWindow(controlId, width, height, text){
    this.controlId = controlId;
    this.id = controlId;// + "_Admin";
    
    //holds the inner html of certain window type
    this.text = text;
    
    //window dimensions
    this.width = width;
    this.height = height;
    this.maximized = false;
   
    this.render();
    
} 

//render the appropriate window type
dhtmlWindow.prototype.render = function(){
        document.write("<div id=\"" + this.id + "\" class=\"window\" style=\"width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"bar\"><a href=\"javascript:" + this.id + ".closeWindow();\">close</a></div><div class=\"content\"><div class=\"content_bak\" style=\"width:" + this.width + "; height:" + this.height + ";\"></div><div class=\"content_area\"style=\"width:" + this.width + "; height:" + this.height + ";\">" + this.text + "</div></div></div>");
}

dhtmlWindow.prototype.dragStart = function(event) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (this.id)
    dragObj.elNode = document.getElementById(this.id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  
  //if the object is a tooltip have it snam to the cursor position
  if(this.type == 'tooltip'){
    dragObj.elStartLeft  = x + 3;
    dragObj.elStartTop   = y + 3;
  }else{
    dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
    dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
  }
  
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {    
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup", dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo, true);
    document.addEventListener("mouseup", dragStop, true);
    event.preventDefault();
  }
}

//close the window
dhtmlWindow.prototype.closeWindow = function(){
    document.getElementById(this.id).style.display = "none";
}

//open the window
dhtmlWindow.prototype.openWindow = function(){
    document.getElementById(this.id).style.display = "";
}

//maximize/restore the window
dhtmlWindow.prototype.maximizeWindow = function(){
    

    if(!this.maximized){
        //temporary dimensions for image manipulation
        this.tempX = document.getElementById(this.id).style.left;
        this.tempY = document.getElementById(this.id).style.top;
        this.tempWidth = document.getElementById(this.id).style.width;
        this.tempHeight = document.getElementById(this.id).style.height
        
        document.getElementById(this.id).style.top = "0px";
        document.getElementById(this.id).style.left = "0px";
        document.getElementById(this.id).style.width = browser.isNS? window.innerWidth-20 + "px" : iecompattest().clientWidth+"px";
        document.getElementById(this.id).style.height = browser.isNS? window.innerHeight-20 +"px" : iecompattest().clientHeight+"px";
        
        document.getElementById(this.id + "maximize").innerHTML = "-";
        
        this.maximized = true;
        
    }else{
        document.getElementById(this.id).style.top = this.tempY;
        document.getElementById(this.id).style.left = this.tempX;
        document.getElementById(this.id).style.width = this.tempWidth;
        document.getElementById(this.id).style.height = this.tempHeight;
        
        
        document.getElementById(this.id + "maximize").innerHTML = "+";
        
        this.maximized = false;
    }
}
