$(function () {
	/* jCarousel Lite */
	$("#carousel5").jCarouselLite({
		visible: 3,
        btnNext: "#carousel5next",
        btnPrev: "#carousel5prev"
    });
	$("#carousel6").jCarouselLite({
		visible: 3,
        btnNext: "#carousel6next",
        btnPrev: "#carousel6prev"
    });

	/* Tabs */
    var tabContainers  = $('div.tabs > div.tab');
    var tabContainersF = $('div.tabsf > div.tab');
    
    $('div.tabs div.tabset a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        
        $('div.tabs div.tabset a').removeClass('active');
        $(this).addClass('active');
        
        return false;
    }).filter(':first').click();

    $('div.tabsf div.tabset a').click(function () {
        tabContainersF.hide().filter(this.hash).show();
        
        $('div.tabsf div.tabset a').removeClass('active');
        $(this).addClass('active');
        
        return false;
    }).filter(':last').click();	
	
	/* últimos multimedia: galería de fotos */
	$(".ultmulgal").click(function() {
		var image = $(this).attr("rel");
		$('#ultmulfoto').html('<img src="'+ image +'" width="296" alt="" />');
		$('#ultmulfoto').hide();
		$('#ultmulfoto').fadeIn('slow');
		$('.ultmulfototxt').html($(this).find('img').attr("rel"));
		return false;
	});
	
	/* últimos multimedia: galería de vídeos */
	$(".ultmulgalv").click(function() {
		var video = $(this).attr("rel");
		$('#ultmulvideo').html('');
		$('#ultmulvideo').flash({
			swf: 'images/player_flv_maxi.swf',
			width: 296,
			height: 218,
			params: { allowFullScreen: true },
			flashvars: {
				flv: video,
				showstop: 1,
				showvolume: 1,
				showtime: 1,
				showiconplay: 1,
				showfullscreen: 1,
				autoplay: 1
			}
		});
		$('#ultmulvideo').hide();
		$('#ultmulvideo').fadeIn('slow');
		$('.ultmulvideotxt').html($(this).find('img').attr("rel"));
		return false;
	});

	/* últimso multimedia: vídeo 1 de la galería */
	var ultmulvideo1 = $('#ultmulvideo a:first').attr('rel');
	var ultmulvideo1prev = $('#ultmulvideo a:first').attr('rel2');
	$("#ultmulvideo").html('');
	$("#ultmulvideo").flash({
		swf: 'images/player_flv_maxi.swf',
		width: 296,
		height: 218,
		params: { allowFullScreen: true },
		flashvars: {
			flv: ultmulvideo1,
			startimage: ultmulvideo1prev,
			showstop: 1,
			showvolume: 1,
			showtime: 1,
			showiconplay: 1,
			showfullscreen: 1
		}
	});
	
	/* buscador por palabras */
	$('#buscapaltext').click(function() {
		$('#buscapaltext').attr('value', '');		
	});	
});

/* buscador de vehículos */
function getBusMarcas(tipo) {
	// llamada ajax rellenar combo modelos del buscador
	$.ajax({
		type: "GET",
		url:  "ajax.php",
		data: "op=busmar&f=" + tipo,
		success: function(result) {
			$("#busselmarca").html(result);
		}
	});	
	$("#busselmodelo").html("<option value=0>-Seleccionar-</option>");
	$("#busselcarro").html("<option value=0>-Seleccionar-</option>");
}

function getBusModelos(marca) {
	// llamada ajax rellenar combo modelos del buscador
	var tipo = $('#bustipoveh').attr('value');
	$.ajax({
		type: "GET",
		url:  "ajax.php",
		data: "op=busmod&f=" + marca + "&t=" + tipo,
		success: function(result) {
			$("#busselmodelo").html(result);
		}
	});	
	$("#busselcarro").html("<option value=0>-Seleccionar-</option>");
}

function getBusCarrocerias(modelo) {
	// llamada ajax rellenar combo carrocerías del buscador
	var tipo = $('#bustipoveh').attr('value');
	var marca = $('#busselmarca').attr('value');
	$.ajax({
		type: "GET",
		url:  "ajax.php",
		data: "op=buscarro&f2="+modelo+"&f="+marca + "&t=" + tipo,
		success: function(result) {
			$("#busselcarro").html(result);
		}
	});	
}

