////////////MENU///////////////////////////////////////////////
$(function() {
	
	$("#menu > li a").bind("mouseover",function() {
		
		var lista = $(this).parent().find("ul");
		
		if (lista.is(":hidden")) {
			
			$(this).parent().addClass("selected");
			lista.stop(true,true).slideDown();
			
		} else if ($(this).parent().is(".selected")) {
			
			$(this).parent().removeClass("selected");
			lista.stop(true,true).slideUp();
			
		}
		
	})
	
	$(".linguas a").hover(function() {
		
		var source = $(this).find("img").attr("src");
		
		$(this).find("img").attr("src",source.replace(".png", "_selected.png"));
		
	}, function() {
		
		var source = $(this).find("img").attr("src");
		
		$(this).find("img").attr("src",source.replace("_selected.png", ".png"));
		
	})
	
	$(".linkModal").live("click",function() {
		$.nyroModalManual({
			width: 830,
			height: 565,
			forceType: 'iframe',
			url: $(this).attr("href")
		})
		return false;
	})
	
	$("#contato .email span").bind("click", function() {
		if ($(this).hasClass("selected")) {
			$(this).removeClass("selected");
			$("#contato input[name=receber_emails]").val("NAO");
		} else {
			$(this).addClass("selected");
			$("#contato input[name=receber_emails]").val("SIM");	
		}
	})
	
	
	$("#listagem_produtos li").hover(function() {
		$(this).find(".legenda").stop(true,true).slideDown(null,"easeOutCirc");
	}, function() {
		$(this).find(".legenda").stop(true,true).slideUp(null,"easeOutCirc");
	})
	
	
	/////////TWEET//////
		$("#twitter").tweet({
			query: "from:performancenut",
			avatar_size: 48,
			count: 2,
			loading_text: "Carregando..."
		});
		
	$("#busca").submit(function() {
		if (!$(this).find("input[type=text]").val()) {
			alert("Preencha	a palavra-chave");
			return false;
		}
	})
	
	
})

function form_erro(mensagem, campo) {
	alert(mensagem);
	campo.focus();
}

function valida_contato(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Preencha o nome", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Preencha o e-mail", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("E-mail Inválido", form.email);
		return false;
	}
	if ($.trim(form.telefone.value) && form.telefone.value.length < 12) {
		form_erro("Telefone inválido", form.telefone);
		return false;
	}
	if (!$.trim(form.assunto.value)) {
		form_erro("Preencha o assunto", form.assunto);
		return false;
	}
	if (!$.trim(form.mensagem.value)) {
		form_erro("Preencha a mensagem", form.mensagem);
		return false;
	}
}

function valida_contato_en(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Fill in the name", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Fill in the e-mail", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("Invalid e-mail address", form.email);
		return false;
	}
	if ($.trim(form.telefone.value) && form.telefone.value.length < 12) {
		form_erro("Invalid phone number", form.telefone);
		return false;
	}
	if (!$.trim(form.assunto.value)) {
		form_erro("Fill in the subject", form.assunto);
		return false;
	}
	if (!$.trim(form.mensagem.value)) {
		form_erro("Fill in the message", form.mensagem);
		return false;
	}
}

function valida_contato_es(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Escriba el nombre", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Rellene el correo electrónico", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("Este correo electrónico no es válida", form.email);
		return false;
	}
	if ($.trim(form.telefone.value) && form.telefone.value.length < 12) {
		form_erro("Este teléfono no es válido", form.telefone);
		return false;
	}
	if (!$.trim(form.assunto.value)) {
		form_erro("Rellene el tema", form.assunto);
		return false;
	}
	if (!$.trim(form.mensagem.value)) {
		form_erro("Rellene el mensaje", form.mensagem);
		return false;
	}
}

function valida_cadastro(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Preencha o nome", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Preencha o e-mail", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("E-mail Inválido", form.email);
		return false;
	}
}

function valida_cadastro_en(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Fill in the name", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Fill in the e-mail", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("Invalid e-mail address", form.email);
		return false;
	}
}

function valida_cadastro_es(form) {
	if (!$.trim(form.nome.value)) {
		form_erro("Escriba el nombre", form.nome);
		return false;
	}
	if (!$.trim(form.email.value)) {
		form_erro("Rellene el correo electrónico", form.email);
		return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
		form_erro("Este correo electrónico no es válida", form.email);
		return false;
	}
}

