﻿var cst = {
    // Utilities
    apply: function (a, b) {
        if (typeof a == 'string') {
            if (typeof cst[a] != 'object') {
                cst[a] = {};
            }
            for (var p in b) {
                cst[a][p] = b[p];
            }
        } else {
            for (var p in a) {
                cst[p] = a[p];
            }
        }
    },
    showErrorMessage: function (message, title) {
        var div = $('#error-modal');
        div.html(message);
        div.dialog({
            modal: true,
            title: title ? title : 'Error',
            create: function(event, ui){
                $('.ui-dialog').wrap('<div class="ui-lightness" />');
            },
            open: function(event, ui){
                $('.ui-widget-overlay').wrap('<div class="ui-lightness" />');
            },
            close: function(event, ui){
                $(".ui-lightness").filter(function () {
                    if ($(this).text() == "")
                        return true;
                    return false;
                }).remove();
            }
        });
    },
    showInfoMessage: function (message, title) {
        var div = $('#info-modal');
        div.html(message);
        div.dialog({
            modal: true,
            title: title ? title : 'Info',
            create: function (event, ui) {
                $('.ui-dialog').wrap('<div class="ui-lightness" />');
            },
            open: function (event, ui) {
                $('.ui-widget-overlay').wrap('<div class="ui-lightness" />');
            },
            close: function (event, ui) {
                $(".ui-lightness").filter(function () {
                    if ($(this).text() == "")
                        return true;
                    return false;
                }).remove();
            }
        });
    },
    usMoney: function (v) {
        v = (Math.round((v - 0) * 100)) / 100;
        v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
        v = String(v);
        var ps = v.split('.'),
            whole = ps[0],
            sub = ps[1] ? '.' + ps[1] : '.00',
            r = /(\d+)(\d{3})/;
        while (r.test(whole)) {
            whole = whole.replace(r, '$1' + ',' + '$2');
        }
        v = whole + sub;
        if (v.charAt(0) == '-') {
            return '-$' + v.substr(1);
        }
        return "$" + v;
    },
    makeInputNumericOnly: function (e) {
        e.keydown(function (e) {
            if (e.keyCode != 46 // Delete
             && e.keyCode != 8  // Backspace
             && e.keyCode != 9  // Tab
             && e.keyCode != 37 // Left Arrow
             && e.keyCode != 39 // Right Arrow
             && (e.keyCode < 48 || e.keyCode > 57)  // Number Keys
             && (e.keyCode < 96 || e.keyCode > 105) // Num Pad
            ) {
                e.preventDefault();
            }
        });
    },
    getNum: function (v, d) {
        var n = Number(v);
        return isNaN(n) ? d : n;
    },
    // Init
    initTabsWithHistory: function (s, onTabShow) {
        $(s).removeClass('hide').tabs({
            select: function (event, ui) {
                $.history.load(ui.panel.id);
            },
            show: onTabShow
        });
        $.history.init(function (hash) {
            $(s).tabs('select', hash == '' ? 0 : ('#' + hash));
        });
    },
    initCarousel: function () {
        $('.carousel').removeClass('hide').jcarousel({
            wrap: 'circular'
        });
    },
    initShadowBox: function () {
        Shadowbox.init({
            modal: true,
            continuous: true,
            flashVars: {
                autostart: true
            }
        });
    },
    // Tip
    initTips: function () {
        $('#tip').dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            width: 300,
            height: 'auto',
            minHeight: 20,
            title: 'Feature Information',
            create: function (event, ui) {
                $('.ui-dialog').wrap('<div class="ui-lightness" />');
            },
            open: function (event, ui) {
                $('.ui-widget-overlay').wrap('<div class="ui-lightness" />');
            },
            close: function (event, ui) {
                $(".ui-lightness").filter(function () {
                    if ($(this).text() == "")
                        return true;
                    return false;
                }).remove();
            },
            dialogClass: 'no-titlebar'
        });

        $('span.tip').removeClass('tip').addClass('qtip').before('<span class="tip">&nbsp;</span>');
        $('span.qtip').prev().mouseover(cst.onQTipOver).mouseout(cst.onQTipOut);

        cst.ctip = $('#ctip').html();
        $('span.ctip').prev().mouseover(cst.onCTipOver).mouseout(cst.onCTipOut);
    },
    onQTipOver: function (event) {
        var html = $(this).next().html();
        var tip = $('#tip');
        tip.html(html);
        tip.dialog('open');

        var h = tip.height();
        var y = event.clientY + 20;
        if ($(window).height() < y + h) {
            y -= h + 45;
        }
        tip.dialog('option', 'position', [event.clientX - 150, y]);
    },
    onQTipOut: function () {
        $('#tip').dialog('close');
    },
    ctip: null,
    onCTipOver: function (event) {
        var html = $(this).next().html();
        $('#ctip').html(html);
    },
    onCTipOut: function () {
        $('#ctip').html(cst.ctip);
    }
};

$(document).ready(function () {
    $("input:visible:enabled:first").focus();
    $(".once").one_time_action({ add_class: "disabled" });
});
