/* -------------- Filtro Setup ------------ */

var fAkt_1 = fAkt_2 = fAkt_3 = fAkt_4 = fAkt_5 = fAkt_6 = fAkt_7 = 
fAkt_8 = fAkt_9 = fAkt_10 = fAkt_11 = fAkt_13 =  0;


$(document).ready(
	function() { 	
		
		CapasFiltros ();
		var ordAkt_1 = ordAkt_2 = ordAkt_3 = ordAkt_4 = 0;
		var ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;
		ColocaOrdenacion();	
		
		//document.getElementById('orden_pulsado').value='4';	
		//document.getElementById('orden_precio').value='menor';
		ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;
		removeClasses();	
		$("#ordFlecha_4").addClass("ordDown");	
		ordAkt_4 = 1;
		ordUpDown_4 = 1;		

		
	}
);


function changeFiltro(thisId,aktId,fContId){
	if(aktId == 0){
		//alert ('abrir');
		$(thisId).addClass("current");
		$("#filtroCont_"+fContId).slideDown("slow"); //antes estaba con fast
		aktId = 1;
		
	}else if(aktId == 1){
		//alert ('cerrar');
		$(thisId).removeClass("current");
		$("#filtroCont_"+fContId).slideUp("slow"); //antes estaba con fast
		aktId = 0;
		//al cerrar el filtro compruebo si está en campo num_filtro y lo quito de él
		//alert('cerrar '+fContId);
		DelNum_Filtro(fContId);
	}		
	return aktId;
}

/* ----------------- Resultados Contenedor Setup ---------------*/
$(function() {  
    $("ul.resTabs").tabs("div.resCont > div.resItem").history(); 
});



/* ----------------- Ordenar Setup ---------------*/
var ordAkt_1 = ordAkt_2 = ordAkt_3 = ordAkt_4 = 0;
var ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;


function CapasFiltros () {
	$("#filtro_1").click(function(){ fAkt_1=changeFiltro($(this),fAkt_1,1); return false;})
	$("#filtro_2").click(function(){ fAkt_2=changeFiltro($(this),fAkt_2,2); return false;})
	$("#filtro_3").click(function(){ fAkt_3=changeFiltro($(this),fAkt_3,3); return false;})
	$("#filtro_4").click(function(){ fAkt_4=changeFiltro($(this),fAkt_4,4); return false;})
	$("#filtro_5").click(function(){ fAkt_5=changeFiltro($(this),fAkt_5,5); return false;})
	$("#filtro_6").click(function(){ fAkt_6=changeFiltro($(this),fAkt_6,6); return false;})
	$("#filtro_7").click(function(){ fAkt_7=changeFiltro($(this),fAkt_7,7); return false;})
	$("#filtro_8").click(function(){ fAkt_8=changeFiltro($(this),fAkt_8,8); return false;})
	$("#filtro_9").click(function(){ fAkt_9=changeFiltro($(this),fAkt_9,9); return false;})
	$("#filtro_10").click(function(){ fAkt_10=changeFiltro($(this),fAkt_10,10); return false;})
	$("#filtro_11").click(function(){ fAkt_11=changeFiltro($(this),fAkt_11,11); return false;})
	//$("#filtro_12").click(function(){ fAkt_12=changeFiltro($(this),fAkt_12,12); return false;})
	$("#filtro_13").click(function(){ fAkt_13=changeFiltro($(this),fAkt_13,13); return false;})
	//$("#filtro_14").click(function(){ fAkt_14=changeFiltro($(this),fAkt_14,14); return false;})
}
function FlechasOrdenHover () {
			$("#ordFlecha_1").hover(				
			   function (){
				 if(ordAkt_1 == 0){
				   $(this).addClass("ordDown");
				   $(this).removeClass("ordNormal");
				 }
				 ColocaOrdenacion();	
			   },
			   function (){
				 if(ordAkt_1 == 0){
				   $(this).addClass("ordNormal");
				   $(this).removeClass("ordDown");
				 }
				 ColocaOrdenacion();	
			   }	
			);
			
			$("#ordFlecha_2").hover(				
			   function (){
				 if(ordAkt_2 == 0){
				   $(this).addClass("ordDown");
				   $(this).removeClass("ordNormal");
				 }
			   },
			   function (){
				 if(ordAkt_2 == 0){
				   $(this).addClass("ordNormal");
				   $(this).removeClass("ordDown");
				 }
			   }		
			);
			
			$("#ordFlecha_3").hover(				
			   function (){
				 if(ordAkt_3 == 0){
				   $(this).addClass("ordDown");
				   $(this).removeClass("ordNormal");
				 }
			   },
			   function (){
				 if(ordAkt_3 == 0){
				   $(this).addClass("ordNormal");
				   $(this).removeClass("ordDown");			   
				 }
			   }		
			);
			
			$("#ordFlecha_4").hover(				
			   function (){
				  if(ordAkt_4 == 0){
				    $(this).addClass("ordDown");
				    $(this).removeClass("ordNormal");
				  }
			   },
			   function (){
				 if(ordAkt_4 == 0){
				   $(this).addClass("ordNormal");
				   $(this).removeClass("ordDown");			   
				 }
			   }		
			);
	
			
	
}

