function fecharBox() {
    $('div.mask').fadeOut('fast');
    $('div.lightbox').fadeOut('fast');
    if (jQuery.browser.msie) {
        if (parseInt(jQuery.browser.version) == 6) {
            $('select').each(function() { $(this).show() });
        }
    }
    setTimeout($('div.lightbox > div').remove(), '1000');
    setTimeout($('div.lightbox > iframe').remove(), '1000');
};
document.onkeydown = function(e) {
    if (e == null) {
        keycode = event.keyCode;
    } else {
        keycode = e.which;
    } if (keycode == 27) {
        fecharBox()
    }
};
function createLightbox(width, height) {
    var bodyHeight = document.documentElement.clientHeight;
    if (height < bodyHeight) {
        var top = (bodyHeight - height) / 2;
    } else {
        var top = '5px';
    }
    // muda a posicao se for ie6
    if (jQuery.browser.msie) {
        if (parseInt(jQuery.browser.version) == 6) {
            var docScroll = document.documentElement.scrollTop;
            if (docScroll <= bodyHeight / 2) {
                var scrollTop = (bodyHeight / 2) - (height / 2);
            } else {
                var scrollTop = (docScroll / 2) + (bodyHeight / 2) + (height / 1);
            }
            var top = parseInt(scrollTop);
        }
    }
    $('div.lightbox').css({
        'left': '48%',
        'top': 80 + "px",
        'width': width + 'px',
        'height': height + 'px',
        'margin-left': -width / 2
    });
};
function iniLightbox() {
    createMask();
};
function createMask() {
    $('div.mask').css({
        'width': '100%',
        'height': '100%',
        'filter': 'alpha(opacity=60)',
        '-moz-opacity': '0.6',
        '-khtml-opacity': '0.6',
        'opacity': '0.6'
    });
    $('div.mask').show();
    if (jQuery.browser.msie) {
        if (parseInt(jQuery.browser.version) == 6) {
            $('select').each(function() { $(this).hide() });
        }
    }
};
function fClose() {
    $('.fechar').each(function() { $(this).remove() });
    setTimeout($('div.lightbox').append('<a href="javascript:fecharBox()" class="fechar">Fechar</a>'), '2000');
};
function lightbox(pathUrl) {
    iniLightbox();
    fClose();
    $('div.lightbox').append('<div><img src="' + pathUrl + '" class="png_bg" /></div>');
    $('div.lightbox').fadeIn();
    createLightbox(520, 300);
    setTimeout("arrumaPosicao();", "3000");
};
var y = 0;
$(document).ready(function() {
    if (y == 0) {
        $('body').append('<div class="mask" /><div class="lightbox"></div>');
        y = y + 1;
    }
});
function arrumaPosicao() {
    var larguraImg = $('div.lightbox img').width();
    //alert(larguraImg + 'imagem');
    if (larguraImg > 520) {
        var novaPosicao = (larguraImg - 500) / 2;
        var btnFechar = novaPosicao + 30;
        //alert(novaPosicao + 'posicao');
        $('div.lightbox img').css('margin-left', -novaPosicao + 'px');
        $('div.lightbox .fechar').css('right', -btnFechar + 'px');
    }

}