$(function(){
	
	Utils.fieldFocus($("input[name=nome]"));
	Utils.fieldFocus($("input[name=email]"));
	Utils.fieldFocus($("input[name=nome_amigo]:last"));
	Utils.fieldFocus($("input[name=email_amigo]:last"));
	
	$(".btAddAmigo").click(function(){
		var htmlCode = '<label class="fieldMd">'+
			'<input type="text" name="nome_amigo" value="Nome" maxlength="100"/></label>'+
			'<label class="fieldMd2">'+
			'<input type="text" name="email_amigo" value="E-mail" maxlength="255"/></label>';
		
		$(this).before(htmlCode);
		
		Utils.fieldFocus($("input[name=nome_amigo]:last"));
		Utils.fieldFocus($("input[name=email_amigo]:last"));
		
		return false;
	});
	
	if ($("#frmAmigoSecreto").size() != 0) {
		$("#frmAmigoSecreto").validate({
			errorLabelContainer: $("ul.error"),
			wrapper: 'li',
			submitHandler:function(form){
				var qtd = 0;
				$(form).find("input[name=nome_amigo]").each(function(index,item){
					var nomeAmigo = $.trim($(item).val());
					var emailAmigo = $.trim($("input[name=email_amigo]").eq(index).val());
					
					if(nomeAmigo!="" && nomeAmigo!="Nome" && emailAmigo!="" && emailAmigo!="E-mail"){
						qtd++;
					}
				});
				
				$("input[name=qtd_amigos]").remove();
				
				var input = $("<input/>");
				input.attr({name:"qtd_amigos",type:"hidden"}).val(qtd);
				$(form).append(input);
				$(form).submit();
			},
			rules: {
				nome: 'required',
				email: {
					required: true,
					email: true
				},
				nome_amigo: 'required',
				email_amigo: {
					required: true,
					email: true
				}
			},
			messages: {
				nome: 'Preencha o campo Nome',
				email: {
					required:'Preencha o campo email',
					email:'Endereço do campo E-mail inválido'
				},
				nome_amigo: 'Preencha o campo Nome do amigo',
				email_amigo: {
					required:'Preencha o campo E-mail do amigo',
					email:'Endereço do campo E-mail do amigo inválido'
				}
			}
		});
	}
	
	if ($("#frmAmigoSecretoPresentes").size() != 0) {
		$("input[name=presente_desc]").each(function(index,item){
			Utils.fieldFocus(item);
		});
	
		var frmPresente = $("#frmAmigoSecretoPresentes").validate({
			errorLabelContainer: $("ul.error"),
			wrapper: 'li',
			rules: {				
				presente_desc: 'required'
			},
			messages: {
				presente_desc: 'Preencha o campo Descrição de presente'
			}
		});
		
		$("input[name=presente_url]").blur(function(){
			re = /^http:\/\//g;		
			if ($(this).val().search(re)==-1 && $.trim($(this).val())!="") {
				$(this).val("http://" +$(this).val());
			}
			frmPresente.element(this);
		});
	}
	
	if($("#opcoesPresentes").size()!=0){
		var ram = parseInt(Math.random() * 4);	
		$("#opcoesPresentes").find("img").eq(ram).show();
	}
	
	$("#jsMeusPresentes").click(function(){
		dataMetrics({pageName:"Natal - Botao - Prefiro minha lista",channel:"Sadia Natal",events:""});
		$("#opcoes").hide();
		$("#opcao1").fadeIn("fast");
		return false;
	});
	
	$("#jsTrocarPresentes").click(function(){
		dataMetrics({pageName:"Natal - Botao - Quero trocar",channel:"Sadia Natal",events:""});
		$("#opcoes").hide();
		$("#opcao2").fadeIn("fast");
		
		return false;
	});
	
	$(".btDescubraPresentesAmigos").click(function(){
		dataMetrics({pageName:"Natal - Descubra o que seu amigo quer ganhar",channel:"Sadia Natal",events:"event51"});
	});
	
	if (typeof sIFR != "undefined") {
		fontePath = {src: '/swf/sifr_frankGothic.swf'};
		sIFR.replace(fontePath, {
			selector: 'h2',
			css: '.sIFR-root,a,a:hover{color:#873a00;text-decoration:none}',
			wmode: 'transparent'
		});
	}
})
