if(window.offscreenBuffering) {
	window.offscreenBuffering=true;
}

/*
 * by Petko D. Petkov; pdp (architect)
 * http://www.gnucitizen.org
 * http://www.gnucitizen.org/projects/jquery-include/
 */
jQuery.extend({
	/*
	 * included scripts
	 */
	includedScripts: {},

	/*
	 * include timer
	 */
	includeTimer: null,

	/*
	 * include
	 */
	include: function (url, onload) {
		if (jQuery.includedScripts[url] != undefined) {
			return;
		}

		jQuery.isReady = false;

		if (jQuery.readyList == null) {
			jQuery.readyList = [];
		}

		var script = document.createElement('script');

		script.type = 'text/javascript';
		script.onload = function () {
			jQuery.includedScripts[url] = true;

			if (typeof onload == 'function') {
				onload.apply(jQuery(script), arguments);
			}
		};
		script.onreadystatechange = function () {
			if (script.readyState == 'complete') {
				jQuery.includedScripts[url] = true;

				if (typeof onload == 'function') {
					onload.apply(jQuery(script), arguments);
				}
			}
		};
		script.src = url;

		jQuery.includedScripts[url] = false;
		document.getElementsByTagName('head')[0].appendChild(script);

		if (!jQuery.includeTimer) {
			jQuery.includeTimer = window.setInterval(function () {
				jQuery.ready();
			}, 10);
		}
	}
});

/*
 * replacement of jQuery.ready
 */
jQuery.extend({
	/*
	 * hijack jQuery.ready
	 */
	_ready: jQuery.ready,

	/*
	 * jQuery.ready replacement
	 */
	ready: function () {
		isReady = true;

		for (var script in jQuery.includedScripts) {
			if (jQuery.includedScripts[script] == false) {
				isReady = false;
				break;
			}
		}

		if (isReady) {
			window.clearInterval(jQuery.includeTimer);
			jQuery._ready.apply(jQuery, arguments);
		}
	}
});


//Vertical Align PlugIn
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
	});
};
})(jQuery);

//Fill Height PlugIn
(function ($) {
$.fn.fillHeigth = function() {
	return this.each(function(i){
		var numElementos = $(this).parent().children().length;
		var alturaPai = $(this).parent().height();
		var alturaFinal = Math.round(alturaPai/numElementos);
		if(alturaFinal > 67)
			alturaFinal = 67;
		$(this).css('height', alturaFinal);
	});
};
})(jQuery);

var total_restantes = 0;
var total_vazio = 0;
var hold = false;

