/**
 * mp_stickers.js - handles:
 *   - assigning stickers to panels or "page"
 *   - docking of stickers in tray
 *   - "find stickers" feature
 *
 * @version 17 Oct 2005
 * @author  Joseph Oster, wingo.com
 */

mp_stickers = function() {
  // CONSTRUCTOR for 'mp_stickers' object
  mp_stickers.lostStickerMsgDIV = document.getElementById("lostStickerMSG");
  mp_stickers.lostStickerBgDIV = document.getElementById("lostStickerBG");
  mp_stickers.lostStickerShowDIV = document.getElementById("lostStickerSD");
  var panelID = "stickers_panel";
  this.panelDIV = document.getElementById(panelID);
  this.panelBODY = document.getElementById(panelID + "_body");
  this.stickerList = new Array();
  this.resize();
  }

mp_stickers.prototype.dockSticker = function(stickerID) {
  for (var i=0; i<this.stickerList.length; i++)
    if (this.stickerList[i].id == stickerID) {
      this.snapSticker(i);
      return;
      }

  this.stickerList.push(document.getElementById(stickerID));
  this.snapSticker(this.stickerList.length-1);
  delete mp_stickers.assigned[stickerID];
  }

mp_stickers.prototype.assignSticker = function(stickerDIV, dropTarget) {
  for (var i=0; i<this.stickerList.length; i++)
    if (this.stickerList[i].id == stickerDIV.id) {
      this.stickerList.splice(i, 1);
      this.redisplay();
      break;
      }
  mp_stickers.assigned[stickerDIV.id] = new mp_stickers.stickerNodes(stickerDIV, dropTarget);
  }

mp_stickers.prototype.snapSticker = function(stickerIdx) {
  var colNum = stickerIdx % this.colsPerRow;
  var rowNum = Math.floor(stickerIdx / this.colsPerRow);
  jt_moveTo(this.stickerList[stickerIdx], (colNum * (mp_stickers.colWidth + this.colGap)) + this.colGap, this.yOff + (rowNum * mp_stickers.rowOffset) + 10);
  var minBodyHeight = ((rowNum+1) * mp_stickers.rowOffset) + 25;
  if (this.panelBODY.offsetHeight < minBodyHeight) this.panelBODY.style.height = minBodyHeight + "px";
  }

mp_stickers.prototype.redisplay = function() {
  this.panelBODY.style.height = "30px";
  for (var i=0; i<this.stickerList.length; i++) this.snapSticker(i);
  }

mp_stickers.prototype.resize = function() {
  if (mp_stickers.stickerList || mp_stickers.assignedStickers) { // don't re-size banner if no stickers!
    if( this.panelDIV && this.panelBODY ) {
	    var panelPos = jt_getOffsetXY(this.panelDIV);
	    var bodyPos = jt_getOffsetXY(this.panelBODY);
	    this.yOff = bodyPos.y - panelPos.y;
	    this.colsPerRow = Math.floor( (this.panelDIV.offsetWidth - 10) / (mp_stickers.colWidth + 10) );
	    this.colGap = Math.floor( (this.panelDIV.offsetWidth - (this.colsPerRow * mp_stickers.colWidth)) / (this.colsPerRow+1) );
	    this.redisplay();
	  }
    }
  }

mp_stickers.stickerPanelY = function(yPos) {
  jt_moveTo(mp_stickers.lostStickerBgDIV, 0, yPos);
  }

mp_stickers.stickerNodes = function(stickerDIV, panelDIV) {
  this.stickerDIV = stickerDIV;
  this.panelDIV = panelDIV;
  }

