var langEn = false;
$(document).ready(function(){
	// menu histórico
	$('.list_historico > li > ul').hide();
	$('.list_historico > li > a').click(function() {
		if($(this).hasClass('mark')) $(this).removeClass('mark');
		else $(this).addClass('mark');
		$('.list_historico > li > ul').each(function() {
			if($(this).css("display") == "block") {
				$(this).hide('slow');
				$(this).parent().children("a").removeClass('mark');
			}
		});
		if($(this).parent().children('ul').css("display") == "block") $(this).parent().children('ul').hide('slow');
		else $(this).parent().children('ul').show('slow');
		
		return false;
	});
	
	// na comunidade
	$('.na-comunidade-sidebar').css('outline', 'none');
	$('.na-comunidade-sidebar').parent().find("ul").hide();
	$('.na-comunidade-sidebar').click(function() {
		$(this).toggleClass('mark');
		$(this).parent().find("ul").toggle('slow');
		return false;
	});
	
	// temporada comgas
	$('.comgas').css('outline', 'none');
	$('.comgas').parent().find("ul").hide();
	$('.comgas').click(function() {
		$(this).toggleClass('mark');
		$(this).parent().find("ul").toggle('slow');
		return false;
	});
	
	// idioma
	var url = document.location.href.split('//');
	url = (url[1] ? url[1] : url[0]).split("/");
	
	for(i = 0; i < url.length; i++) if(url[i] == 'en') {
		langEn = true;
		break;
	}
	if(langEn) $("a[title='English']").css("text-decoration", "underline");
	else $("a[title='Português']").css("text-decoration", "underline");
	
	// projeto hover
	$(".menuparent > ul").hover(function() {
		$(this).parent().children("a").addClass("bot_projeto_mark");
	}, function() {
		$(this).parent().children("a").removeClass("bot_projeto_mark");
	});
	
	// projeto
	if($("#mark_projeto").length) {
		$("#menu > li > ul").css("display", "block");
	}
	
	oficinaIndex = 0;
	for(i = 0; i < url.length; i++) if(url[i] == 'oficina' || url[i] == 'workshop') {
		oficinaIndex = i;
		break;
	}
	if(oficinaIndex > 0) {
		$("a:contains('" + url[i + 1] + "')").parent().children("ul").show();
		$("a:contains('" + $(".box > h3").text() + "')").addClass('mark');
	}
	
	// vídeos
	if($("#mark_videos").length) {
		var idx = url[url.length - 1];
		if(idx == "videos.html") idx = 'a';
		if(idx == "0") idx = '#';
		$("div.alphabetical > ul > li").each(function() {
			if($(this).children("a").text() == idx) $(this).children("a").css("background", "#3d708a");
		});
	}
	
	// accessbar
	var ieDefaultSize = 12;
	if($.browser.msie) $("body").css("font-size", ieDefaultSize + "px");
	if($.cookie('font_size') != null) $("body").css("font-size", parseInt($.cookie('font_size')) + "px");
	
	// newsletter
	var newsletterText;
	$("#form_email").focus(function(){
		if(newsletterText == null) newsletterText = $("#form_email").val();
		if($("#form_email").val() == newsletterText) $("#form_email").val("");
	});
	$("#form_email").blur(function(){if($("#form_email").val().length == 0){$("#form_email").val(newsletterText)}});
});

// barra de acessibilidade
var minFontSize = 10;
var maxFontSize = 14;
var oldCSS
var newCSS = '/oficinas/css/alternativo.css';

function aumentaFonte() {
	var newSize = parseInt($("body").css("font-size").replace("px", "")) + 1;
	if(newSize <= maxFontSize) {
		$("body").css("font-size", newSize + "px");
		$.cookie('font_size', newSize, { expires: 7 });
	}
}

function diminuiFonte() {
	var newSize = parseInt($("body").css("font-size").replace("px", "")) - 1;
	if(newSize >= minFontSize) {
		$("body").css("font-size", newSize + "px");
		$.cookie('font_size', newSize, { expires: 7 });
	}
}

function altoContraste() {
	if(oldCSS == null) oldCSS = $("link[@media=screen]").attr("href");
	$("link[@media=screen]").attr("href", newCSS);
}

function voltarContraste() {
	if(newCSS != oldCSS) $("link[@media=screen]").attr("href", oldCSS);
}








// contato
function submitContato() {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if($("#form_name").val().length == 0) {
		if(!langEn) alert("Campo Nome deixado em branco.");
		else alert("Field Name is required.");
		$("#form_name").focus();
		return false;
	}
	if($("#form_email").val().length == 0) {
		if(!langEn) alert("Campo Email deixado em branco.");
		else alert("Field Email is required.");
		$("#form_email").focus();
		return false;
	}
	else if(!filter.test($("#form_email").val())) {
		if(!langEn) alert("Campo Email inválido.");
		else alert("Field Email is invalid.");
		$("#form_email").focus();
		return false;
	}
	if($("#form_assunto").val().length == 0) {
		if(!langEn) alert("Campo Assunto deixado em branco.");
		else alert("Field Subject is required.");
		$("#form_assunto").focus();
		return false;
	}
	if($("#form_msg").val().length == 0) {
		if(!langEn) alert("Campo Mensagem deixado em branco.");
		else alert("Field Message is required.");
		$("#form_msg").focus();
		return false;
	}
	
	return true;
}

// convide
function submitConvide() {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if($("#form_name").val().length == 0) {
		if(!langEn) alert("Campo Nome deixado em branco.");
		else alert("Field Name is required.");
		$("#form_name").focus();
		return false;
	}
	if($("#form_email").val().length == 0) {
		if(!langEn) alert("Campo Email deixado em branco.");
		else alert("Field Email is required.");
		$("#form_email").focus();
		return false;
	}
	else if(!filter.test($("#form_email").val())) {
		if(!langEn) alert("Campo Email inválido.");
		else alert("Field Email is invalid.");
		$("#form_email").focus();
		return false;
	}
	if($("#form_email_amigo").val().length == 0) {
		if(!langEn) alert("Campo Email do amigo deixado em branco.");
		else alert("Field Email of your friend is required.");
		$("#form_email_amigo").focus();
		return false;
	}
	else if(!filter.test($("#form_email_amigo").val())) {
		if(!langEn) alert("Campo Email do amigo inválido.");
		else alert("Field Email of your friend is invalid.");
		$("#form_email_amigo").focus();
		return false;
	}
	if($("#form_msg").val().length == 0) {
		if(!langEn) alert("Campo Mensagem deixado em branco.");
		else alert("Field Message is required.");
		$("#form_msg").focus();
		return false;
	}
	
	return true;
}

// newsletter
function submitNewsletter() {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if($("#form_email").val().length == 0) {
		if(!langEn) alert("Email deixado em branco.");
		else alert("Email is required.");
		$("#form_email").focus();
		return false;
	}
	else if(!filter.test($("#form_email").val())) {
		if(!langEn) alert("Email inválido.");
		else alert("Email is invalid.");
		$("#form_email").focus();
		return false;
	}
	if(!langEn) alert("Email cadastrado com sucesso!");
	else alert("Email successfully registered!");
	return true;
}