
var gSaveTimeout, gSavedNoteContent;
var gSaveInProgress = false;
var gDefaultText = 'Type your own notes about this page here. You can access all your notes from your member home page. Everything you write is saved automatically.';

function getXHR()
{
	var xh=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	 try {
	  xh = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xh = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xh = false;
	  }
	 }
	@end @*/
	if (!xh && typeof XMLHttpRequest!='undefined') {
	  xh = new XMLHttpRequest();
	}
	return xh;
}

function httpPost(url,data,handler)
{
	var xh = getXHR();
	var s='';
	xh.open("POST", url,true);
	xh.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xh.onreadystatechange=function() { handler(xh); };
	var d = new Array();
	for(var i in data) d[d.length] = encodeURIComponent(i)+'='+encodeURIComponent(data[i]);
	xh.send(d.join('&'));
}

function httpGet(url,data,handler)
{
	var xh = getXHR();
	var s='';
	var d = new Array();
	if(data) for(var i in data) d[d.length] = encodeURIComponent(i)+'='+encodeURIComponent(data[i]);
	var q = d.join('&');
	if(q) q = '?'+q;
	xh.open("GET", url+q,true);
	xh.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xh.onreadystatechange=function() { handler(xh); };
	xh.send(null);
}

function initHandler(xh)
{
	if(xh.readyState==4)
	{
		var i;
		var n = xh.responseText;
		var ln = document.createElement('div');
		ln.id = 'notes';
		ln.innerHTML = n;
		if(location.pathname.search('/members')!=0 && document.getElementsByTagName('p').length > 2 && (i = document.getElementsByTagName('p').item(0).nextSibling)) i.parentNode.insertBefore(ln,i);
		else if(i = document.getElementById('xtoc')){ i.parentNode.insertBefore(ln,i); i.style.display = 'none'; i.style.display = 'block';}
		else if(i = document.getElementById('breadcrumbs').nextSibling) i.parentNode.insertBefore(ln,i);
		var noteBox;
		if(noteBox = document.getElementById('note_content'))
		{
			gSavedNoteContent = noteBox.value;
			noteBox.onfocus = startSaveTimer;
			noteBox.onblur = saveNote;
			if(location.pathname.search('/members')==0 && (!gSavedNoteContent || gSavedNoteContent==gDefaultText)) document.getElementById('note_wrapper').className = ''; //default to collapsed for members pages
			document.fspUpdate(ln);
		}
	}
}

function startSaveTimer()
{
	if(this.value==gDefaultText) { this.value=''; gSavedNoteContent = ''; }
	gSaveTimeout = setTimeout('clearTimeout(gSaveTimeout);maybeSaveNote()',10*60*1000);
}

function maybeSaveNote()
{
	if(!gSaveInProgress) saveNote();
}

function saveNote()
{
	if(gSaveTimeout) clearTimeout(gSaveTimeout);
	if(document.getElementById('note_content'))
	{
		var v = document.getElementById('note_content').value;
		if(v!=gSavedNoteContent)
		{
			var data = new Array();
			data['path'] = window.location.pathname + window.location.search;
			data['content'] = v;
			data['title'] = document.title.substr(0,document.title.search(' \:'));
			gSaveInProgress = true;
			httpPost('/notes',data,confirmSave);
		}
	}
}

function confirmSave(xh)
{
	if(xh.readyState==4)
	{
		gSaveInProgress = false;
		eval(xh.responseText);
		startSaveTimer();
	}
}

function noteInit()
{
	var data = new Array();
	data['path'] = location.pathname + location.search;
	try{
	httpGet('/notes',data,initHandler);
	} catch(e) {}
}

window.onload = noteInit;
window.onbeforeunload = saveNote;