function Session()
{
    this.properties = new Object();
    function sessionInitialize()
    {
        var pt = Session.prototype;
        pt.load = function load(location) {
            var values = location.search.substring(1);
            if (values) {
                var pairs = values.split(/\&/);
                for (var i in pairs) {
                    var nameVal = pairs[i].split(/\=/);
                    if (nameVal[0] && nameVal[1]) {
                        this.properties[unescape(nameVal[0])] = unescape(nameVal[1]);
                    }
                }
            }
            this.search = location.search.substring(1);
            this.loaded = true;
        }
        pt.store = function store(location) {
	    var queryString = "?";
	    var empty = true;
            var sep = "";
            for (key in this.properties) {
                queryString += sep + escape(key) + "=" + escape(this.properties[key]);
                sep = "&";
		empty = false;
            }
	    if (! empty) {
		location.search = queryString;
	    }
        }
        pt.setProperty = function setProperty(name, value) {
            this.properties[name] = value;
        }
        pt.getProperty = function getProperty(name) {
            return this.properties[name];
        }
	pt.isPropertyDefined = function isPropertyDefined(name) {
	    return this.getProperty(name) !== undefined;
	}
	pt.isPropertyTrue = function isPropertyTrue(name) {
	    var value = this.getProperty(name);
	    var bValue = (value == "true");
	    // Boolean seems to not be working the way it is advertised
	    //alert("Getting " + name + " value=" + value + " bValue=" + bValue);
	    //return Boolean(this.getProperty(name));
	    return bValue;
	}
        pt.dump = function dump(displayFunction) {
            for (var key in this.properties) {
                displayFunction(key, this.properties[key]);
            }
        }
        pt.initialized = true;
    }
    if (!Session.prototype || !Session.prototype.initialized) {
        sessionInitialize();
    }
}
 
/***************/
/* Methods that belong in an application specific file */
function onPageLoad() {
    sc = new Session();
    sc.load(window.location);
    if (! window.sc.isPropertyDefined("popups")) {
	window.sc.setProperty("popups", doPopUpsWork());
    }
    if (window.name != "PictureWindow" && window.name != "MainWindow") {
	window.name = "MainWindow";
    }
}

function usePictureWindow(location) {
    var followLink = true;
    var popOn = window.sc.isPropertyTrue("popups")
    if (window.sc.isPropertyTrue("popups")) {
	var saveSession = new Object;
	window.sc.store(saveSession);
        var height = window.screen.availHeight
        var width = window.screen.availWidth
	var href = addSearchToLocation(location, saveSession);
        var newWin = window.open(href, "PictureWindow",
            "fullscreen=yes,width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes");
        if (newWin) {
            newWin.focus();
            followLink = false;
        }
        else {
            window.sc.setProperty("popups", false);
            window.sc.store(location);
        }
    }
    else {
        window.sc.store(location)
    }
    return followLink;
}

function useMainWindow(location) {
    var followLink = true;
    if (window.sc.isPropertyTrue("popups")) {
        var newWin;
	var saveSession = new Object;
	window.sc.store(saveSession);
	var href = addSearchToLocation(location, saveSession);
        newWin = window.open(href, "MainWindow");
        if (newWin) {
            newWin.focus();
            followLink = false;
            if (window.name == "PictureWindow") {
                window.close();
            }
        }
        else {
            window.sc.setProperty("popups", false);
            window.sc.store(location);
        }
    }
    else {
        window.sc.store(location);
    }
    return followLink;
}

/* something hokey about having to manipulate a location/URL as a string. If we could manipulate locations properly then wouldn't
   need this.
 */
function addSearchToLocation(location, saveSession) {
    var url = location.toString()
    url = url.replace(/#.*$/, '')
    return url + saveSession.search + location.hash;
}

function doPopUpsWork() {
    var newWin;
    var works = false;
    newWin = window.open("", "PictureWindow");
    if (newWin) {
        newWin.close();
        works = true;
    }
    return works;
}


/*********************/
/* Debugging functions */
/* for web page JavaScript */

function htmlDisplayMap(name, value) {
    document.write("&nbsp;&nbsp;&nbsp;" + name +"=>" +value + "<br/>");
}


function displayProperties(win, object, type)
{
    win.document.write("Properties for " + type + "<br/>");
    for (var key in object) {
      win.document.write("&nbsp;&nbsp;" + key + " => " + object[key]+"<br/>");
    }
}


function writeVar(varStr)
{
    var value = eval(varStr);
    window.document.write(varStr + ": " + value + "<br/>");
}

/* for command line JavaScript */
function disp(object)
{
    for (var key in object) {
	print(key + " => " + object[key] + "\n");
    }
}

