// JavaScript
/*******************************************/

function menuFeedBack(obj, move) {
	$(obj).css("background-position",move);
}

/** ********* CAMPOS FORMULÁRIO ********** */

function checarCampo(campo, valor) {
	if (campo.value == valor) {
		campo.value = '';
	}
}

function checarCampoVazio(campo, valor) {
	if (campo.value == '') {
		campo.value = valor;
	}
}

/** ************************************** */

var time = 2;
var timeFade = .5;
var btIniciado = 0;

jQuery(document).ready(
	function(){
		
		/******** Máscaras  *********/
		$('input[type=text]').focus(
			function()
			{
				if($(this).val() == $(this).attr('rel'))
				{
					$(this).val('');
					
					switch($(this).attr('name'))
					{
						case "data":
							$(this).mask("99/99/9999");
							break;
						case "cep":
							$(this).mask("99.999-999");
							break;
						case "telefone":
							$(this).mask("(99) 9999-9999");
							break;
						case "fax":
							$(this).mask("(99) 9999-9999");
							break;
					}
				}
			}
		).blur(
			function()
			{				
				if(!$(this).hasClass('mask'))
				{
					if(!$(this).val())
					{
						$(this).val($(this).attr('rel'));
					}
				}
			}
		)
		
		/*****************************/
		$('.checkbox').click(
			function()
			{
				elementAtivo = $(".checkbox.ativo[rel='"+$(this).attr('rel')+"']");
				
				if(elementAtivo.size())
				{
					elementAtivo.removeClass('ativo');
					$('#'+$(this).attr('rel')).val('');
					
					if(elementAtivo.attr('alt') != $(this).attr('alt'))
					{
						$(this).addClass('ativo');
						$('#'+$(this).attr('rel')).val($(this).attr('alt'));
					}
				}
				else
				{
					$(this).addClass('ativo');
					$('#'+$(this).attr('rel')).val($(this).attr('alt'));
				}
				
				return false;
			}
		);
		/******* Limpar campos ********/
		$('.btHandler').focus(
				function()
				{
					if($(this).val() == $(this).attr('rel'))
					{
						$(this).val("");
					}
				}
			).blur(
				function()
				{
					if($(this).val() == "")
					{
						$(this).val($(this).attr('rel'));
					}
				}
			)
			
		/****** Feedback bts de formulario *******/
		$('.bts').mouseenter(
				function()
				{
					$(this).css("background","#004F99")
					$(this).css("color","#FFF")
					$(this).css("border","1px solid #004F99")
				}
			).mouseleave(
				function()
				{
					$(this).css("border","1px solid #656565")
					
					if ($(this).parent().attr('id') == ('enviar') || $(this).parent().attr('id') == ('btEnviar'))
					{
						$(this).css("background","#656565")
					} 
					else
					{
						$(this).css("background","#EDEDED")	
						$(this).css("color","#000000")
					}
				}
			)		

		/////////////////////////////////////////////////
		/////////////////////////////////////////////////# HEADER e MATERIAIS_EXIBIR
		
		$(".barBotoes li").mouseover(
			function()
			{
				if($("form",$(this)).size())
				{
					$("form",$(this)).fadeIn();
					$("> a",$(this)).click(
						function()
						{
							return false;
						}
					)
					$(".mensagem a.btTransition",$(this)).click(
						function()
						{
							$(this).parent().fadeOut(500, 
								function()
								{
									$(".inputs",$(this).parent()).fadeIn();
								}
							);
							return false;
						}
					)
				}
			}
		).mouseleave(
			function()
			{
				if($("form",$(this)).size())
				{
					$("form",$(this)).fadeOut();
				}
			}
		)
		
		/////////////////////////////////////////////////
		
		/*** banners ambiente ***
		$('#btIniciarSlide').click(
			function()
			{
				if ( btIniciado == 0)
				{
					btIniciado = 1;
				}
				else
				{
					btIniciado = 0;	
				}
				$('#fotos').oneTime(1000*time, function()
				{
					mudarBannerAmbiente();
						
				})
				return false;
			}
				
		)
		/**************************/
	}
)



