﻿/// <reference path="jquery.js" />
var $U = {
    focus: function(c) {
        c = $('#' + c)[0];

        try {
            c.select();
        }
        catch (e) {
        }

        try {
            c.focus();
        }
        catch (e) {
        }
    },
    disableInputs: function(target, disable) {
        $(target).find('input,textarea,select').each(function() {
            $(this).attr('disabled', disable);
        });
    },
    showProgress: function(message) {
        $('#progressBox').remove();
        $('<div id=\"progressBox\" class=\"progressBox\" style=\"z-index:1002\"></div>').appendTo('body');

        var modal = $('#progressBox');
        var messageHtml = '<div class=\"progressSmall\">' +
                            '    <span style=\"padding-left:15px\">' + message + '</span>' +
                            '</div>';

        modal.html(messageHtml);

        //        var x;
        //        var y;

        //        if (target) {
        //            var elm = $(target);
        //            var pos = elm.offset();

        //            x = pos.left;
        //            y = (pos.top + elm[0].offsetHeight);
        //        }

        //        if ((!x) && (!y)) {
        //            $U.center(modal);
        //        }
        //        else {
        //            $U.setLocation(modal, x, y)
        //        }

        $U.center(modal);
        modal.fadeIn('normal');
    },

    hideProgress: function() {
        $('#progressBox').fadeOut('normal',
                                    function() {
                                        $('#progressBox').remove();
                                    }
                                 );
    },
    messageBox: function(title, message, isError, onOk) {
        var className = isError ? 'errorMessage' : 'notifyMessage';

        $('#messageBox').empty().append('<div class=\"' + className + '\">' + message + '</div>').css('display', '');
        $('#messageBox').dialog(
                                {
                                    modal: true,
                                    resizable: false,
                                    title: title,
                                    width: 460,
                                    overlay: {
                                        opacity: 0.5,
                                        background: '#999'
                                    },
                                    buttons:
                                    {
                                        "Ok": function() {
                                            if (typeof (onOk) == 'function') {
                                                onOk();
                                            }

                                            $(this).dialog('close');
                                        }
                                    },
                                    close: function() {
                                        $(this).dialog('destroy');
                                    }
                                }
                            );
    },
    showModal: function() {
        var modalBG = $('#modalBG');
        var width = $(document).width() + 'px';
        var height = $(document).height() + 'px';

        modalBG.css({ width: width, height: height, opacity: 0.5 });

        $U.setLocation(modalBG, 0, 0);
        modalBG.show();
    },

    hideModal: function() {
        $('#modalBG').hide();
    },

    setLocation: function(e, x, y) {
        e.css({ position: 'absolute', left: (x + 'px'), top: (y + 'px') });
    },

    center: function(e) {
        //        var x = (($U.getViewPortWidth() - e[0].offsetWidth) / 2);
        //        var y = (($U.getViewPortHeight() - e[0].offsetHeight) / 2) + $U.getViewPortScrollY();

    //        $U.setLocation(e, x, y);
        absolute = false;
        e.css({
            position: absolute ? 'absolute' : 'fixed',
            left: '50%',
            top: '45%',
            zIndex: '9999'
        }).css({
            marginLeft: '-' + (e.outerWidth() / 2) + 'px',
            marginTop: '-' + (e.outerHeight() / 2) + 'px'
        });

        if (absolute) {
            e.css({
                marginTop: parseInt(e.css('marginTop'), 10) + jQuery(window).scrollTop(),
                marginLeft: parseInt(e.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
    },

    getViewPortWidth: function() {
        var width = 0;

        if ((document.documentElement) && (document.documentElement.clientWidth)) {
            width = document.documentElement.clientWidth;
        }
        else if ((document.body) && (document.body.clientWidth)) {
            width = document.body.clientWidth;
        }
        else if (window.innerWidth) {
            width = window.innerWidth;
        }

        return width;
    },

    getViewPortHeight: function() {
        var height = 0;

        if (window.innerHeight) {
            height = (window.innerHeight);
        }
        else if ((document.documentElement) && (document.documentElement.clientHeight)) {
            height = document.documentElement.clientHeight;
        }

        return height;
    },

    getViewPortScrollX: function() {
        var scrollX = 0;

        if ((document.documentElement) && (document.documentElement.scrollLeft)) {
            scrollX = document.documentElement.scrollLeft;
        }
        else if ((document.body) && (document.body.scrollLeft)) {
            scrollX = document.body.scrollLeft;
        }
        else if (window.pageXOffset) {
            scrollX = window.pageXOffset;
        }
        else if (window.scrollX) {
            scrollX = window.scrollX;
        }

        return scrollX;
    },

    getViewPortScrollY: function() {
        var scrollY = 0;

        if ((document.documentElement) && (document.documentElement.scrollTop)) {
            scrollY = document.documentElement.scrollTop;
        }
        else if ((document.body) && (document.body.scrollTop)) {
            scrollY = document.body.scrollTop;
        }
        else if (window.pageYOffset) {
            scrollY = window.pageYOffset;
        }
        else if (window.scrollY) {
            scrollY = window.scrollY;
        }

        return scrollY;
    }

};