var SITE_URL = "/routeone/ajax";

var destaqueCount = 5;
var currentDestaque = 1;
var setTimeoutTime;

var options = {
		url :SITE_URL + '?cmd=MailsSite', // override for form's 'action' attribute
		beforeSubmit :beforeAjaxForm, // pre-submit callback
		success :successAjaxForm
	};

jQuery(document).ready( function() {

	$.ajaxSetup({
		cache : false,
		contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
	});
	
	jQuery(".ibox").each( function() {
		
		$(this).colorbox( {
			iframe :true,
			overlayClose :false,
			width : 700,
			height : 575
		});
	});
	
	jQuery(".ibox1024").each( function() {
		
		$(this).colorbox( {
			iframe :true,
			overlayClose :false,
			width : 1024,
			height : 780
		});
	});
	
	jQuery(".ibox800").each( function() {
		
		$(this).colorbox( {
			iframe :true,
			overlayClose :false,
			width : 800,
			height : 575
		});
	});

	jQuery(".ibox500").each( function() {
		
		$(this).colorbox( {
			iframe :true,
			overlayClose :false,
			width : 500,
			height : 350
		});
	});
	
	$("#dataEmbarque").mask("99/99/9999");
	$("#dataRetorno").mask("99/99/9999");
	$("#telefone").mask("(99)9999-9999");
	$("#telefoneVistos").mask("(99)9999-9999");
	$("#telefoneEmpresas").mask("(99)9999-9999");
	$("#telefoneReserva").mask("(99)9999-9999");
	$("#numeroCriancasInformacoes").mask("9");
	$("#numeroAdultosInformacoes").mask("9");

	$("#dataEmbarque").Watermark("data de partida");
	$("#dataRetorno").Watermark("data de retorno");
	$("#telefone").Watermark("telefone de contato");
	$("#nome").Watermark("seu nome");
	$("#emailInformacoes").Watermark("seu email");
	$("#destino").Watermark("seu destino");
	$("#numeroAdultosInformacoes").Watermark("número adultos");
	$("#numeroCriancasInformacoes").Watermark("número crianças");
	
	$(".hotsiteCombo").each(function(){
		$(this).mouseout(function(){
			$("#comboList").hide();
		});
	});

	$("#budgetHomeForm").validate( {
		errorClass :"errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			emailInformacoes : {
				required :true,
				email :true
			}
			
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});

	$("#contactForm").validate( {
		errorClass :"errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			nome : {
				required :true
			},
			email : {
				required :true,
				email :true
			},
			telefone : {
				required :true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	$("#vistosInnerForm").validate( {
		errorClass :"errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			email : {
				required :true,
				email :true
			},
			mensagem : {
				required :true
			},
			pais : {
				required :true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	$("#trainsInnerForm").validate( {
		errorClass :"errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			email : {
				required :true,
				email :true
			},
			mensagem : {
				required :true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	$("#formSolicitacaoReserva").validate( {
		errorClass :"errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(options);
		},
		rules : {
			emailInformacoes : {
				required :true,
				email :true
			}
			
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	var newsOption = {
			url :SITE_URL + '?cmd=Proxy', // override for form's 'action' attribute
			beforeSubmit :beforeNewsForm, // pre-submit callback
			success :successNewsletterForm
		};
	
	$("#formNewsletter").validate( {
		errorClass : "errorField",
		submitHandler : function(form) {
			$(form).ajaxSubmit(newsOption);
		},
		rules : {
			mail : {
				required :true,
				email :true
			}
			
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	$("#dadosCadastrais").validate( {
		errorClass : "errorField",
		rules : {
			nome : {
				required :true
			},
			senha : {
				required :true
			},
			cpf : {
				required :true
			},
			telefonePrimario : {
				required :true
			},
			dataNascimento : {
				required :true
			},
			logradouro : {
				required :true
			},
			bairro : {
				required :true
			},
			cep : {
				required :true
			},
			confirmpassword : {
				required :true,
				equalTo: "#senha"
			},
			rg : {
				required :true
			},
			telefoneSecundario : {
				required :true
			},
			email : {
				required :true,
				email: true
			},
			numero : {
				required :true
			},
			cidade : {
				required :true
			},
			estado: {
				required :true
			},
			pais: {
				required :true
			}
		},
		errorPlacement : function(error, element) {
			return true;
		}
	});
	
	destaqueCount = $("#destaqueCount").val();
	
	setTimeoutTime = setInterval('destaqueLoop()', 6000);
	
});

function destaqueLoop(){
	
	$("#highlight" + currentDestaque).fadeOut(function(){
		
		$("#destaque" + currentDestaque).removeClass("current");
		
		currentDestaque ++;
		
		if(currentDestaque > destaqueCount){
			currentDestaque = 1;
		}
		
		$("#highlight" + currentDestaque).fadeIn();
		$("#destaque" + currentDestaque).addClass("current");
	});

}

function setDestaque(index){
	
	clearTimeout(setTimeoutTime);
	
	$("#highlight" + currentDestaque).fadeOut(function(){
		$("#destaque" + currentDestaque).removeClass("current");
		currentDestaque = index;
		$("#highlight" + currentDestaque).fadeIn();
		$("#destaque" + currentDestaque).addClass("current");
	});
	
}

function beforeAjaxForm() {
	$("#waiting").show();
}

function successAjaxForm(responseText, statusText) {

	$("#success").hide();
	$("#error").hide();
	
	var params = {
		to :'configuration',
		subject :'[ROUTE ONE] Solicitacao de Informacoes ',
		html :responseText
	}

	$.post(SITE_URL + '?cmd=SendMail', params, function(data) {
		if (data == "true") {
			$("#waiting").hide();
			$("#success").show();

		} else {
			$("#waiting").hide();
			$("#error").show();
		}
	});

}

function beforeNewsForm() {
	$("#newsSubmit").hide();
	$("#newsWaiting").show();
}

function successNewsletterForm(responseText, statusText) {

	$("#newsSuccess").hide();
	$("#newsError").hide();

	if (responseText == "true") {
		$("#newsWaiting").hide();
		$("#newsSuccess").show();
	} else {
		$("#newsDefault").hide();
		$("#newsWaiting").hide();
		$("#newsError").show();
		$("#newsSubmit").show();
	}


}

function recordAccess(page) {
	
	var pageTracker = _gat._getTracker("UA-8502787-3");
	
	pageTracker._trackPageview(page);
}

function comboShow(idElemento) {	
	 $("#"+idElemento).show("fast");
}

function comboHide(idElemento) {	
	 $("#"+idElemento).hide("fast");
}

function normalizeForm(formId) {
	
	var text;
	var value;
	
	$("#" + formId + " :input").each(function(){
		
		value = $(this).val();
		text = normalizeText(value);
		
		$(this).attr("value", text);
		
	});
	
	$("#" + formId + " :textarea").each(function(){
		
		value = $(this).val();
		text = normalizeText(value);
		
		$(this).attr("value", text);
		
	});
	
}
	
function normalizeText(text) {
	
	if(text == undefined) {
		return undefined;
	}
	
	text = text.replace(new RegExp('[áãâã]','gi'), 'a'); 
	text = text.replace(new RegExp('[éèê]','gi'), 'e'); 
	text = text.replace(new RegExp('[íìî]','gi'), 'i'); 
	text = text.replace(new RegExp('[óòôõ]','gi'), 'o'); 
	text = text.replace(new RegExp('[úùû]','gi'), 'u'); 
	text = text.replace(new RegExp('[ç]','gi'), 'c');
	
	text = text.replace(new RegExp('[ÁÃÂÃ]','gi'), 'A'); 
	text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'E'); 
	text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'I'); 
	text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'O'); 
	text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'U'); 
	text = text.replace(new RegExp('[Ç]','gi'), 'C');

	return text;
}; 
