	
	// ANALYTICS

	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-25476862-1']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();

	
	// FIN ANALYTICS
	
	var defaultSelectVal = "";
	var defaultSelectText = "";
	
	$(document).ready(function(){
		
		$(document).scrollTop(0);
		
		$('#home').click(function() {
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 500 );
		   removeActive();
		   $(this).children('a').addClass('activo');
		   return false;
		});
		
		$('#btn-descubre').click(function() {
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 760}, 500 );
		   removeActive();
		   $('#c30').children('a').addClass('activo');
		   return false;
		});
		
		$('#c30').click(function() {
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 760}, 500 );
		   removeActive();
		   $(this).children('a').addClass('activo');
		   return false;
		});
		
		$('#images').click(function() {
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 1300 + 180}, 500 );
		   removeActive();
		   $(this).children('a').addClass('activo');
		   return false;
		});
		
		$('#try').click(function() {
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 1990 + 400}, 500 );
		   removeActive();
		   $(this).children('a').addClass('activo');
		   return false;
		});
		
		$('#location').click(function() {
		   removeActive();
		   $(this).children('a').addClass('activo');
		   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 2750 + 400}, 500 );
		   return false;
		});
		
		
		function removeActive(){
			$('#home a').removeClass('activo');
			$('#c30 a').removeClass('activo');
			$('#images a').removeClass('activo');
			$('#try a').removeClass('activo');
			$('#location a').removeClass('activo');
		}
		
		
		/* Btn - Ficha técnica */
		
			$("a#c30-vs").fancybox({
				'hideOnContentClick': true
			});

		/* Section car - btn lupita */
		
			$("a.lupita-01").fancybox({
				'hideOnContentClick': true
			});
		
			$("a.lupita-02").fancybox({
				'hideOnContentClick': true
			});
			
			$("a.lupita-03").fancybox({
				'hideOnContentClick': true
			});
		
		/* Section images - btn zoom */
		
			$("a.zoom-01").fancybox({
				'hideOnContentClick': true
			});
		
			$("a.zoom-02").fancybox({
				'hideOnContentClick': true
			});
			
			$("a.zoom-03").fancybox({
				'hideOnContentClick': true
			});
			
			$("a.zoom-04").fancybox({
				'hideOnContentClick': true
			});
			
			$("a.zoom-05").fancybox({
				'hideOnContentClick': true
			});

		// Envio de formulario
		
		$('#formulario').submit(function(){
			
			if ($('#nombre').val() == ''){
				alert("Ingrese su nombre");
				$('#nombre').focus();
				return false;
			}		
			
			if ($('#mail').val() == ''){
				alert("Ingrese su Email");
				$('#mail').focus();
				return false;
			}
			
			if (!validarEmail($("#mail").val())) {
				alert("El E-mail ingresado no es válido");
				return false;
			}
			
			if ($('#telefono').val() == ''){
				alert("Ingrese su Telefono");
				$('#telefono').focus();
				return false;
			}
			
			if ($('#auto').val() == ''){
				alert("Ingrese que automóvil manejas actualmente");
				$('#auto').focus();
				return false;
			}
			
			if ($('#suc').val() == ''){
				alert("Seleccione la sucursal más cercana");
				return false;
			}
			
			var serialize = $('#formulario').serialize();
			
			$.ajax({
				type   : "POST",
				url    : "mail.php",
				data   : serialize,
				success: function( datos ){
					jAlert('Tu mensaje ha sido enviado, te responderemos a la brevedad. Gracias.', 'Solicitud Test Drive');
					$('#nombre').val("");
					$('#rut').val("");
					$('#telefono').val("");
					$('#mail').val("");
					$('#auto').val("");
					$('#current').html(defaultSelectText);
					$('#suc').val(defaultSelectVal);
					return false;
				}
			});
			
			return false;
			
		});
		
		
		// SELECT 
		
		$("#sucursal").css("display","none");
		$('#options').css("display","none");
		$("#replace").css("display","block");
		
		$('#sucursal').find('option').each(function(x,f){
			var option = document.createElement('li');
			
			$(option).html($(f).text())
					 .attr('rel',$(f).val());
			
			$(option).click(function(){
				var val = $(this).attr('rel');
				var text = $(this).html();
				$('#options').find('li').removeClass('active');
				$(this).addClass('active');
				$('#current').html(text);
				$("#sucursal").val(val);
				$("#suc").val(val);
				$('#options').css("display","none");
			})
			
			$('#options').append(option);
			
			if ($(f).attr('selected') != undefined) {
				$('#current').html($(f).text());
				$(option).addClass('active');
				$("#suc").val($(f).val());
				defaultSelectVal = $(f).val();
				defaultSelectText = $(f).text();
			}
			
		});
		
		$("#current").click(function(){
			$('#options').fadeIn();
		});
		
		$("#options").mouseleave(function(){
			$(this).css("display","none");
		})
		
	});
	
	function validarEmail(valor) {
		if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(valor)){
			return (false)
		} else {
			return (true);
		}
	}

