function pto_add_edit_hints(eo,tid) {
 if (eo.href.match(/_pto_width/)) return;
 var tgt=document.getElementById(tid);
 if (!tgt) return;
 if (document.all) style=tgt.currentStyle;
 else style=window.getComputedStyle(tgt,'');
// document.body.innerHTML+=(ct_dump_array(style));

 family=style.fontFamily;
 /*
 if (family.indexOf(';')) {
   arr=family.split(';');
   family=arr[0];
 }
 */

 buf=tgt.offsetWidth
     +';'+family
     +';'+style.fontSize
     ;
 eo.href+='&_pto_hints='+buf;
}

function pto_apply_edit_hints(oname,hints) {
  if (!hints) return;

  if (document.all) {
    var ve=document.getElementById('viz_'+oname);
    var eo=ve;
    var ofs=8;
  }
  else {
    var ve=document.getElementById(oname);
    var eo=ve.contentWindow.document.body;
    var ofs=0;
  }

  arr=hints.split(';');
  ve.style.width=1.0*arr[0]+ofs;
  eo.style.fontFamily=arr[1]; 
  eo.style.fontSize=arr[2];
  ve.className='content_area';  // 2008-03-17 joel 
  //document.body.innerHTML+=(ct_dump_array(window.getComputedStyle(eo,'')));
  //document.body.innerHTML+=(ct_dump_array(eo.currentStyle));
}

function ct_resize_ve(oname,dx,dy) {
  if (document.all) {
    oname='viz_'+oname;
    width=document.body.clientWidth;
    height=document.body.clientHeight;
    ve=document.getElementById(oname);
    ve.style.overflowX='auto';
  }
  else {
    width=window.innerWidth;
    height=window.innerHeight;
    ve=document.getElementById(oname);
  }
  if (dx!='keep') ve.style.width=dx;
  if (dy!='keep') ve.style.height=dy;
}

function ct_apply_size(ox,oy,dx,dy) {
  ox.value=dx;
  oy.value=dy;
  ox.onchange();
  oy.onchange();
}

function ct_textarea_move_to_end(ta) {
  ta.scrollTop = ta.scrollHeight;
  // move caret to end
  if (ta.createTextRange) {
    var range = ta.createTextRange();
    range.collapse(false);
    range.select();
  }
}

// ------------ page note
function toggle_note(btn,url) {
  pt_note_sticky=!pt_note_sticky;
  if (pt_note_sticky) btn.src='/pix/menu/pindown.gif';
  else btn.src='/pix/menu/pinup.gif';
  url+=pt_note_sticky?1:0;

  loadDoc(url,function(res){ });
//  document.body.innerHTML+="debug: "+ct_dump_array(btn.style);
}

function pt_note_show(oname,hotspot) {
  obj=document.getElementById(oname);
  ctmenu.show(oname,hotspot,'bottom');
  // need mouseout to check stickiness
  obj.onmouseout2=ctmenu._onmouseout;
  obj.onmouseout=function(e){ if (!pt_note_sticky) this.onmouseout2(e); }
}
function pt_note_hide(oname) {
  if (!pt_note_sticky) ctmenu.hide(oname);
}

// ------------ edit mode
function toggle_edit(obj,url) {
  pt_edit_mode=!pt_edit_mode;

  // if (pt_edit_mode) obj.innerHTML=obj.innerHTML.replace(/ON/,'OFF');
  // else obj.innerHTML=obj.innerHTML.replace(/OFF/,'ON');

  if (pt_edit_mode) obj.innerHTML=obj.innerHTML.replace(/off/,'on');
  else obj.innerHTML=obj.innerHTML.replace(/on/,'off');

  newval=pt_edit_mode?'inline':'none';

  // 2008-07-25 joel: rid of hori scrollbar
  if (obj=document.getElementById('edit_obj_buttons')) obj.style.display=newval;

  arr=document.getElementsByName('edit_obj');
  for ( i = 0; i < arr.length; i++ ) arr[i].style.display=newval;

  // 2007-10-28 jl: unnamable objs
  for (i = 0; i < 10; i++ ) 
    if (obj=document.getElementById('edit_obj_'+i)) obj.style.display=newval;

  // note menu
  var anmenu=document.getElementById('notemenu');
  if (anmenu) {
    // alert('sticky:'+pt_note_sticky+' edit:'+pt_edit_mode);
    if (pt_note_sticky && pt_edit_mode) {
      var anhotspot=document.getElementById('note_hotspot');
      pt_note_show('notemenu',anhotspot);
    }
    else anmenu.style.visibility='hidden';
  }

  netbar_view(pt_edit_mode?false:true); // reverse

  loadDoc(url+newval, function(res){ });
//  document.body.innerHTML+="debug: "+ct_dump_array(obj.style);
}

function netbar_view(viz) {
  // netbar
  var nbobj=document.getElementById('netbar');
  if (nbobj) {
    nbviz=viz?'inline':'none'; // opposite
    nbobj.style.display=nbviz;

    var bodyobj=document.getElementsByTagName('body');
    bodyobj[0].style.backgroundPositionY=viz?30:0;

    var ncobj=document.getElementById('netcontent');
    if (ncobj) {
      ncobj.style.top=viz?30:0;
      ncobj.style.position=viz?'relative':'static';
    }
    // alert(ncobj.style.top);
  }
} 
