/**
 * @author Andrew Masri
 */

//called on document ready
//this handler monitors the browser address bar for any change in the url fragment after the #
//this site uses # url fragments to record AJAX page content changes without the url changing.  This allows proper bookmarking and browser history 
//jQuery(window).load(function () {
function urlHandler() {
	
	checkHash();	//on page (re)load
	window.onhashchange = checkHash; //when hash portion of url changes (supported in IE8, FF3.6, Safari5, Chrome)
	
	// Poll the hash every 1000 milliseconds for the benefit of older browsers
	window.setInterval(checkHash, 1000);
}



var oldHash = false;
var newHash = false;
var retries = 0;

function checkHash() {	
	// Grab the hash
	newHash = document.location.hash;

	// Check to see if it changed
	if ((newHash || oldHash) && (newHash != oldHash)) {
	  	oldHash = newHash;	// Update the oldHash for the next check

		var newUrl = window.location.href.replace(/#none/, '').replace(/#/, '/');

		updateWebpage(window.location.href.replace(/#none/, '').replace(/#/, "/"), '#pageContainer', false, '.ajaxUpdate');	//update the current page's html/vars so that it becomes the new page
		
		updateSelectedTreeNode();		//retries in case the tree hasn't initiallized yet...
	} 
}



//update the dhtmlx menu tree node
function updateSelectedTreeNode() {
	if (typeof tree == 'undefined') return;
	
	treeNode = newHash.replace(/#page\//, "");
	if (treeNode != tree.getSelectedItemId()) {
		tree.clearSelection(tree.getSelectedItemId());	//unselect the existing tree node (this is for the case where the new node is not found) 
		tree.openItem(treeNode);
		tree.selectItem(treeNode, false);
		pageId = currentId = treeNode;
		if (++retries < 3) {
			setTimeout('updateSelectedTreeNode()', 1000); //retry in case the tree is not full loaded
		} else {
			retries = 0;
			log('System Error', 'Tree '+treeNode+' could not be selected');
		}
	} else {
		retries = 0;
	}
}




	
	




