function NotePane(el) {

  var i;

  this.frame           = el;
  this.titleBar        = noteFindByClassName(el, "noteTitleBar");
  this.titleBarText    = noteFindByClassName(el, "noteTitleBarText");
  this.titleBarButtons = noteFindByClassName(el, "noteTitleBarButtons");
  this.clientArea      = noteFindByClassName(el, "noteClientArea");
  this.textArea        = noteFindByClassName(el, "notesText");

  this.isOpen      = false;
  this.isMinimized = false;

  this.open       = noteOpen;
  this.close      = noteClose;
  this.restore    = noteRestore;
  this.minimize   = noteMinimize;
  this.makeActive = noteMakeActive;

  this.frame.noteContainer = this;
  this.frame.onmousemove  = noteResizeCursorSet;
  this.frame.onmouseout   = noteResizeCursorRestore;
  this.frame.onmousedown  = noteResizeDragStart;

  this.titleBar.noteContainer = this;
  this.titleBar.onmousedown  = noteMoveDragStart;

  this.clientArea.noteContainer = this;
  this.clientArea.onclick      = noteClientAreaClick;
  
  var initLt, initWd, w, dw;

  initLt = this.frame.style.left;
  initWd = parseInt(this.frame.style.width);
  this.frame.style.left = -this.titleBarText.offsetWidth + "px";

  w = this.frame.offsetWidth;
  this.frame.style.width = w + "px";
  dw = this.frame.offsetWidth - w;
  w -= dw;     
  this.frame.style.width = w + "px";

  this.isOpen = true;
  this.minimize();
  this.minimumWidth = this.frame.offsetWidth - dw;

  this.titleBarText.style.width = "";
  this.clipTextMinimumWidth = this.frame.offsetWidth - dw;

  this.minimumHeight = 100;

  this.restore();
  this.isOpen = false;
  initWd = Math.max(initWd, this.minimumWidth);
  this.frame.style.width = initWd + "px";

  if (this.clipTextMinimumWidth >= this.minimumWidth)
    this.titleBarText.style.width = (noteCtrl.minimizedTextWidth + initWd - this.minimumWidth) + "px";

  this.frame.style.left = initLt;
}

function noteOpen() {
  if (this.isOpen)
    return;
  this.makeActive();
  this.isOpen = true;
  if (this.isMinimized)
    this.restore();
  this.textArea.style.display = "block";
  this.clientArea.style.display = "block";
  this.frame.className = "notePane";
}

function noteClose() {
  this.textArea.style.display = "none";
  this.clientArea.style.display = "none"; 
  this.frame.className = "notePaneHidden";
  this.isOpen = false;
}

function noteMinimize() {
  if (!this.isOpen || this.isMinimized)
    return;
  this.makeActive();
  this.restoreFrameWidth = this.frame.style.width;
  this.restoreTextWidth = this.titleBarText.style.width;
  if (this.minimumWidth)
    this.frame.style.width = this.minimumWidth + "px";
  else
    this.frame.style.width = "";
  this.titleBarText.style.width = noteCtrl.minimizedTextWidth + "px";
  this.isMinimized = true;
}

function noteRestore() {
  if (!this.isOpen || !this.isMinimized)
    return;
  this.makeActive();
  this.frame.style.width = this.restoreFrameWidth;
  this.titleBarText.style.width = this.restoreTextWidth;
  this.isMinimized = false;
}

function noteMakeActive() {
  if (noteCtrl.active == this)
    return;    
  if (noteCtrl.active) {
    if (noteCtrl.active.frame) {
        if ((noteCtrl.active.frame.className != "notePaneHidden") && noteCtrl.active.frame.className != "notePaneDeleted") {
            noteCtrl.active.frame.className = "notePaneInactive";
        }
    }
  }
  this.frame.className = "notePane";
  this.frame.style.zIndex = ++noteCtrl.maxzIndex;  
  noteCtrl.active = this;
}