function FlechasOrdenClick () {
	$("#ordFlecha_1").click(
			function (){		
			  document.getElementById('orden_pulsado').value='1';	
			  ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;			  
			  if(ordUpDown_1 == 0){	
				   removeClasses();			
				   $(this).addClass("ordDown");	
				   ordAkt_1 = 1;
				   ordUpDown_1 = 1;
				   //marco los campos
				   //alert ('abajo');
				   document.getElementById('orden_cv').value='menor';				  
				   
			  }else if(ordUpDown_1 == 1){
				removeClasses();							
				$(this).addClass("ordUp");
				ordAkt_1 = 1; 
				ordUpDown_1 = 0;
				//alert ('arriba);
				document.getElementById('orden_cv').value='mayor';
			  }
			  xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
			  return false;
			}
		);
		
		$("#ordFlecha_2").click(
			function (){		
			  document.getElementById('orden_pulsado').value='2';		
			  ordUpDown_1 = ordUpDown_3 = ordUpDown_4 = 0;	
			  if(ordUpDown_2 == 0){	
				   removeClasses();			
				   $(this).addClass("ordDown");	
				   ordAkt_2 = 1;
				   ordUpDown_2 = 1;
				   document.getElementById('orden_km').value='menor';
			  }else if(ordUpDown_2 == 1){
				removeClasses();							
				$(this).addClass("ordUp");
				ordAkt_2 = 1; 
				ordUpDown_2 = 0;
				document.getElementById('orden_km').value='mayor';
			  }
			  xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
			  return false;
			}
		);
		
		$("#ordFlecha_3").click(
			function (){
			  document.getElementById('orden_pulsado').value='3';		
			  ordUpDown_1 = ordUpDown_2 = ordUpDown_4 = 0;	
			  if(ordUpDown_3 == 0){	
				   removeClasses();			
				   $(this).addClass("ordDown");	
				   ordAkt_3 = 1;
				   ordUpDown_3 = 1;
				   document.getElementById('orden_fecha').value='menor';
			  }else if(ordUpDown_3 == 1){
				removeClasses();							
				$(this).addClass("ordUp");
				ordAkt_3 = 1; 
				ordUpDown_3 = 0;
				document.getElementById('orden_fecha').value='mayor';
			  }
			  xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
			  return false;
			}
		);
		
		$("#ordFlecha_4").click(
			function (){
			  document.getElementById('orden_pulsado').value='4';	
			  ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = 0;	
			  if(ordUpDown_4 == 0){	
				   removeClasses();			
				   $(this).addClass("ordDown");	
				   ordAkt_4 = 1;
				   ordUpDown_4 = 1;
				   document.getElementById('orden_precio').value='menor';
			  }else if(ordUpDown_4 == 1){
				removeClasses();							
				$(this).addClass("ordUp");
				ordAkt_4 = 1; 
				ordUpDown_4 = 0;
				document.getElementById('orden_precio').value='mayor';
			  }
			  xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
			  return false;
			}
		);
	
}