function valida_cadastro_evento(form) {
	if (!$.trim(form.nome.value)) {
            form_erro("Preencha o nome", form.nome);
            return false;
	}
	if (!$.trim(form.email.value)) {
            form_erro("Preencha o e-mail", form.email);
            return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
            form_erro("E-mail Inválido", form.email);
            return false;
	}
        if (!$.trim(form.telefone.value) && !$.trim(form.celular.value)) {
            form_erro("Obrigatório informar um número de telefone ou celular", form.telefone);
            return false;
	}
        if (!$.trim(form.endereco.value)) {
            form_erro("Preencha o Endereço", form.endereco);
            return false;
	}
        if (!$.trim(form.numero.value)) {
            form_erro("Preencha o Numero", form.numero);
            return false;
	}
        if (!$.trim(form.bairro.value)) {
            form_erro("Preencha o Bairro", form.bairro);
            return false;
	}
        if (!$.trim(form.cidade.value)) {
            form_erro("Preencha a Cidade", form.cidade);
            return false;
	}
        if (!$.trim(form.estado.value)) {
            form_erro("Preencha o Estado", form.estado);
            return false;
	}
        if (!$.trim(form.profissao.value)) {
            form_erro("Preencha a profissão", form.profissao);
            return false;
	}
        if (!$.trim(form.reg_profissional.value)) {
            form_erro("Preencha o Registro Profissional", form.reg_profissional);
            return false;
	}
        return true;
}

function valida_cadastro_evento_es(form) {
	if (!$.trim(form.nome.value)) {
            form_erro("Escriba el nombre", form.nome);
            return false;
	}
	if (!$.trim(form.email.value)) {
            form_erro("Rellene el E-mail", form.email);
            return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
            form_erro("No válida de E-mail", form.email);
            return false;
	}
        if (!$.trim(form.telefone.value) && !$.trim(form.celular.value)) {
            form_erro("Obligatorio ingresar un número de teléfono o teléfono celular", form.telefone);
            return false;
	}
        if (!$.trim(form.endereco.value)) {
            form_erro("Dirección completa", form.endereco);
            return false;
	}
        if (!$.trim(form.numero.value)) {
            form_erro("Proporcione el número de", form.numero);
            return false;
	}
        if (!$.trim(form.bairro.value)) {
            form_erro("Rellene el Vecindario", form.bairro);
            return false;
	}
        if (!$.trim(form.cidade.value)) {
            form_erro("Introducir en la ciudad", form.cidade);
            return false;
	}
        if (!$.trim(form.estado.value)) {
            form_erro("Rellene el Estado", form.estado);
            return false;
	}
        if (!$.trim(form.profissao.value)) {
            form_erro("Dile a la profesión", form.profissao);
            return false;
	}
        if (!$.trim(form.reg_profissional.value)) {
            form_erro("Completa el Registro Profesional", form.reg_profissional);
            return false;
	}
        return true;
}

function valida_cadastro_evento_en(form) {
	if (!$.trim(form.nome.value)) {
            form_erro("Fill in the name", form.nome);
            return false;
	}
	if (!$.trim(form.email.value)) {
            form_erro("Fill in the email", form.email);
            return false;
	}
	if (form.email.value.indexOf("@", 0) == -1 || form.email.value.indexOf(".", 0) == -1) {
            form_erro("Invalid Email", form.email);
            return false;
	}
        if (!$.trim(form.telefone.value) && !$.trim(form.celular.value)) {
            form_erro("Obligatory enter a phone number or cell phone", form.telefone);
            return false;
	}
        if (!$.trim(form.endereco.value)) {
            form_erro("Complete Address", form.endereco);
            return false;
	}
        if (!$.trim(form.numero.value)) {
            form_erro("Fill in the Number", form.numero);
            return false;
	}
        if (!$.trim(form.bairro.value)) {
            form_erro("Fill in the District", form.bairro);
            return false;
	}
        if (!$.trim(form.cidade.value)) {
            form_erro("Fill in the City", form.cidade);
            return false;
	}
        if (!$.trim(form.estado.value)) {
            form_erro("Fill in the State", form.estado);
            return false;
	}
        if (!$.trim(form.profissao.value)) {
            form_erro("Fill in the profession", form.profissao);
            return false;
	}
        if (!$.trim(form.reg_profissional.value)) {
            form_erro("Complete the Professional Registration", form.reg_profissional);
            return false;
	}
        return true;
}

