jQuery.fn.modal = function(options) {

    // VERIFICANDO O HREF
    if (!options) {
        if (this.attr('href')) var options = { url: this.attr('href') };
    } else {
        if (!options.url) if (this.attr('href')) options.url = this.attr('href');
    }

    var settings = {
        url: '#',
        backgroundColor: '#000',
        backgroundOpacity: 0,
        position: 'center',
        referencePosition: this,
        top: 0,
        left: 0,
        closeEsc: true,
        closeClickOut: false,
        autoOpen: false,
        preservDoPostBack: true
    };
    options = jQuery.extend(settings, options);


    function openModal() {
        var larguraHtml = $('html')[0].scrollWidth;
        /*CREATE ELEMENTS*/
        $('body').append($('<img />').attr({ src: "/neston/images/load.gif", width: 100, height: 100, alt: "", className: 'load' })).append($('<div></div>').addClass('bg_modal')).append($('<div></div>').addClass('view_modal'));

        // verificando se o body é menor do que o screen
        var altura = $('html')[0].scrollHeight < $(window).height() ? $(window).height() : $('html')[0].scrollHeight;
        $('.bg_modal').width(larguraHtml).height(altura);

        /*OPACITY*/
        if (options.backgroundOpacity != 0) {
            $('.bg_modal').css('background-color', options.backgroundColor);
            $('.view_modal').css('opacity', 0);
            $('.bg_modal').css('opacity', 0);
        }

        // escondendo selects
        $('select').css('visibility', 'hidden');

        // posicionamento
        if (options.position == 'relative') {
            var offset = options.referencePosition.offset();
            leftModal = offset.left;
            topModal = offset.top;
        }

        /*SHOW BACKGROUND*/
        $('.bg_modal').fadeTo('fast', options.backgroundOpacity, function() {

            $.ajax({
                async: true,
                type: "GET",
                url: options.url,
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                cache: false,
                success: function(texto) {
                    $('.view_modal').html(texto);

                    /*REMOVE LOAD*/
                    $('.load').remove();

                    GB_getPageScrollTop = function() {
                        var yScrolltop;
                        if (self.pageYOffset) {
                            yScrolltop = self.pageYOffset;
                        } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft) {
                            yScrolltop = document.documentElement.scrollTop;
                        } else if (document.body) {
                            yScrolltop = document.body.scrollTop;
                        }
                        return yScrolltop;
                    }

                    /*CENTRALIZE MODAL*/
                    if (options.position != 'center') {
                        var alturaModal = parseInt(options.top) + parseInt(topModal) + parseInt($('.view_modal').height());
                        if (altura < alturaModal) {
                            options.top = 0;
                            topModal = altura - parseInt($('.view_modal').height());
                        }
                        $('.view_modal').css({
                            marginTop: topModal,
                            marginLeft: leftModal,
                            left: options.left,
                            top: options.top
                        }
					);

                    } else {
                        $('.view_modal').css({ marginTop: parseInt(GB_getPageScrollTop() - ($('.view_modal').height() / 2)), marginLeft: -parseInt($('.view_modal').width() / 2) });
                    }

                    /*MODAL HIDE*/
                    if (options.backgroundOpacity != 0) $('.view_modal').fadeTo('fast', 1);

                    /*CLOSE MODAL*/
                    $("a[rel='modalclose']").click(function() {
                        closeModal();
                        return false;
                    })



                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    //alert(textStatus);
                }
            });
        });


        if (options.closeClickOut == true) {
            $('.bg_modal').click(function() {
                closeModal();
            });
        }


        if (options.closeEsc == true) {
            $(window).keydown(function(event) {
                if (event.keyCode == 27) closeModal();
            });
        }
        return false;
    }



    if (options.autoOpen == false) {
        this.click(openModal);
    } else {
        openModal();
    }




    /*CLOSE MODAL*/
    function closeModal() {
        if ($.browser.msie) {
            $('.view_modal').css({ background: 'none' })

            $('div.view_modal').hide();
            $('div.view_modal').remove();

            $('div.bg_modal').hide();
            $('div.bg_modal').remove();
            $('select').css('visibility', 'visible');
        }
        else {
            //HIDE MODAL
            $('.view_modal').fadeTo('fast', 0, function() { $(this).remove(); });

            //HIDE BACKGROUND
            $('.bg_modal').fadeOut('fast', 0, function() {
                $(this).remove();
                $('select').css('visibility', 'visible');
            });
        }

        $(window).unbind();
        $('.bg_modal').unbind();

        if (options.preservDoPostBack)
            __doPostBack = newDoPostBack;


        /*
        if ($.browser.msie) {
        $('.view_modal').css({ background: 'none' })
        }
        //HIDE MODAL
        $('.view_modal').hide(function() { $(this).remove(); });

        //HIDE BACKGROUND
        $('.bg_modal').hide();
        $('.bg_modal').remove();
        $('select').css('visibility', 'visible');

        $(window).unbind();
        $('.bg_modal').unbind();

        if (options.preservDoPostBack)
        __doPostBack = newDoPostBack;
        */

        /*
        if ($.browser.msie) {
        $('.view_modal').css({ background: 'none' })
        }
        //HIDE MODAL
        $('.view_modal').fadeTo('fast', 0, function() { $(this).remove(); });

        //HIDE BACKGROUND
        $('.bg_modal').fadeOut('fast', 0, function() {
        $(this).remove();
        $('select').css('visibility', 'visible');
        });

        $(window).unbind();
        $('.bg_modal').unbind();

        if (options.preservDoPostBack)
        __doPostBack = newDoPostBack;
        */
    }

    function newDoPostBack(eventTarget, eventArgument) {
        var theForm = document.forms[0];
        if (!theForm) {
            theForm = document.aspnetForm;
        }
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            document.getElementById("__EVENTTARGET").value = eventTarget;
            document.getElementById("__EVENTARGUMENT").value = eventArgument;
            theForm.submit();
        }
    }



    this.css('visibility', 'visible');
};

$(document).ready(function() {
    $('a[rel="modal"]').each(function() {
        $(this).modal();
    });
});
