﻿var VALOR = {
    debug: false,
    initCallback: undefined,
    servicesRoot: "",
    logError: function (error) {
        var uriReplacements = {};

        var errorlog = {
            Message: error.responseText
        };

        if (VALOR.debug) {
            VALOR.Util.handleError(errorlog.Message);
        }
    },
    init: function () {
        VALOR.Util.initDown(VALOR);
        VALOR.fixLanguage();
    },
    onReady: function (callback) {
        VALOR.Util.log("VALOR.onReady()");
        VALOR.initCallback = callback;
    },
    fixLanguage: function () {
        if ($.cookie("VALOR_LANGUAGE") == "Spanish")
            VALOR.setLanguage("Spanish");
        else
            VALOR.setLanguage("English");
    },
    setLanguage: function (language) {
        $.cookie("VALOR_LANGUAGE", language);
        if ($.cookie("VALOR_LANGUAGE") == "Spanish") {
            $(".spanish").show();
            $(".english").hide();
            $(".languageLink").html("[English]");
        } else {
            $(".spanish").hide();
            $(".english").show();
            $(".languageLink").html("[Español]");
        }
    },
    changeLanguage: function () {
        if ($.cookie("VALOR_LANGUAGE") == "Spanish")
            VALOR.setLanguage("English");
        else
            VALOR.setLanguage("Spanish");
    },
    blockUI: function (message) {
        $.blockUI({ message: "<h3>" + message + "&nbsp;&nbsp;<img src='/Images/indicator.gif'></h3>" });
    },
    unblockUI: function () {
        $.unblockUI();
    },
    displayError: function (errMsg, timeout) {
        if (!timeout)
            timeout = 5000;
        $("#errorMsg").html(errMsg);
        $("#errorMessagePanel").showStatus(timeout);
    },
    displaySuccess: function (successMsg, timeout) {
        if (!timeout)
            timeout = 5000;
        $("#successMsg").html(successMsg);
        $("#successMessagePanel").showStatus(timeout);
    }
};

VALOR.Util = {
    messages: new Array(),
    init: function () {
        try {
            VALOR.Util.log('VALOR.Util.init()');
        }
        catch (e) {
            VALOR.Util.handleError(e);
        }
    },
    handleError: function (e) {
        try {
            $.unblockUI();
        }
        catch (e) { }
        try {
            var output = "\r\n--------------error----------------\r\n";
            for (var a in e) {
                if (!$.isFunction(e[a])) {
                    output += a + " : " + e[a] + "\r\n";
                }
            }
            output += "----------------------------------" + "\r\n";
            VALOR.Util.logMessage([output], 'error');
        }
        catch (evt) {
            alert('Severe Error: Unable to Handle Error Internally -- ');
        }
    },
    log: function () {
        VALOR.Util.logMessage(arguments, 'log');
    },
    logMessage: function (args, level) {
        if (VALOR.debug) {
            level = level === undefined || level === null ? 'log' : level;

            $('#debug').length || $('<div id="debug"><h2>debug output<\/h2><\/div>').appendTo('body');
            $.each(args, function (i, arg) {
                $('<div/>').addClass(level + 'Log')
                               .html('[' + level + '] ' + arg)
                               .appendTo('#debug');
            });
        }
    },
    getNextMessage: function () {
        return this.messages.shift();
    },
    //
    // Initialize anything that has an init.
    //
    initDown: function (parentObj) {
        //if it has an init() run it
        $.each(parentObj, function (name, obj) {
            if ($.isPlainObject(obj) && $.isFunction(obj.init)) {
                obj.init();
            }
        });
    }
}

VALOR.UI = {
    init: function () {
        VALOR.Util.log('VALOR.UI.init()');

        try {
            //$(document.body).append('<div id="dialog" title="" class="hidden"></div>');

            $.datepicker.setDefaults({
                showAnim: 'blind',
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true
            });

            $(".date").datepicker();
        }
        catch (e) {

        }
    },

    Dialog: {
        create: function (id, modal, height, width, addToForm, options) {
            try {
                return $('#' + id).dialog($.extend({
                    autoOpen: false,
                    bgiframe: true,
                    height: height,
                    position: 'center',
                    width: width,
                    resizable: false,
                    modal: modal,
                    open: function (event, ui) {
                        if (addToForm) {
                            $(this).parent().appendTo($("form:first"));
                        }
                    }
                }, options));
            }
            catch (e) {

            }
        },
        open: function (id) {
            $('#' + id).dialogOpen();
        },
        close: function (id) {
            $('#' + id).dialogClose();
        },
        showHtml: function (title, html, height, width, onOk) {
            try {
                $('#dialog').dialog('destroy');
                $('#dialog').html(html);
                $("#dialog").dialog({
                    bgiframe: true,
                    resizable: false,
                    height: height,
                    width: width,
                    draggable: false,
                    modal: true,
                    closeOnEscape: false,
                    buttons: {
                        'Ok': function () {
                            $(this).dialog('close');
                            if (onOk) {
                                onOk();
                            }
                        }
                    },
                    title: title,
                    open: function (event, ui) {
                        $(".ui-dialog-titlebar-close").hide();
                    }
                });
            }
            catch (e) {

            }
        },
        showConfirmation: function (icon, title, html, onYes, onNo) {
            try {
                html = String.format('<div class="{0}Img"></div>{1}', icon, html);
                $('#confirmationDialog').dialog('destroy');
                $('#confirmationDialog').html(html);
                $("#confirmationDialog").dialog({
                    bgiframe: true,
                    resizable: false,
                    height: 140,
                    width: 350,
                    draggable: false,
                    resizable: false,
                    modal: true,
                    closeOnEscape: false,
                    buttons: {
                        'Cancel': function () {
                            $(this).dialog('close');
                            if (onNo) {
                                onNo();
                            }
                        },
                        'Ok': function () {
                            $(this).dialog('close');
                            if (onYes) {
                                onYes();
                            }
                        }
                    },
                    title: title,
                    open: function (event, ui) {
                        $(".ui-dialog-titlebar-close").hide();
                    }
                });
            }
            catch (e) {

            }
        },
        showErrorMessage: function (container, title, errors) {
            var errorPanel = $("#" + container + " .errorPanel");

            if (errorPanel.length > 0) {
                var icon = $("<span />").addClass("exclamationImg");

                errors = $.makeArray(errors);

                errorPanel.empty();
                errorPanel.append(icon).append(title).show();

                var errorList = $("<ul />");

                $.each(errors, function (index, error) {
                    $("<li />").html(error).appendTo(errorList);
                });

                errorList.appendTo(errorPanel);
            }
        },
        hideErrorMessage: function (container) {
            $(".errorPanel", "#" + container).hide();
        }
    }
}

$(function () {
    VALOR.init();
});