function noteClientAreaClick(event) {
  this.noteContainer.makeActive();
}

function noteMoveDragStart(event) {
  var target;
  var x, y;

  target = event.target.tagName;

  if (target == "AREA")
    return;

  this.noteContainer.makeActive();

  x = event.pageX;
  y = event.pageY;
  
  noteCtrl.xOffset = noteCtrl.active.frame.offsetLeft - x;
  noteCtrl.yOffset = noteCtrl.active.frame.offsetTop  - y;

  document.addEventListener("mousemove", noteMoveDragGo,   true);
  document.addEventListener("mouseup",   noteMoveDragStop, true);
  event.preventDefault();  

  noteCtrl.inMoveDrag = true;
}

function noteMoveDragGo(event) {
  var x, y;
  if (!noteCtrl.inMoveDrag)
    return;

    x = event.pageX;
    y = event.pageY;
    event.preventDefault();

  noteCtrl.active.frame.style.left = (x + noteCtrl.xOffset) + "px";
  noteCtrl.active.frame.style.top  = (y + noteCtrl.yOffset) + "px";
}

function noteMoveDragStop(event) {
  noteCtrl.inMoveDrag = false;
  document.removeEventListener("mousemove", noteMoveDragGo,   true);
  document.removeEventListener("mouseup",   noteMoveDragStop, true);
}

function noteResizeCursorSet(event) {

  var target;
  var xOff, yOff;

  if (this.noteContainer.isMinimized || noteCtrl.inResizeDrag)
    return;

  target = event.target;
  if (target != this.noteContainer.frame)
    return;

  xOff = event.layerX;
  yOff = event.layerY;

  noteCtrl.resizeDirection = "";
  if (yOff <= noteCtrl.resizeCornerSize)
    noteCtrl.resizeDirection += "n";
  else if (yOff >= this.noteContainer.frame.offsetHeight - noteCtrl.resizeCornerSize)
    noteCtrl.resizeDirection += "s";
  if (xOff <= noteCtrl.resizeCornerSize)
    noteCtrl.resizeDirection += "w";
  else if (xOff >= this.noteContainer.frame.offsetWidth - noteCtrl.resizeCornerSize)
    noteCtrl.resizeDirection += "e";

  if (noteCtrl.resizeDirection == "") {
    this.onmouseout(event);
    return;
  }

  this.noteContainer.frame.style.cursor = noteCtrl.resizeDirection + "-resize";
}

function noteResizeCursorRestore(event) {
  if (noteCtrl.inResizeDrag)
    return;
  this.noteContainer.frame.style.cursor = "";
}

function noteResizeDragStart(event) {

  var target;
  target = event.target;
  if (target != this.noteContainer.frame)
    return;

  this.noteContainer.makeActive();

  if (this.noteContainer.isMinimized)
    return;

  noteCtrl.xPosition = event.pageX;
  noteCtrl.yPosition = event.pageY;

  noteCtrl.oldLeft   = parseInt(this.noteContainer.frame.offsetLeft,  10);
  noteCtrl.oldTop    = parseInt(this.noteContainer.frame.offsetTop,   10);
  noteCtrl.oldWidth  = parseInt(this.noteContainer.frame.offsetWidth, 10);
  noteCtrl.oldHeight = parseInt(this.noteContainer.frame.offsetHeight, 10);

  document.addEventListener("mousemove", noteResizeDragGo,   true);
  document.addEventListener("mouseup"  , noteResizeDragStop, true);
  event.preventDefault();

  noteCtrl.inResizeDrag = true;
}

