﻿/*
galerie.js
 
Heinrich-Böll-Stiftung Berlin,
<e-fork> Dresden, Germany
November 2007
 
storage function from
http://aktuell.de.selfhtml.org/artikel/javascript/wertuebergabe/#windowname
 
*/
 
var storage = new function () {
    /* --------- Private Properties --------- */
 
    var dataContainer = {};
 
    /* --------- Private Methods --------- */
 
    function linearize () {
        var string = "", name, value;
        for (name in dataContainer) {
            name = encodeURIComponent(name);
            value = encodeURIComponent(dataContainer[name]);
            string += name + "=" + value + "&";
        }
        if (string != "") {
            string = string.substring(0, string.length - 1);
        }
        return string;
    }
 
    function read () {
        if (window.name == '' || window.name.indexOf("=") == -1) {
            return;
        }
        var pairs = window.name.split("&");
        var pair, name, value;
        for (var i = 0; i < pairs.length; i++) {
            if (pairs[i] == "") {
                continue;
            }
            pair = pairs[i].split("=");
            name = decodeURIComponent(pair[0]);
            value = decodeURIComponent(pair[1]);
            dataContainer[name] = value;
        }
    }
 
    function write () {
        window.name = linearize();
    }
 
    /* --------- Public Methods --------- */
 
    this.set = function (name, value) {
        dataContainer[name] = value;
        write();
    };
 
    this.save = function (name, value) {
        dataContainer[name] = value;
    };
 
    this.get = function (name) {
        var returnValue = dataContainer[name];
        return returnValue;
    };
 
    this.getAll = function () {
        return dataContainer;
    };
 
    this.remove = function (name) {
        if (typeof(dataContainer[name]) != undefined) {
            delete dataContainer[name];
        }
        write();
    };
 
    this.removeAll = function () {
        dataContainer = {};
        write();
    };
 
    this.toName = function () {
        return linearize();
    };
 
    /* --------- Construction --------- */
 
    read();
};
 
function openGalerieImg (arrPics, id, w, h) {
 
    //alert("Array: " + arrPics.join("\n"))
    var picURI = arrPics[id];
    storage.removeAll();
    for (var i = 0; i < arrPics.length; i++) {
        storage.save("bild" + i, arrPics[i]);
    }
    storage.save("aktiv", id);
    var msgWinName = storage.toName();
 
    if (!w) { 
        w = "width=550";
    } else {
        w = "width=" + w;
    }
    if (!h) { 
        h = ",height=750";
    } else {
        h = ",height=" + h;
    }
    if (msgWin && (!msgWin.closed)) {
        msgWin.close();
        msgWin = "";
    }
    //alert("URI: " + picURI);
    //alert("Name: " + msgWinName);
    msgWin = window.open(picURI, "Galerie", w + h + ",top=30,left=100,toolbar=no,locationbar=no,menubar=no,status=no,scrollbars=no,resizable=no,dependend=yes");
    if (!msgWin.closed) {
        msgWin.name = msgWinName;
        //storage.removeAll();
        msgWin.opener = self;
        msgWin.focus();
    }
}
