var fc = fc || {};

(function($) {
    fc.modal = {
        _caller: null,
        _modalType: '',
        _body: null,
        _content: null,
        _overlay: null,

        settings: {
            topOffset: 175,
            leftOffset: 293,
            width: 587,
            title: '',
            url: '',
            data: [],
            offset: 0,
            footer: '&nbsp;',
            overlayOpacity: 0.8,
            overlayOnClick: function() {
                fc.modal.close();
            },
            cookiePrefix: '',
            cookie: false
        },

        close: function() {
            fc.modal.settings.offset = 0;
            fc.modal._body.removeClass('modal-body-show').addClass('modal-body-hide').hide();
            fc.modal.hideOverlay();
        },

        next: function(callback) {
            if (fc.modal.settings.data.length > 0 && fc.modal.settings.offset < fc.modal.settings.data.length) {
                var data = fc.modal.settings.data[fc.modal.settings.offset];

                if (data.trim()) {
                    fc.modal._content.html(data);
                    fc.modal.settings.offset++;

                    if (typeof (callback) == 'function') {
                        callback.call();
                    }

                    $(document).trigger('fc.modal.onShow', [fc.modal._content]);
                } else {
                    fc.modal.skip();
                }
            } else if (fc.modal.settings.url) {
                $.get(fc.modal.settings.url,
				    {
				        offset: fc.modal.settings.offset,
				        rnd: Math.random()
				    },
				    function(data) {
				        if (data.trim()) {
				            fc.modal._content.html(data);
				            fc.modal.settings.offset++;

				            if (typeof (callback) == 'function') {
				                callback.call();
				            }

				            $(document).trigger('fc.modal.onShow', [fc.modal._content]);
				        } else {
				            fc.modal.skip();
				        }
				    });
            } else {
                fc.modal.skip();
            }
        },

        splash: function(settings) {
            $.extend(fc.modal.settings, settings);
            fc.modal.open(null);
        },

        open: function(el) {
            fc.modal._caller = el;

            var rel = $(el).attr('rel');
            var regExp = /\[(?:.*)\]/;
            fc.modal._modalType = fc.modal.settings.cookiePrefix + regExp.exec(rel);

            if (fc.modal.settings.cookie && fc.modal._modalType && getCookie(fc.modal._modalType)) {
                return true;
            }

            var _arguments = arguments;

            fc.modal.showOverlay(function() {
                if (fc.modal._body == null) {
                    fc.modal._body = $('<div class="modal-body modal-body-hide"></div>').appendTo("body");
                }

                if (fc.modal._content == null) {
                    if (fc.modal.settings.title) {
                        fc.modal._body.append('<div class="modal-header"><div class="modal-tl"></div><div class="modal-tr"></div><div class="modal-top"></div><h3>' + fc.modal.settings.title + '</h3><span class="modal-close" onclick="fc.modal.close();">X</span></div>');
                    }

                    $('<div class="modal-content-top-wrapper"><div class="modal-content-tl"></div><div class="modal-content-tr"></div><div class="modal-content-top">&nbsp;</div></div>').appendTo(fc.modal._body);
                    var _content_wrap = $('<div class="modal-content-wrapper"></div>').appendTo(fc.modal._body);

                    fc.modal._content = $('<div class="modal-content"><div style="text-align: center;"><img src="/images/e/ajax-loader.gif"/></div></div>').appendTo(_content_wrap);

                    if (fc.modal.settings.footer) {
                        fc.modal._body.append('<div class="modal-footer"><div class="modal-bl"></div><div class="modal-br"></div><div class="modal-bottom"></div>' + fc.modal.settings.footer + '</div>');
                    }
                } else {
                    if (fc.modal.settings.title) {
                        fc.modal._body.find('.modal-header h3').html(fc.modal.settings.title);
                    }

                    fc.modal._content.html('<div style="text-align: center;"><img src="/images/e/ajax-loader.gif"/></div>');
                }

                if ($.browser.opera) {
                    windowHeight = window.innerHeight;
                    windowWidth = window.innerWidth;
                } else {
                    windowHeight = $(window).height();
                    windowWidth = $(window).width();
                };

                var _left = (windowWidth / 2) - fc.modal.settings.leftOffset;
                if (_left < 0) _left = 0;
                var _top = (windowHeight / 2) - fc.modal.settings.topOffset;
                if (_top < 0) _top = 0;

                var _width = fc.modal.settings.width;
                if (_width > windowWidth)
                    _width = windowWidth;

                fc.modal._body.removeClass('modal-body-hide').show().css({ width: _width + 'px', left: _left + 'px', top: _top + 'px' }).addClass('modal-body-show');

                if ($.browser.msie && $.browser.version == 6) {
                    fc.modal._body.css({ top: $(window).scrollTop() + _top + 'px' });
                }

                if (_arguments.length > 1) {
                    fc.modal.next(_arguments[1]);
                } else {
                    fc.modal.next(null);
                }
            });

            return false;
        },

        skip: function() {
            if (fc.modal.settings.cookie && fc.modal._modalType) {
                setCookie(fc.modal._modalType, 1, fc.modal.settings.offset > 1 ? 24 : 1, '/');
            }
            if (fc.modal._caller && fc.modal._caller.href) {
                document.location.href = fc.modal._caller.href;
            } else {
                fc.modal.close();
            }
        },

        showOverlay: function(callback) {
            if (fc.modal._overlay == null) {
                fc.modal._overlay = $('<div class="modal-overlay modal-overlay-hide"></div>').appendTo("body").bind('click', fc.modal.settings.overlayOnClick);
            }

            fc.modal._overlay.hide().removeClass('modal-overlay-hide').css({ opacity: fc.modal.settings.overlayOpacity }).addClass('modal-overlay-show').fadeIn(200, callback);
        },

        hideOverlay: function() {
            fc.modal._overlay.removeClass('modal-overlay-show').addClass('modal-overlay-hide').hide();
        }
    }

    $.fn.modal = function(settings) {
        $.extend(fc.modal.settings, settings);

        $(this).each(function() {
            $(this).bind('click', function() {
                if (!fc.modal.open(this))
                    return false;
            });
        });
    }

    $.fn.modal_lite = function(settings) {
        $.extend(fc.modal.settings, { leftOffset: 293, topOffset: 175, width: 587 }, settings);

        if (!fc.modal.open(this))
            return false;
    }
})(jQuery);

$(document).bind('fc.modal.onShow', function(event, _content) {
    if (!$.browser.msie || $.browser.version > 6) {
        if (fc.modal._body.height() > windowHeight)
            fc.modal._body.css('position', 'absolute');
        else
            fc.modal._body.css('position', 'fixed');
    }
});

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };