﻿if (typeof (HWS) == "undefined") var HWS = {};

HWS.ImageViewer = function(imgs) {
    this.zoom = function() {
        switch (this.src) {
            case this.imgs.Small: this.src = this.imgs.Medium; break;
            case this.imgs.Medium: this.src = this.imgs.Large; break;
            case this.imgs.Large: this.src = this.imgs.Small; break;
        }
        this.showImage();
    }

    this.next = function() {
        this.idx++;
        if (this.idx >= this.imgs.Small.length) this.idx = 0;
        this.btnNext.innerHTML = "next (" + (this.idx + 1) + "/" + this.imgs.Medium.length + ")";
        this.showImage();
    }

    this.showImage = function() {
        if (this.img) this.div.removeChild(this.img);
        this.img = document.createElement("img");
        this.img.src = this.src[this.idx];
        this.div.insertBefore(this.img, this.div.firstChild);
    }

    this.setImages = function(imgs) {
        this.imgs = imgs;
        if(this.idx >= this.imgs.Medium.length) this.idx = 0;
        this.src = imgs.Medium;

        if (imgs.Medium.length > 1)
            this.btnNext.style.display = "";
        else
            this.btnNext.style.display = "none";

        this.btnNext.innerHTML = "next (" + (this.idx + 1) + "/" + this.imgs.Medium.length + ")";
        this.showImage();
    }

    var instance = this;
    var id = "iv" + Math.floor(Math.random() * 10000);
    document.write("<div id=\"" + id + "\"></div>");

    this.div = document.getElementById(id);
    this.div.appendChild(document.createElement("br"));

    this.btnNext = document.createElement("span");
    this.btnNext.style.display = "none";
    this.btnNext.style.cursor = "pointer";
    this.btnNext.style.textDecoration = "underline";
    this.btnNext.onmousedown = function() { instance.next(); };
    this.div.appendChild(this.btnNext);

    var zoom = document.createElement("span");
    zoom.innerHTML = "zoom";
    zoom.style.cursor = "pointer";
    zoom.style.textDecoration = "underline";
    if (imgs.Medium.length > 1) zoom.style.marginLeft = "10px";
    zoom.onmousedown = function() { instance.zoom(); };
    this.div.appendChild(zoom);

    this.idx = 0;
    this.img = null;
    this.setImages(imgs);
}

HWS.QueryString = function() {
    this.get = function(key) {
        for (var i = 0; i < this.split.length; i++) {
            if (this.split[i][0] == key.toLowerCase()) return this.split[i][1];
        }
        return null;
    }

    this.getAll = function() {
        var all = "";
        for (var i = 0; i < this.split.length; i++) {
            all = all + "&" + this.split[i][0] + "=" + encodeURIComponent(this.split[i][1]);
        }
        return "?" + all.substring(1);
    }

    this.set = function(key, value) {
        for (var i = 0; i < this.split.length; i++) {
            if (this.split[i][0] == key.toLowerCase()) {
                this.split[i][1] = value;
                return;
            }
        }
        this.split[this.split.length] = [key, value];
    }

    var query = window.location.search;
    if (query.indexOf("?") == 0) query = query.substring(1);
    this.split = query.split("&");
    for (var i = 0; i < this.split.length; i++) {
        this.split[i] = this.split[i].split("=");
        this.split[i][0] = this.split[i][0].toLowerCase();
    }
}

HWS.Popup = function (params) {
    this.close = function () {
        if (overlay) document.body.removeChild(overlay);
        document.body.removeChild(dlg);
        return false;
    }

    function setButtons() {
        if (params.button) {
            var btn = document.getElementById(params.button.id);
            btn.onclick = params.button.onclick;
        }
    }

    function resp(resp) {
        resp = eval("(" + resp + ")");
        document.body.style.cursor = "default";
        params.headline = resp.headline;
        params.content = resp.content;
        showPopup();
    }

    function showPopup() {
        if (params.overlay) {
            overlay = document.createElement("div");
            overlay.className = "hwsOverlay";
            document.body.appendChild(overlay);
        }

        dlg = document.createElement("div");
        dlg.className = "hwsPopup ";
        document.body.appendChild(dlg);

        if (params.left) dlg.style.left = params.left;
        if (params.top) dlg.style.top = params.top;

        var capt = document.createElement("div");
        capt.onmousedown = function (event) { dragStart(event, dlg); };
        capt.className = "titlebar";
        dlg.appendChild(capt);

        var title = document.createElement("span");
        title.className = "title";
        capt.appendChild(title);

        if (!params.noClose) {
            var lnk = document.createElement("a");
            lnk.innerHTML = "x";
            lnk.href = "#";
            lnk.onclick = function () { return instance.close(); };
            capt.appendChild(lnk);
        }

        var bdy = document.createElement("div");
        bdy.className = "body";
        dlg.appendChild(bdy);

        title.innerHTML = params.headline;
        bdy.innerHTML = params.content;
        setButtons();

        if (params.buttonOk) {
            var div = document.createElement("div");
            div.style.textAlign = "center";
            bdy.appendChild(div);
            var inp = document.createElement("input");
            inp.type = "button"
            inp.value = "Ok";
            inp.onclick = function () { return instance.close(); };
            div.appendChild(inp);
        }

    }

    if (typeof (params) == "string") {
        var content = params;
        params = {};
        params.content = content;
        params.headline = "";
        params.buttonOk = true;
    }

    if (typeof (params) == "undefined") params = {};
    if (typeof (params.overlay) == "undefined") params.overlay = true;
    if (typeof (params.headline) == "undefined") params.headline = "";
    if (typeof (params.noClose) == "undefined") params.noClose = false;

    var dlg;
    var overlay = null;
    var instance = this;

    if (typeof (params.url) == "undefined") {
        showPopup();
    }
    else {
        document.body.style.cursor = "wait";
        ajg(resp, params.url);
    }
}

