this.NavMenu = {
	init: function(currentSection) {
		$(".nav-menu-ul").children().each(function()
		{
			if($(this).attr("sec") == currentSection)
			{
				var uri = $(this).css("background-image").replace(".png", "-selected.png");
				$(this).css("background-image", uri);
				$(this).addClass("nav-menu-selected");
			} else {
				$(this).hover(function() {
					 $(this).addClass("nav-menu-hover");
				},function(){
					 $(this).removeClass("nav-menu-hover");
				});
			}
		});
	}
}


this.Ofertas = {
	timer:			0,
	timeout:		10000,
	currentIndex:	-1,
	count:			0,
	initialized:	false,
	init: function()
	{
		Ofertas.currentIndex = -1;

		$(".ofertas").cycle({
			fx:			'fade',
			timeout:	0,
			speed:		200
		});

		if(!Ofertas.initialized)
		{
			Ofertas.initialized = true;
			var items			= $(".ofertas-menu-li li");
			Ofertas.count		= items.length;
			items.each(function(i) {
					$(this).click(function() {
						Ofertas.setIndex(i);
					});
			});
		}

		Ofertas.setIndex(0);
	},
	setIndex: function(index)
	{
		clearTimeout(Ofertas.timer);

		if(index >= Ofertas.count)
			index = 0;

		Ofertas.currentIndex = index;

		$('.ofertas').cycle(index);
		var items = $(".ofertas-menu-li li");
		items.each(function(i) {
			if(i != index)
				$(this).removeClass("ofertas-menu-selected");
			else
				$(this).addClass("ofertas-menu-selected");
		});

		Ofertas.timer = setTimeout("Ofertas.cycle()", Ofertas.timeout);
	},
	cycle: function()
	{
		Ofertas.setIndex(Ofertas.currentIndex + 1);
	}
}

this.Promo = {
	timer:			0,
	timeout:		10000,
	currentIndex:	-1,
	count:			0,
	setup: function()
	{
		Promo.count = $('.promo-home-container').children().length;
		$('.promo-home-container').cycle({
			fx: 'fade',
			timeout: 0,
			speed: 200
		});

		var items = $(".promo-home-menu ul li");
		items.each(function(i) {
				$(this).click(function() {
					Promo.setIndex(i);
				});
		});
		$(".promo-home-banner-msk").click(function() {
			Promo.openLink();
		});
	},
	openLink: function() {
		var container = $(".promo-home-container").find("a");
		var link	= $(container[Promo.currentIndex - 1]);
		link		= link ? link.attr("href") : "";
		if(link)
			window.open(link, "_self");
	},
	setIndex: function(index)
	{
		//clearTimeout(Promo.timer);

		if(index >= Promo.count)
			index = 0;

		Promo.currentIndex = index;


		try
		{
			var links = $(".promo-home-container").find("a");
			var link	= $(links[Promo.currentIndex - 1]);
			link		= link ? link.attr("href") : "";
			$(".promo-home-banner-msk").css("cursor", link ? "pointer" : "default");
		} catch (e) {
		}
		

		if(index == 0)
		{
			$(".ofertas-menu").show();
			Ofertas.init();
		} else {
			$(".ofertas-menu").hide();
		}

		$('.promo-home-container').cycle(index);
		var items = $(".promo-home-menu ul li");
		items.each(function(i) {
			if(i == index)
				$(this).removeClass("promo-home-menu-disabled");
			else
				$(this).addClass("promo-home-menu-disabled");
		});
		
		$(".promo-home-arrow").css("top", ((index * 86) + 30));

		//Promo.timer = setTimeout("Promo.cycle()", Promo.timeout);
	},
	cycle: function()
	{
		Promo.setIndex(Promo.currentIndex + 1);
	},
	init: function() {
		$(function() {
			Promo.setup();
			Promo.setIndex(0);
		});
	}
}

this.FormContato = {
	inputs: ["#input_nome", "#input_email", "#input_fone", "#input_msg"],
	submit: function()
	{
		if(!Utils.validateForm(FormContato.inputs))
		{
			FormContato.showMessage("Erro", "Preencha todos os campos");
			return;
		}
		var postData= String("fid={fid}&nome={nome}&email={email}&fone={fone}&mensagem={mensagem}")
							.split("{fid}").join(escape($("#fid").val()))
							.split("{nome}").join(escape($("#input_nome").val()))
							.split("{email}").join(escape($("#input_email").val()))
							.split("{fone}").join(escape($("#input_fone").val()))
							.split("{mensagem}").join(escape($("#input_msg").val()));

		$.ajax({
			type:		"POST",
			url:		APP_PATH + "formContato.php",
			data:		postData,
			success:	function(data)
			{
				if(data.indexOf("OK") != -1)
				{
					FormContato.showMessage("Mensagem enviada!", "");
					Utils.resetForm(FormContato.inputs);
				} else {
					FormContato.showMessage("Erro", "");
				}
			},
			error: function(jqXHR, textStatus, errorThrown)
			{
				FormContato.showMessage("Erro.", "");
			}
		});
		
	},
	showMessage: function(title, msg)
	{
		AlertModal.show(title, msg);
	},
	init: function()
	{
		Utils.addInputsLabels(FormContato.inputs);
		$(".fale-bt-submit a").click(function() {
			FormContato.submit();
		});
	}
}

