﻿(function ($) {
    $.extend($.rcn,
    {
        ui: {
            reloadPage: function () {
                var prm = Sys.WebForms.PageRequestManager.getInstance();
                if (prm.get_isInAsyncPostBack())
                    prm.abortPostBack();
                location.reload();
            },
            loader: {
                show: function () {
                    $($.rcn.settings.selectors.loaderLayer).jqm({ onHide: function () { $.rcn.ui.loader.afterLoad = null; }, modal: true }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.loaderLayer).jqmHide();
                },
                afterLoad: null
            },
            login: {
                show: function () {
                    $($.rcn.settings.selectors.loginLayer).jqm({ onHide: function () { $.rcn.ui.login.afterLogin = null; } }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.loginLayer).jqmHide();
                },
                afterLogin: null
            },
            createUser: {
                show: function (sourceOfRegisterPage) {
                    if(sourceOfRegisterPage)
                        $.rcn.trigger($.rcn.events.registerPageOpened, [sourceOfRegisterPage]);
                    $($.rcn.settings.selectors.createUserLayer).jqm({ onHide: function () { $.rcn.ui.createUser.afterCreate = null; } }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.createUserLayer).jqmHide();
                },
                afterCreate: null
            },
            createCustomer: {
                show: function () {
                    $($.rcn.settings.selectors.createCustomerLayer).jqm({ onHide: function () { $.rcn.ui.createCustomer.afterCreate = null; } }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.createCustomerLayer).jqmHide();
                },
                afterCreate: null
            },
            continueWithoutLogin: {
                show: function () {
                    $($.rcn.settings.selectors.continueWithoutLoginLayer).jqm({ onHide: function () { $.rcn.ui.continueWithoutLogin.afterCreate = null; } }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.continueWithoutLoginLayer).jqmHide();
                },
                afterCreate: null
            },
            product: {
                show: function (productId) {
                    $.ajaxSetup({
                        url: '/javascripts/Page/detail-min.js',
                        cache: true
                    });

                    $.ajax({
                        type: "POST",
                        url: '/detail/Product/' + productId,
                        success: function (productDetail) {
                            var productLayer = $($.rcn.settings.selectors.productLayer);
                            productLayer.find('#productLayerError').hide();
                            productLayer.find('#productLayerContent').html(productDetail).show();
                            productLayer.jqm({ onHide: function () { } }).jqmShow();
                            $.rcn.ui.initResources();
                            $.getScript('/javascripts/Page/detail-min.js');
                            jQuery.rcn.trigger(jQuery.rcn.events.showProductLayer);
                        },
                        error: function () {
                            var productLayer = $($.rcn.settings.selectors.productLayer);
                            productLayer.find('#productLayerError').show();
                            productLayer.find('#productLayerContent').hide();
                            productLayer.jqm({ onHide: function () { } }).jqmShow();
                        }
                    });
                },
                hide: function () {
                    $($.rcn.settings.selectors.productLayer).jqmHide();
                }
            },
            contentLayer: {
                show: function (content, header) {
                    var contentLayer = $($.rcn.settings.selectors.contentLayer);
                    if (header)
                        contentLayer.find('#contentLayerHeader').html(header);
                    contentLayer.find('#contentLayerContent').html(content);
                    contentLayer.jqm({ onHide: function () { } }).jqmShow();
                },
                hide: function () {
                    $($.rcn.settings.selectors.contentLayer).jqmHide();
                }
            },
            resourceidentifiers: new Array(),
            resources: {},
            getResource: function (identifier) {
                return $.rcn.ui.resources[identifier];
            },
            addResourceIdentifier: function (identifiers) {
                if ($.isArray(identifiers)) {
                    if($.inArray(identifiers[0], $.rcn.ui.resourceidentifiers) == -1)
                        $.rcn.ui.resourceidentifiers = $.rcn.ui.resourceidentifiers.concat(identifiers);
                } else {
                    $.rcn.ui.resourceidentifiers.push(identifiers);
                }
            },
            initResources: function () {
                var serializedIdentifiers = {};

                if ($.rcn.ui.resourceidentifiers.length == 0)
                    return {};

                $.each($.rcn.ui.resourceidentifiers, function (key, val) {
                    serializedIdentifiers[key] = val;
                });

                $.ajax({
                    url: '/services/apis/1.0/content/GetResources',
                    dataType: 'json',
                    data: {
                        identifiers: serializedIdentifiers
                    },
                    success: function (data) {
                        $.rcn.ui.resources = data;
                    },
                    async: false,
                    type: 'POST'
                });
            },
            init: function () {
                if (!$.rcn.settings.isLoggedIn) {
                    // Elements with the reqlogin class will have a popup layer with login and on success login registered handlers at the element will be fired    
                    $('.reqlogin').each(function (i, element) {
                        var events = $(this).data("events");
                        if (events && events.click) {
                            var clickHandlers = $(this).data("events").click.slice(0);
                            $(this).unbind('click').click(function () {
                                $.rcn.ui.login.afterLogin = function () {
                                    $.each(clickHandlers, function (i, handleObj) {
                                        var func = handleObj.handler;
                                        func();
                                    });
                                };
                                $.rcn.ui.login.show();
                            });
                        }
                    });
                }
            }
        }
    });
})(jQuery);

