// bob4: contentEdible on denis
// This is a forked, much-simplified version for my DENIS About Me profile, then temporarily used for extinguishedscholar wordpress.
// (c) 2006 by nathan vander wilt. All rights reserved.
//
// In the future I may license this under different terms. Until then, please do not use or reuse.
// natevw at yahoo dot com
// http://extinguishedscholar.com

kBackspaced=1;
kSpecialKey=2;

window.onload=init_things;
window.onunload=save_text;
blinker=0;
flags=0;

function init_things() {
 field=getByID('nvw_contentEdible');
 if( !field ) return;

 var intxt=readCookie('nvw-aboutme');
 if (intxt) {
  intxt=unescape(intxt);
  intxt+='<div id="nvw_cursor">|</div>';
  field.innerHTML=intxt;
 }
 
 cursor=getByID('nvw_cursor');
 if (!cursor) return;
 cursor.style.visibility="hidden";
 field.onclick=function(e) { start_edit(e); };

}


function start_edit(e) {
 document.onkeydown=checkit;
 document.onkeypress=typeit;
 document.onclick=function(e) { end_edit(e); }
 cursor.style.display="inline";
 if (!blinker) blinky();
}

function end_edit(e) {
 if (!e) var e = window.event;
 if (e.keyCode) code=e.keyCode;
 else if (e.which) code=e.which;
 var tg = (e.srcElement) ? e.srcElement : e.target;
 if (!tg || tg.id=="nvw_contentEdible") return;
 document.onkeydown=null;
 document.onkeypress=null;
 document.onclick=null;
 if (blinker) window.clearTimeout(blinker);
 blinker=0;
 cursor.style.display="none";
 save_text();
}


function save_text() {
 txt=field.innerHTML;
 txt=txt.replace(/\<(DIV|div)[\s|\!-\~]*nvw_cursor[\s|\!-\~]*\<\/(DIV|div)\>/, '');
 if (escape(txt.length)>3900) {
   txt=txt.substr(0,3500)+"...";
 }
 createCookie('nvw-aboutme',escape(txt),31);
}

function checkit(e) {  // We need to swallow the backspace event and detect special keys
 flags=0;
 var code;
 if (!e) var e = window.event;
 if (e.keyCode) code=e.keyCode;
 else if (e.which) code=e.which;
 else if (e.charCode) code=e.charCode;


 switch (code) {
  case 8:
   backspace();
   flags|=kBackspaced;
   return false;
  case 37: // arrow left
  case 38: // arrow up
  case 39: // arrow right
  case 40: // arrow down
  case 46: // delete
   flags|=kSpecialKey;
   if ( inUA('msie') && !inUA('opera') ) typeit(e);
 }
}

function inUA(str) {
 return (navigator.userAgent.toLowerCase().search(str) > 0);
}

function typeit(e) {		// (c) 2006 nathan vander wilt, as is most of this file.
 var code;
 var retval=false;
 if (!e) var e = window.event;
 
 if (e.keyCode) code=e.keyCode;
 else if (e.which) code=e.which;
 else if (e.charCode) code=e.charCode;

 if (e.ctrlKey || e.metaKey) return true;

 var to_add="";
 
 switch (code) {
  case  6:
  case  9:
  case 16:
  case 17:
  case 18:
  case 27:
  case 63232:
  case 63233:
  case 63236:
  case 63237:
  case 63238:
  case 63239:
  case 63240:
  case 63241:
  case 63242:
  case 63243:
  case 63244:
  case 63245:
  case 63246:
  case 63247:
  case 63248:
  case 63273:
  case 63275:
  case 63276:
  case 63277:
   break;
  case 63234:
   cursor_left();
   break;
  case 63235:
   cursor_right();
   break;
  case 63272:
   cursor_right();
   backspace();
   break;
  case 37:
   if (flags & kSpecialKey) {
    cursor_left();
	break;
   }
  case 63:
  case 33:
  case 34:
  case 35:
  case 36:
  case 38:
  case 40:
   if (e.shiftKey) to_add+=String.fromCharCode(code);
   break;
  case 46:
   if (flags & kSpecialKey) {
    cursor_right();
	backspace();
   }
   else to_add+=String.fromCharCode(code);
   break;
  case 39:
   if (flags & kSpecialKey) {
    cursor_right();
	break;
   }
   else to_add+=String.fromCharCode(code);
   break;
  case 191:
   if (e.altKey) to_add+=String.fromCharCode(code);
   break;
  case 8:
   if (!(flags & kBackspaced)) backspace();
   break;
  case 32:
  case 13:
   to_add+=" "
   break;

  default:
   to_add+=String.fromCharCode(code);
 }

 insert_text(to_add);
 
 flags^=kBackspaced;

 return retval;
}

function insert_text(text) {
 var loc=cursor;
 prevText(loc).nodeValue=prevText(loc).nodeValue+text;
}

function backspace() {
 var loc=cursor;
 var prev=prevText(loc).nodeValue;
 prevText(loc).nodeValue=prevText(loc).nodeValue.substring(0,prev.length-1);
}

function prevText(node) {
 if (!node.previousSibling) return;
 if (node.previousSibling.nodeType==3) return node.previousSibling; 
}

function nextText(node) {
 if (!node.nextSibling) return;
 if (node.nextSibling.nodeType==3) return node.nextSibling;
}

function cursor_left() {
 var loc=cursor;
 var prev=prevText(loc).nodeValue;
 if (!prev.length) return;
 if (!nextText(loc)) {
  var nnext=document.createTextNode(".");
  field.appendChild(nnext);
  nnext.nodeValue="";
 }
 var next=nextText(loc).nodeValue;
 var move=prev.substr(prev.length-1,1);
 nextText(loc).nodeValue=move+next;
 prevText(loc).nodeValue=prev.substring(0,prev.length-1);
}

function cursor_right() {
 var loc=cursor; 
 var next=nextText(loc).nodeValue;
 if (!next.length) return;
 var prev=prevText(loc).nodeValue;
 var move=next.substring(0,1);
 nextText(loc).nodeValue=next.substring(1);
 prevText(loc).nodeValue=prev+move;
}

function blinky() {
 if (!cursor) return; 
 if (cursor.style.visibility!="hidden") cursor.style.visibility="hidden";
 else  cursor.style.visibility="visible";
 blinker=window.setTimeout('blinky()',500);
}

function getByID(n){
  with (window.document) {
	if (getElementById) return getElementById(n);
	else if (all) return all[n];
    else if (layers) return layers[n];
  }
}

// cookie wrappers from Scott Andrew / ppk
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//#nvw_cursor { display:inline; margin: 0px 0px 0px 0px; color: black; text-decoration: none; }