this.FormTrabalhe = {
	inputs: [],
	req_fields: ["#nome_completo","#cpf","#rg","#rua", "#num_endereco","#bairro","#cidade","#dia_nascimento","#mes_nascimento","#ano_nascimento"],
	fields: ["fid", "nome_completo","cpf","rg","rua","num_endereco","bairro","cidade","estado","cep","email","telefone_fixo","celular","dia_nascimento","mes_nascimento","ano_nascimento","nivel_academico","empresa","cargo","atividades_desenvolvidas","data_de_admissao","data_de_desligamento","empresa2","cargo2","atividades_desenvolvidas2","data_de_admissao2","data_de_desligamento2"],
	submit: function()
	{
		
		if(!Utils.validateForm(FormTrabalhe.req_fields))
		{
			
			FormTrabalhe.showMessage("Erro", "Preencha todos os campos");
			return;
		}

		var postData = [];
		for(var i in FormTrabalhe.fields)
			postData.push(FormTrabalhe.fields[i] + "=" + escape($(FormTrabalhe.inputs[i]).val()));

		var idiomas = ["ingles","espanhol","alemao","frances","italiano"];
		for(var i in idiomas)
			postData.push(idiomas[i] + "=" + ($("#" + idiomas[i] + ":checked").val() ? '1' : '0'));


		postData = postData.join("&");

		$.ajax({
			type:		"POST",
			url:		APP_PATH + "cadastro-trabalhe",
			data:		postData,
			success:	function(data)
			{
				if(data.indexOf("OK") != -1)
				{
					FormTrabalhe.showMessage("Dados enviados!", "");
					Utils.resetForm(FormTrabalhe.inputs);
				} else {
					FormTrabalhe.showMessage("Erro", "");
				}
			},
			error: function(jqXHR, textStatus, errorThrown)
			{
				FormTrabalhe.showMessage("Erro", "");
			}
		});
		
	},
	showMessage: function(title, msg)
	{
		AlertModal.show(title, msg);
	},
	init: function()
	{
		for(var i in FormTrabalhe.fields)
			FormTrabalhe.inputs.push("#" + FormTrabalhe.fields[i]);

		$(".trabalhe-bt-submit").click(function() {
			FormTrabalhe.submit();
		});
	}
}


