﻿/* customizacao do lightbox */
var opacity = '5'; 							/* valor da opacidade da mascara. Utilizar numeros de 0 a 10 */
var mask = 'mask'; 						/* define a classe da mascara */

var cBox = 'Fechar'; 					/* texto do botao fechar do lightbox */
var cCss = 'fechar'; 					/* define a classe do botão fechar */

var num = 27; 							/* tecla para fechar o lightbox. 27 é o numero do ESC, caso nao queira nenhuma utilizar o valor null */

var exCss = 'TopBar'; 					/* define a classe que o .load irá buscar no arquivo externo para carregar no lightbox */
var imgLoading = 'img/lightbox/ajax-loader.gif' 		/*caminho para a imagem de loading */
var cClose = true							/* ativa o fechamento da tela no clique da mascara. Valores true ou false */

/******************************************************************************************************************************************/

function loading() {
	createMask();
	$('div.lightbox').ajaxStart(function() {
		$(this).fadeIn();
		$('div.lightbox .lightbox-content').hide();
		$(this).append('<img src="' + imgLoading + '" style="position:absolute;left:50%;top:50%;margin-left:-110px">');
	});
	$('div.lightbox').ajaxSuccess(function() {
		$(this).children('img').remove();
		$('div.lightbox .lightbox-content').fadeIn();
	});
};

