/* Author: mespi@espiratecnologias.com
   Librería de funciones básicas
*/

$(document).ready(function() {
	$('a.delete').click(function() {
		if (jQuery(this).attr('title').indexOf("el ") > -1) return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionado?');
		else return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionada?');
	}); 
		
	if ($.fn.fancybox) {
		$('a.fancybox').fancybox({ 'overlayShow': true, 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
		$('a.thumb').fancybox();
	}
		
	$('a.external').click(function() {
		this.target = "_blank";
	});
	
	$('a.close-notification').click(function(e) {
		$(this).parent().fadeOut('slow');
		e.preventDefault();
	});
		
	if ($('div.success').length) {
		setTimeout(function() {
			$('div.success').fadeOut('slow');	
		}, 3000);
	}
	
	if ($('p.call-to-action a').length) {
		$('p.call-to-action a').click(function(e) {
			$('div.form-lo-quiero').fadeIn('slow');
			e.preventDefault();
		});
		
		$('div.form-lo-quiero p.close').live('click',function(){
			$('div.form-lo-quiero').fadeOut('slow');
		});
		$('form#form-vehiculo-ocasion input#form-lo-quiero-submit').click(function(e) {

			var nombre = $('input#str_nombre').val();			
			var email = $('input#str_email').val();			
			var telefono = $('input#str_phone').val();			
			var vehiculo = $('input#str_vehiculo').val();			
			$.post('/includes/ajax.php', { source:'call-to-action', nombre:nombre, email:email, telefono:telefono, vehiculo:vehiculo },
				function(data) {
					$('div.form-lo-quiero').fadeOut('fast', function() {
						$('div.form-lo-quiero').html(data);
/* 						$('form#form-vehiculo-ocasion').hide(); */
						$('div.form-lo-quiero').fadeIn('fast');
					});
	      		}
	      	);
	      	
	      	e.preventDefault();	
	    });	
		$('form#form-vehiculo-ocasion input#form-lo-quiero-submit').live('click', function(e) {

			var nombre = $('input#str_nombre').val();			
			var email = $('input#str_email').val();			
			var telefono = $('input#str_phone').val();			
			var vehiculo = $('input#str_vehiculo').val();			
			$.post('/includes/ajax.php', { source:'call-to-action', nombre:nombre, email:email, telefono:telefono, vehiculo:vehiculo },
				function(data) {
					$('div.form-lo-quiero').fadeOut('fast', function() {
						$('div.form-lo-quiero').html(data);
/* 						$('form#form-vehiculo-ocasion').hide(); */
						$('div.form-lo-quiero').fadeIn('fast');
					});
	      		}
	      	);
	      	
	      	e.preventDefault();	
	    });	    
	}

	$('#main-nav a.toggle').click(function(e) {
		$('#main-nav a.toggle').each(function(index) {
			$(this).next().slideUp();		
		});
		if ($(this).next().css('display') == 'none') $(this).next().slideDown();
		else $(this).next().slideUp();
		e.preventDefault();
	});
	
	if ($('html').hasClass('ie6') || $('html').hasClass('ie7') || $('html').hasClass('ie8')) {
		$('.promos section#main div.wrapper article:nth-child(4n + 1)').css('margin-right','0 !important');
		$('.promos section#main div.wrapper article:nth-child(5n + 1').css('clear','both !important');
	}
	
	if ($('li.tarjeta').length) {
		$('li.tarjeta a').click(function(e) {
			if ($('div.customer-form').is(":visible")) $('div.customer-form').fadeOut('normal');
			else $('div.customer-form').fadeIn('normal');
			e.preventDefault();
		});
	}

	if ($('figure#servicios-gallery').length) {
		$('figure#servicios-gallery a.thumbnail').click(function(e) {
			$('figure#servicios-gallery a').each(function(index) {
				$(this).attr('rel','gallery');
			});
			$(this).attr('rel','gallery_aux');
			$.fancybox.cancel();
			var estaImagen = $(this).attr('href');
			$('figure#servicios-gallery a').removeClass('active');
			$(this).addClass('active');
			$('#large-image img').fadeOut(
				'fast',
				function() {
					$('#large-image').attr('href', estaImagen);		
					$('#large-image img').attr('src', estaImagen);						
					$('#large-image img').fadeIn();
				}
			)
			e.preventDefault();
		});
	}
	
	$('p.close').click(function() {
		$('div.form-lo-quiero').hide();
	});

	if ($('#phones ul').length > 0) {
		path = new Array("0", "1", "2", "3", "4", "3", "2", "1");
		var i = 1;
		$(document).everyTime('5s',function() {
			incremental = path[i];
			$("#phones ul").animate({
				left: -(incremental*200)
			}, 1500, "swing");
			i++;
			if (i == path.length) {
				i = 0;
			}		
	
		});
	}
	
	if ($('html').hasClass('oldie')) {
		$('.promos section#main div.wrapper article:nth-child(4n + 1)').css('margin-right', '0');
		$('.promos section#main div.wrapper article:nth-child(5n + 1)').css('clear', 'both');
		$('menu.text-menu li:first').css('margin', '0');
		$('menu.text-menu li:first').css('zIndex', '3');
	}
});





