_uacct = "UA-916042-9";
urchinTracker();

var pageIndex = 0;
var hashCache = new Array();
var currentHash = '';
var IE = (navigator.appName.indexOf('Microsoft')!=-1);
var IE6 = ((IE) && (navigator.appVersion.indexOf('6.')!=-1));
var editButton = null;
var editPassword ='';

// http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
function addEvent(obj, evType, fn){
 if (IE) { eval('obj.on'+evType+'='+fn); return true;}
 if (obj.addEventListener) { obj.addEventListener(evType, fn, false); return true; } 
 else if (obj.attachEvent) { var r = obj.attachEvent("on"+evType, fn); return r; } 
 else return false;
}
function $(id) {
	return document.getElementById(id);
}

// Add Event when page is loaded.
addEvent(window, 'load', function() {
	if (IE) {
		resizeIE();
		addEvent( document.getElementsByTagName('body')[0], 'resize', resizeIE );
	}
	aNodes = document.getElementsByTagName('a');
	for (var i=0; i<aNodes.length; i++)
		if (aNodes[i].href.indexOf('#')!=-1)
			addEvent( aNodes[i], 'click', navigation );
	hash = '';
	for (var i = 0; (i < $('Content').childNodes.length); i++) {
		myPage = $('Content').childNodes[i];
		if (myPage.firstChild) {
			hash = (myPage.firstChild.name?myPage.firstChild.name:hash);
			hashCache.push( hash );
		}
	}

	if (!IE) {
		editButton = document.createElement('div');
		editButton.className = 'editButton';
		document.getElementsByTagName('body')[0].appendChild(editButton);
		editButton.innerHTML = '<a href="#" onclick="return startEditor()">Edit</a>';
	}
	moveTo(location.hash.length<2?2:getHashIndex(location.hash));
/*	window.setInterval(function() {
		if (location.hash.substr(1,location.hash.length)!=currentHash)
			if (location.hash.substr(1,location.hash.length)!='') moveTo(getHashIndex(location.hash));
	}, 200);
*/
} );


function navigation() {
	this.blur();
	if ((this.id=='NavigationUp') || (this.id=='NavigationDown'))
		moveTo((this.id=='NavigationUp'?pageIndex-1:pageIndex+1));
	else
		moveTo(getHashIndex(this.href));
	return (!IE);
}

function moveTo(index) {
//	alert(index);
	if ((index >= 0) && index < getHashIndex('xxx')-1) {
		scroll(pageIndex * 550, index * 550, pageIndex * 550);
		pageIndex = index;
		window.setTimeout( function() {
			$('NavigationUp').href = '#' + (index>0?hashCache[index]:'');	
			$('NavigationDown').href = '#' + (index<hashCache.length-1?hashCache[index+2]:'');	
		}, 100);
		currentHash = hashCache[index];
		urchinTracker(currentHash);
	}
}

function scroll(start, stop, current) {
	if ((stop-current)*(stop-current) > 64) {
		current += (stop-start)/25;
		if (IE)
			$('Content').style.paddingTop = String(Math.round(9220 + (document.documentElement.clientHeight / 2) - current)) + 'px';
		else 
			$('Content').style.marginTop = Math.round(-current) + 'px';
		window.setTimeout( 'scroll('+start+','+stop+','+current+');', 40 );
	}
}

function getHashIndex(hashUrl) {
//	alert(hashUrl);
	target = hashUrl.substr(hashUrl.indexOf('#')+1, hashUrl.length);
	targetIndex = 0;
	for (var i = 0; (i < $('Content').childNodes.length); i++) {
		myPage = $('Content').childNodes[i];
		if (myPage.firstChild) if (myPage.firstChild.name) 
			if (myPage.firstChild.name == target) break;
		if (myPage.nodeType == 1) targetIndex++;
	}
	return targetIndex -1;
}

function resizeIE() {
	$('Container').style.height = document.documentElement.clientHeight -1;
	$('Container').style.marginTop =  - document.documentElement.clientHeight / 2;
	$('Container').style.backgroundPosition = 'center '+(document.documentElement.clientHeight-600) / 2+'px'; 
	$('Content').style.paddingTop = String(Math.round(9220 + (document.documentElement.clientHeight / 2) - pageIndex*550)) + 'px';
	$('ContentLinks').style.top = String(9852 + (document.documentElement.clientHeight / 2)) + 'px';
	$('NavigationDown').style.top = String(10220 + (document.documentElement.clientHeight / 2)) + 'px';
	aNodes = $('NavigationLinks').getElementsByTagName('a');
	for (i=0; i<aNodes.length; i++) aNodes[i].style.height = String((document.documentElement.clientHeight / 2) - 210) + 'px';
}

function startEditor() {
	if (!editPassword) editPassword = prompt('Enter Editor Password');
	if (editPassword) request('save.php?pass='+editPassword, '', function(responseText) {
		if (responseText!='accepted') return;
		if (document.getElementById('ContentBox').firstChild.nodeName == 'TEXTAREA')
			document.getElementById('ContentBox').innerHTML = document.getElementById('ContentBox').firstChild.value;
		editButton.innerHTML = '<a href="#" onclick="return save()" class="save">Save</a>';
//		editButton.innerHTML = '<a href="#" onclick="return editBody()" class="save">Edit Source</a><a href="#" onclick="return save()" class="save">Save</a>';

		divNodes = document.getElementsByTagName('div');
		for (i=0; i<divNodes.length; i++)
			if ((divNodes[i].className=='body') || (divNodes[i].className=='head'))
				divNodes[i].innerHTML = '<textarea class="editor">'+divNodes[i].innerHTML.replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</textarea>'; 
	} );
	return false;
}

function save() {
	if (document.getElementById('ContentBox').firstChild.nodeName == 'TEXTAREA') {
		document.getElementById('ContentBox').innerHTML = document.getElementById('ContentBox').firstChild.value;
	} else {
		divNodes = document.getElementsByTagName('div');
		for (i=0; i<divNodes.length; i++) 
			if ((divNodes[i].className=='body') || (divNodes[i].className=='head'))
				divNodes[i].innerHTML = divNodes[i].firstChild.value;
	}
	document.getElementsByTagName('body')[0].removeChild(editButton);

	content = 	'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n' +
				'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n' + document.documentElement.innerHTML + '\n</html>';

	request( 'save.php?pass='+editPassword, content, function(responseText) { location.reload(); } );

	return false;
}

function editBody() {
	divNodes = document.getElementsByTagName('div');
	for (i=0; i<divNodes.length; i++) 
		if ((divNodes[i].className=='body') || (divNodes[i].className=='head'))
			divNodes[i].innerHTML = divNodes[i].firstChild.value;
	
	document.getElementById('ContentBox').innerHTML = '<textarea class="editor">'+document.getElementById('Content').innerHTML.replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</textarea>'; 
	editButton.innerHTML = '<a href="#" onclick="return startEditor()" class="save">Edit Pages</a><a href="#" onclick="return save()" class="save">Save</a>';
	
	return false;
}

function request( url, data, onResponse) {
	xhr = new XMLHttpRequest();
	xhr.open("POST", url, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	xhr.onreadystatechange = function() { if(xhr.readyState == 4) onResponse(xhr.responseText); }
	xhr.send('data='+encodeURIComponent(data));
}