var writeContent = {
  // offX and offY can be numbers or "c"
  offX: 20,
  offY: 20,
  dragId:   "dragDiv",  // id of positioned div to be dragged
  handleId: "",         // optional, place null or "" if no handle
  writeId:  "",    // optional, will write to dragId if null or empty string
  
  draggable: false,   // set true once dragObj.init called
  hideFlag: true,     // used in checkHide (document click)
  
  init: function() {   // initialize for dragging
    if (this.handleId) dragObj.init(this.handleId, this.dragId);
    else dragObj.init(this.dragId);
    this.draggable = true;
    // add handlers for hiding layer (esc key and doc click)
//    fww_event.add( document, "mousedown",   writeContent.checkHide, false );
    fww_event.add( document, "click",   writeContent.checkHide, false );
    fww_event.add( document, "keydown", writeContent.checkKey,  true );
  },
  
  // called onclick of links (from wrapIt)
  set: function(e, cntnt, wd, offx, offy) {
    this.hideFlag = false;  // click on link to show layer is also document click, which would hide it
    var wobj = this.writeId? document.getElementById( this.writeId ): document.getElementById( this.dragId );
    var dobj = document.getElementById( this.dragId );
    if ( !this.draggable ) this.init();
    this.hide();
    wobj.innerHTML = cntnt;
    
    if (wd) {
      // wd might be width of image, so add border and padding
      // rely on styles set inline (or lengthy code needed)
      var bw = dobj.style.borderWidth? parseInt(dobj.style.borderWidth): 0;
      var pw = wobj.style.padding? parseInt(wobj.style.padding): 0;
      wd += 2 * bw + 2 * pw;
      dobj.style.width = wd + "px"; 
    }
    this.positionIt(e, dobj, offx, offy);
  }, 
  
  positionIt: function(e, o, offx, offy) {
    var x=0, y=0; viewport.getAll();
    // check positioning choices
    if ( this.offX == "t" ) {
      x = 0
    } else {  // use mouse location onclick to position
      x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
      offx = offx || this.offX;  // check for passed offsets
      if ( x + o.offsetWidth + offx > viewport.width + viewport.scrollX ) 
        x = viewport.width + viewport.scrollX - o.offsetWidth;
      else x = x + offx;
    }
    
    if ( this.offY == "t" ) {
      y = 0
    } else {
      y = e.pageY? e.pageY: e.clientY + viewport.scrollY; 
      offy = offy || this.offY; 
      if ( y + o.offsetHeight + offy > viewport.height + viewport.scrollY )
        y = viewport.height + viewport.scrollY - o.offsetHeight;
      else y = y + offy;
    }
    o.style.left = x + "px"; o.style.top = y + "px";
    document.getElementById(this.dragId).style.visibility = "visible";
    setTimeout("writeContent.hideFlag = true",200);  // delayed until after checkHide 
  },
  
  checkKey: function(e) { // check for esc key
    e = e? e: window.event;  if ( e.keyCode == 27 ) writeContent.hide();
  }, 

  // doc click hides
  checkHide: function(e) { 
    fww_event.DOMit(e);
    // hide the layer if you click anywhere in the document 
    // except a link that displays the layer (hideFlag), or on the layer itself, 
    // unless that click on the layer is on the layer's close box    
    if (e.tgt.nodeType && e.tgt.nodeType == 3) e.tgt = e.tgt.parentNode;  // text node?
    if ( contained( e.tgt, document.getElementById("dragDiv") ) ) {
      if ( e.tgt.tagName && e.tgt.tagName == "IMG" ) writeContent.hide(); 
      if ( e.tgt.tagName == "A" && e.tgt.href.indexOf("writeContent.hide") != -1 ) writeContent.hide();
      else return;
    }
    if (writeContent.hideFlag) writeContent.hide();
  },

  hide: function() { document.getElementById(writeContent.dragId).style.visibility = "hidden"; }
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  while ( oNode.parentNode ) {
    oNode = oNode.parentNode;
    if ( oNode == oCont ) return true;
  }
  return false;
}


