<!--
var disableNotes = false;
var agent = navigator.userAgent;
var MSIE = false;
if (/MSIE/.test(agent)) MSIE = true;
var notes = new Array();
var curNote = 0;
var curOver = 0;
var cTitle = 0;
var debug_list = new Array();
var debug_idx = 0;
var debug_last = 0;
var debug_cnt = 0;
function debug(s) {
if (s == debug_last) {
debug_cnt++;
debug_list.pop();
} else {
debug_cnt = 1;
debug_last = s;
debug_idx = (debug_idx + 1) % 100;
if (debug_list.length > 20) debug_list.shift();
}
debug_list.push((debug_idx < 10 ? '0' : '') + debug_idx + ': ' + s + '(' + debug_cnt + ')');
var o = getObj('debug_div');
if (o) {
o.innerHTML = '';
for (var i = debug_list.length - 1; i >= 0; i--)
o.innerHTML += debug_list[i] + '<br/>';
}
}
function Note(t, x, y, w, h, newId, nick, writable) {
  this.borderNorm = '1px outset #cccccc';
  this.borderMove = '1px groove #cccccc';
  this.borderSel = '1px dotted #cccccc';
  if (newId)
    this.noteId = newId;
  else
    this.is_new = 1;
  this.nick = nick;
  this.title = t;
  this.left = x;
  this.top = y;
  this.width = w;
  this.height = h;
  if (writable) this.writable = writable;
  this.aa = 0;
  this.norm = function norm() {
    if (this.width < 1) this.width = 1;
    if (this.height < 1) this.height = 1;
    if (this.width > this.cNote.offsetWidth) this.width = this.cNote.offsetWidth;
    if (this.height > this.cNote.offsetHeight) this.height = this.cNote.offsetHeight;
    if (this.left < 0) this.left = 0;
    if (this.top < 0) this.top = 0;
    if (this.left + this.width > this.cNote.offsetWidth) this.left = this.cNote.offsetWidth - this.width;
    if (this.top + this.height > this.cNote.offsetHeight) this.top = this.cNote.offsetHeight - this.height;
  }
  this.moveDelta = function moveDelta(dx, dy) {
    this.left += dx;
    this.top += dy;
    this.norm();
    this.display();
    if (!this.editing) this.dblClick();
  }
  this.resizeDelta = function resizeDelta(dx, dy) {
    this.width += dx;
    this.height += dy;
    this.norm();
    this.display();
    if (!this.editing) this.dblClick();
  }
  this.mouseDown = function mouseDown(e) {
//    if (!editNotes) return false;
    if (e.preventDefault) e.preventDefault();
    realHideTitle();
    if (e.x) e.layerX = e.x;
    if (e.y) e.layerY = e.y;
    co = o = getEventObj(e);
    if (o.cNote) o = o.cNote.node;
    if (!o.cNote) o = curNote.node;
    if (!o.cNote) return false;
    if (!o.cNote.writable) return false;
    curNote = o.cNote;
    if (MSIE || (co == o) || 1) {
      if (e.layerX < 6) {
        if (e.layerY < 6)
          o.cNote.resizing = 1;
        else if (e.layerY > o.cNote.height - 3)
          o.cNote.resizing = 7;
        else
          o.cNote.resizing = 8;
      } else if (e.layerX > o.cNote.width - 3) {
        if (e.layerY < 6)
          o.cNote.resizing = 3;
        else if (e.layerY > o.cNote.height - 3)
          o.cNote.resizing = 5;
        else
          o.cNote.resizing = 4;
      } else if (e.layerY < 6) {
        o.cNote.resizing = 2;
      }  else if (e.layerY > o.cNote.height - 3) {
       o.cNote.resizing = 6;
      } else
        o.cNote.moving = true;
    } else
      o.cNote.moving = true;
    o.sy = e.pageY ? e.pageY : e.screenY;
    o.sx = e.pageX ? e.pageX : e.screenX;
    return false;
  }
  this.mouseUp = function mouseUp(e) {
    if (e.preventDefault) e.preventDefault();
    o = getEventObj(e);
    if (!o.cNote && curNote) o = curNote.node;
    curNote = 0;
    if (!o || !o.cNote) return false;
    o.cNote.moving = false;
    o.cNote.resizing = false;
    o.cNote.node.style.border = '';
    o.cNote.mouseOver(e);
    o.cNote.mouseMove(e);
    return false;
  }
  var a = 1;
  this.mouseMove = function mouseMove(e) {
    if (e.preventDefault) e.preventDefault();
    if (e.x) e.layerX = e.x;
    if (e.y) e.layerY = e.y;
    co = o = getEventObj(e);
    if (o && o.cNote) o = o.cNote.node;
    if (0 && co.nodeName == 'SPAN' && co.cNote && o.childNodes.length) {
      if (co.childNodes[0]) { e.layerX += 1; e.layerY += 1; }
      if (co.childNodes[0].childNodes.length && co.childNodes[0].childNodes[0]) { e.layerX += 1; e.layerY += 1; }
    }
if (!co.cNote || !co.cNote.writable) return false;
    if (!o || !o.cNote) o = curNote.node;
/* HERE - mousemove */
    if (o && !o.cNote) realHideTitle();
    if (!o || !o.cNote) {
      return false;
    }
    curOver = o.cNote;

  if (hideTitleTimer) clearTimeout(hideTitleTimer);
  hideTitleTimer = 0;


    var c = 'move';
    //if (MSIE) c = 'hand'; else c = 'pointer';
    if (MSIE || (co == o) || 1) {
      if (e.layerX < 6) {
        if (e.layerY < 6)
          c = 'nw-resize';
        else if (e.layerY > o.cNote.height - 3)
          c = 'sw-resize';
        else
          c = 'w-resize';
      } else if (e.layerX > o.cNote.width - 3) {
        if (e.layerY < 6)
          c = 'ne-resize';
        else if (e.layerY > o.cNote.height - 3)
          c = 'se-resize';
        else
          c = 'e-resize';
      } else if (e.layerY < 6) {
        c = 'n-resize';
      }  else if (e.layerY > o.cNote.height - 3) {
        c = 's-resize';
      }
    }
    if (!o.cNote.resizing && !o.cNote.moving)
      o.cNote.cursor = c;
//    if (co.cNote && editNotes)
    if (co.cNote && co.cNote.writable && !co.isTitle)
      co.style.cursor = o.cNote.cursor;
    ny = e.pageY ? e.pageY : e.screenY;
    nx = e.pageX ? e.pageX : e.screenX;
//    if (o.cNote) {
//      if (c == 'move')
//        o.cNote.node.childNodes[0].style.border = o.cNote.borderMove;
//      else
    if (o.cNote.resizing || o.cNote.moving)
      o.cNote.node.style.border = '1px dotted #cccccc';
    else
      o.cNote.node.style.border = o.cNote.writable ? '1px solid #ccee77' : '1px solid yellow';
//    }
    if (o.cNote.moving) {
      o.cNote.moveDelta(nx - o.sx, ny - o.sy);
    } else {
      if (o.cNote.resizing == 1) {
        o.cNote.moveDelta(nx - o.sx, ny - o.sy);
        o.cNote.resizeDelta(-(nx - o.sx), -(ny - o.sy));
      } else if (o.cNote.resizing == 2) {
        o.cNote.moveDelta(0, ny - o.sy);
        o.cNote.resizeDelta(0, -(ny - o.sy));
      } else if (o.cNote.resizing == 3) {
        o.cNote.moveDelta(0, ny - o.sy);
        o.cNote.resizeDelta(nx - o.sx, -(ny - o.sy));
      } else if (o.cNote.resizing == 4) {
        o.cNote.moveDelta(0, 0);
        o.cNote.resizeDelta(nx - o.sx, 0);
      } else if (o.cNote.resizing == 5) {
        o.cNote.moveDelta(0, 0);
        o.cNote.resizeDelta(nx - o.sx, ny - o.sy);
      } else if (o.cNote.resizing == 6) {
        o.cNote.moveDelta(0, 0);
        o.cNote.resizeDelta(0, ny - o.sy);
      } else if (o.cNote.resizing == 7) {
        o.cNote.moveDelta(nx - o.sx, 0);
        o.cNote.resizeDelta(-(nx - o.sx), ny - o.sy);
      } else if (o.cNote.resizing == 8) {
        o.cNote.moveDelta(nx - o.sx, 0);
        o.cNote.resizeDelta(-(nx - o.sx), 0);
      }
    }
    o.sx = nx;
    o.sy = ny;
    return false;
  }
  this.mouseOver = function mouseOver(e) {
    if (e.preventDefault) e.preventDefault();
    o = getEventObj(e);
    if (o.cNote) o = o.cNote.node;
    //if (!o.oldcursor) o.oldcursor = o.style.cursor;
    if (o.cNote && o.cNote.node && o.cNote.node.childNodes.length) o = o.cNote.node.childNodes[0];
//    if (o.cNote)
//      o.style.border = this.borderNorm;
    if (o.cNote && !o.cNote.editing) {
      curOver = o.cNote;

  if (hideNotesTimer) clearTimeout(hideNotesTimer);
  hideNotesTimer = 0;
  if (hideTitleTimer) clearTimeout(hideTitleTimer);
  hideTitleTimer = 0;

      if (cTitle == o.cNote) return false;
/* HERE mouseover */

      realHideTitle();

      // display note text
      nd = document.createElement('div');
      nd.innerHTML = o.cNote.title;
      if (o.cNote.nick) nd.innerHTML += '&nbsp;<small>' + o.cNote.nick + '</small>';
      setRecProp(nd, 'cNote', o.cNote);
      setRecProp(nd, 'isTitle', 1);
      if (o.cNote.title.length > 100){
      		nd.style.width = '250px';
    	}
      nd.style.border = '1px solid black';
      nd.style.backgroundColor = o.cNote.writable ? '#F3FFC2' : '#cccc77';
      nd.style.left = (o.cNote.offsetLeft + o.cNote.left) + 'px';
      nd.style.top = (o.cNote.offsetTop + o.cNote.top + o.cNote.height + 8) + 'px';
      nd.style.zIndex = 1001;
      nd.style.textAlign = 'justify';
      nd.style.display = 'block';
      nd.style.padding = '4px';
      nd.style.position = 'absolute';
      addEvent(nd, 'mousemove', o.cNote.mouseMove);
      addEvent(nd, 'mouseover', o.cNote.mouseOver);
      addEvent(nd, 'mouseout', o.cNote.mouseOut);
      o.cNote.cNote.appendChild(nd);
      o.cNote.tdiv = nd;
      cTitle = o.cNote;
    }
    return false;
  }
  this.mouseOut = function mouseOut(e) {
    if (e.preventDefault) e.preventDefault();
    o = getEventObj(e);
    if (o && o.cNote) o = o.cNote.node;
    if (!o || !o.cNote) o = curNote.node;
    if (!o || !o.cNote) return false;
/* HERE mouseout */
    hideTitle();
    if (o.cNote.moving || o.cNote.resizing) return false;
    curOver = 0;
    curNote = 0;
    o.style.cursor = '';
    //o.oldcursor = 0;
    if (o && o.cNote)
      o.style.border = '';
    if (o.childNodes.length) o = o.childNodes[0];
    if (o == o.cNote.node) {
      o.cNote.moving = false;
      o.cNote.resizing = false;
    }
    return false;
  }
  this.save = function save(t) {
    this.title = t;
    this.editing = false;
    this.is_new = 0;
    f = getObj('noteFormDiv');
    f.style.display = 'none';
    if (this.noteId)
      postHTTP(root+'upd_ph/', 'photoId='+photoId+'&updateNote='+this.noteId+'&t='+encodeURIComponent(this.title)+'&x='+this.left+'&y='+this.top+'&w='+this.width+'&h='+this.height);
    else
      postHTTP(root+'upd_ph/', 'addNote=1&photoId='+photoId+'&t='+encodeURIComponent(this.title)+'&x='+this.left+'&y='+this.top+'&w='+this.width+'&h='+this.height, this.setId, this);
  }
  this.setId = function setId(newId, obj) {
    obj.noteId = newId;
  }
  this.cancel = function cancel() {
    if (this.is_new) return this.drop();
    f = getObj('noteForm');
    f.cNote.editing = false;
    f.cNote.left = f.oldLeft;
    f.cNote.top = f.oldTop;
    f.cNote.width = f.oldWidth;
    f.cNote.height = f.oldHeight;
    f = getObj('noteFormDiv');
    f.style.display = 'none';
    displayNotes();
  }
  this.drop = function drop() {
    f = getObj('noteForm');
    postHTTP(root+'upd_ph/', 'photoId='+photoId+'&dropNote='+this.noteId);
    f.cNote.editing = false;
    f.cNote.deleted = true;
    f = getObj('noteFormDiv');
    f.style.display = 'none';
    this.hide();
  }
  this.dblClick = function dblClick(e) {
//    if (!editNotes) return false;
    if (e) {
      if (e.preventDefault) e.preventDefault();
      o = getEventObj(e);
    } else
      o = this.node;
    if (o.cNote) o = o.cNote.node;
    if (!o.cNote) return false;
    if (!o.cNote.writable) return false;
    if (o.cNote.editing) return false;
    f = getObj('noteForm');
    addEvent(f, 'mousemove', o.cNote.mouseMove);
    addEvent(f, 'mouseover', o.cNote.mouseOver);
    addEvent(f, 'mouseout', o.cNote.mouseOut);
    f.note.value = o.cNote.title;
    if (f.cNote)
      f.cNote.editing = false;
    o.cNote.editing = true;
    f.cNote = o.cNote;
    f.oldLeft = o.cNote.left;
    f.oldTop = o.cNote.top;
    f.oldWidth = o.cNote.width;
    f.oldHeight = o.cNote.height;
    realHideNotes();
    o.cNote.display();
    return false;
  }
  this.resizeWindow = function resizeWindow(root) {
    this.offsetLeft = getOffsetLeft(root);
    this.offsetTop = getOffsetTop(root);
    this.display(1);
  }
  this.attach = function attach(root) {
    if (this.cNote) this.cNote.removeChild(this.node);
    this.cNote = root;
    this.cNote.appendChild(this.node);
    this.resizeWindow(root);
    return this;
  }
  this.display = function display(justPos) {
    if (this.deleted) return false;
    if (disableNotes) return false;
    with (this.node) {
      style.zIndex = 1000;
      style.left = (this.offsetLeft + this.left - 3) + 'px';
      style.top = (this.offsetTop + this.top - 3) + 'px';
      style.width = (this.width + 6) + 'px';
      style.height = (this.height + 6) + 'px';
      if (!justPos) style.display = 'block';
      style.position = 'absolute';
    }
    if (this.node.childNodes.length)
    with (this.node.childNodes[0]) {
      style.position = 'relative';
      style.left = '0px';
      style.top = '0px';
      style.width = (this.width + 4) + 'px';
      style.height = (this.height + 4) + 'px';
      if (!justPos) style.display = 'block';
    }
    if (this.node.childNodes.length && this.node.childNodes[0].childNodes.length)
    with (this.node.childNodes[0].childNodes[0]) {
      style.position = 'relative';
      style.left = '0px';
      style.top = '0px';
      style.width = (this.width + 2) + 'px';
      style.height = (this.height + 2) + 'px';
      if (!justPos) style.display = 'block';
    }
    if (this.editing) {
      f = getObj('noteFormDiv');
      f.style.zIndex = 1001;
      f.style.position = 'absolute';
      f.style.left = (this.offsetLeft + this.left) + 'px';
      f.style.top = (this.offsetTop + this.top + this.height + 8) + 'px';
      if (!justPos) f.style.display = 'block';
    }
    return this;
  }
  this.hide = function hide() {
    if (this.editing) return false;
    this.node.style.display = 'none';
    this.node.style.border = '';
  }
  this.createNode = function createNode() {
    nd = document.createElement('span');
    nd.style.display = 'none';
    nt = document.createElement('span');
    nt.style.border = '1px solid black';
    nd.appendChild(nt);
    ndi = document.createElement('img');
    ndi.style.border = '1px solid white';
    ndi.src = root+'img/s.gif';
    nt.appendChild(ndi);
    nd.cNote = this;
    nt.cNote = this;
    ndi.cNote = this;
//    ndi.style.border = this.borderNorm;
    addEvent(nd, 'mouseover', this.mouseOver);
    addEvent(nd, 'mouseout', this.mouseOut);
    addEvent(nd, 'mousedown', this.mouseDown);
    addEvent(nd, 'mouseup', this.mouseUp);
    addEvent(nd, 'mousemove', this.mouseMove);
    addEvent(nt, 'mouseover', this.mouseOver);
    addEvent(nt, 'mouseout', this.mouseOut);
    addEvent(nt, 'mousedown', this.mouseDown);
    addEvent(nt, 'mouseup', this.mouseUp);
    addEvent(nt, 'mousemove', this.mouseMove);
    addEvent(ndi, 'mouseover', this.mouseOver);
    addEvent(ndi, 'mouseout', this.mouseOut);
    addEvent(ndi, 'mousedown', this.mouseDown);
    addEvent(ndi, 'mouseup', this.mouseUp);
    addEvent(ndi, 'mousemove', this.mouseMove);
    addEvent(ndi, 'dblclick', this.dblClick);
    this.node = nd;
  }
  this.createNode();
}
function realHideTitle() {
  if (cTitle) {
    cTitle.cNote.removeChild(cTitle.tdiv);
    cTitle.tdiv = 0;
    cTitle = 0;
  }
  if (hideTitleTimer) clearTimeout(hideTitleTimer);
  hideTitleTimer = 0;
}
var hideTitleTimer = 0;
function hideTitle() {
  if (hideTitleTimer) clearTimeout(hideTitleTimer);
  hideTitleTimer = setTimeout('realHideTitle();', 500)
}
function resizeWindow() {
  for (i = 0; i < notes.length; i++)
    notes[i].resizeWindow(notes[i].cNote);
}
addEvent(window, 'resize', resizeWindow);
function createNote(t, x, y, w, h, noteId, nick, writable) {
  if (noteId)
    return notes[notes.length] = new Note(t, x, y, w, h, noteId, nick, writable);
  else
    return notes[notes.length] = new Note(t, x, y, w, h, 0, nick, 1);
}
function displayNotes() {
  if (hideNotesTimer) clearTimeout(hideNotesTimer);
  hideNotesTimer = 0;
  f = getObj('noteForm');
  if (f)
    if (f.cNote)
      if (f.cNote.editing)
        return false;
  for (i = 0; i < notes.length; i++)
    notes[i].display();
}
var hideNotesTimer = 0;
function realHideNotes() {
  for (i = 0; i < notes.length; i++)
    notes[i].hide();
  realHideTitle();
  if (hideNotesTimer) clearTimeout(hideNotesTimer);
  hideNotesTimer = 0;
}
function hideNotes() {
  if (hideNotesTimer) clearTimeout(hideNotesTimer);
  hideNotesTimer = setTimeout('realHideNotes();', 500);
}
function setRecProp(e, p, v) {
  if (!e || !e.childNodes || !e.childNodes.length) return;
  e[p] = v;
  for (i = 0; i < e.childNodes.length; i++)
    setRecProp(e.childNodes[i], p, v);
}

//-->
