function Session()
{
    this.properties = new Object();
}

(function () {
    var pt = Session.prototype;
    pt.load = function(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;
        return this;
    };
    pt.store = function(location) {
        location.search = "?"
        var sep = ""
        for (key in this.properties) {
            location.search += sep + escape(key) + "=" + escape(this.properties[key])
            sep = "&"
        }
        return this;
    };
    pt.setProperty = function(name, value) {
        this.properties[name] = value
        return this;
    };
    pt.getProperty = function(name, defaultValue) {
        var value = this.properties[name];
        if (value === undefined) {
            value = defaultValue;
        }
        return value;
    };
    pt.isPropertyDefined = function(name) {
        var value = this.properties[name];
        return value !== undefined;
    };
    pt.dump = function(displayFunction) {
        for (var key in this.properties) {
            displayFunction(key, this.properties[key])
        }
        return this;
    };
}());


/***************/
/* Methods that belong in an application specific file */
/* Methods that belong in an application specific file */
function onPageLoad() {
    window.sc = new Session();
    window.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;
    window.sc.load(location); /* hack to see if it works */
    if (window.sc.getProperty("popups") && window.sc.getProperty("popups") == 'true') {
        var newWin;
        var height = window.screen.availHeight
        var width = window.screen.availWidth
        var saveSession = new Object;
        window.sc.store(saveSession);
        var href = addSearchToLocation(location, saveSession);
        newWin = window.open(href, "PictureWindow",
                             "fullscreen=yes,width=" + width + ",height=" + height + ",resizable=yes,location=no,menubar=no,status=no");
        if (newWin) {
            newWin.focus();
            followLink = false;
        }
        else {
            window.sc.setProperty("popups", false);
            window.sc.store(location)
        }
    }
    else {
        window.sc.store(location)
    }
    return followLink;
}

function gotoMainWindow(location) {
    if (window.name == 'MainWindow') {
        window.location.replace(location.href);
    } else {
        if (window.name == "PictureWindow") {
            window.close();
        }
        var mainWindow = window.open(location.href, 'MainWindow');
        if (mainWindow) {
            mainWindow.focus();
        }
    }
}

function useMainWindow(location) {
    var followLink = true;
    window.sc.load(location); /* hack to see if it works */
    if (window.sc.getProperty("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 put in the "//". If we could manipulate locations properly then wouldn't
   need this.
 */
function addSearchToLocation(location, saveSession) {
    var path, query, anchor;
    var match = location.href.match(/^[^?#]*/);
    var path = match[0];
    match = location.href.match(/^[^?#]*\?([^#]*)/);
    if (match && match.length == 2) {
        if (saveSession.search) {
            query = saveSession.search + '&' + match[1];
        } else {
            query = '?' + match[1];
        }
    } else {
        if (saveSession.search) {
            query = saveSession.search;
        } else {
            query = '';
        }
    }
    match = location.href.match(/^[^#]*(#.*)/);
    if (match && match.length == 2) {
        anchor = match[1];
    } else {
        anchor = '';
    }
    return path + query + anchor;
}

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")
  }
}


