///<reference path="/Web/Js/TESCO.js" />
///<reference path="/Web/Js/system/event.js" />
///<reference path="/Web/Js/system/event.manager.js" />
///<reference path="/Web/Js/system/exception.js" />
///<reference path="/Web/Js/system/DOM.node.js" />
///<reference path="/Web/Js/system/connection/XMLHTTP.js" />
///<reference path="/Web/Js/system/connection/ajax.js" />
///<reference path="dialogue.js" />

TESCO.$("sites.retail.UI").Checkout = (function() {

    //	constants
    var NODE = TESCO.system.DOM.node;
    var APP = TESCO.sites.Configuration.application;
    var CON = TESCO.system.connection;

    //	private static
    //#region
    var _actionTable = [{
        val: {
            action: "Remove",
            stopEvent: true
        },
        className: "removeItem"







    },
		{
		    val: {
		        action: "",
		        stopEvent: false
		    },
		    className: "checkItem"
		},
		{
		    val: {
		        action: "Add",
		        stopEvent: true
		    },
		    className: "useItem"
		}
	];

    function _getPaymentItemID(control) {
        switch (control.action.action) {
            case "Add":
                return document.getElementById(control.attributes.ajaxId).value;
            case "Remove":
                var _id = control.parent.getElementsByTagName('input')[0].name;
                return _id.substring(_id.indexOf("-") + 1);
            default:
                return control.node.name.substring(control.node.id.indexOf("-") + 1);
        }
    }
    //#endregion

    //	private instance
    //#region
    function _clickHandler(e) {
        var t = e.target;
        var _control = {
            node: t,
            parent: e.target.parentNode,
            event: e,
            attributes: this.attributes
        }
        _actionTable[1].val.action = (t.checked ? "Select" : "Deselect");
        _control.action = NODE.getAction(_actionTable, t);

        if (NODE.getAncestorByAttributeRegExp(t, "class", new RegExp('invalid')) && _control.action.action != "Remove") {
            e.prevent();
            return;


        }

        if (_control.action != "default") {
            if (_control.action.action == 'Add') {
                _request.call(this, _control);
            } else if (_control.action.action == 'Remove') {
                _createDialogue.call(this, _control);
            } else {
                _request.call(this, _control);








            }
            if (_control.action.stopEvent) {
                e.prevent();
            }
        }
    }

    function _request(control) {
        var _ajaxId = _getPaymentItemID(control);
        var _pin = null;
        if (control.attributes.ajaxPin) {
            _pin = document.getElementById(control.attributes.ajaxPin).value;
        }
        this.background.show();
        var _checkout = this;
        var _con = new CON.ajax(false);
        CON.eggTimer.show(this.container);
        _con.request(control.attributes.ajaxUrl, new control.attributes.ent(_ajaxId, _pin, control.action.action));
        TESCO.system.event.attach(_con, "complete",
			function(e) {
			    _checkout.background.hide();
			    CON.eggTimer.hide(document.getElementById(_checkout.attributes.container));
			    _render.call(_checkout, e, control);
			}
        );
        TESCO.system.event.attach(_con, "error",
            function(e) {
                _checkout.background.hide();
                CON.eggTimer.hide(document.getElementById(_checkout.attributes.container));
                e.exception.dialogue.show(e.exception);
            }
        );
    }

    function _render(e, control) {

        var _container = document.getElementById(this.attributes.payOptionContainer);
        var _content = e.response.response.content;

        //Render item listing
        var _itemList = _content[0].nodeValue;
        NODE.removeChildNodes(_container);
        _container.appendChild(_itemList);

        //Render totals
        var _itemTotals = _content[1].nodeValue;
        var _totals = document.getElementById(this.attributes.totals);
        NODE.removeChildNodes(_totals);
        _totals.appendChild(_itemTotals);

        //Render payment summary
        var _paymentSummary = _content[2].nodeValue;
        var _paySumContainer = document.getElementById("paymentSummaryContainer");
        NODE.removeChildNodes(_paySumContainer);
        _paySumContainer.appendChild(_paymentSummary);

        //Render order summary
        //var _orderSummary = _content[3].nodeValue;
        //var _orderSumContainer = document.getElementById("orderSummaryContainer");
        var _orderSummary = NODE.getElementsByClassAndTagName(_content[3].nodeValue, "miniOrderSummary", "dl")[0];
        var _orderSumContainer = NODE.getElementsByClassAndTagName(document.getElementById("orderSummaryContainer"), "miniOrderSummary", "dl")[0];
        NODE.removeChildNodes(_orderSumContainer);
        _orderSumContainer.appendChild(_orderSummary);

        //Update title
        _updateTitle.call(this, control, _container);

        //Clear form fields
        document.getElementById(this.attributes.ajaxId).value = "";
        if (this.attributes.ajaxPin) {
            document.getElementById(this.attributes.ajaxPin).value = "";
        }
    }

    function _updateTitle(control, container) {
        this.count = container.getElementsByTagName("div").length - 1;
        var _payItemTitle = document.getElementById(control.attributes.payItemTitle);
        this.count <= -1 ? NODE.addClassName(_payItemTitle, "hide") : NODE.removeClassName(_payItemTitle, "hide");
    }

    function _createDialogue(control) {
        var _checkout = this;
        var _rpiDialogue = new TESCO.sites.retail.UI.Dialogue.Ajax(new TESCO.sites.retail.UI.Dialogue.Request("RemovePaymentItem"));



















        function __confirm() {
            _request.call(_checkout, control);
            __clean();
            _rpiDialogue.hide();
        }

        function __clean() {
            _rpiDialogue.removeEventListener("confirm", __confirm);
            _rpiDialogue.removeEventListener("cancel", __clean);
            _rpiDialogue.removeEventListener("error", __clean);
        }

        _rpiDialogue.addEventListener("confirm", __confirm);
        _rpiDialogue.addEventListener("cancel", __clean);



        _rpiDialogue.show();
    }
    //#endregion

    //	constructor
    function _constructor() {
        var _checkout = this;
        this.container = document.getElementById(this.attributes.container);
        TESCO.system.event.attach(this.container, "click",
			function(e) {
			    _clickHandler.call(_checkout, e);
			}
		);
        new TESCO.UI.Collapse.Section(
			this.attributes.collapseContainer,
			this.attributes.collapseBtn,
			true, {
			    "collapseImg": APP.img.URI + "/Sites/Retail/" + APP.name + "/Online/Checkout/" + this.attributes.toggleClose,
			    "expandImg": APP.img.URI + "/Sites/Retail/" + APP.name + "/Online/Checkout/" + this.attributes.toggleOpen, "preventDefault": true
			}
		);
        return this;
    }

    _constructor.prototype.NAME = "TESCO.sites.retail.UI.Checkout";

    return _constructor;
})();


