$(document).ready(function() {
	// Texto del buscador
	$("#textoBusqueda").click(function(){
		if ($(this).val()=="Buscar") $(this).val('');
	}).focusout(function(){
		if ($(this).val()=='') $(this).val("Buscar");
	});
	// Enlace volver
	if ($("#volver")._exists()) { $("#volver a").attr({href: document.referrer, title:"volver a la página anterior"}); }
	// Crear target="_blank"
	$("a[class=popup]").each(function(i){
		tituloEnlace = $(this).attr("title") + " (se abre en nueva ventana) ";
		$(this).attr("title", tituloEnlace);
		if ($(this).html().toLowerCase().indexOf("img") == -1)
			$('<span>&nbsp;</span><img src="images/popup.png" class="vertical-middle" alt="" />').insertAfter($(this));
	})._blank();
	// Zoom imágenes
	if ($("a[rel^='prettyPhoto']")._exists()) {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			counter_separator_label: ' de ',
			theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		});
	}
	// Validación formularios
	if ($("form").hasClass("validar")) {
		$("form.validar").each(function(){
			$("#"+$(this).attr("id")).validate();
		});
		$("form.validar input:text").setMask();
	}
});

// Pestaña accidente
$(window).scroll(function(){
	$('#accidente').animate({top:$(window).scrollTop()-13+"px" },{queue: false, duration: 350});
});

// Google Analitycs
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-410926-6']);
_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);
})();

$.fn._blank = function() {
	function clickHandler(e) {
		// si el usuario ha utilizado una tecla de control no hacemos nada
		if (e.ctrlKey || e.shiftKey || e.metaKey)
			return;
		// abrimos la ventana
		var w = window.open(this.href, "_blank");
		if (w && !w.closed) {
			// si efectivamente hemos logrado abrirla la ponemos en foco
			w.focus();
			// y cancelamos el comportamiento por defecto del enlace
			e.preventDefault();
		}
	}
	this
		.filter("a[@href]") // que no se nos cuele algo que no sea un enlace
		.bind("click", clickHandler);
	return this; // permitimos concatenabilidad
}

$.fn._exists = function(){ return jQuery(this).length>0; }

function isNumeric(value)  { 
	if (value.match(/^\d+$/) == null) 
		return false; 
	else 
		return true; 
}

function actualizaFecha(fecha, dias) { // fecha dd/mm/yyyy
	dias--;
	milisegundos = parseInt(dias*24*60*60*1000);
	fecha = new Date(fecha.substring(6), parseInt(fecha.substring(3,5) - 1), fecha.substring(0,2), 0, 0, 0);
	tiempo = fecha.getTime();
	total = fecha.setTime(parseInt(tiempo+milisegundos));
	dia = fecha.getDate();
	if (dia < 10) dia = "0"+dia;
	mes = fecha.getMonth(); mes++;
	if (mes < 10) mes = "0"+mes;
	anio = fecha.getFullYear();
	return dia+"/"+mes+"/"+anio;
}

// Fedérate
function compruebaTarjeta(obj, dias) {
	if (parseFloat(dias) == 0) {
		$('#dtFechaViaje, #ddFechaViaje').hide();
		$('#fechaViaje').removeClass('required date');
	} else {
		$('#dtFechaViaje, #ddFechaViaje').show();
		$('#fechaViaje').addClass('required date');
	}
}
