﻿if (typeof (HWS) == "undefined") var HWS = {};
if (typeof (HWS.Amazon) == "undefined") HWS.Amazon = {};

HWS.Amazon.selCategory = function (fid, loc, si) {
    var sel = document.getElementById("selCategories");
    var lnk = document.getElementById("lnkCategory");
    var bnid = sel.options[sel.selectedIndex].value;
    lnk.href = "/shop/products.aspx?fid=" + fid + "&bnid=" + bnid + "&loc=" + loc + "&si=" + si;
}

HWS.Amazon.addOption = function (optionid) {
    if (typeof (HWS.Amazon.Options) == "undefined") HWS.Amazon.Options = [];
    HWS.Amazon.Options[HWS.Amazon.Options.length] = optionid;
}

HWS.Amazon.showDescription = function(fid, loc, asin) {
    function respShowDescription(resp) {
        document.body.style.cursor = "default";
        div.innerHTML = resp;
    }

    document.body.style.cursor = "wait";
    var div = document.getElementById("proddesc");
    div.innerHTML = "";
    ajg(respShowDescription, "/shop/shopstreamer.aspx?mode=desc&fid=" + fid + "&loc=" + loc + "&asin=" + asin);
}

HWS.Amazon.showReviews = function(fid, loc, asin, page) {
    function respShowReviews(resp) {
        document.body.style.cursor = "default";
        div.innerHTML += resp;
    }

    document.body.style.cursor = "wait";
    var div = document.getElementById("reviews");
    var span = document.getElementById("morereviews");
    div.removeChild(span);
    ajg(respShowReviews, "/shop/shopstreamer.aspx?mode=reviews&fid=" + fid + "&loc=" + loc + "&asin=" + asin + "&page=" + page);
}

HWS.Amazon.sc = function(fid, loc, id, param) {
    function respDummy(resp) { }

    function switchImage() {
        var image = document.getElementById("img" + id);
        if (image == null) return;
        image.src = div.style.display == "none" ? "/shop/images/plus.gif" : "/shop/images/minus.gif";
    }

    function respSc(resp) {
        document.body.style.cursor = "default";
        div.innerHTML = resp;
        switchImage();
        var td = document.getElementById("tdCategories");
        aj(respDummy, "/shop/shopstreamer.aspx?mode=savestate&fid=" + fid + "&loc=" + loc, "c=" + encodeURIComponent(td.innerHTML));
    }

    var div = document.getElementById("sub" + id);
    if (div.innerHTML == "") {
        document.body.style.cursor = "wait";
        ajg(respSc, "/shop/shopstreamer.aspx?fid=" + fid + param);
    }
    else {
        div.style.display = div.style.display == "none" ? "" : "none";
        switchImage();
        var td = document.getElementById("tdCategories");
        aj(respDummy, "/shop/shopstreamer.aspx?mode=savestate&fid=" + fid + "&loc=" + loc, "c=" + encodeURIComponent(td.innerHTML));
    }
}

HWS.Amazon.products = function (bnid, si) {
    function getOptionValue(name) {
        var options = document.getElementsByName(name);
        for (var i = 0; i < options.length; i++) {
            if (options[i].checked) return options[i].value;
        }
        return 0;
    }

    var qs = new HWS.QueryString();

    if (typeof (HWS.Amazon.Options) != "undefined") {
        if (qs.get("si") == si) {
            for (var i = 0; i < HWS.Amazon.Options.length; i++) {
                var val = getOptionValue("opt" + HWS.Amazon.Options[i]);
                if (val != 0) bnid += "," + val;
            }
        }
    }

    qs.set("bnid", bnid);
    qs.set("si", si);
    qs.set("page", "1");

    var cbAmazonOnly = document.getElementById("cbAmazonOnly");
    if (cbAmazonOnly != null) qs.set("merch", cbAmazonOnly.checked ? "amazon" : "");

    document.location = "/shop/products.aspx" + qs.getAll();
    return false;
}

HWS.Amazon.search = function() {
    var qs = new HWS.QueryString();
    qs.set("search", document.getElementById("query").value);
    qs.set("page", "1");
    document.location = "/shop/products.aspx" + qs.getAll();
}

HWS.Amazon.checkSubmitSearch = function(ev) {
    var e = ev || window.event;
    if (e.keyCode == 13) HWS.Amazon.search();
}

HWS.Amazon.refreshCart = function(fid, loc) {
    function respRefreshCart(resp) {
        document.body.style.cursor = "default";
        document.location = document.location;
    }

    var inputs = document.getElementsByTagName("input");
    var form = "";
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].id.substring(0, 8) == "quantity")
            form += "&" + inputs[i].id + "=" + encodeURIComponent(inputs[i].value);
    }
    document.body.style.cursor = "wait";
    aj(respRefreshCart, "/shop/shopstreamer.aspx?mode=refreshcart&fid=" + fid + "&loc=" + loc, form);
}

HWS.Amazon.variation = function(fid, loc, sel) {
    function setOptions(sel, values) {
        for (var i = 0; i < values.length; i++) {
            var select = document.getElementById("var" + i);
            if (select.id != sel.id) {
                var selection = [];
                for (var j = 0; j < values.length; j++) {
                    selection[j] = values[j];
                }

                for (var j = 1; j < select.options.length; j++) {
                    selection[i] = select.options[j].text;
                    if (variationExists(selection))
                        select.options[j].style.color = "#000000";
                    else
                        select.options[j].style.color = "#cccccc";
                }
            }
        }
    }

    function respVariation(resp) {
        document.body.style.cursor = "default";
        var product = eval("(" + resp + ")");
        iv.setImages(product.Images);
        var div = document.getElementById("prodDetails");
        if (!div) return;
        div.innerHTML = product.Details;
    }

    function variationExists(selection) {
        for (var i = 0; i < HWS.Variations.length; i++) {
            var match = true;
            for (var j = 0; j < selection.length; j++) {
                if (selection[j] != "" && HWS.Variations[i].Attributes[j] != selection[j]) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return HWS.Variations[i].Asin;
            }
        }
        return null;
    }

    var values = [];
    var cnt = HWS.Variations[0].Attributes.length;

    var allset = true;
    for (var i = 0; i < cnt; i++) {
        var select = document.getElementById("var" + i);
        values[i] = select.options[select.selectedIndex].text;
        if (select.selectedIndex == 0) {
            allset = false;
            values[i] = "";
        }
    }

    setOptions(sel, values);
    if (!allset) return;

    var asin = variationExists(values);
    if (asin) {
        document.body.style.cursor = "wait";
        ajg(respVariation, "/shop/shopstreamer.aspx?mode=variation&fid=" + fid + "&loc=" + loc + "&asin=" + asin);
    }
}

