﻿/*globals ajaxError, ajaxErrorHandler, showLoading */

(function (AVON, $, undefined) {
    "use strict";
    var GlobalConstructor = function () {
        var publicMembers = {},
        /****************************************************************************************
        The base URL for the site and is used with ajax calls
        ****************************************************************************************/
        siteBaseURL = '/shop/',

        /****************************************************************************************
        Description:  Defines the base URL for the the usercontrol rendering
        service. this allows for loading ascx controls with .net
        elements via ajax.
        ****************************************************************************************/
        userControlServiceURL = siteBaseURL +
                                    'Services/LoadViewService.svc/LoadUserControl?path=';

        /****************************************************************************************
        Description:  Wraps calls to load an external ascx control via ajax.
        The parameters are defined as follows:

        ascx            - The user control view to load.
        querystring     - The querystring parameters to append onto the user control
        cache           - Specifies whether to use the cache for the loaded control
        successmethod   - Provides the callback function following a successful ajax call
        completemethod  - Provides the callback function following completion (success or error)

        Note: Any error in the Ajax call is handled the same for all instances. The ajaxError
        function is used.
        ****************************************************************************************/
        publicMembers.loadASCXViaAjax = function (ascx, querystring, cache, successmethod, completemethod) {
            AVON.common.showLoading('loading');

            // load the control via ajax from the loadview svc url
            $.ajax({
                url: userControlServiceURL + ascx + querystring,
                dataType: 'html',
                cache: cache,
                success: successmethod,
                complete: completemethod,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('Username', AVON.credentials.userId);
                    xhr.setRequestHeader('Password', AVON.credentials.password);
                    xhr.setRequestHeader('Timestamp', AVON.credentials.timestamp);
                },
                error: function (request, status, error) {
                    AVON.common.ajaxErrorHandler(request, status, error, 'loadingDiv');
                }
            });

            AVON.common.hideModal('loadingDiv');
        };

        publicMembers.getParameterByName = function (name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)",
                regex = new RegExp(regexS),
                results = regex.exec(window.location.href);
            if (results === null) {
                return "";
            }
            else {
                return decodeURIComponent(results[1].replace( /\+/g , " "));
            }
        };

        publicMembers.format = function () {
            var s = arguments[0], i, reg;
            for (i = 0; i < arguments.length - 1; i+=1) {
                reg = new RegExp("\\{" + i + "\\}", "gm");
                s = s.replace(reg, arguments[i + 1]);
            }

            return s;
        };


        return publicMembers;
    };

    AVON.global = new GlobalConstructor();
}
(window.AVON = window.AVON || {}, jQuery));