this.FormSMS = {
	inputs: ["#nome", "#email", "#celular", "#celular_ddd", "#loja_preferida"],
	submit: function()
	{
		if(!$("#termos").attr("checked"))
		{
			FormSMS.showError("Você precisa aceitar os termos");
			return;
		}
		if(!Utils.validateForm(FormSMS.inputs))
		{
			FormSMS.showError("Preencha todos os campos");
			return;
		}
		var postData= String("fid={fid}&nome={nome}&email={email}&celular={celular}&loja_preferida={loja_preferida}")
							.split("{fid}").join(escape($("#fid").val()))
							.split("{nome}").join(escape($("#nome").val()))
							.split("{email}").join(escape($("#email").val()))
							.split("{celular}").join(escape($("#celular_ddd").val()) + escape($("#celular").val()))
							.split("{loja_preferida}").join(escape($("#loja_preferida").val()));
		$.jsonp({
			url:		"http://www.3pod.com.br/rissul/sms/sms.php/?a=c&"+ postData,
			context:	document.body,
			complete:	function() {}
		});
	},
	show: function()
	{
		FormSMS.setState(1);
		$(".modal-sms").show();
	},
	setState: function(state)
	{
		FormSMS.resetOutputMessages();

		state == 2 ? $("#confirmacao_sms").show() : $("#confirmacao_sms").hide();
		state == 1 ? $("#cadastro_sms").show()    : $("#cadastro_sms").hide();
		state == 3 ? $("#descadastro_sms").show() : $("#descadastro_sms").hide();
	},
	confirmar: function() {
		if(!Utils.validateForm(["#confirmacao"]))
		{
			FormSMS.showError("Informe o código de confirmação");
			return;
		}
		var codigo = $("#confirmacao").val();

		$.jsonp({
			url:		"http://www.3pod.com.br/rissul/sms/sms.php/?a=v&codigo=" + codigo,
			context:	document.body,
			complete:	function() {}
		});
	},
	resetOutputMessages: function()
	{
		$(".sms-alert").html("");
		$(".sms-output").html("");
		$(".sms-alert").hide();
	},
	descadastrar: function() {
		
		if(!Utils.validateForm(["#d_celular_ddd", "#d_celular"]))
		{
			FormSMS.showError("Preencha todos os campos");
			return;
		}
		var postData= String("fid={fid}&celular={celular}")
							.split("{fid}").join(escape($("#fid").val()))
							.split("{celular}").join(escape($("#d_celular_ddd").val()) + escape($("#d_celular").val()));
		$.jsonp({
			url:		"http://www.3pod.com.br/rissul/sms/sms.php/?a=d&"+ postData,
			context:	document.body,
			complete:	function() {}
		});
	},
	onResult: function(data)
	{
		if(data == "100" || data == 100)
		{
			FormSMS.showMessage("Você receberá, em seu celular, um código para confirmação de cadastro. Aguarde");
			Utils.resetForm(FormSMS.inputs);

			FormSMS.setState(2);

		} else {
			FormSMS.showError("Erro");
		}
	},
	confirmarResult: function(data)
	{
			if(data.indexOf("OK") != -1)
			{
				FormSMS.showMessage("Parabéns! Seu celular já está cadastrado para receber as ofertas Insupperáveis Rissul. Aproveite!");
				$(".bt-confirmacao-sms").hide();
			} else {
				FormSMS.showError("Código inválido");
			}
	},
	descadastrarResult: function(data)
	{
		FormSMS.resetOutputMessages();

		if(data.indexOf("OK") != -1)
			{
				FormSMS.showMessage("Seu celular foi descadastrado e você não irá mais receber as ofertas por sms");
				$(".bt-descadastro-sms").hide();
			} else {
				FormSMS.showError("Erro");
			}
	}
	,showMessage: function(title, msg)
	{
		$(".sms-output").html(title + "<br/>" + (msg || ""));
	},
	showError: function(msg)
	{
		$(".sms-alert").show();
		$(".sms-alert").html(msg || "");
	},
	init: function()
	{
		Utils.addInputsLabels(["#celular", "#nome", "#email", "#confirmacao"]);
		$(".bt-submit-sms").click(function() {
			FormSMS.submit();
		});

		$(".bt-confirmacao-sms").click(function() {
			FormSMS.confirmar();
		});

		$(".bt-descadastro-sms").click(function() {
			FormSMS.descadastrar();
		});
		//FormSMS.setState(2);
	}
}

this.Utils = {
	
	delegate: function (that, thatMethod)
	{
		return function() { return thatMethod.apply(that,arguments); }
	},
	resetForm: function(inputs)
	{
		for(var i = 0; i < inputs.length; i++)
			$(inputs[i]).val($(inputs[i])[0].title);
	},
	validateForm: function(inputs)
	{
		for(var i = 0; i < inputs.length; i++)
			if($(inputs[i])[0].title == $(inputs[i]).val() || !$(inputs[i]).val())
				return false;

		return true;
	},
	addInputsLabels: function(inputs)
	{
		for(var i = 0; i < inputs.length; i++)
			Utils.addInputLabel($(inputs[i]), $(inputs[i])[0].title);
	},
	addInputLabel: function(o, label)
	{
		o.val(label);
		o.focus(function()
		{
			if ($(this).val() == label)
				$(this).val("");
		});
		o.blur(function()
		{
			if ($(this).val() == "")
				$(this).val(label);
		});
	},
	createCookie: function (name, value, days)
	{
		var expires;
		if (days)
		{
			var date	= new Date();
			date.setTime(date.getTime()+(days * 24 * 60 * 60 * 1000));
			expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	},
	readCookie: function (name)
	{
		var nameEQ	= name + "=";
		var ca		= document.cookie.split(';');
		for(var i=0; i < ca.length; i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	},
	eraseCookie: function (name)
	{
		createCookie(name,"",-1);
	}
}

this.AlertModal = {
	initialized: false,
	show: function(title, msg)
	{
		if(!AlertModal.initialized)
			AlertModal.init();

		$(".modal").show();

		$(".modal-title").html(title);
		$(".modal-msg").html(msg);
	},
	init: function()
	{
		AlertModal.initialized = true;
		$(".modal").click(function() {
			$(".modal").hide();
		});
	}
}

