/***
@title:
Center

@version:
2.0

@author:
Andreas Lagerkvist

@date:
2008-09-17

@url:
http://andreaslagerkvist.com/jquery/center/

@license:
http://creativecommons.org/licenses/by/3.0/

@copyright:
2008 Andreas Lagerkvist (andreaslagerkvist.com)

@requires:
jquery

@does:
This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

@howto:
jQuery('#my-element').center(true); would center the element with ID 'my-element' using absolute position (leave empty for fixed).

@exampleHTML:
<p>I should be fixed centered</p>

<p>The paragraph above and the paragraph beneath this one are centered. They should be in the middle of the viewport.</p>

<p>I should be absolutely centered</p>

@exampleJS:
jQuery('#jquery-center-example p:first-child').center();
jQuery('#jquery-center-example p:last-child').center(true);
***/

;(function($) {
    $.fn.center = function (absolute) {
        return this.each(function () {
            var t = $(this);

            t.css({
                position:   absolute ? 'absolute' : 'fixed',
                left:       '50%',
                top:        '50%'
            }).css({
                marginLeft: '-' + (t.outerWidth() / 2) + 'px',
                marginTop:  '-' + (t.outerHeight() / 2) + 'px'
            });

            if (absolute) {
                t.css({
                    marginTop:  parseInt(t.css('marginTop'), 10) + $(window).scrollTop(),
                    marginLeft: parseInt(t.css('marginLeft'), 10) + $(window).scrollLeft()
                });
            }
        });
    };
})(jQuery);









;(function($) {
    /*
     * http://www.west-wind.com/weblog/posts/459873.aspx
     */
    $.fn.centerXXX = function(options) {
        /// <summary>Centers the selementected items in the browser window. Takes into account scroll position.
        /// Ideally the selementected set should only match a single element.
        /// </summary>
        /// <param name='fn' type='Function'>Optional function called when centering is complete. Passed DOM element as parameter</param>
        /// <param name='forceAbsolute' type='Boolean'>if true forces the element to be removed from the document flow
        ///  and attached to the body element to ensure proper absolute positioning.
        /// Be aware that this may cause ID hierachy for CSS styles to be affected.
        /// </param>
        ///  In instances where the centered div is larger than the window size, the plugin as it exists now will position it above the top of the window, potentially rendering some contents inaccessible.
        /// <returns type='jQuery' />
        var opt = { forceAbsolute: true,
                    container: window,    // selementector of element to center in
                    completeHandler: null,
                    minX: 0,
                    minY: 0
                  };
        $.extend(opt, options);

        return this.each(function(i) {
            var element = $(this);
            var container = $(opt.container);
            var isWin = opt.container == window;

            // force to the top of document to ENSURE that
            // document absolute positioning is available
            if (opt.forceAbsolute) {
                if (isWin) {
                    element.appendTo('body');
                } else {
                    element.appendTo(container.get(0));
                }
            }

            element.css('position', 'absolute');
            //element.addClass('center');

            // internal only
            function center() {
                // have to make absolute

                // height is off a bit so fudge it
                var heightFudge = isWin ? 2.0 : 1.8;

                var x = (isWin ? container.width() : container.outerWidth()) / 2 - element.outerWidth() / 2;
                var y = (isWin ? container.height() : container.outerHeight()) / heightFudge - element.outerHeight() / 2;

                element.css('left', x < opt.minX ? opt.minX + container.scrollLeft() : x + container.scrollLeft());
                element.css('top', y < opt.minY ? opt.minY + container.scrollTop() : y + container.scrollTop());

                // if specified make callback and pass element
                if (opt.completeHandler) {
                    opt.completeHandler(this);
                }
            }

            center();

            $(window).scroll(center);
            $(window).resize(center);

            // http://www.bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm
            // try to prevent memory leaks (doesn't wok in IE)
            /*
            container.bind('DOMNodeRemoved', function() {
                if ($('.center').size() == 0) {
                    alert('unbind');
                    $(window).unbind('scroll', center);
                }
            });
            */
        });
    };
})(jQuery);
