// modalbox v1.5
  // last update: 2011.08.16
  // erik@erikdana.com
    (function($){
        $.fn.modal = function(options) {  
            var opt = {
                iframeWidth: 500,
                iframeHeight: 300,
                loaded: $,
                ajaxHeight: 0,
                ajaxWidth: 0
            };
     
            return this.each(function() {
                if (options) {
                    $.extend(opt, options);
                }
                // onclick starts modal
                $(this).click(function(){
                    
                    // options
                    var link = $(this),
                        img = link.children('img').attr('src'),
                        href = link.attr('href');
                    
                    // append the modalbox
                    $('body').prepend('<div class="overlay"><a class="close">x</a></div><div class="modalbox"></div>');
                    //append loading
                    $('html').css({cursor: 'wait'});
                    $('.overlay').append('<span class="load">carregando...</span>');
                    $('.overlay').fadeIn(800, function(){
                    
                        // for images
                        if (link.hasClass('img')) {
                            $('.modalbox').append('<img src="'+href+'" />');
                        }
                        // for iframe
                        else if (link.hasClass('iframe')) {
                            $('.modalbox').addClass('iframe').append('<iframe src="'+href+'" width="'+opt.iframeWidth+'" height="'+opt.iframeHeight+'">Seu navegador n&atilde;o suporta iframes</iframe>');
                        }
                        // ajax (load another page, must be same domain)
                        else if (link.hasClass('ajax')) {
                            $('.modalbox').addClass('ajax').load(href, function(){
                                if (opt.ajaxWidth != 0 && opt.ajaxHeight != 0){
                                    var W = opt.ajaxWidth,
                                        H = opt.ajaxHeight;
                                }
                                $('.modalbox')
                                    .css({marginLeft: -W/2, marginTop: -H/2, width: W, height: H})
                                    .animate({top: '40%', left: '50%'}, 200, function(){
									alert()
										$('.modalbox').animate({top: '50%'}, 1000);
									});
                                // remove loading
                                $('html').css({cursor: 'default'});
                                $('.load').fadeOut(500, function(){
                                    $(this).remove();
                                    opt.loaded();
                                });
                            });
                            return false;
                        }
                        
                        // check if modalbox item is loaded
                        function loadModal() {
                            $('.modalbox').children().load(function(){
                               resize();
                            });
                        }
                        loadModal();
                    });
                    function resize() {

                             var W = $('.modalbox').children().width(),
                                 H = $('.modalbox').children().height(),
								 winH = $(window).height();
							
							if (H > winH){
								$('.modalbox img').css({height: winH - 20});
								H = winH -20;
								var W = $('.modalbox').children().width();
							}
							
							
                            $('.modalbox')
                                .css({marginLeft: -W/2, marginTop: -H/2, width: W, height: H})
                                .animate({top: '50%', left: '50%'});
                            // remove loading
                            $('html').css({cursor: 'default'});
                            $('.load').fadeOut(500, function(){
                                $(this).remove();
                                opt.loaded();
                            });
                    }
                    
                    // close
                    function closeModal() {
                        $('.modalbox').stop().fadeOut(500, function(){
                            $('.overlay').slideUp(500, function(){
                                $('.overlay, .modalbox').remove();
                            });
                        });
                    }
                    
                    $('.overlay, .close').live('click', function() {
                        closeModal();
                    });
                    
                    // keypress esc closes modalbox
                    $(document).bind('keydown', Kd);
                    function Kd(event){
                        if (event.which == 27) {
                            closeModal();
                        }
                    }
                    return false;
                });
            });
        };
    })(jQuery);