/*
function mudarBannerAmbiente()
{
	var bannerAtivo = $("#fotos img.ativo");
	var idBannerAtivo = $('#fotos img').index(bannerAtivo);
	
	if(idBannerAtivo < $('#fotos img').size()-1)
	{
		var nextId = idBannerAtivo+1;
	}
	else
	{
		var nextId = 0;
	}

	mudarImgAmbiente(nextId);
	
}

function mudarImgAmbiente(nextId)
{
	var bannerAtivo = $("#fotos img.ativo");
	var idBannerAtivo = $('#fotos img').index(bannerAtivo);
	
	$('#fotos').stopTime();
	
	var nextBanner = $('#fotos img:eq('+nextId+')');

	if ( btIniciado == 1)
	{
		bannerAtivo.delay(100).fadeOut(1000*timeFade,
			function()
			{
				$(this).removeClass('ativo');
				
				$('#fotos').oneTime(1000*time, function() {
					mudarBannerAmbiente();
				});
			}
		);
		nextBanner.fadeIn(1000*timeFade,
			function()
			{
				$(this).addClass('ativo');
			}
		)
	}
	else 
	{
		bannerAtivo.stop(true);
		nextBanner.stop(true);	
	}
	
}

*/

function sendAmostra(id_cadastro,produto)
{
	if(stopSend == false)
	{
		stopSend = true;
		$('#liAmostra .loader').fadeIn();

		$.ajax({
			url: "amostra_ajax.php",
			type: "POST",
			dataType: "json",
			data: "produto="+produto+"&id_cadastro="+id_cadastro,
			error: function(data) {
				alert(data.status+" |1| "+data.msg);			
				//$("h2").text(data.msg);
			},
			success: function(data) {
				switch(data.status)
				{
					case 'true':						
						$('#liAmostra .loader').fadeOut();
						break;
					case 'false':
						alert(data.status+" |2| "+data.msg);
						break;
						
				}
				$('#'+idForm+' .msg').text(data.msg);
			},
			complete: function(data) {
				stopSend = false;
				//alert(data.status+" |3| "+data.msg);
			}
		});
	}
}

function sendLogin(idForm,address)
{
	if(stopSend == false)
	{
		var stopSubmit = validarCampos(idForm);
		
		$('#'+idForm+' input:submit').blur();
		
		if(!stopSubmit)
		{
			stopSend = true;
			$('#'+idForm+' .msg').fadeIn();
			$('#'+idForm+' .msg').text('enviando...');
			
			$.ajax({
				url: address,
				type: "POST",
				dataType: "json",
				data: $("#"+idForm).serialize(),
				error: function(data) {
					$('#'+idForm+' .msg').text('erro interno no servidor (1000)!')					
				},
				success: function(data) {
					switch(data.status)
					{
						case 'true':						
							window.location.reload(true);
							break;
						case 'false':
							$('#'+idForm+' .msg').css("color","#E53000");							
							break;
					}
					$('#'+idForm+' .msg').text(data.msg);
				},
				complete: function(data) {
					stopSend = false;
					$('#'+idForm+' .msg').animate({'left':'+=0px'},500,
						function()
						{
							$(this).fadeOut();		
						}
					)
				}
			});
		}
		else
		{
			$('#'+idForm+' .msg').text('*preencha os campos obrigatórios!').css("color","#E53000");
		}
	}
}


////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////CONTATO//////////////////////////////////////

var stopSend = false;

function sendForm(idForm,address)
{
	if(stopSend == false)
	{
		var stopSubmit = validarCampos(idForm);
		
		$('#'+idForm+' input:submit').blur();
		
		if(!stopSubmit)
		{
			stopSend = true;
			$('#'+idForm+' .msg').text('enviando...');
			$('#'+idForm+' .loader').fadeIn();
			
			$.ajax({
				url: address,
				type: "POST",
				dataType: "json",
				data: $("#"+idForm).serialize(),
				error: function(data) {
					$('#'+idForm+' .msg').text('erro interno no servidor (1000)!');
					alert(data.toSource());
				},
				success: function(data) {
					switch(data.status)
					{
						case 'true':
							$('#'+idForm+' .msg').css("color","#656565");
							
							$('#'+idForm+' .clearValue').val('');
							$('#'+idForm+' .dropdown_toggle span').html('');
							$('#'+idForm+' .clearSelect').removeAttr("selected");
							$('#'+idForm+' .resetValue').each( 
								function()
								{
									$(this).val($(this).attr('rel'));
								}
							);							
//							$('#'+idForm+' .resetSelect').each(
//								function()
//								{
//									$('.dropdown .dropdown_toggle span',$(this).parent()).html($('select option:eq(0)',$(this).parent().parent()).text())
//								}
//							);
							$('#'+idForm+' .resetSelect').each(
								function()
								{
									$('.dropdown .dropdown_toggle span',$(this).parent()).html($('select option:eq(0)',$(this).parent()).text())
								}
							);
							

							break;
						case 'false':
							$('#'+idForm+' .msg').css("color","#E53000");							
							break;
					}
					$('#'+idForm+' .msg').text(data.msg);
					
				},
				complete: function(data) {
					stopSend = false;
					$('#'+idForm+' .loader').fadeOut();
					
					if(idForm == "formNews")
					{
						$('#'+idForm+' .msg').fadeIn(500,
							function()
							{
								$(this).animate({"left" : 0},1500,
									function()
									{
										$(this).fadeOut();
									}
								)
							}
						);	
					}
				}
			});
		}
		else
		{
			$('#'+idForm+' .msg').text('*preencha os campos obrigatórios!').css("color","#E53000");
			
			if(idForm == "formNews")
			{
				$('#'+idForm+' .msg').fadeIn(500,
					function()
					{
						$(this).animate({"left" : 0},1500,
							function()
							{
								$(this).fadeOut();
							}
						)
					}
				);	
			}
			
		}
	}
}