////////BUSCA//////////
function abreBusca() {
	if ($("#busca").is(":hidden")) {
		$("#busca").slideDown("slow", "easeOutExpo");	
	} else {
		$("#busca").slideUp("fast", "easeOutExpo");
	}
	
}

function conteudo_scroll() {
	$('.scroll').jScrollPane();	
}

function album_carousel(id_conteudo) {
	
	////////////ALBUM//////////////////////////////////////////////////////////////////////////
		function album_itemload(carousel, state) {
			// Check if the requested items already exist
			if (carousel.has(carousel.first, carousel.last)) {
				return;
			}
		
			jQuery.get(
				'../xml/album_conteudo.php',
				{
					first: carousel.first,
					last: carousel.last,
					conteudo: id_conteudo
				},
				function(xml) {
					album_additem(carousel, carousel.first, carousel.last, xml);
				},
				'xml'
			);
			
		}
		
		function album_additem(carousel, first, last, xml) {
			// Set the size of the carousel
			carousel.size(parseInt(jQuery('total', xml).text()));
		
			$('image', xml).each(function(i) {
				carousel.add(first + i, album_htmlhelper($(this).find('imagem').text(), $(this).find('legenda').text(), $(this).find('url').text()));
			});
		};
		
		function album_htmlhelper(imagem, legenda, url) {
			
			var marcacao = '<a href="' + url + '" class="linkModal"><img src="../admin/4/fotos/' + imagem + '" alt="" /></a>';
			return marcacao;
			
		};
		
		function album_itemvisivel() {
			
		}
		
		$("#album_carousel ul").jcarousel({
			scroll: 3,
			animation: 1000,
			itemLoadCallback: album_itemload,
			itemFirstInCallback: album_itemvisivel
		})	
}