function noteResizeDragGo(event) {

 var north, south, east, west;
 var dx, dy;
 var w, h;

  if (!noteCtrl.inResizeDrag)
    return;

  north = false;
  south = false;
  east  = false;
  west  = false;
  if (noteCtrl.resizeDirection.charAt(0) == "n")
    north = true;
  if (noteCtrl.resizeDirection.charAt(0) == "s")
    south = true;
  if (noteCtrl.resizeDirection.charAt(0) == "e" || noteCtrl.resizeDirection.charAt(1) == "e")
    east = true;
  if (noteCtrl.resizeDirection.charAt(0) == "w" || noteCtrl.resizeDirection.charAt(1) == "w")
    west = true;

  dx = event.pageX - noteCtrl.xPosition;
  dy = event.pageY - noteCtrl.yPosition;

  if (west)
    dx = -dx;
  if (north)
    dy = -dy;

  w = noteCtrl.oldWidth  + dx;
  h = noteCtrl.oldHeight + dy;
  if (w <= noteCtrl.active.minimumWidth) {
    w = noteCtrl.active.minimumWidth;
    dx = w - noteCtrl.oldWidth;
  }
  if (h <= noteCtrl.active.minimumHeight) {
    h = noteCtrl.active.minimumHeight;
    dy = h - noteCtrl.oldHeight;
  }
  if (east || west) {
    noteCtrl.active.frame.style.width = w + "px";
  }
  if (north || south)
    noteCtrl.active.frame.style.height = h + "px";

  if (east || west) {
    if (w < noteCtrl.active.clipTextMinimumWidth)
      noteCtrl.active.titleBarText.style.width = (noteCtrl.minimizedTextWidth + w - noteCtrl.active.minimumWidth) + "px";
    else
      noteCtrl.active.titleBarText.style.width = "";
  }

  if (west)
    noteCtrl.active.frame.style.left = (noteCtrl.oldLeft - dx) + "px";
  if (north)
    noteCtrl.active.frame.style.top  = (noteCtrl.oldTop  - dy) + "px";

  event.preventDefault();
}

function noteResizeDragStop(event) {
  noteCtrl.inResizeDrag = false;
  document.removeEventListener("mousemove", noteResizeDragGo,   true);
  document.removeEventListener("mouseup", noteResizeDragStop, true);
}

function noteFindByClassName(el, className) {
  var i, tmp;
  if (el.className == className)
    return el;
  for (i = 0; i < el.childNodes.length; i++) {
    tmp = noteFindByClassName(el.childNodes[i], className);
    if (tmp != null)
      return tmp;
  }
  return null;
}

var noteCtrl = new Object();

function openNote(noteId) {
    notePane = document.getElementById("notePane" + noteId);
    noteTextArea = document.getElementById("noteText" + noteId);    
    noteClientArea = document.getElementById("noteClientArea" + noteId);

    scrollToAnchor("noteContainer"+noteId); 
    if (notePane) {
        positionNotePane(noteId);    
        var noteObj = new NotePane(notePane);
        noteObj.open();   
        
        if (noteClientArea) {
            noteClientArea.style.display = "block";
        }
             
        if (noteTextArea) {            
            noteTextArea.style.display = "block";
            noteTextArea.focus();
        }
 
    }     
    return false;
}

function closeNote(noteId) {
    notePane = document.getElementById("notePane" + noteId);
    noteClientArea = document.getElementById("noteClientArea" + noteId);
    noteTextArea = document.getElementById("notesText" + noteId);
    //need to hide these divs first in order to remove flicker
    if (noteClientArea) {
        noteClientArea.style.display = "none";
    }
    
    if (noteTextArea) {
        noteTextArea.style.display = "none";
    }    
    
    if (notePane) {
        var noteObj = new NotePane(notePane);
        noteObj.close();
        updateNoteText(noteId);
    }
    return true;
}

function updateNoteText(noteId) {
    notesText = document.getElementById("notesText"+noteId);
    if (notesText) {
        notesTextPrint = document.getElementById("notesTextPrint"+noteId);
        notesLink = document.getElementById("notesLink"+noteId); 
        if (notesTextPrint) { 
            notesTextPrint.innerHTML = notesText.value;
        }
        if (notesLink) {
            notesLink.innerHTML = notesText.value;
        }
    }
}

