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() });
		}
	}
};
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;
			var scrollTop = (docScroll / 2) + (bodyHeight / 2) + (height / 2);
			var top = parseInt(scrollTop);
		}
	}
	$('div.lightbox').css({
		'left': '48%',
		'top': top + "px",
		'width': width + 'px',
		'height': height + 'px',
		'margin-left': -width / 2
	});
};
function iniLightbox() {
	$('div.lightbox > div').remove();
	$('div.lightbox > iframe').remove();
	createMask();
};
function createMask() {
	$('div.mask').css({
		'width': '100%',
		'height': '100%',
		'filter': 'alpha(opacity=50)',
		'-moz-opacity': '0.5',
		'-khtml-opacity': '0.5',
		'opacity': '0.5'
	});
	$('div.mask').show();
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version) == 6) {
			$('select').each(function() { $(this).hide() });
		}
	}
};
var tamanho = 0;
function carrossel() {
	var elemSize = 224;
	var elemMove = 2;
	var count = 0;
	$(function() {
		countElement = $('.carrossel .window ul').children().size();
		tamanho = countElement * elemSize;
		$(".carrossel .window ul").css('width', tamanho + 'px');
	});
	function getElements() {
		var countElement = $('.carrossel .window ul').children().size();
		countElement = countElement / elemMove - 1;
		return countElement;
	};
	var proximoNavega = $("a.proxima");
	proximoNavega.click(function() {
		qtdElements = getElements();
		if (count < qtdElements) {
			$(".carrossel .window ul").animate({
				'left': '-=' + elemSize * elemMove + 'px'
			}, "slow");
			count++;
			if (count >= qtdElements) {
				$(this).fadeOut();
				$('a.anterior').fadeIn();
				return false;
			} else {
				$('a.proxima').fadeIn();
				$('a.anterior').fadeIn();
				return false;
			}
			return false;
		} else {
			return false;
		};
	});
	$("a.anterior").hide();
	var anteriorNavega = $("a.anterior");
	anteriorNavega.click(function() {
		if (count > 0) {
			$(".carrossel .window ul").animate({
				'left': '+=' + elemSize * elemMove + 'px'
			}, "slow");
			count--;
			if (count == 0) {
				$(this).fadeOut();
				$('a.proxima').fadeIn();
				return false;
			} else {
				$('a.proxima').fadeIn();
				$('a.anterior').fadeIn();
				return false;
			}
		} else if (count == 0) {
			return false;
		};
	});
};
function fClose() {
	$('.fechar').each(function() { $(this).remove() });
	setTimeout($('div.lightbox').append('<a href="javascript:fecharBox()" class="fechar">Fechar</a>'), '2000');
};
var countCarroussel = 0;
var pathUrl = null;
var temp = null;
function lightbox(vS, vF) {
	$('.lightbox a').each(function() {
		if (vS != null) {
			var pathFile = vF.split('.');
			var size = vS.split('.');
			var pathUrl = vF;
		} else {
			var pathFile = $(this).attr('href').split('.');
			var size = $(this).attr('rel').split('.');
			var pathUrl = $(this).attr('href');
		}
		var pathCss = pathUrl.split('.');
		var htmlTitle = '';
		if (!($(this).attr('title') == 'undefined' || $(this).attr('title') == 'null' || $(this).attr('title') == '' || $(this).attr('title') == ' ')) {
			var title = $(this).attr('title');
			htmlTitle = '<div class="titulo"><span class="tit-left" /><h4>' + title + '</h4><span class="tit-right" /> <br /></div>';
		};
		if (size[0] == 'carrossel') {
			if (countCarroussel != 0) {
				$(this).remove();
			}
			countCarroussel = countCarroussel + 1;
			$('body').append('<div class="temp")></div>');
			$('body .temp').append('<li><img src="' + pathUrl + '" /></li>');
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[1], size[2]);
				$('div.lightbox').css({ 'padding': '0' });
				fClose();
				$('div.lightbox').append('<div class="carrossel">' + htmlTitle + '<a href="javascript:void(0);" class="anterior" title="Anteriores">Anteriores</a><div class="window"><ul>' + temp.html() + '</ul></div><a href="javascript:void(0);" class="proxima" title="Proximas">Proximas</a></div> ');
				carrossel();
				$('div.lightbox').fadeIn();
				return false;
			});
		}
		else if (size[0] == 'iframe') {
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[1], size[2]);
				fClose();
				$('div.lightbox').append('' + htmlTitle + '<iframe src="' + pathUrl + '" width="' + size[1] + '" height="' + size[2] + '" frameborder="0" allowtransparency="true">');
				$('div.lightbox').fadeIn();
				return false;
			});
		}
		else if (size[0] == 'linkout') {
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[1], size[2]);
				$('.fechar').each(function() { $(this).remove() });
				$('div.lightbox').append('' + htmlTitle + '<iframe src="' + pathUrl + '" width="' + size[1] + '" height="' + size[2] + '" frameborder="0" allowtransparency="true">');
				$('div.lightbox').fadeIn();
				return false;
			});
		}
		else if (pathFile[1] == 'swf' || pathFile[1] == 'vimeo' || pathFile[0].substring(7, 12) == 'vimeo' || pathFile[1] == 'youtube' || pathFile[0].substring(7, 14) == 'youtube') {
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[0], size[1]);
				fClose();
				$('div.lightbox').append('' + htmlTitle + '<div><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + size[0] + '" height="' + size[1] + '"><param name="movie" value="' + pathUrl + '"><param name="quality" value="high"><param name="wmode" value="transparent"><param name="menu" value="disable"><param name="loop" value="true"><EMBED src="' + pathUrl + '" quality="high" wmode="transparent" menu="disable" width="' + size[0] + '" height="' + size[1] + '" loop="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED></OBJECT></div>');
				$('div.lightbox').fadeIn();
				return false;
			});
		}
		else if (pathFile[1] == 'jpg' || pathFile[1] == 'gif' || pathFile[1] == 'png') {
			iniLightbox();
			createLightbox(size[0], size[1]);
			fClose();
			$('div.lightbox').append('' + htmlTitle + '<div><img src="' + pathUrl + '" class="png_bg" /></div>');
			$('div.lightbox').fadeIn();
			return false;
		}
		else if (pathFile[0].substring(0, 1) == '#' || pathFile[0].substring(0, 1) == '') {
			var thisElem = $(this).attr('href');
			var htmlElem = $(thisElem);
			if (pathFile[0].substring(0, 1) == '#') {
				var idElem = $(thisElem).attr('id');
				var divHtml = '<div id="' + idElem + '">'
			}
			else if (pathFile[0].substring(0, 1) == '') {
				var classElem = $(thisElem).attr('class');
				var divHtml = '<div class="' + classElem + '">'
			}
			$(thisElem).remove();
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[0], size[1]);
				fClose();
				$('div.lightbox').append('' + htmlTitle + '<div style="width:' + size[0] + 'px; height:' + size[1] + 'px; ">' + divHtml + '' + htmlElem.html() + '</div></div>');
				$('div.lightbox').fadeIn();
				return false;
			});
		}
		else {
			$(this).click(function() {
				iniLightbox();
				createLightbox(size[0], size[1]);
				fClose();
				$('div.lightbox').append('' + htmlTitle + '<div class="lightbox-content"></div>');
				$('div.lightbox-content').load(pathUrl + ' .external-content');
				$('div.lightbox').fadeIn();
				return false;
			});
		}
	});
	var temp = $('.temp');
	$('.temp').remove();
};
var y = 0;
$(document).ready(function() {
	if (y == 0) {
		$('body').append('<div class="mask" /><div class="lightbox"></div>');
		y = y + 1;
	}
	lightbox();
});

