Core.UI = (function() {

    var _PopupBox = {
        'Container': {},
        'Title': {},
        'Progress': {},
        'CloseButton': {},
        'Parts': {},
        'Visible': false,
        'Speed': 450
    };

    var _Overlay = {
        'Element': {},
        'Speed': 250,
        'Visible': false
    };

    var _resizeTimer;

    var _adjustWindows = function() {
        var x = parseInt($(window).width());
        var y = parseInt($(window).height());

        var BoxHeight = parseInt(_PopupBox.Container.height());
        var BoxWidth = parseInt(_PopupBox.Container.width());

        _PopupBox.Container.css({ 'position': 'fixed', 'top': ((y / 2) - (BoxHeight / 2)) + 'px', 'left': ((x / 2) - (BoxWidth / 2)) + 'px', 'width': BoxWidth + 'px', 'height': BoxHeight + 'px' }, 350, 'linear');
    };

    var _o = {
        'Init': function() {
            // Create overlay div.
            _Overlay.Element = $(document.createElement('div'));
            _Overlay.Element.addClass('Overlay');
            $('body').append(_Overlay.Element);

            // Create Popup Box.
            _PopupBox.Container = $('<div>').addClass('PopupBox');
            _PopupBox.Title = $('<div>').addClass('Title');
            _PopupBox.CloseButton = $('<a>').addClass('CloseButton' ).click(function() {
                _o.SetPopupBox({ 'Show': false });
            });
            _PopupBox.Progress = $('<div>').addClass('Progress');

            // Create 9 divs for sides and corners.
            _PopupBox.Parts.TopLeft = $('<div>').addClass('TopLeft');
            _PopupBox.Parts.Top = $('<div>').addClass('Top');
            _PopupBox.Parts.TopRight = $('<div>').addClass('TopRight');
            _PopupBox.Parts.Left = $('<div>').addClass('Left');
            _PopupBox.Parts.Content = $('<div>').addClass('Content');
            _PopupBox.Parts.Right = $('<div>').addClass('Right');
            _PopupBox.Parts.BottomLeft = $('<div>').addClass('BottomLeft');
            _PopupBox.Parts.Bottom = $('<div>').addClass('Bottom');
            _PopupBox.Parts.BottomRight = $('<div>').addClass('BottomRight');

            // Nest the divs.
            _PopupBox.Parts.Top.append(_PopupBox.Title);
            _PopupBox.Parts.Top.append(_PopupBox.CloseButton);
            _PopupBox.Parts.TopRight.append(_PopupBox.Parts.Top);
            _PopupBox.Parts.TopLeft.append(_PopupBox.Parts.TopRight);

            _PopupBox.Parts.Right.append(_PopupBox.Parts.Content);
            _PopupBox.Parts.Left.append(_PopupBox.Parts.Right);

            _PopupBox.Parts.Bottom.append(_PopupBox.Progress);
            _PopupBox.Parts.BottomRight.append(_PopupBox.Parts.Bottom);
            _PopupBox.Parts.BottomLeft.append(_PopupBox.Parts.BottomRight);

            _PopupBox.Container.append(_PopupBox.Parts.TopLeft)
                .append(_PopupBox.Parts.Left)
                .append(_PopupBox.Parts.BottomLeft);

            $('body').append(_PopupBox.Container);

            var resizeTimer = null;
            $(window).bind('resize', function() {
                if (_resizeTimer) clearTimeout(_resizeTimer);
                _resizeTimer = setTimeout(_adjustWindows, 500);
            });

        },
        'CenterDiv': function(Options) {
            if (typeof Options.Element === 'undefined')
                return;

            var Height = parseInt((typeof Options.Height !== 'undefined') ? Options.Height : Options.Element.height());
            var Width = parseInt((typeof Options.Width !== 'undefined') ? Options.Width : Options.Element.width());

            var x = parseInt($(window).width());
            var y = parseInt($(window).height());

            Options.Element.css({ 'position': 'absolute', 'top': (((y / 2) - (Height / 2)) + parseInt($(window).scrollTop())) + 'px', 'left': ((x / 2) - (Width / 2)) + 'px' }, 350, 'linear');
        },
        'SetOverlay': function(Options) {
            // Get the direction of the animation.
            var Show = (typeof Options.Show !== 'undefined') ? Options.Show : !_Overlay.Visible;

            // Return if it's already visible/not visible.
            if (Show === _Overlay.Visible)
                return;

            // Stop any animations on the overlay.
            _Overlay.Element.stop();

            if (Show === true) {
                // Animate the overlay to fade in.
                _Overlay.Element.css({ 'display': 'block', 'opacity': 0 });
                _Overlay.Element.animate({ 'opacity': 0.75 }, _Overlay.Speed, 'linear', function() {
                    // Fire compelete event.
                    if (typeof Options.Complete !== 'undefined')
                        Options.Complete();
                });
            } else {
                // Animate the overlay to fade out.
                _Overlay.Element.animate({ 'opacity': 0 }, _Overlay.Speed, 'linear', function() {
                    _Overlay.Element.css({ 'display': 'none' });

                    // Fire compelete event.
                    if (typeof Options.Complete !== 'undefined')
                        Options.Complete();
                });
            }

            // Remember the position.
            _Overlay.Visible = Show;
        },
        'SetPopupBox': function(Options) {

            var Show = (typeof Options.Show !== 'undefined') ? Options.Show : true;

            _PopupBox.Container.stop();

            if (Show === false) {
                if (typeof Options.SupressOverlay === 'undefined' || Options.SupressOverlay === false)
                    _o.SetOverlay({ 'Show': false });

                // Hide the messagebox.
                _PopupBox.Container.animate({ 'opacity': 0 }, _PopupBox.Speed, 'linear', function() {
                    _PopupBox.Container.css({ 'display': 'none', 'opacity': 0, 'top': 0, 'left': 0, 'width': '', 'height': '' });
                    _PopupBox.Visible = false;
                    _PopupBox.Parts.Content.empty();

                    if (typeof Options.Complete !== 'undefined') Options.Complete();
                });
            } else {
                var _f = function() {
                    // Display the message box.
                    _PopupBox.Title.html((typeof Options.Title !== 'undefined') ? Options.Title : '');
                    _PopupBox.Parts.Content.html((typeof Options.Text !== 'undefined') ? Options.Text : '');

                    _PopupBox.Progress.css('display', ((typeof Options.ShowProgress !== 'undefined' && Options.ShowProgress === true) ? 'block' : 'none'));
                    _PopupBox.CloseButton.css('display', ((typeof Options.ShowCloseButton !== 'undefined' && Options.ShowCloseButton === false) ? 'none' : 'block'));

                    _PopupBox.Container.css({ 'display': 'block', 'opacity': 0 });

                    var x = parseInt($(window).width());
                    var y = parseInt($(window).height());

                    var BoxHeight = 0;
                    if (typeof Options.Height !== 'undefined') {
                        BoxHeight = parseInt(Options.Height);
                    } else {
                        BoxHeight = parseInt(_PopupBox.Container.height());
                    }

                    var BoxWidth = 0;
                    if (typeof Options.Width !== 'undefined') {
                        BoxWidth = parseInt(Options.Width);
                    } else {
                        BoxWidth = parseInt(_PopupBox.Container.width());
                    }

                    _PopupBox.Container.css({ 'position': 'fixed', 'top': ((y / 2) - (BoxHeight / 2)) + 'px', 'left': ((x / 2) - (BoxWidth / 2)) + 'px', 'width': BoxWidth + 'px' });

                    //_PopupBox.Parts.Bottom.css({ 'height': ((typeof Options.ShowProgress !== 'undefined' && Options.ShowProgress === true) ? 30 : 10) + 'px' });

                    _PopupBox.Container.animate({ 'opacity': 1 }, _PopupBox.Speed, 'linear', function() {
                        _PopupBox.Visible = true;

                        if (typeof Options.Complete !== 'undefined') Options.Complete();
                    });
                }

                Core.UI.SetOverlay({ 'Show': true });

                if (_PopupBox.Visible) {
                    _PopupBox.Container.animate({ 'opacity': 0 }, _PopupBox.Speed, 'linear', function() {
                        _PopupBox.Container.css({ 'top': 0, 'left': 0 });
                        _f();
                    });
                } else {
                    _f();
                }
            }
        }
    };

    return _o;
})();