mp_stickers.findStickers = function(pageNum) {
  var numStickers = jt_objLength(mp_stickers.assigned);
  if (numStickers > 0) {
    mp_stickers.panelOn = true;
    jt_ShowNoneElm(pageToolsDIV, false);
    mp_stickers.stickerPanelY(document.body.scrollTop);
    jt_ShowNoneElm(mp_stickers.lostStickerBgDIV, true, "block");
    mp_stickers.lostStickerBgDIV.divClose = mp_stickers.restoreStickers;
    jt_okPanels.mp_findStickers = mp_stickers.lostStickerBgDIV;
    jt_ShowNoneElm(mp_stickers.lostStickerMsgDIV, true, "block");

    var colsPerRow = Math.floor( (document.body.offsetWidth - 10) / (mp_stickers.colWidth + 10) );
    var rowsPerPage = Math.floor( (document.body.clientHeight - 10 - 20) / (mp_stickers.rowOffset + 10) );
    var stkrsPerPage = rowsPerPage * colsPerRow;

    var npMsg = ""; // next/prev msg & links
    if (numStickers > stkrsPerPage) {
      var totPages = Math.ceil(numStickers / stkrsPerPage);
      npMsg = "&nbsp;(page " + (pageNum+1) + " of " + totPages + ",";
      if (pageNum != 0) npMsg += ' <a href="javascript:mp_stickers.findStickers(' + (pageNum-1) + ')">prev</a>';
      if (pageNum != (totPages-1)) npMsg += ' <a href="javascript:mp_stickers.findStickers(' + (pageNum+1) + ')">next</a>';
      npMsg += ")";
      numStickers = stkrsPerPage;
      }
    document.getElementById("lostStickerNB").innerHTML = npMsg;

    var needWidth = numStickers * (mp_stickers.colWidth + 10) + 10;
    if (needWidth > document.body.offsetWidth) mp_stickers.lostStickerBgDIV.style.width = colsPerRow * (mp_stickers.colWidth + 10) + 10;
    else if (needWidth > mp_stickers.lostStickerBgDIV.offsetWidth) mp_stickers.lostStickerBgDIV.style.width = needWidth + "px";
    mp_stickers.lostStickerBgDIV.style.height = "0px";

    var stickerIdx = 0;
    var pageIdx = 0;
    for (var prop in mp_stickers.assigned) with (mp_stickers.assigned[prop]) {
      if (Math.floor(stickerIdx / stkrsPerPage) == pageNum) {
        swapSrc(stickerDIV, "altSrcA");
        stickerDIV.style.zIndex = 2900;
        stickerDIV.style.width = "100px";
        stickerDIV.style.border = "1px solid #7fff00";
        stickerDIV.xTrue = stickerDIV.offsetLeft;
        stickerDIV.yTrue = stickerDIV.offsetTop;
        stickerDIV.fsOnDisplay = true;

        var colNum = pageIdx % colsPerRow;
        var rowNum = Math.floor(pageIdx / colsPerRow);
        var xPos = (colNum * (mp_stickers.colWidth + 10)) + 10;
        var yPos = 20 + (rowNum * mp_stickers.rowOffset) + 10;
        mp_stickers.lostStickerBgDIV.appendChild(stickerDIV);
        jt_moveTo(stickerDIV, xPos, yPos);
        var minBodyHeight = ((rowNum+1) * mp_stickers.rowOffset) + 35;
        if (mp_stickers.lostStickerBgDIV.offsetHeight < minBodyHeight) mp_stickers.lostStickerBgDIV.style.height = minBodyHeight + "px";
        pageIdx++;
        }
      else if (mp_stickers.assigned[prop].stickerDIV.fsOnDisplay) mp_stickers.restoreOneSticker(mp_stickers.assigned[prop]);
      stickerIdx++;
      }
    }
    else alert("You have no stickers assigned.");
  }

mp_stickers.showStickerLoc = function(ev) {
  if (mp_stickers.panelOn) {
    var e = jt_fixE(ev);
    var node = e.target ? e.target : e.srcElement;
    if (mp_stickers.assigned[node.id]) { // sticker is assigned (ignore stickers in tray!)
      var anchorPos = jt_getOffsetXY(mp_stickers.assigned[node.id].panelDIV);
      var newY = anchorPos.y + node.yTrue;
      jt_moveTo(mp_stickers.lostStickerShowDIV, anchorPos.x + node.xTrue, newY);
      jt_ShowNoneElm(mp_stickers.lostStickerShowDIV, true, "block");
      if ((newY + mp_stickers.lostStickerShowDIV.offsetHeight) > (document.body.scrollTop + document.body.clientHeight)) {
        window.scrollTo(0, newY - document.body.clientHeight + mp_stickers.lostStickerBgDIV.offsetHeight);
        mp_stickers.stickerPanelY(document.body.scrollTop);
        }
      else if (newY < document.body.scrollTop) {
        if (newY + mp_stickers.lostStickerShowDIV.offsetHeight < document.body.clientHeight) newY = 0; // jump to top of page
        window.scrollTo(0, newY);
        mp_stickers.stickerPanelY(newY);
        }
      }
    }
  }

mp_stickers.hideStickerLoc = function() {
  jt_ShowNoneElm(mp_stickers.lostStickerShowDIV, false);
  }

mp_stickers.restoreOneSticker = function(sNodes) {
  sNodes.panelDIV.appendChild(sNodes.stickerDIV);
  jt_moveTo(sNodes.stickerDIV, sNodes.stickerDIV.xTrue, sNodes.stickerDIV.yTrue);
  sNodes.stickerDIV.style.width = "";
  sNodes.stickerDIV.style.border = "";
  swapSrc(sNodes.stickerDIV, "altSrcB");
  sNodes.stickerDIV.fsOnDisplay = false;
  }

mp_stickers.restoreStickers = function() {
  if (mp_stickers.panelOn) {
    mp_stickers.panelOn = false;
    if (jt_okPanels.mp_findStickers) delete jt_okPanels.mp_findStickers;
    mp_stickers.hideStickerLoc();
    jt_ShowNoneElm(mp_stickers.lostStickerBgDIV, false);
    jt_ShowNoneElm(mp_stickers.lostStickerMsgDIV, false);
    jt_ShowNoneElm(pageToolsDIV, true);
    for (var prop in mp_stickers.assigned) {
      if (mp_stickers.assigned[prop].stickerDIV.fsOnDisplay && !mp_stickers.assigned[prop].stickerDIV.nowDragging) {
        mp_stickers.restoreOneSticker(mp_stickers.assigned[prop]);
      }
    }
  }
}

mp_stickers.lostStickerMsgDIV;
mp_stickers.lostStickerBgDIV;
mp_stickers.lostStickerShowDIV;

mp_stickers.assigned = new Object();
mp_stickers.panelOn = false;
mp_stickers.colWidth = 100;
mp_stickers.rowOffset = 90;

mp_stickers.stickerList = "";
mp_stickers.assignedStickers = "";