function getBusPaso3(veh,radio,combus,precio) {
	// llamada ajax paso 3 del buscador
	$.ajax({
		type: "GET",
		url:  "ajax.php",
		data: "op=buspaso3&v="+veh+"&r="+radio+"&c="+combus+"&p="+precio,
		success: function(result) {
			// metemos la tabla de resultados en #dbuscadorp3
			$("#dbuscadorp3").html(result);
			// aplicamos el jquery tablesorter a la tabla
			$("#sortablesearch").tablesorter({
				headers: {
					0: { sorter: false }
				},
				dateFormat: 'dd/mm/yy'
			}); 
		}
	});	
}

function getBusResultado(url) {
	// saltar a url en buscador al seleccionar checkbox de resultado
	document.location.href = url;
}

/* buscador de vehículos de ocasión */
function getBusVOModelos(marca) {
	// llamada ajax rellenar combo modelos del buscador
	$.ajax({
		type: "GET",
		url:  "ajax.php",
		data: "op=busvomod&f=" + marca,
		success: function(result) {
			$("#busvoselmodelo").html(result);
		}
	});	
}

/* formularios */
function clickBorrame(obj, txt) {
	if(obj.value == txt) obj.value = "";
}

function isEmpty(str) { 
	var strRE;
	strRE = new RegExp(); 
	strRE.compile('^[\s ]*$', 'gi'); 
	return strRE.test(str); 
} 

function isValidEmail(str) {
	var apos, dotpos;
	apos = str.indexOf("@");
	dotpos = str.lastIndexOf(".");
	if(apos<1||dotpos-apos<2) {
		return false;
	} else {
		return true;
	}
}

function checkFormComentarios() {
	if(($('#comnombre').attr('value')) == 'Nombre') {
		alert('Por favor, rellene su nombre.');
		return false;
	}
	if(isEmpty($('#comnombre').attr('value'))) {
		alert('Por favor, rellene su nombre.');
		return false;
	}
	if(!isValidEmail($('#comemail').attr('value'))) {
		alert('Por favor, introduzca una direccion de email valida.');
		return false;
	}
	if(isEmpty($('#comtexto').attr('value'))) {
		alert('Por favor, rellene el texto de su comentario.');
		return false;
	}
	
	$('#send-comment-form').submit();
}

function checkFormContactar() {
	if(($('#contnombre').attr('value')) == 'Nombre') {
		alert('Por favor, rellene su nombre.');
		return false;
	}
	if(isEmpty($('#contnombre').attr('value'))) {
		alert('Por favor, rellene su nombre.');
		return false;
	}
	if(($('#contapellidos').attr('value')) == 'Apellidos') {
		alert('Por favor, rellene sus apellidos.');
		return false;
	}
	if(isEmpty($('#contapellidos').attr('value'))) {
		alert('Por favor, rellene sus apellidos.');
		return false;
	}
	if(!isValidEmail($('#contemail').attr('value'))) {
		alert('Por favor, introduzca una direccion de email valida.');
		return false;
	}
	if(isEmpty($('#conttexto').attr('value'))) {
		alert('Por favor, rellene el texto de su consulta.');
		return false;
	}
	
	$('#send-contact-form .enviar').attr('disabled', true);
	$('#send-contact-form').submit();
}

function checkFormContactarVO() {
	if(isEmpty($('#contnombre').attr('value'))) {
		alert('Por favor, rellene su nombre.');
		return false;
	}
	if(isEmpty($('#contapellidos').attr('value'))) {
		alert('Por favor, rellene sus apellidos.');
		return false;
	}
	if(isEmpty($('#conttlf').attr('value'))) {
		alert('Por favor, rellene su numero de telefono.');
		return false;
	}
	if(!isValidEmail($('#contemail').attr('value'))) {
		alert('Por favor, introduzca una direccion de email valida.');
		return false;
	}
	if(isEmpty($('#conttexto').attr('value'))) {
		alert('Por favor, rellene el texto descriptivo.');
		return false;
	}
	
	$('#send-vocontact .enviar').attr('disabled', true);
	$('#send-vocontact').submit();
}