function initNotes() {
  noteCtrl.maxzIndex                        =   10;
  noteCtrl.resizeCornerSize                 =  5;
  noteCtrl.minimizedTextWidth               = 100;
  noteCtrl.inMoveDrag                       = false;
  noteCtrl.inResizeDrag                     = false;
  noteCtrl.active                           = null;
}

function clickNoteCheckbox(checkbox) {
    var hidden = document.getElementById("notesToDelete");
    var selected = new Array();
    if (hidden) {
        if (hidden.value.length > 0) selected = hidden.value.split(",");
        for (note in selected) {
            if (checkbox.value == selected[note]) {
                selected.splice(note, 1); // remove note
            }        
        }
        if (checkbox.checked) {
            selected.push(checkbox.value); // add note
            selected.sort();
        }        
        hidden.value = selected.join(",");
        //alert(hidden.value);
    }
    return false;
}

function openNoteFromViewer(noteId) {
    scrollToAnchor("noteContainer"+noteId);
    openNote(noteId);
    noteContainer = document.getElementById("noteContainer"+noteId);
    if (noteContainer) {
        noteContainer.className = "noteContainer";
    }
    positionNotePane(noteId);
}

function positionNotePane(noteId) {
    notePane = document.getElementById("notePane"+noteId);
    articlePane = document.getElementById("articlePane");
    noteContainer = document.getElementById("noteContainer"+noteId);
    if (notePane && articlePane) {        
        noteLeft = articlePane.offsetWidth/2 - notePane.offsetWidth/2;
        
        if (noteContainer) {
            noteTop = noteContainer.offsetTop + 25;
        } else {
            noteTop = articlePane.scrollTop + (articlePane.offsetHeight/2 - notePane.offsetHeight/2);
        }
        
        notePane.style.top = noteTop + "px";
        notePane.style.left = noteLeft + "px";
    }
}

function scrollToAnchor(anchorId) {
    anchor = document.getElementById(anchorId);
    articlePane = document.getElementById("articlePane");
    if (anchor && articlePane) {
        articlePane.scrollTop = anchor.offsetTop;
    }    
}

function deleteNotes() {
    var hidden = document.getElementById("notesToDelete");
    var selected = new Array();
    if (hidden) {
        if (hidden.value.length > 0) selected = hidden.value.split(",");
        for (note in selected) {
            noteContainer = document.getElementById("noteContainer"+selected[note]);
            if (noteContainer) {
                noteContainer.parentNode.removeChild(noteContainer);
            }
            notePane = document.getElementById("notePane"+selected[note]);
            if (notePane) {
                notePane.parentNode.removeChild(notePane);
            }      
        }
    }
    return true;
}

function openNoteFromAnchor() {
    var anchorStr = getURLAnchor();
    var noteAnchorIndex = anchorStr.indexOf("note");
    if ( noteAnchorIndex > -1 ) {
        var noteId = anchorStr.substr(noteAnchorIndex+4);
        openNote(noteId);
        noteContainer = document.getElementById("noteContainer"+noteId);
        if (noteContainer) {
            noteContainer.className = "noteContainer";
        }
        positionNotePane(noteId);
    }
}

function getURLAnchor() {
  var strAnchor = "";
  var strHref = window.location.href;  
  if ( strHref.indexOf("#") > -1 ) {
    strAnchor = strHref.substr(strHref.indexOf("#"));
  }
  return strAnchor;
}

function showNoteSummary(noteId) {
    noteTextPrint = document.getElementById("notesTextPrint" + noteId);
    if (noteTextPrint) {
        noteTextPrint.className = "notesTextPrintHover";
    }
}

function hideNoteSummary(noteId) {
    noteTextPrint = document.getElementById("notesTextPrint" + noteId);
    if (noteTextPrint) {
        noteTextPrint.className = "notesTextPrint";
    }
}
