j = jQuery;
j(document).ready(function() {
	Cufon.replace('.fonte1,.menu-topo a',{fontFamily: 'Myriad Pro' });
	Cufon.replace('.titulo',{fontFamily: 'Myriad Pro Light' });
	Cufon.replace('.titulo',{
		color: '-linear-gradient(#DC261D, #B62018)'
	});
	j('.listPF dt').click(function(){
		j(this).toggleClass("open");
		j(this).next().slideToggle() 
		//if(j(this).next().css("display") == "none")j(this).next().fadeIn();
		//else j(this).next().fadeOut();
	});
	j('.menustyle01 a').hover(function(){
		if(j(this).hasClass('on')){ muda = false;}
		else { muda = true;}
		if(muda == true){j(this).addClass('on');}
	}, function() {
		if(muda == true){j(this).removeClass('on');}
	});
	j('#slideestrutura a').live('click',function(){
		j('#slideestrutura .atual').removeClass('atual');
		j(this).addClass('atual');
		var classerecebeimg = 'imgestrutura';
		var imagem = j(this).attr('href');
		var imagem2 = j('.'+classerecebeimg).find('img').attr('src');
		if(imagem == imagem2){return false;}
		mudaImagem(imagem,classerecebeimg);
		return false;
	});
	function mudaImagem(imagem,recebeimg){
		var dimensoes = j('.'+recebeimg);
		dimensoes.removeAttr("width");
		dimensoes.removeAttr("height");
		var largura = dimensoes.width();
		var altura  = dimensoes.height();
		j('.'+recebeimg).css({height: altura+'px'});
		j('.'+recebeimg).find('img').fadeOut();
		var img = new Image();
		j(img).load(function (){ //começa o load da imagem para dentro da variavel
			j.ajax({
				timeout: 30000,
				error: function() {
					alert("Imagem indisponível no momento.");
					j('.'+recebeimg).find('img').fadeIn();
			},
				success: function() {
					j('.'+recebeimg).html(img);
					var pic = j('.'+recebeimg).find('img');
					pic.removeAttr("height");
					var largura = pic.width()+2;
					var altura  = pic.height()+5;
					j('.'+recebeimg).stop().animate({width:largura+"px",height:altura+"px"}, 500,function(){
						j(img).stop().fadeIn(500);
					});
				}
			});
		}).attr('src', imagem);
	}
	j('#outrosvideos li a').live('click',function(event){
		var video = j(this).attr('id');
		var video = video.split('video');
		var video = video[1];
		var video2 = j('.boxvideo .video').attr('id');
		var video2 = video2.split('video');
		var video2 = video2[1];
		mudaVideo(video,video2,event.target);
		return false;
	});
	function mudaVideo(video,video2,alvo){
		j.post("trocavideo.php", {videoid: video,videoid2: video2},
			function(data){
				var datas = data.split('//split//');
				var data1 = datas[0];
				var data2 = datas[1];
				j('.boxvideo').html(data1);
				j(alvo).closest('li').html(data2);
			}
		);
	};
	
	/*
	j('.menustyle01 a').live('click',function(){
		var id = j(this).attr('id');
		j('.menustyle01 a').removeClass('on');
		j(this).addClass('on');
		muda = false;
		j('.itens').fadeOut('fast',function(){
			j('#'+id).fadeIn('fast');
		});
	});
	*/
	
	/*j('.reflete,cufon,canvas,twitter').reflect({height:0.33,opacity:0.5});
	j('.reflete').vflection({
		color:'black', 
		parent: 'li',
		height: 20,
		darkness: 20,
		opacity: 20
	});*/
	j('a[rel="external"]').click(function(){window.open(j(this).attr('href'));return false;});
	/*j('.linkenderecos').click(function(){
		if(j(this).hasClass('on')){return false;}
		var endereco = j(this).attr('id');
		j('.linkenderecos').removeClass('on');
		j(this).addClass('on');
		j('.enderecos').hide();
		j('.'+endereco).fadeIn('fast');
	});*/
	
	j('.linkenderecos').click(function(){
		if(j(this).hasClass('on')){return false;}
		var endereco = j(this).attr('id');
		j('.linkenderecos').removeClass('on');
		j(this).addClass('on');
		j('.enderecos').css({'opacity':0,'z-index':1});
		j('.'+endereco).css({'z-index':2});
		j('.'+endereco).animate({opacity:1},500);
	});
	
	
	j('.limpa').focus(function(){
		if( this.value == this.defaultValue ){
			this.value = "";
		}
	}).blur(function(){
		if( !this.value.length ){
			this.value = this.defaultValue;
		}
	});
	j('form.validaform').submit(function(){
		var formid = j(this).attr('id');
		var form = 'form[id='+formid+']';
		var action = j(this).attr('action');
		var params = j(form).serialize();
		var temerro = "";
		var validateMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		j('.input').removeClass('erro');
		j('.textarea').removeClass('errotext');
		j(form+' input').each(function(){
			if(!j(this).hasClass('null') && j(this).is(':visible') && this.type != 'image' && this.type != 'submit' && this.type != 'radio' && this.type != 'checkbox' ){
				if(this.value == this.defaultValue || this.value == ''){
					j(this).parent('div.input').addClass('erro');
					temerro = 1;
				}
				if(j(this).hasClass('email')){
					if(!validateMail.test(j(this).val())){
						j(this).parent('div.input').addClass('erro');
						if(j(this).parent('div.input').prev('label').find('span').length == 0){
							j(this).parent('div.input').prev('label').append('<span class="erroemail">email inválido</span>');
							j(this).parent('div.input').prev('label').find('.erroemail').fadeIn();
						}else{
							j(this).parent('div.input').prev('label').find('.erroemail').fadeIn();
						}
						temerro = 1;
					}
				}
			}
		})
		j(form+' textarea').each(function(){
			if(!j(this).hasClass('null') && j(this).is(':visible')){
				if(this.value == this.defaultValue){
					j(this).parent('.textarea').addClass('errotext');
					temerro = 1;
				}
			}
		})
		if(temerro){
			var oerro = j(form+' .erro:first').find('input').attr('title');
			if(!oerro){var oerro = j(form+' .errotext:first').find('textarea').attr('title');}
			if(oerro){
				j('.campoerrado').html('Preencha '+oerro);
				j('.campoerrado').hide();
				j('.campoerrado').fadeIn('fast');
			}else{
				j('.campoerrado').fadeOut('fast');
			}
			if(j('.erro').length > 0){
				j(form+' .erro:first').find('input').focus();
			}
			else{
				j(form+' .errotext:first').find('input').focus();
			}
			return false;
		}
		else{
			j(form+' fieldset .campoerrado').hide();
			if(!j(form).hasClass('normal')){
				var formdimensoes = j(form);
				formdimensoes.removeAttr("width");
				formdimensoes.removeAttr("height");
				var largura = formdimensoes.width();
				var altura  = formdimensoes.height();
				//alert(largura+"___"+altura);
				j(form).addClass('loading');
				j(form).css({height:altura, width:largura});
				j(form+' fieldset').fadeOut();
				setTimeout(function () {
					j.ajax({
					data: params,
					type: 'POST',
					url: action,
					timeout: 15000,
					error: function(retorno){
						j(form).removeClass('loading');
						j(form).find('fieldset').fadeIn();
						alert("Desculpe, houve um erro ao enviar os dados, tente mais tarde.");
						},
					success: function(retorno){ 
							j(form).removeClass('loading');
							j(form).find('.retorno').fadeIn();
							j(form)[0].reset();
							return false;
						}
					})
					return false;
				}, 3000);
				return false;
			}
		}
		return false;
	});
	j('input,textarea').focus(function(){
		j(this).parent().removeClass('erro');
		j(this).parent().removeClass('errotext');
		j(this).parent().prev().find('.erroemail').fadeOut();
	});
	j('.retorno a').click(function(){
		j(this).closest('.retorno').fadeOut(function(){
			j(this).closest('form').find('fieldset').fadeIn();
		});									
	});
});
