﻿var utilities = {

    //isVisible:            boolean value to show/hide the ajax
    adjustAJAX: function (isVisible) {

        var ajaxWrapper = $(".ajax-wrapper");

        if (isVisible) {
            ajaxWrapper.show();
        } else {
            ajaxWrapper.hide();
        }
    },

    //element:             jQuery selector to hook unobtrusive validation up for
    ajaxEnableValidation: function (element) {

        //http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html
        jQuery.validator.unobtrusive.parse(element);

    },

    //urlString:            url to parse
    //parametersJSON:       JSON object of the item values that are to be replaced in the url
    buildRouteUrl: function (urlString, parametersJSON) {

        for (var parameter in parametersJSON) {

            if (parameter != undefined) {

                //Replace parameters
                urlString = urlString.replace("%5B" + parameter + "%5D", parametersJSON[parameter]);
            }

        }

        return urlString;
    },

    //numberString          number string to format
    formatNumber: function (numberString) {

        numberString += '';
        var x = numberString.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';

        var rgx = /(\d+)(\d{3})/;

        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }

        return x1 + x2;

    },

    //paramName             url parameter name to look for
    getUrlParam: function (paramName) {
        var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i');
        var match = window.location.search.match(reParam);

        return (match && match.length > 1) ? match[1] : '';
    },


    //route:                route to the action to load
    //viewContainerId:      element to load the view in
    //callback:             callback function
    loadView: function (route, viewContainerId, callback) {
        //Load overlay content
        $("#" + viewContainerId).load(route, "", function () { eval(callback); });
    },

    //route:                route to the action to load
    //data:                 json formatted data
    //viewContainerId:      element to load the view in
    //callback:             callback function
    loadViewData: function (route, data, viewContainerId, callback) {

        //Load overlay content
        $("#" + viewContainerId).load(route, data, function () { eval(callback); });

    },

    //route:                route to the action to load
    //title:                modal title
    //height:               modal height
    //width:                modal width
    //callback:             callback function
    loadViewModal: function (route, title, height, width, callback) {

        var modal = $("#ModalDialog");

        modal.dialog({ height: height });
        modal.dialog({ width: width });
        modal.dialog({ title: title });

        //Load overlay content
        $(modal).load(route, "", function () { eval(callback); });

        var buttons = new Array();
        
        //Add the first button
        buttons[0] = {
            text:"Ok",
            click: function () {
                $(this).dialog("close");
            }
        };

        modal.dialog("option", "buttons", buttons);
        
        modal.dialog("open");

    },

    //route:                route to the action to load
    //data:                 json formatted data
    //title:                modal title
    //height:               modal height
    //width:                modal width
    //callback:             callback function
    loadViewModalData: function (route, data, title, height, width, callback) {

        var modal = $("#ModalDialog");

        modal.dialog({ height: height });
        modal.dialog({ width: width });
        modal.dialog({ title: title });

        //Load overlay content
        $.ajax({
            url: route,
            type: "POST",
            data: data,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {

                $(modal).html(data);

                eval(callback);
            }
        });

        modal.dialog("open");

    },

    modalClose: function () {
        var modal = $("#ModalDialog");

        modal.dialog("close");
    },

    //automaticallyClose:       boolean value of whether to actually automatically close the dialog
    //redirectUrl:              url to redirect to
    modalCloseDelay: function (automaticallyClose, redirectUrl) {

        var modal = $("#ModalDialog");

        modal.dialog("option", "buttons", [
            {
                text: "Ok",
                click: function () {
                    if (modal.dialog("isOpen")) {
                        $(this).dialog("close");

                        if (redirectUrl) {
                            window.location = redirectUrl;
                        }
                    }
                }
            }
        ]);

        if (automaticallyClose) {
            setTimeout("utilities.modalClose()", 3000);
        }

    },

    modalComplete: function () {
        utilities.ajaxEnableValidation("#ModalDialog");
    },

    //title:                modal title
    //height:               modal height
    //width:                modal width
    //doneText:             text that is used for the Save Action (if none provided, will use Save)
    //willPost:             boolean value on wether or not the done action button causes a postback
    //hasCancel:            boolean to determine whether to show the cancel button
    //cancelText:           text that is used for the Cancel Action (if none provided, will use Cancel)
    modalOpen: function (title, height, width, doneText, willPost, hasCancel, cancelText) {

        //Set default values
        if (hasCancel == undefined) {
            hasCancel = true;
        }

        //Set default values
        if (willPost == undefined) {
            willPost = true;
        }

        var modal = $("#ModalDialog");

        modal.dialog({ height: height });
        modal.dialog({ width: width });
        modal.dialog({ title: title });

        var buttons = new Array();

        //Add the first button
        buttons[0] = {
            text: (doneText ? doneText : "Save"),
            click: function () {

                var form = $(this).find("form");

                if ((form.length > 0) && (willPost)) {
                    form.submit();
                } else {
                    $(this).dialog("close");
                }
            }
        };

        //Add the second button
        if (hasCancel) {
            buttons[1] = {
                text: (cancelText ? cancelText : "Cancel"),
                click: function () { $(this).dialog("close"); }
            };
        }

        modal.dialog("option", "buttons", buttons);

        modal.dialog("open");

        modal.find(".ajax-loading").show();
    },

    updateCartQuickView: function (routeCartQuickView, routeCartConfirmation) {

        //used to prevent caching in IE
        var random = new Date().getTime();

        $("#CartQuickView").load(routeCartQuickView + "/" + random);

        utilities.loadViewModal(routeCartConfirmation, "Cart Updated", 140, 250);
    }
};


