﻿var site = {
    HM: false,
    menu: false,
    domready: function () {
        this.HM = new HistoryManager();
        this.menu = new menu();
        new pages();
        new artistsList();
        new dynamicText();
        new detail();
        new cart();
        new contact();
        new keyboard();
        window.addEvent("load", function () { site.load(); });
        this.fireEvent("domready");
    },
    load: function () {
        new imgPop();
        this.HM.start();
    },
    addHistoryEvent: function (name, HMUID) {
        this.HM.addEvent(HMUID + ':changed', function (e) { site.call(name, { a: "history", v: e }); });
    },
    call: function (action, args) {
        this.fireEvent(action, args);
        return void (0);
    },
    pop: function (id, url) {
        var pos = window.getSize();
        window.open(url, id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=455,height=' + pos.y);
        return void (0);
    },
    _lazyShippingRequest: false,

    _CalculateShippingRequest: function () {
        if (!this._lazyShippingRequest) {
            this._lazyShippingRequest = new Request.JSON({ "url": "/ajax/cart/getPrice.aspx", secure : false });
            this._lazyShippingRequest.addEvents({
                "success": function (resp) {
                    
                    $(resp.shippingPriceId).set("text", Number(resp.shipping).format({ decimals: 2, decimal: ".", group: "" }));
                    
                    $(resp.totalPriceId).set("text", ((Number(resp.shipping) + Number(resp.total)) * (1 + Number(resp.vatRate))).format({ decimals: 2, decimal: ".", group: "" }));

                    $(resp.VATRateId).set("text", (Number(resp.vatRate)*100).format({ decimals: 0, decimal: ".", group: "" }) + "%");
                    $(resp.VATTotalId).set("text", ((Number(resp.shipping) + Number(resp.total)) * Number(resp.vatRate)).format({ decimals: 2, decimal: ".", group: "" }));
                    $(resp.VATDivId).setStyle("display", resp.vatRate > 0 ? "block" : "none");
                    if (resp.result) {
                        if ($("cartShippingProblem"))
                            $("cartShippingProblem").removeClass("cartShippingProblemActive");
                        if ($("stepOneCartContinue"))
                            $("stepOneCartContinue").removeClass("buttonInactive");
                    } else {
                        if ($("cartShippingProblem"))
                            $("cartShippingProblem").addClass("cartShippingProblemActive");
                        if ($("stepOneCartContinue"))
                            $("stepOneCartContinue").addClass("buttonInactive");
                    }
                }
            });
        }
        this._lazyShippingRequest.cancel();
        return this._lazyShippingRequest;
    },
    CalculateShipping: function (o) {
        $(o.shippingPriceId).set("text", "...");
        $(o.totalPriceId).set("text", "...");
        this._CalculateShippingRequest().post(o);
    },
    fillCountries: function (selectId, popContainer) {
        $(selectId).addEvents({
            "mousedown": function () {
                site.menu.Hold();
            },
            "change": function () {
                site.selectCountry($(selectId).get('value'));
                site.menu.Unhold();
            }
        });
    },
    selectCountry: function (countryId) {
        site.menu.Unhold();
        new URI().set('query', 'shipping=' + countryId).go();
    },
    submitForm: function (obj) {
        var form = $(obj.id);
        // validation
        var passed = true;
        form
            .getElements("input.valid")
                .each(function (input) {
                    var isValid = input.get("value").test(input.get("rel"));
                    if (!isValid)
                        input.addClass("error");
                    else
                        input.removeClass("error");
                    passed = passed && isValid;
                });
        if (passed) {
            $(obj.resultContainer).setStyle("background-image", "url(/assets/ajax-loader.gif)");
            $(obj.input).setStyle("visibility", "hidden");
            new Request.HTML({ "url": obj.url, "update": $(obj.resultContainer) })
                .addEvent("complete", function () { if ($('newsletter')) { $('newsletter').setStyle('visibility', 'hidden'); $(obj.resultContainer).setStyle("background-image", "none"); } })
                .post(form.toQueryString());
        }
        return void (0);
    }
};
Object.append(site, new Events());
window.addEvent("domready", function () { site.domready(); });