function removeClasses(){
	
	ordAkt_1 = ordAkt_2 = ordAkt_3 = ordAkt_4 = 0;
		
	for (var i=1; i<=4; i++) {
						
		if($("#ordFlecha_"+i).hasClass("ordUp")){
		   $("#ordFlecha_"+i).removeClass("ordUp");
		}
		
		if($("#ordFlecha_"+i).hasClass("ordDown")){
		   $("#ordFlecha_"+i).removeClass("ordDown");
		}
		
		$("#ordFlecha_"+i).addClass("ordNormal");
	}

}

$(document).ready(function(){
		FlechasOrdenHover ();
		FlechasOrdenClick ();
});

function goToFicha(id)
{
	document.location.href = id;
	return false;
}

function hideOthers(){
	for(var i = 1; i <= 6; i++){
		$("#ofAmpl_"+i).hide();
	}
}

function hideOthers_2(){
	for(var i = 1; i <= 3; i++){
		$("#ofCochesAmpl_"+i).hide();
	}
}


function CapasOfertasDestacadas () {
	
	
	$("#ofDest_1").mouseenter(function(){
		hideOthers();
		$("#ofAmpl_1").show();	
	})
	
	$("#ofDest_2").mouseenter(function(){
		hideOthers();
		$("#ofAmpl_2").show();	
	})

	
	$("#ofDest_3").mouseenter(function(){
		hideOthers();
		$("#ofAmpl_3").show();	
	})

	
	$("#ofDest_4").mouseenter(function(){
		hideOthers();
		$("#ofAmpl_4").show();	
	})

	$("#ofDest_5").mouseenter(function(){
		hideOthers();
		$("#ofAmpl_5").show();	
	})

	
	$("#ofDest_6").mouseenter(function(){
		hideOthers();							   
		$("#ofAmpl_6").show();	
	})

	$("#ofAmplBigCont").mouseleave(function(){
		hideOthers();							   				
	})
	

	$("#ofAmpl_1,#ofAmpl_2,#ofAmpl_3,#ofAmpl_4,#ofAmpl_5,#ofAmpl_6,#ofCochesAmpl_1,#ofCochesAmpl_2,#ofCochesAmpl_3").mouseleave(function(){			
		$(this).hide();		
	})
	
	
	$("#ampInfoBtn_1").mouseover(
		function (){
		  hideOthers_2();
		  $("#ofCochesAmpl_1").show();	
		  return false;
		}
	);
	
	$("#ampInfoBtn_2").mouseover(
		function (){
		  hideOthers_2();	
		  $("#ofCochesAmpl_2").show();	
		  return false;
		}
	);
	
	$("#ampInfoBtn_3").mouseover(
		function (){
		  hideOthers_2();
		  $("#ofCochesAmpl_3").show();	
		  return false;
		}
	);
	
	$("#cochesDestBigCont").mouseleave(
		function (){
		  hideOthers_2();			  
		  return false;
		}
	);
				
	
}

/*$(document).ready(
	function() { 		
		CapasOfertasDestacadas ();		
	}
);


$(document).ready(
	function() { $('#ofAmpl').bgiframe({ height: 1000, width: 900  }); }
);
*/

/* -------------- Funcion Datos iniciales PARKING ------------ */
function getValoresInicialesParking () {	
	$("#ErrorComparativa").hide();
	
	//cuando vuelves de un filtro o de cambio de orden -> marcar la ordenación
	ordAkt_1 = ordAkt_2 = ordAkt_3 = ordAkt_4 = 0;
	ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;
	
	ColocaOrdenacion();

	FlechasOrdenHover ();
	FlechasOrdenClick ();
	
	
} //fin función inicializa

