//*****************************************************************************
// 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 Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

iecompattest = function(){
    return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function adminWindow(controlId, controlType, width, height, text){
    this.controlId = controlId;
    this.id = controlId + "_Admin";
    
    this.type = controlType;
    
    //holds the inner html of certain window type
    this.text = text;
    
    //window dimensions
    this.width = width + "px";
    this.height = height + "px";
    this.maximized = false;
   
    this.render();
    
} 

//render the appropriate window type
adminWindow.prototype.render = function(){
    switch(this.type){
    case "text":
        document.write("<div id=\"" + this.id + "\" class=\"adminwindow\" style=\"left:400px; top:150px; width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"bar\" onmousedown=\"" + this.id + ".dragStart(event)\"><a id=\"" + this.id + "maximize\" href=\"javascript:" + this.id + ".maximizeWindow();\">+</a><a href=\"javascript:" + this.id + ".closeWindow();\">x</a></div><div class=\"content\"><textarea id=\"" + this.id + "textarea\" rows=\"20\" cols=\"10\"></textarea><div><br /><input class=\"button\" type=\"button\" value=\"Preview\" onclick=\"" + this.id + ".textPreview();\" /><input class=\"button\" type=\"button\" value=\"Commit\" onclick=\"" + this.id + ".textCommit(); " + this.id + ".textPreview(); " + this.id + ".closeWindow();alert('Changes Complete!')\" /></div></div></div>");
        //fill the text area with the corresponing text
        this.textAreaFill();
        
        break;
    case "tooltip":
         //the tooptip window accepts a text value
         document.write("<div id=\"" + this.id + "\" class=\"adminwindow\" style=\"left:400px; top:150px; width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"content\"><span>" + this.text + "</span></div></div>");
       
         break;
         
     case "image":
        //document.write("<div id=\"" + this.id + "\" class=\"adminwindow\" style=\"left:400px; top:150px; width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"bar\" onmousedown=\"" + this.id + ".dragStart(event)\"><a id=\"" + this.id + "maximize\" href=\"javascript:" + this.id + ".maximizeWindow();\">+</a><a href=\"javascript:" + this.id + ".closeWindow();\">x</a></div><div class=\"content\"><br /><br /><input id=\"" + this.id + "file\" type=\"file\" /><div><br /><input class=\"button\" type=\"button\" value=\"Commit\" onclick=\"" + this.id + ".imageCommit();\" /></div></div></div>");
        document.write("<div id=\"" + this.id + "\" class=\"adminwindow\" style=\"left:400px; top:150px; width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"bar\" onmousedown=\"" + this.id + ".dragStart(event)\"><a id=\"" + this.id + "maximize\" href=\"javascript:" + this.id + ".maximizeWindow();\">+</a><a href=\"javascript:" + this.id + ".closeWindow();\">x</a></div><div class=\"content\"><br /><br /><iframe src=\"/admin/scripts/imageUpload.aspx?name=" + this.controlId.substring(0, this.controlId.indexOf("_img_1"))  + "\" frameborder=\"0\"></iframe></div></div></div>");
        //fill the text area with the corresponing text
        
        break;
    case "faq":
        document.write("<div id=\"" + this.id + "\" class=\"adminwindow\" style=\"left:400px; top:150px; width:" + this.width + "; height:" + this.height + ";display:none;\"><div class=\"bar\" onmousedown=\"" + this.id + ".dragStart(event)\"><a id=\"" + this.id + "maximize\" href=\"javascript:" + this.id + ".maximizeWindow();\">+</a><a href=\"javascript:" + this.id + ".closeWindow();\">x</a></div><div class=\"content\"><textarea id=\"" + this.id + "textarea\" rows=\"20\" cols=\"10\" style=\"height:100px;\"></textarea><div><br /><input class=\"button\" type=\"button\" value=\"Preview\" onclick=\"" + this.id + ".textPreview();\" /><input class=\"button\" type=\"button\" value=\"Commit\" onclick=\"" + this.id + ".textCommit();" + this.id + ".textPreview();\" /></div></div></div>");
        //fill the text area with the corresponing text
        //this.textAreaFill();
    }
}

//fills the textarea with text from the corresponding label
adminWindow.prototype.textAreaFill = function(){
    document.getElementById(this.id + "textarea").value = decodeHtml(document.getElementById(this.controlId).innerHTML);
}

adminWindow.prototype.textPreview = function(){
   document.getElementById(this.controlId).innerHTML = encodeHtml(document.getElementById(this.id + "textarea").value); 
}

adminWindow.prototype.textCommit = function(){
    makeRequest("/admin/data.aspx", "name=" + this.controlId.substring(0, this.controlId.indexOf("_lbl_Text")) + "&text=" + encodeHtml(document.getElementById(this.id + "textarea").value) + "&header=&method=set");
 }
  
adminWindow.prototype.imageCommit = function(){
    makeRequest("/admin/data.aspx", "name=" + this.controlId.substring(0, this.controlId.indexOf("_img_1")) + "&url=" + encodeHtml(document.getElementById(this.id + "file").value) + "&method=setImage");

}

adminWindow.prototype.faqCommit = function(){
    makeRequest("/admin/data.aspx", "name=" + this.controlId.substring(0, this.controlId.indexOf("_lbl_Text")) + "&text=" + encodeHtml(document.getElementById(this.id + "textarea").value) + "&header=&method=set");
}

adminWindow.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
adminWindow.prototype.closeWindow = function(){
    document.getElementById(this.id).style.display = "none";
}

//open the window
adminWindow.prototype.openWindow = function(){
    document.getElementById(this.id).style.display = "";
}

//maximize/restore the window
adminWindow.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;
    }
}

function dragGo(event) {

  var x, y;

  // 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;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {
  // Stop capturing mousemove and mouseup events.
  //alert(event);

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup", dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo, true);
    //document.getElementById(this.id).style.display = "none";
    document.removeEventListener("mouseup", dragStop, true);
  }
}

//encode text to html
function encodeHtml(str) {
     var encodedHtml = str;
     //special characters
     
     //whitespace characters
     encodedHtml = encodedHtml.replace(/\r\n/g,"<br />");// carraige return line feed
     encodedHtml = encodedHtml.replace(/\r/g,"<br />");//carraige return
     encodedHtml = encodedHtml.replace(/\n/g,"<br />");//line feed
     encodedHtml = encodedHtml.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");//tab
     
     return unescape(encodedHtml);
} 

//decode html to text
function decodeHtml(str) {
     var decodeHtml = str;
     //special characters
     
     //whitespace characters
     decodeHtml = decodeHtml.replace(/<br>/gi,"\r\n");// carraige return line feed
     //decodedHtml = decodedHtml.replace(/\r/g,"<br />");//carraige return
     //decodedHtml = decodedHtml.replace(/\n/g,"<br />");//line feed
    // decodedHtml = decodedHtml.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");//tab
     
     return unescape(decodeHtml);
} 