function veja_tambem(id_conteudo, id_categoria, id_subcategoria, id_lingua) {
	
	////////////VEJA TAMBEM//////////////////////////////////////////////////////////////////////////
		
		$("#veja_tambem li").live("mouseenter", function() {
			$(this).find(".legenda").stop(true,true).slideDown();
		})
		
		$("#veja_tambem li").live("mouseleave", function() {
			$(this).find(".legenda").stop(true,true).slideUp();
		})
		
		function vejatambem_itemload(carousel, state) {
			// Check if the requested items already exist
			if (carousel.has(carousel.first, carousel.last)) {
				return;
			}
		
			jQuery.get(
				'../xml/veja_tambem.php',
				{
					first: carousel.first,
					last: carousel.last,
					conteudo: id_conteudo,
					categoria: id_categoria,
					subcategoria: id_subcategoria ? id_subcategoria : "",
					lingua: id_lingua
				},
				function(xml) {
					vejatambem_additem(carousel, carousel.first, carousel.last, xml);
				},
				'xml'
			);
			
		}
		
		function vejatambem_additem(carousel, first, last, xml) {
			// Set the size of the carousel
			carousel.size(parseInt(jQuery('total', xml).text()));
		
			$('image', xml).each(function(i) {
				carousel.add(first + i, vejatambem_htmlhelper($(this).find('imagem').text(), $(this).find('resenha').text(), $(this).find('url').text()));
			});
		};
		
		function vejatambem_htmlhelper(imagem, resenha, url) {
			
			var marcacao = '<a href="' + url + '" class="img"><img src="../admin/4/fotos/' + imagem + '" alt="" /></a>';
			marcacao += '<div class="legenda">' + resenha + '</div>';
			return marcacao;
			
		};
		
		$("#veja_tambem ul").jcarousel({
			scroll: 3,
			animation: 1000,
			itemLoadCallback: vejatambem_itemload
		})
}
//////////INIT HOME///////////////////////////////////////
	function initHome(id_lingua) {
		
		$("#vitrine li").live("mouseenter mouseleave", function(event) {
			if (event.type == 'mouseenter') {
				$(this).find(".legenda").stop(true,true).slideDown();
			} else {
				$(this).find(".legenda").stop(true,true).slideUp();
			}
			
		})
		
		
		////////////VITRINE//////////////////////////////////////////////////////////////////////////

		function mycarousel_itemLoadCallback(carousel, state) {
			// Check if the requested items already exist
			if (carousel.has(carousel.first, carousel.last)) {
				return;
			}
		
			jQuery.get(
				'../xml/vitrine.php',
				{
					first: carousel.first,
					last: carousel.last,
					lingua: id_lingua
				},
				function(xml) {
					mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
				},
				'xml'
			);
			
		}
		
		function mycarousel_itemAddCallback(carousel, first, last, xml) {
			// Set the size of the carousel
			carousel.size(parseInt(jQuery('total', xml).text()));
		
			$('image', xml).each(function(i) {
				carousel.add(first + i, mycarousel_getItemHTML($(this).find('imagem').text(), $(this).find('resenha').text(), $(this).find('url').text()));
			});
		};
		
		function mycarousel_getItemHTML(imagem, resenha, url) {
			
			var marcacao = '<a href="' + url + '" class=\"img\"><img src="../admin/4/fotos/' + imagem + '" alt="" /></a>';
			marcacao += '<div class="legenda">' + resenha + '</div>';
			return marcacao;
			
		};
		
		$("#vitrine ul").jcarousel({
			scroll: 2,
			animation: 1000,
			itemLoadCallback: mycarousel_itemLoadCallback
		})
		
		///////////////////FLASHTV//////////////////////////////////////////////////////////
			function flashtv_itemload(carousel, state) {
				// Check if the requested items already exist
				if (carousel.has(carousel.first, carousel.last)) {
					return;
				}
			
				jQuery.get(
					'../xml/flashtv.php',
					{
						first: carousel.first,
						last: carousel.last,
						lingua: id_lingua
					},
					function(xml) {
						flashtv_additem(carousel, carousel.first, carousel.last, xml);
					},
					'xml'
				);
				
			}
			
			function flashtv_additem(carousel, first, last, xml) {
				// Set the size of the carousel
				carousel.size(parseInt(jQuery('total', xml).text()));
			
				$('image', xml).each(function(i) {
					carousel.add(first + i, flashtv_htmlhelper($(this).find('imagem').text(), $(this).find('url').text()));
				});
			};
			
			function flashtv_htmlhelper(imagem, url) {
				
				var foto = '<img src="../admin/5/fotos/' + imagem + '" alt="" />'
				
				if (url) {
					var marcacao = '<a href="' + url + '">' + foto + '</a>';
				} else {
					var marcacao = foto;
				}
				
				return marcacao;
				
			};
			
			function flashtv_itemvisible(carousel, idx, item) {
				$("#flashtv .menu").animate({
					backgroundPosition: (item - 1) * 15 + "px"
				}, 1000, "easeOutCirc"
				)
			}
			
			function flashtv_initcallback(carousel) {
				$("#flashtv .menu li").click(function() {
					carousel.scroll($(this).index("#flashtv .menu li") + 1);
				})
				
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				})
			}
			
			$(".jcarousel-skin-flashtv").jcarousel({
				scroll: 1,
				auto: 6,
				animation: 500,
				wrap: "last",
				easing: "easeOutExpo",
				initCallback: flashtv_initcallback,
				itemLoadCallback: flashtv_itemload,
				itemFirstInCallback: { onBeforeAnimation: flashtv_itemvisible }
			})
		
	}
	


function Mascara(tipo, campo, teclaPress) {
    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var s = new String(campo.value);
    // Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g, '');

    tam = s.length + 1;

    if (tecla != 9 && tecla != 8) {
        switch (tipo) {
            case 'TEL':
				$(campo).attr("maxlength","12")
                if (!(tecla >= 48 && tecla <= 57)) {
                    if (tecla != 0) {
                        return false
                    }
                } else {
                    if (tam > 2 && tam < 4)
                        campo.value = s.substr(0, 2) + ' ' + s.substr(2, tam);
                    if (tam >= 7 && tam < 11)
                        campo.value = s.substr(0, 2) + ' ' + s.substr(2, 4) + '-' + s.substr(6, tam - 6);
                }

                break;

			case 'NUMEROS':
                if (!(tecla >= 48 && tecla <= 57)) {
                    if (tecla != 0) {
                        return false
                    }
                }
                break;
        }
    }
}

