var disablePopupClose = false;
/**
 * jQuery - Popup plugin file.
 */
(function($)
{
    //OddsMenu class
    function Popup($link, $content, options)
    {
        this.link = $link;
        this.content = $content;
        this.overlay = null;
        this.overlayContent = null;
        this.overlayBackground = null;
        this.disableClose = false;
        this.disableShow = false;

        this.settings = $.extend({}, $.fn.popup.defaults, options || {});
    }



    //Popup class methods
    $.extend(Popup.prototype,
    {
        /**
         * init Popup
         */
        init:function()
        {
            this.createOverlay();
			//mapovanie close buttonu
            this.initClose();

            this.overlayContent.append(this.content);
            this.content.css('position', 'absolute');
            this.content.hide();

            //kontrola ci linka sa nerovna contentu v pripade ze je to triggerova js a nie na click nejakej linky
            if (this.link != this.content) this.link.click($.proxy(this.show, this));
        },

        initClose:function()
        {
            this.content.find('.closePopupAction').click($.proxy(this.hide, this));
            if (this.settings.enableOverlayClose) this.overlayBackground.click($.proxy(this.hide, this));
        },

        attachLink:function($link)
        {
            $link.click($.proxy(this.show, this));
        },

        /**
         * create overlay holder
         */
        createOverlay: function()
        {
            if(!this.overlay)
            {
                if($('#overlay').length == 0)
                {
                    $('body').append(
						$('<div id="overlay" />').append(
							$('<div id="overlayBackground" />').append(
                                $('<div class="bg" />')
                            ),
							$('<div id="overlayContent" />')
						)
					);
                }
                this.overlay = $('#overlay');
                this.overlayContent = $('#overlayContent');
                this.overlayBackground = $('#overlayBackground');
                this.overlay.hide();
            }
        },


        /**
         * show
         */
        show:function(event)
        {
            if (event) event.preventDefault();

            
            this.settings.beforeShow.call(this,event);

            if (this.settings.disableShow) return;

            this.overlay.show();
            this.overlay.css('visiblity', 'hidden');
			
            var top = (this.overlay.height() - this.content.height()) / 2;
            var left = (this.overlay.width() - this.content.width()) / 2;
			
            this.content.css('top', top);
            this.content.css('left', left);
            this.overlay.css('visiblity', 'visible');
            this.content.show();

            this.settings.afterShow.call();
        },

        /**
         * hide
         */
        hide: function(event)
        {
            if (event) event.preventDefault();

            if (disablePopupClose) return;

            this.content.hide();
            this.overlay.hide();
            this.settings.afterClose.call();
        }

    });

    //plugin code
    $.extend($.fn,
    {
        popup:function(options,contentAttr)
        {
            var ret;
            this.each(function()
            {
                var $this = $(this);


                if (contentAttr == 'href' )
                {
                    var $content = $('#' + $this.attr('href').replace('#', ''));
                }
                else
                {
                    var $content = $this;
                }

                $this.popup =  $content.data('popup');
                if (!$this.popup)
                {
					$this.popup = new Popup($this,$content,options);
                    $this.popup.init();
					$content.data('popup', $this.popup);
				}
                else
                {
                    if (contentAttr == 'href' )
                    {
                        $this.popup.attachLink($this);
                    }
                }
				ret = ret ? ret.add($this) : $this;
            });
            return ret;
        }


    });

     $.fn.popup.defaults = {

        disableClose : false,
        disableShow : false,
        enableOverlayClose : true,
        afterClose:function(){},
        beforeShow:function(popup,event){},
        afterShow:function(){}
    };
})(jQuery);