(function ($) {
	$.fn.scrollMenu = function() {
		return this.each(function(i){	
			var limite = 3;
			var tempo_anim = 650;
			var htmlMenu1;
			var htmlMenu2;
			var numItem1 = 0;
			var numItem2 = 0;
			
			$(this).wrap("<div id='crop-menu'/>");
								  
			if($("div.selecaoMenu").length > 0){	
				var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
				
				if(ie6){
					$("#crop-menu").css("top", "45px");
					$("#crop-menu").css("left", "-532px");
				}
				
				htmlMenu1 = $("div.menuConteudo ul:eq(1)").html();
				htmlMenu2 = $("div.menuConteudo ul:eq(2)").html();
				numItem1 = $("div.menuConteudo ul:eq(1) li").length;
				numItem2 = $("div.menuConteudo ul:eq(2) li").length;
				$("div.menuConteudo ul:eq(1)").css("display", "block");
				$("div.menuConteudo ul:eq(2)").css("display", "none");
				
				$("div.selecaoMenu ul li:eq(0)").click(function (){
					$("div.menuConteudo ul:eq(1)").html(htmlMenu1);
					if(numItem1 > limite){
						$("#scroll-menu-up").css("display", "block");
						$("#scroll-menu-down").css("display", "block");						
					}else{
						$("#scroll-menu-up").css("display", "none");
						$("#scroll-menu-down").css("display", "none");
					}
					$("div.menuConteudo ul:eq(1)").css("top", "0");
					$(document).pngFix(); 
				});
				$("div.selecaoMenu ul li:eq(1)").click(function (){
					$("div.menuConteudo ul:eq(1)").html(htmlMenu2);
					if(numItem2 > limite){
						$("#scroll-menu-up").css("display", "block");
						$("#scroll-menu-down").css("display", "block");
					}else{
						$("#scroll-menu-up").css("display", "none");
						$("#scroll-menu-down").css("display", "none");
					}
					$("div.menuConteudo ul:eq(1)").css("top", "0");
					$(document).pngFix(); 
				});
				
				$("div.selecaoMenu ul li span").vAlign();
			}else{
				$("#crop-menu").css("position", "relative");
				$("#crop-menu").css("margin-top", "0");
				$(document).pngFix(); 
			}			
			
				
			if($("li", this).length > limite){
				var altura_item = $("li", this).height();
				var altura_limite = $("li", this).length * altura_item;
				var altura_menu = $(this).height();
				var tempo_individual = Math.round(tempo_anim / limite);
				var seta_inf_top = 240;
				
				$("#crop-menu").before("<div id='scroll-menu-up'><a href='#'><img src='/entidade/428/1578/img/btSetaMenuSup.png'/></a></div>");
				$("#crop-menu").after("<div id='scroll-menu-down'><a href='#'><img src='/entidade/428/1578/img/btSetaMenuInf.png'/></a></div>");
				
				$("#scroll-menu-up").css("display", "none");
				
				$(this).css("position", "relative");
				
				$("#scroll-menu-up").click(function(){
					if(hold) return;
													
					var top = $("#crop-menu ul").css("top");
					if(top.indexOf("px") > 0)
						top = parseInt(top.split("px")[0]);
					else if(top.indexOf("pt") > 0)
						top = parseInt(top.split("pt")[0]);
						
					top = top + $("#crop-menu").height() - 1;
					
					if(top <= 0){
						hold = true;
						
						$("#crop-menu ul").animate({top: top}, tempo_anim, "linear", function(){ hold = false; });
						
						if(seta_inf_top != $("#scroll-menu-down").position().top){
							$("#scroll-menu-down").css("display", "block");
						}
						
						if(top == 0)
							$("#scroll-menu-up").css("display", "none");
					}else{
						$("#scroll-menu-up").css("display", "none");
					}
				});
				
				$("#scroll-menu-down").click(function(){
					if(hold) return;
																		  
					var top = $("#crop-menu ul").css("top");
					
					if(top.indexOf("px") > 0)
						top = parseInt(top.split("px")[0]);
					else if(top.indexOf("pt") > 0)
						top = parseInt(top.split("pt")[0]);
					else
						top = parseInt(top);
						
					if(isNaN(top))
						top = 0;
						
					top = top - $("#crop-menu").height() + 1;
					
					$("#scroll-menu-up").css("display", "block");
					
					var top_destino = altura_limite + top;
					
					if(top_destino >= 0){
						hold = true;
						
						$("#crop-menu ul").animate({top: top}, tempo_anim, "linear", function(){ hold = false; });
						
						if(top_destino < altura_item * limite){
							$("#scroll-menu-down").css("display", "none");
						}
					}
				});
			}
		});
	};
})(jQuery);


