(function($) {
    $.fn.flexidialog = function(opt) {
        var opts = $.extend({}, $.fn.flexidialog.defaults, opt);

        this.each(function() {
            if (this.jqueryflexiDialog == null) {
                this.jqueryflexiDialog = new FlexiDialog(this, opts);
            } else {
                this.jqueryflexiDialog.open();
            }
        });
        return $;
    }

    $.fn.flexipopup = function(opt) {
        var opts = $.extend({}, $.fn.flexipopup.defaults, opt);
        var r = $;

        this.each(function() {            
            if (this.jqueryflexiPopUp == null) {
                this.jqueryflexiPopUp = new FlexiPopUp(this, opts);
            } else {
                r = this.jqueryflexiPopUp;
                this.jqueryflexiPopUp.open();                
                return false;
            }
        });
        return r;
    }

    var FlexiDialog = function(element, opt) {        

                
        var $element = $(element);
        var popup = null;
               
        $element.addClass('flexidialog');
        $element.html('<div class="flexidialog-content">'+$element.html()+'</div>');        

        $element.prepend('<div class="flexidialog-up"><div></div></div> ')
        $element.append('<div class="flexidialog-down"><div></div></div>');
        $element.append('<a href="#" class="flexidialog-close" title="Zatvoriť okno"></a>');                                

        $element.find('.flexidialog-close').click(function() {
            popup.close();
            return false;
        });

        popup = new FlexiPopUp(element, opt);

        if (opt.width != null) {
            $element.css('min-width', opt.width);
        }

        if (opt.height != null) {
            $element.css('min-height', opt.height);
        }

        if (opt.autoOpen) {
            popup.open();
        }
    }

    var FlexiPopUp = function(element, opt) {
        this.open = function() {
            var winH = $(window).height();
            var winW = $(window).width();

            $element.css('top',  winH/2-$element.height()/2 + $(window).scrollTop());
            $element.css('left', winW/2-$element.width()/2);

            if ($overlay != null) {
                $overlay.fadeIn(opt.fadeSpeed);
            }

            $element.fadeIn(opt.fadeSpeed);

            if ($.isFunction(opt.open)) {
                opt.open.call(element);
            }
        }

        this.close = function() {
            if ($overlay != null) {
                $overlay.fadeOut(opt.fadeSpeed);
            }

            if ($iframe != null) {
                $iframe.hide();
            }

            $element.fadeOut(opt.fadeSpeed, function() {
                if ($.isFunction(opt.close)) {
                    opt.close.call(element);
                }
            });
        }

        function overlayHeight() {
            // handle IE 6
            if ($.browser.msie && $.browser.version < 7) {
                var scrollHeight = Math.max(
                    document.documentElement.scrollHeight,
                    document.body.scrollHeight
                );
                var offsetHeight = Math.max(
                    document.documentElement.offsetHeight,
                    document.body.offsetHeight
                );

                if (scrollHeight < offsetHeight) {
                    return $(window).height() + 'px';
                } else {
                    return scrollHeight + 'px';
                }
            // handle "good" browsers
            } else {
                return $(document).height() + 'px';
            }
        }

        function overlayWidth() {
            // handle IE 6
            if ($.browser.msie && $.browser.version < 7) {
                var scrollWidth = Math.max(
                    document.documentElement.scrollWidth,
                    document.body.scrollWidth
                );
                var offsetWidth = Math.max(
                    document.documentElement.offsetWidth,
                    document.body.offsetWidth
                );

                if (scrollWidth < offsetWidth) {
                    return $(window).width() + 'px';
                } else {
                    return scrollWidth + 'px';
                }
            // handle "good" browsers
            } else {
                return $(document).width() + 'px';
            }
        }

        // constructor
        var $element = $(element);
        var $overlay = null;
        var $iframe = null;
                
        $('body').append($element);
        $element.css({'z-index':1001});

        if($.browser.msie && $.browser.version=='6.0') {
            $iframe = $element.before('<iframe></iframe>').prev('iframe');
            $iframe.css({
                'display': 'block',
                'position': 'absolute',
                'top': '0',
                'left': '0',
                'width': overlayWidth(),
                'height': overlayHeight(),
                'z-index': '-1',
                'filter': 'mask()'
            });
        }

        if (opt.overlay) {
            $overlay = $element.before('<div class="flexidialog-overlay"></div>').prev('.flexidialog-overlay');
            $overlay.css({                
                'left':0,
                'top':0,
                'display': 'none',
                'width':overlayWidth(),
                'height':overlayHeight(),
                'z-index':1000
            });

            $(window).resize(function() {
                $overlay.css({
                    width: overlayWidth(),
                    height: overlayHeight()
                });
            });
        }
        
        if (opt.autoOpen) {
            this.open();
        }
    }

    $.fn.flexidialog.defaults = {
        overlay: true,
        autoOpen: true,
        width: 400,
        height: 150,
        open: null,
        close: null
    }

    $.fn.flexipopup.defaults = {
        overlay: true,
        overlayTransp: 80,
        fadeSpeed: 'fast',
        autoOpen: true,
        width: 400,
        height: 150,
        open: null,
        close: null
    }
})(jQuery);