/*
 * Popup plugin
 * Autor: Rafał Wartalski
 *
 */

(function(){

    $.fn.extend({
        popup: function(options){

            options = options || {};

            var popup = $('#popup');
            var popupBox = $('#popupBox');
            
            var popupBoxWidth = options.width || 600;
            var popupBoxHeight = options.height || 300;
            
            var mouseOverPopupBox = false;
            var popupBoxHeader = $('#popupBox .header');
            var popupBoxClose = $('#popupBox .close');
            var popupBoxContent = $('#popupBox .content');

            if(popup.length != 1){
                popup = $('<div id="popup" />').css({
                    position: 'absolute',
                    top: 0,
                    left: 0
                }).appendTo($('body'))
                .click(function(){
                    if(!mouseOverPopupBox){
                        _hidePopup();
                    }
                });

                $(document).bind('keydown.popup', function(e){
                    if(e.keyCode == options.closeKey){
                        e.preventDefault();
                        _hidePopup();
                    }
                });

                popupBox = $('<div id="popupBox" />')
                .css({
                    position: 'relative',
                	width: popupBoxWidth,
                	height: popupBoxHeight
                	
                })
                .appendTo(popup)
                .bind('mouseover', function(){
                    mouseOverPopupBox = true;
                })
                .bind('mouseout', function(){
                    mouseOverPopupBox = false;
                });

                popupBoxHeader = $('<div class="header" />')
                .appendTo(popupBox);

                if(options.header){
                    popupBoxHeader.html('<h3>' + options.header + '</h3>');
                }

                popupBoxClose = $('<div class="close" />')
                .appendTo(popupBoxHeader)
                .bind('click', function(){
                    _hidePopup()
                });

                popupBoxHeader.append('<div style="clear: both"></div>');

                popupBoxContent = $('<div class="content" />')
                .appendTo(popupBox);

                popup.hide();
            }

            this.each(function(){
                $(this).click(function(e){
                    e.preventDefault();
                    _showPopup(e);
                    _preloader();
                    if(options.load){
                        popupBoxContent.html(options.load.call());
                    }
                    else{
                        var href = $(this).attr('href');
                        load(href);
                    }
                });
            });

            _showPopup = function(e){
                var popupWidth = $(document).width();
                var popupHeight = $(document).height();

                popup.width(popupWidth);
                popup.height(popupHeight);
                
                popupBox.css({
                    left: parseInt($(window).width()/2 - popupBoxWidth / 2),
                    top: parseInt($(document).scrollTop() + $(window).height()/2 - popupBoxHeight / 2)
                });

                popup.fadeIn();
            }

            _hidePopup = function(){
                popup.fadeOut();
                popupBoxContent.html('');

                if(options.afterHide && $.isFunction(options.afterHide)){
                    options.afterHide.call();
                }
            }

            _preloader = function(){
                if(options.preloader){
                    popupBoxContent.html('<div class="preloader"></div>');
                }
            }

            load = function(href){
                $.ajax({
                    url: href,
                    success: function(data){
                        if(data){
                            popupBoxContent.html(data);
                        }
                    }
                })

            }
        }
    });
    
})(jQuery)