//Inicializar
$(document).ready(function(){	
	var menu_restaurante_inativo = false;	
	var menu_eventos_inativo = false;
	var count = 1;
						   
	/* Identificar menus com somente um item e associar a classe "inativo" */
	if($("#menu ol li#menuRestaurante ul li").length > 1 
		 || $("#menu ol li#menuEventos ul li").length > 1){
		
		if($("#menu ol li#menuRestaurante ul li").length <= 1){
			$("#menu ol li#menuRestaurante a:eq(0)").attr("class", "inativo");
			menu_restaurante_inativo = true;
		}
			
		if($("#menu ol li#menuEventos ul li").length <= 1){
			$("#menu ol li#menuEventos a:eq(0)").attr("class", "inativo");
			menu_eventos_inativo = true;
		}
	}
		
	
	/* Ajustar menus das páginas internas */
	$("#conteudo div.menuConteudo ul li a").vAlign();
	
	/* Ajustar posicionamento e visibilidade de elementos de acordo com a página escolhida */
	$("#telefones").css("display", "block");
	$("#telefones li").css("display", "none");
	if(!menu_restaurante_inativo && !menu_eventos_inativo){
		$("#telefones li").css("display", "block");
		$("#telefones li span.nome").css("display", "none");
		$("#telefones li span.tel").css("display", "block");	
		$("#telefones li:eq(1)").addClass("dir");
	}else{
		if(menu_restaurante_inativo){
			$("#cabecalhoInterno ul").css("left", "490px");
			$("#telefones li:eq(1)").css("display", "block");
			$("#telefones li:eq(1) span.nome").css("display", "none");
			$("#telefones li:eq(1) span.tel").css("display", "block");
		}else{
			$("#cabecalhoInterno ul").css("left", "220px");
			$("#telefones li:eq(0)").css("display", "block");
			$("#telefones li:eq(0) span.nome").css("display", "none");
			$("#telefones li:eq(0) span.tel").css("display", "block");
		}
	}
	
	/* Inserir espaço entre as fotos das Galerias */
	$("#conteudo div.conteudoGaleria ul li").each(
		function(){
			if(count  % 4 == 0)
				$(this).css("margin-right", "0");
			count++;
		}
	);	
	
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	var ie7 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 7.0") != -1);
	
	if (ie55 || ie6){
		if($("div.dadosAtendimento").length > 0)
			$("#conteudo .voltar").css("left", "369px");		
	}else if(ie7){
		if($("#destaquesHome").length > 0){
			$("div#menu ol li#menuEventos ul li a:eq(0)").css("position", "relative");
			$("div#menu ol li ul li a").css("position", "relative");
			$("div#menu ol li#menuEventos ul li a:eq(0)").css("left", "-22px");
		}
	}
	
	/* Adicionar classes aos inputs */
	$("input[type=button],input[type=submit]").addClass("botao");
	$("input[type=file]").addClass("file");
	
	/* Inserir estilo ativo ao passar o mouse sobre o menu das páginas internas */
	$("#conteudo div.menuConteudo li").each(
		function(){
			if($(this).attr("class") == ""){
				$(this).hover(
					function(){
						$(this).attr("class", "ativo");
					},
					function(){
						$(this).attr("class", "");
					}
				);
			}
		}
	);	
	
	/* Alinhar rodapé e redimensionar view interna */
	if($("#destaquesHome").length <= 0){
		posicionarCaixaInterna();
	
		$(window).resize(function() {
			posicionarCaixaInterna();
		});
	}
	
	if($("div.menuConteudo ul").length > 1)
		$("div.menuConteudo ul:eq(1)").scrollMenu();
	else
		$("div.menuConteudo ul").scrollMenu();
		
	$("#conteudo div.conteudoInterna dl dd span.descricao").vAlign();
	
	$("div#rodape").append("<div id='google_translate_element'></div>");
	
	$("a.voltar").click(function(event){
		event.preventDefault();
		if(history.length == 0){
			if(menu_restaurante_inativo)
				$(location).attr("href","/entidade/428/?evento=1");
			else
				$(location).attr("href","/entidade/428/?restaurante=1");
		}else{
			history.back();
		}
	});
	
	/* Correção de transparência de PNGs para IE6 */
	$(document).pngFix(); 
});

function posicionarCaixaInterna(){	 
	var diff = 0;
	var rodape_top = $(window).height() - 95;
	var caixa_height = 0;
	var over_height = 0;
	
	if(rodape_top < 600)
		rodape_top = 600;
		
	caixa_height = $("div#rodape").css("top");
	
	$("div#rodape").css("top", rodape_top);
	
	diff = rodape_top - 600;
	
	caixa_height = 271 + diff;
	over_height = 214 + diff;
	
	$("#conteudo div.conteudoInterna").height(caixa_height);
	$("div#conteudoInternaContainer").height(over_height);
	
	if($("ol#telefones").css("position") == "absolute"){
		$("ol#telefones").css("top", -60 -diff);
	}
}

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
	pageLanguage: 'pt',
	includedLanguages: 'en'
  }, 'google_translate_element');
}
