var currentLocation;

function displayLocation(newLocation) {
    var allDivs = document.getElementsByTagName('div');
    // Within class 'mainBody', show only a div with id==newLocation
    for( i=0; i<allDivs.length; i++) {
	if(allDivs[i].className=='mainBody') {
	    if(allDivs[i].id==newLocation) {
		allDivs[i].style.display='block';
		currentLocation = newLocation;
	    } else {
		allDivs[i].style.display='none';
	    }
	}
    }
}

function debug(str) {
    //alert(str);
    //window.status = str;

    // Use Firebug console
    console.log(str);
}

function enableFirebugConsole() {
    try {
	console.assert(1);
    } catch(e) {
	if (typeof loadFirebugConsole == 'function') {
	    loadFirebugConsole();
	}  else {
	    console = {
		log: function(arg) {},
		debug: function() {},
		info: function() {},
		warn: function() {},
		assert: function() {}
	    }
	}
    }
}

function showLocation(newLocation) {
    debug('showLocation: ' + newLocation);	
    var element = document.getElementById(newLocation);
    if(!element || element.tagName != 'DIV' || element.className != 'mainBody') {
	debug('Location ' + newLocation + ' does not exist');
    } else {
	displayLocation(newLocation);
    }
}

function myHashListener() {
    var date = new Date();
    debug('myHashListener: ' + date + '\n' +
	  ' currentLocation: ' + currentLocation +'\n' +
	  ' window.location: '+ window.location +'\n' +
	  ' location: ' + location + '\n' +
	  ' document.URL: ' + document.URL + '\n');
    var hash = window.location.hash;
    if(hash != currentLocation) {
	if( hash=='') {
	    newLocation = 'mainDivLocation';
	} else {
	    newLocation = hash.substr(1);
	}
	showLocation(newLocation);
    }
}

function initialize() {

    // Firebug stuff 
    enableFirebugConsole();

    window.onload = function() {
	debug('onload handler called');
	window.setInterval(myHashListener, 200);
	return false;
    }
}

function addLink(newLocation) {
    showLocation(newLocation);
    return false;
}