var http = getHTTPObject();
var previousPage = '';
var expectedHash = '';

function hide() {
	$('content').hide();
	$('content').innerHTML='';
	$('loading_bar').show();
}

function show(text) {
	$('content').innerHTML=text;
	window.setTimeout("showNow()", 500);
}

function showNow() {
	$('loading_bar').hide();
	Effect.BlindDown('content', { duration: 1.0 });
}

function getHTTPObject() {
	var xmlhttp;
	try {	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
		try	{	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");	}
		catch(oc)	{	xmlhttp=null;	}
	}
	if(!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); }
	return xmlhttp;
}
function doSearch(page) {
	if (http != null && previousPage != page) {
		previousPage=page;
		window.location.hash = page;
		hide();
		http.open('GET','/?page='+page,true);
		http.onreadystatechange=handleHttpResponse;
		http.send(null);
	}
}
function doTitle(page) {
	if (http != null) {
		http.open('GET','/?title='+page,true);
		http.onreadystatechange=handleHttpResponseTitle;
		http.send(null);
	}
}
function handleHttpResponse() {
	if(http.readyState==4) {
		if(http.responseText!='') { text=http.responseText; }
		else { text='<h1>Unknown page.</h1>'; }
		doTitle(previousPage);
		show(text);
	}
}
function handleHttpResponseTitle() {
	if(http.readyState==4) {
		title='Balamand Educational Foundation';
		if(http.responseText!='') { title=http.responseText; }
		document.title=title;
	}
}

function handleHistory()
{
  if ( window.location.hash != expectedHash )
  {
    expectedHash = window.location.hash;
    var newoption = expectedHash.substring(1);
    if (newoption == "") {
    	newoption = 'about';
    }
    doSearch( newoption );
  }
  return true;
}

function pollHash() {
  handleHistory();
  window.setInterval("handleHistory()", 1000);
  return true;
}