/* -------------- Funcion Datos iniciales ------------ */
function getValoresIniciales (num_filtro) {	
	//$("#ErrorComparativa").hide();
	//if (num_filtro>0) { $("#filtroCont_" + num_filtro).slideDown("fast"); }
	
	//alert(num_filtro);
	
	AbreNum_Filtros(num_filtro);
	CapasFiltros ();

	//cuando vuelves de un filtro o de cambio de orden -> marcar la ordenación
	ordAkt_1 = ordAkt_2 = ordAkt_3 = ordAkt_4 = 0;
	ordUpDown_1 = ordUpDown_2 = ordUpDown_3 = ordUpDown_4 = 0;
	//alert ($("#orden_pulsado").val());
	ColocaOrdenacion();

	FlechasOrdenHover ();
	FlechasOrdenClick ();
	//CapasOfertasDestacadas ();	

	//function() { $('#ofAmpl').bgiframe({ height: 1000, width: 900  }); }
	
} //fin función inicializa


/* -------------- Funcion que asigna valor a un campo hidden y llama al xajax ------------ */
function ColocaOrdenacion() {
	if ($("#orden_pulsado").val()==1) {
		ordAkt_1 =1;
		removeClasses();	
		if ($("#orden_cv").val()=="menor") {	
			$("#ordFlecha_1").addClass("ordDown");	
			ordUpDown_1 = 1;
		} else {
			$("#ordFlecha_1").addClass("ordUp");
		}
	} else if ($("#orden_pulsado").val()==2) {
		ordAkt_2 =1;
		removeClasses();	
		if ($("#orden_km").val()=="menor") {
			$("#ordFlecha_2").addClass("ordDown");	
			ordUpDown_2 = 1;
		}else {
			$("#ordFlecha_2").addClass("ordUp");
		}	
	} else if ($("#orden_pulsado").val()==3) {
		ordAkt_3 =1;
		removeClasses();	
		if ($("#orden_fecha").val()=="menor") {
			$("#ordFlecha_3").addClass("ordDown");	
			ordUpDown_3 = 1;
		} else {
			$("#ordFlecha_3").addClass("ordUp");
		}
	} else if ($("#orden_pulsado").val()==4) {
		ordAkt_4 =1;
		removeClasses();	
		if ($("#orden_precio").val()=="menor") {
			$("#ordFlecha_4").addClass("ordDown");	
			ordUpDown_4 = 1;
		} else {
			$("#ordFlecha_4").addClass("ordUp");
		}
	}
	
}

/* -------------- Funcion que asigna valor a un campo hidden y llama al xajax ------------ */
function set_Campofiltro(etiqueta, valor, num_filtro){
	
	document.getElementById(etiqueta).value=valor;
	AddNum_Filtro(num_filtro);
	xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
}

/* -------------- Funcion que asigna valor a un  2 campos, desde y hasta y llama al xajax ------------ */
function set_CampoDosfiltro(etiqueta_d, etiqueta_h, valor_d, valor_h, num_filtro){
	document.getElementById(etiqueta_d).value=valor_d;
	document.getElementById(etiqueta_h).value=valor_h;
	
	//document.getElementById('num_filtro').value=num_filtro;
	AddNum_Filtro(num_filtro);
	xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
}
/* -------------- Funcion que asigna valor y texto a dos campo hidden y llama al xajax ------------ */
function set_Campofiltrotexto(etiqueta1, etiqueta2, valor, texto, num_filtro){
	document.getElementById(etiqueta1).value=valor;
	document.getElementById(etiqueta2).value=texto;
	//document.getElementById('num_filtro').value=num_filtro;
	AddNum_Filtro(num_filtro);
	xajax_actualizaResultados(xajax.getFormValues('filtrosForm'));
}
/* -------------- Funcion que asigna valor a un campo  ------------ */
function set_Campo(etiqueta, valor){
	//document.getElementById(etiqueta).value=valor;
	$("#" + etiqueta).val(valor); 
}
/* -------------- Funcion que añade o quita a la comparativa de coches  ------------ */
function Click_Comparativa(eschecked, nombre) {
	
	$aux_id=nombre.substr(5);
	
	valor_act=document.getElementById('ids_comparativa').value;
	Arr_ids=valor_act.split('#');
	total_arr=Arr_ids.length - 1;	
	$("#ErrorComparativa").hide();
	
	
	
	if (eschecked==true) {
		if (total_arr<3) {
			if (valor_act.length > 0) {
				txt='';
				for (i=0;i<total_arr;i++){ 
				    txt=txt + Arr_ids[i] + "#";
				}
				txt=txt + $aux_id + "#";
				document.getElementById('ids_comparativa').value=txt;
			} else {
				document.getElementById('ids_comparativa').value=$aux_id + "#";
			}
			//alert (document.getElementById('ids_comparativa').value);
		} else {
			document.getElementById(nombre).checked=false;
		}	
	} else { //checked==false
		txt='';
		for (i=0;i<total_arr;i++){ 
			if (Arr_ids[i]!=$aux_id) {
				txt=txt + Arr_ids[i] + "#";
			}
		}
		document.getElementById('ids_comparativa').value=txt;
		//alert (document.getElementById('ids_comparativa').value);
	}
}


