﻿Element.implement({
    fadeDisplay: function (c) {
        var e =
            this.get("tween"),
            d = "opacity";
        switch (c) {
            case "in":
                this.setStyle("display", "block");
                e.start(d, 1);
                break;
            case "out":
                e.start(d, 0).chain(function () {
                    this.setStyle("display", "none");
                } .bind(this));
                break;
        }
    }
});
var cart = new Class({
    _HMUID: "cart",
    _visitorInfos: false,
    _isInited: false,
    _curStep: 0,
    _containerWarpId: "shoppingCartWarp",
    _containerId: "shoppingCart",
    _contentId: "shoppingCartContent",
    _call: function (e) {
        switch (e.a) {
            case "remove":
                this._remove(e);
                break;
            case "add":
                this._add(e);
                break;
            case "validate":
                this._validate(e);
                break;
            case "open":
                this._open(e);
                break;
            case "history":
                this._open({ s: e.v });
                break;
            case "close":
                this._hide(e.hasItem);
                break;
            case "pay":
                this._pay();
                break;
        }
    },
    initialize: function () {
        site.addHistoryEvent("cart", this._HMUID);
        site.addEvent("cart", function (e) { this._call(e); } .bind(this));
        this._visitorInfos = new Hash.Cookie("com.theliteroom.visitorInfos");
    },
    _validate: function (e) {

        this._validateStep(e.s);
    },
    _add: function (e) {
        $("menuCart").addClass("menuCartActive");
        this._openStep(1, { "action": "add", "id": e.i });
        if ($(this._contentId + "_4"))
            $(this._contentId + "_4").destroy();
    },
    _remove: function (e) {
        this._openStep(1, { "action": "remove", "id": e.i });
        if ($(this._contentId + "_4"))
            $(this._contentId + "_4").destroy();
    },
    _open: function (e) {
        this._openStep(e.s, { action: "open" });
    },
    _opencart: function () {
        if (!this._isInited)
            this._initcart();
        this._show();
    },
    _pay: function () {
        
        if (!$('havereadConditions').get("checked")) {
            $('havereadConditionsLabel').addClass("error");
            return;
        }
        else
            $('havereadConditionsLabel').removeClass("error");
        $('payForm').submit();
    },
    _show: function () {
        $(document.body).addClass("nomenu");
        $(this._containerWarpId).fadeDisplay("in");
    },
    _hide: function (hasItem) {
        $(document.body).removeClass("nomenu");
        $(this._containerWarpId).fadeDisplay("out");
        if(hasItem && !$("menuCart").hasClass("menuCartActive"))
            $("menuCart").addClass("menuCartActive")
    },

    _initcart: function () {
        new Element("div", { "class": "popModal", "id": this._containerWarpId, "styles": { "display": "block"} }).adopt(
            new Element("div", { "id": this._containerId })
                .adopt(
                    new Element("div", { "id": this._contentId })
                )
            ).inject($(document.body));
        this._isInited = true;
    },
    _validateStep: function (step) {
        // check fields : 
        var passed = true;
        $(this._contentId + "_" + step)
            .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)
            return;
        // writes values in session // cookies;
        // update visitorInfos

        var infosObj = {};
        $(this._contentId + "_" + step)
            .getElements("input.cartInput")
            .each(function (input) {
                infosObj[input.get("name")] = input.get("value");
            });
        // sends to server
        this._openStep(step + 1, { action: 'update', infos: infosObj });
    },

    _openStep: function (step, action) {
        this._opencart();
        for (var i = 1; i <= step; i++) {
            if ($(this._contentId + "_" + i))
                $(this._contentId + "_" + i).fadeDisplay("in");
            else
                this._updateStep(i, { action: "open" });

        }
        for (var i = (step + 1); i <= 4; i++) {
            if ($(this._contentId + "_" + i))
                $(this._contentId + "_" + i).fadeDisplay("out");
        }

        if (action && (action.action != "open" || !$(this._contentId + "_" + step)))
            this._updateStep(step, action);

        site.HM.set(this._HMUID, step);
        // close all layers if they exist
        // open the step, create if it doesn't exists
    },
    _updateStep: function (step, action) {
        if (!$(this._contentId + "_" + step))
            $(this._contentId).adopt(
                new Element("div", { "id": this._contentId + "_" + step }).adopt(this._loadingDiv())
            );
        else
            $(this._contentId + "_" + step).empty().adopt(this._loadingDiv());

        new Request.HTML({ "update": $(this._contentId + "_" + step), "url": "/ajax/cart/step" + step + ".aspx", "events": { "complete": this._stepComplete.pass(step, this)} }).post(action);
    },
    _loadingDiv: function () {
        return new Element("div", { "class": "loadingDiv" }).adopt(new Element("img", { "src": "/assets/logo_animated.gif" }));
    },
    _stepComplete: function (step) {

    }
});