function closeBox() {
	$('div.' + mask + '').fadeOut('fast');
	$('div.lightbox').fadeOut('fast');
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version) == 6) {
			$('select').each(function() { $(this).show() });
		}
	}
	$('div.lightbox > div').remove();
	$('div.lightbox > iframe').remove();
	$('div.lightbox >img ').remove();
	$('.' + cCss + '').each(function() { $(this).remove() });
};
document.onkeydown = function(e) {
	if (e == null) {
		keycode = event.keyCode;
	} else {
		keycode = e.which;
	} if (keycode == num) {
		closeBox()
	}
};
function createLightbox(width, height) {
	var bodyHeight = document.documentElement.clientHeight;
	var bodyWidth = document.documentElement.clientWidth;
	if (jQuery.browser.safari) {
		var docScroll = document.body.scrollTop;
	} else {
		var docScroll = document.documentElement.scrollTop;
	}
	var docScroll = parseInt(docScroll);
	var bodyHeight = parseInt(bodyHeight);
	var height = parseInt(height);

	if (bodyHeight < height) {
		var top = (15 + docScroll);
		$('div.lightbox').css('position', 'absolute');
	} else {
		if (jQuery.browser.msie) {
			if (parseInt(jQuery.browser.version) == 6) {
				var top = (((bodyHeight / 2) - (height / 2)) + docScroll);
				$('div.lightbox').css('position', 'absolute');
			} else {
				var top = ((bodyHeight / 2) - (height / 2));
				$('div.lightbox').css('position', 'fixed');
			}
		} else {
			var top = ((bodyHeight / 2) - (height / 2));
			$('div.lightbox').css('position', 'fixed');
		}
	}
	var top = parseInt(top);

	if (bodyWidth < width) {
		var left = (width / 2) + 10 + 'px';
	} else {
		var left = '48%';
	}
	$('div.lightbox').css({
		'left': left,
		'top': top + "px",
		'width': width + 'px',
		'height': height + 'px',
		'margin-left': -width / 2
	});
	$('div.lightbox').append('<a href="javascript:closeBox()" class="' + cCss + '">' + cBox + '</a>');
};
function createMask() {
	var bodyHeight = document.documentElement.clientHeight;
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version) == 6) {
			var docScroll = document.documentElement.scrollTop;
			$('select').each(function() { $(this).hide() });
			var hb = bodyHeight;
		} else {
			var docScroll = '0';
			var hb = '100%';
		}
	} else {
		var docScroll = '0';
		var hb = '100%';
	}
	$('div.' + mask + '').css({
		'top': docScroll + "px",
		'filter': 'alpha(opacity=' + opacity * 10 + ')',
		'-moz-opacity': opacity / 10,
		'-khtml-opacity': opacity / 10,
		'opacity': opacity / 10,
		'height': hb
	});
	$('div.' + mask + '').show();
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version) == 6) {
			$(window).scroll(function() {
				var docScroll = document.documentElement.scrollTop;
				$('div.' + mask + '').css('top', docScroll + 'px');
				var leftScroll = document.documentElement.scrollLeft;
				$('div.' + mask + '').css('left', leftScroll + 'px');
			});
		}
	}
};
function create(n, size, htmlTitle, pathFile, pathUrl, divHtml, htmlElem, x) {
	if (x == '1') {
		loading();
	} else {
		createMask();
}


	if (n == 'i') {
		// iframe
		createLightbox(size[1], size[2]);
		$('div.lightbox').append('' + htmlTitle + '<iframe src="' + pathUrl + '" width="' + size[1] + '" height="' + size[2] + '" frameborder="0" allowtransparency="true">');
	} else if (n == 's') {
		// swf, youtube, vimeo
		createLightbox(size[0], size[1]);
		$('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>');
	} else if (n == 'g' || n=='e') {
    	createLightbox(size[0], size[1]);
    	$('div.lightbox').append('' + htmlTitle + '<div><img src="' + pathUrl + '" class="png_bg" /></div>');
    	$('div.lightbox').fadeIn();
	} else if (n == 'd') {
		// id
		createLightbox(size[0], size[1]);
		$('div.lightbox').append('' + htmlTitle + '<div style="width:' + size[0] + 'px; height:' + size[1] + 'px; ">' + divHtml + '' + htmlElem.html() + '</div></div>');
	} /*else if (n == 'e') {
		// conteudo externo
		createLightbox(size[0], size[1]);
		$('div.lightbox').append('<div class="lightbox-content"></div>');
		$('div.lightbox-content').load(pathUrl + ' .' + exCss + '');
	}*/

	if (x != '1') {
		$('div.lightbox').fadeIn();
	}
}
var pathUrl = null;
function lightbox(vS, vF) {
	if (vS != null) {
		var pathFile = vF.split('.');
		var size = vS.split('.');
		var pathUrl = vF;
			if (size[0] == 'iframe') {
				create('i', size, '', pathFile, pathUrl);
			}
			else if (pathFile[1] == 'swf' || pathFile[1] == 'vimeo' || pathFile[0].substring(7, 12) == 'vimeo' || pathFile[1] == 'youtube' || pathFile[0].substring(7, 14) == 'youtube') {
				create('s', size, '', pathFile, pathUrl);
			}
			else if (pathFile[1] == 'jpg' || pathFile[1] == 'gif' || pathFile[1] == 'png') {
				create('g', size, '', pathFile, pathUrl);
			}
			else if (pathFile[0].substring(0, 1) == '#') {
				var thisElem = $(this).attr('href');
				var htmlElem = $(thisElem);
				var idElem = $(thisElem).attr('id');
				var divHtml = '<div id="' + idElem + '">'
				$(thisElem).remove();
				create('d', size, '', pathFile, pathUrl, divHtml, htmlElem);
			}
			else {
				create('e', size, '', pathFile, pathUrl, divHtml, htmlElem, '1');
			}
			
			if (cClose == true) {
				$('div.' + mask + '').click(function() { closeBox() });
			}
	} else {
		$('.lightbox a').each(function() {
			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] == 'iframe') {
				$(this).click(function() {
					create('i', size, htmlTitle, pathFile, pathUrl);
					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() {
					create('s', size, htmlTitle, pathFile, pathUrl);
					return false;
				});
			}
			else if (pathFile[1] == 'jpg' || pathFile[1] == 'gif' || pathFile[1] == 'png') {
				$(this).click(function() {
					create('g', size, htmlTitle, pathFile, pathUrl);
					return false;
				});
			}
			else if (pathFile[0].substring(0, 1) == '#') {
				var thisElem = $(this).attr('href');
				var htmlElem = $(thisElem);
				var idElem = $(thisElem).attr('id');
				var divHtml = '<div id="' + idElem + '">'
				$(thisElem).remove();
				$(this).click(function() {
					create('d', size, htmlTitle, pathFile, pathUrl, divHtml, htmlElem);
					return false;
				});
			}
			else {
				$(this).click(function() {
					create('e', size, htmlTitle, pathFile, pathUrl, divHtml, htmlElem, '1');
					return false;
				});
			}
	
			if (cClose == true) {
				$('div.' + mask + '').click(function() { closeBox() });
			}
		});
	}
};
var y = 0;
$(document).ready(function() {
	if (y == 0) {
		$('body').append('<div class="' + mask + '" /><div class="lightbox"></div>');
		y = y + 1;
	}
	lightbox();
});