/* -------------- al pulsar el botón de comparar  ------------ */
function Comparar_coches() {
	
	valor_act=document.getElementById('ids_comparativa').value;
	Arr_ids=valor_act.split('#');
	total_arr=Arr_ids.length - 1;	
	if (total_arr<2) {
		xajax_PintaErrorComparativa(2);
	} else {	
		el = document.getElementById("overlay");
		el.style.visibility =  "hidden";
		var api = $("#overlay").expose({api: true}); 
		api.close();
		document.filtrosForm.action='/comparativa.php'; 
		document.filtrosForm.submit()
	}
}
//----------------------------------------------------------
function EliminarCookie (url_borrar) {
	var stay=confirm("¿Está seguro de que desea eliminar ese coche del parking?");
	if (stay) {
		document.location=url_borrar;
	} else {
		document.location='/parking.php';
	}
}
//----------------------------------------------------------
function DesactivarCarga() {
	
	var api = $("#overlayCarga").expose({api: true}); 
	api.close();
	hidediv('overlayCarga');
	

	$(function() { 
	    $("a[rel]").overlay({ 
	 
	    	left: 360,
	    	top: 110, 
	        expose: { 
	            color: '#000000', 
	            opacity: 0.7, 
				loadSpeed: 100,
	            closeSpeed: 200,
	            api: true
	        }			
	    }); 	
	});	
	
	
	
	
	
}



//-------- ocultarDivs--------------
function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}
//---------------- Añade un filtro al campo num_filtro------
function AddNum_Filtro(num) {

	valor=document.getElementById('num_filtro').value;
	document.getElementById('num_filtro').value=valor + '#' + num;
}
//-------------- Elimina un filtro del campo num_filtro
function DelNum_Filtro(num){
	valor=document.getElementById('num_filtro').value;
	Avalor=valor.split('#');
	new_valor='';
	for (i=0;i<Avalor.length;i++){ 
		if ((Avalor[i]!='') & (Avalor[i]!=num) & (Avalor[i]!=0)) {
			new_valor= new_valor + '#' + Avalor[i];
		}
	} 
	document.getElementById('num_filtro').value=new_valor;
}
//----------------- Abre todos los filtros contenidos en el campo
function AbreNum_Filtros(filtros) {
	valor=filtros;
	Avalor=valor.split('#');
	for (i=0;i<Avalor.length;i++){ 
		if ((Avalor[i]!='') & (Avalor[i]>0)) {
			$("#filtroCont_" + Avalor[i]).slideDown("fast");
		}
	} 
}

	/* --------------- Overlay -------------- */
	$(function() { 
	    $("a[rel]").overlay({ 
	    	left: 360,
	    	top: 110, 
	        expose: { 
	            color: '#000000', 
	            opacity: 0.7, 
				loadSpeed: 100,
	            closeSpeed: 200,
	            api: true
	        }			
	    }); 	
	});
	
	$(document).ready(
			function() { $('#overlay,#overlayCarga').bgiframe({ height: 1000, width: 900  }); }
	);
	
	

