// Requires Prototype 1.6.0 to be included...
// Copyright pi@parsimonious.org
var basePages = "";
var firstHash = "";

window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        }
        , fromJSON: function(s) {
                return s.evalJSON();
        }
});

window.onload = function() {
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(handleHistoryChange);
        
        // determine our current location so we can
        // initialize ourselves at startup
        var initialLocation = dhtmlHistory.getCurrentLocation();

        // if no location specified, use the default
        if (initialLocation.length == 0) {initialLocation = basePages; location.hash = basePages;}

        // now initialize our starting UI
        updateUI(initialLocation);
};

function addBaseDivContent(id, page) {
        if(firstHash.length == 0) {
	        firstHash = location.hash;
        }
        var hash = firstHash;
	id = id + "-";
        if(hash != "") {
	        if(hash.indexOf(id) != -1) {
		        page = hash.substring(hash.indexOf(id));
		        page = page.substring(page.indexOf("-") + 1);
		        if(page.indexOf(";") != -1) {
		                page = page.substring(0, indexOf(";"));
		        }
	        }
        }
        if(basePages.length == 0) {
                basePages = id + page;
        } else {
                basePages += ";" + id + page;
        }
	location.hash = basePages;
}

function handleHistoryChange(newLocation, historyData) {
        // use the history data to update our UI
        if(newLocation == null || newLocation.length == 0) {
                updateUI(basePages);
        } else {
                updateUI(newLocation);
        }
}

function load(id, page)
{
        var cur = dhtmlHistory.getCurrentLocation();
        if(cur.length == 0) {cur = basePages;}
        if(cur.indexOf(id + '-') != -1) {
                var next = cur.substring(0, cur.indexOf(id + '-')) + id + '-' + page;
                cur = cur.substring(cur.indexOf(id + '-'));
                if(cur.indexOf(';') != -1)
                {
                        cur = cur.substring(cur.indexOf(';'));
                        next = next + cur;
                }
                cur = next;
        } else {
                cur = cur + ';' + id + '-' + page;
        }
        dhtmlHistory.add(cur, null);
        updateUI(cur);
}

function updateUI(loc)
{
        var hash = loc;
        if(hash.indexOf('-') != -1)
        {
                while(hash.indexOf(';') != -1)
                {
                        id = hash.substring(0, hash.indexOf('-'));
                        page = hash.substring((hash.indexOf('-')) + 1, hash.indexOf(';'));
			if(id == "ff") {
				var p = "/scripts/where.pl";
				new Ajax.Updater("where", p, {parameters: { page: page } });
			}	
                        new Ajax.Updater(id, page);
                        hash = hash.substring(hash.indexOf(';') + 1);
                }
                if(hash.indexOf('-') != -1) {
                        id = hash.substring(0, hash.indexOf('-'));
                        page = hash.substring((hash.indexOf('-')) + 1);
                        new Ajax.Updater(id, page);
			if(id == "ff") {
				var p = "/scripts/where.pl";;
				new Ajax.Updater("where", p, {parameters: { page: page} });
			}	
                }
        }
}