function sendSalesforce(idForm,address)
{
	if(stopSend == false)
	{
		var msg;
		var stopSubmit = validarCampos(idForm);
		
		$('#'+idForm+' input:submit').blur();
		
		if(!stopSubmit)
		{
			
			stopSend = true;
			$('#'+idForm+' .msg').text('enviando...');
			$('#'+idForm+' .loader').fadeIn();
			
			$.ajax({
				url: address,
				type: "POST",
				dataType: "text",
				data: $("#"+idForm).serialize(),
				error: function(data) {
					$('#'+idForm+' .msg').text('erro interno no servidor (1000)!');
				},
				success: function(data) {
					if(!data)
					{
						msg = "mensagem enviada";
						
						$('#'+idForm+' .msg').css("color","#656565");
						
						$('#'+idForm+' .clearValue').val('');
						$('#'+idForm+' .dropdown_toggle span').html('');
						$('#'+idForm+' .clearSelect').removeAttr("selected");
						$('#'+idForm+' .resetValue').each( 
							function()
							{
								$(this).val($(this).attr('rel'));
							}
						);
						
						$('#'+idForm+' .resetSelect').each(
							function()
							{
								$('.dropdown .dropdown_toggle span',$(this).parent()).html($('select option:eq(0)',$(this).parent()).text())
							}
						);
					}
					else
					{
						msg = "falha ao enviar!";
						$('#'+idForm+' .msg').css("color","#E53000");							
					}
					
					$('#'+idForm+' .msg').text(msg);
					
				},
				complete: function(data) {
					stopSend = false;
					$('#'+idForm+' .loader').fadeOut();
					
					if(idForm == "formNews")
					{
						$('#'+idForm+' .msg').fadeIn(500,
							function()
							{
								$(this).animate({"left" : 0},1500,
									function()
									{
										$(this).fadeOut();
									}
								)
							}
						);	
					}
				}
			});
		}
		else
		{
			$('#'+idForm+' .msg').text('*preencha os campos obrigatórios!').css("color","#E53000");
			
			if(idForm == "formNews")
			{
				$('#'+idForm+' .msg').fadeIn(500,
					function()
					{
						$(this).animate({"left" : 0},1500,
							function()
							{
								$(this).fadeOut();
							}
						)
					}
				);	
			}
			
		}
	}
}


function validarCampos(formId)
{
	var stopSubmit;
	
	if($("#"+formId+" .campoObrigatorio").size())
	{
		for(i=0;i<$("#"+formId+" .campoObrigatorio").size();i++)
		{
			var objectInput = $("#"+formId+" .campoObrigatorio:eq("+i+")");
			
			switch($("#"+formId+" .campoObrigatorio").get(i).tagName)
			{
				case "INPUT":
					switch(objectInput.attr('type'))
					{
						case "text":
						case "password":
							if(!objectInput.val() || objectInput.attr('rel') &&  objectInput.attr('rel') == objectInput.val())
							{
								objectInput.addClass("focusCampo");
								objectInput.blur(function(){
									if($(this).val() || $(this).attr('rel') && $(this).val() && $(this).attr('rel') != $(this).val())
									{
										$(this).removeClass("focusCampo");
										//$(this).unbind();
									}
								});
								
								stopSubmit = true;	
							}
							break;
					
						case "hidden":
							var objectRel = $("#"+objectInput.attr('rel'));
	
							if(!objectInput.val())
							{
								objectRel.addClass("focusCampo");
								objectRel.blur(function(){
									$(this).removeClass("focusCampo");
									//$(this).unbind();
								});
								
								stopSubmit = true;	
							}
							break;
					}
					break;
				case "SELECT":
					if(!objectInput.val())
					{
						objectInput.addClass("focusCampoSelect");
						objectInput.change(function(){
							$(this).removeClass("focusCampoSelect");
							$(this).unbind();
						});
						
						stopSubmit = true;	
					}
					break;
			}
		}
		
		if(stopSubmit == true)
		{
			return true;
		}
	}	
}

////////////////////////////////////////////////////////////////////////////////////
