$(function() {
		$('.ajaximagen').each(function(){
						data = eval('('+$(this).children().text()+')');
						$(this).parent().append('<img src="'+data.src+'"></img>');
						$(this).remove();
					});				   
	$("#botonactualizar").css("display", "inline");	   
	$("#tabs").tabs();
	var precio = false;
	var desde;
	var hasta;
	var galletita=document.cookie.split(';');
	var filtros = {};
	var caracteristicas = {};
	var buscar=false;
	var hoy=new Date();
	hoy.setDate(hoy.getDate());
	var humandesde= 'Haga Click';
	var desde=$.datepicker.formatDate('yy-mm-dd', hoy);
	hoy.setDate(hoy.getDate()+1);
	var humanhasta= 'Haga Click';
	var hasta=$.datepicker.formatDate('yy-mm-dd', hoy);
	var seleccionardesde=false;
	var seleccionarhasta=false;
	for(key in galletita){
		var temp=galletita[key].replace(/^\s*|\s*$/g,"");
		if (temp=='buscar=true'){ 
			buscar=true;
		}else if (temp=='seleccionardesde=true'){
				seleccionardesde=true;
	 	}else if  (temp=='seleccionarhasta=true'){
				seleccionarhasta=true;
		} else {
			var temp=temp.split('=');
			if (temp[0]=='humandesde'){
					humandesde=temp[1];
			}else if (temp[0]=='humanhasta'){
				humanhasta=temp[1];
			}else if (temp[0]=='desde'){
				desde=temp[1];
			}else if (temp[0]=='hasta'){
				hasta=temp[1];
			}else if (temp[0]=='filtros'){
				var temp2 = temp[1].split(':');
				while(temp2.length){
					filtros[temp2.pop()]=true;
				}
			}else if (temp[0]=='caracteristicas'){
				var temp2 = temp[1].split(':');
				while(temp2.length){
					caracteristicas[temp2.pop()]=true;
				}
			}
		}
	}
	if(humandesde==false){
		humandesde == 'Haga Click';
		seleccionardesde = false;
	}
	if(humanhasta == false){
		humanhasta == 'Haga Click';
		seleccionarhasta = false;
	}
	optionprecio();
	errormayor();
		
	$('#spandesde').text(humandesde);
	$('#spanhasta').text(humanhasta);
	var tdatepicker=false;
	$("#desde").datepicker({
			minDate: 0,
			maxDate: '+2y',
			changeMonth: true,
			changeYear: true,
			onSelect:function(textDate){
				document.cookie='seleccionardesde=true';
				seleccionardesde=true;
				tdatepicker=true;
			    $('#spandesde').text($.datepicker.formatDate('dd/mm/yy',new Date(textDate)));
				var fecha=new Date(textDate);
				fecha.setDate(fecha.getDate()+1);
			    $('#hasta').datepicker('option','minDate',fecha);						
			},
			onClose: function(date){
				if (tdatepicker){
					tdatepicker=false;
					desde=$.datepicker.formatDate('yy-mm-dd',new Date(date));
					humandesde=$.datepicker.formatDate('dd/mm/yy',new Date(date));
					document.cookie='desde='+desde;						
           			document.cookie='humandesde='+humandesde;							
					$('#botonactualizar').css('display', 'inline');
					errormayor();
					optionprecio();
				}else{
				}
			} 
		}
	);
	$('#spandesde, #caldesde').click(function(){
		$('#desde').datepicker("show");
	});
	$("#hasta").datepicker({	
			minDate: 1,
			maxDate: '+2y',
			changeMonth: true,
			changeYear: true,
			onSelect:function(textDate){
				document.cookie='seleccionarhasta=true';
				seleccionarhasta=true;
				tdatepicker=true;
			    $('#spanhasta').text($.datepicker.formatDate('dd/mm/yy',new Date(textDate)));
			   // $('#desde').datepicker('option','maxDate',new Date(textDate))							
			},
			onClose: function(date){
				if (tdatepicker){
					hasta=$.datepicker.formatDate('yy-mm-dd',new Date(date));
					document.cookie='hasta='+hasta;
					humanhasta=$.datepicker.formatDate('dd/mm/yy',new Date(date));
						document.cookie='humanhasta='+humanhasta;
					tdatepicker=false;
					$('#botonactualizar').css('display', 'inline');
					errormayor();
					optionprecio();
				}else{
				}
			} 
		}
	);
	$('#spanhasta, #calhasta').click(function(){
		$('#hasta').datepicker("show");
	});
	$('#botonbuscar').click(function(){
		document.cookie='buscar=true';
	});
	function optionprecio(){
		if(humandesde === 'Haga Click' || humanhasta === 'Haga Click'){
			$('#precio').hide();
		}else{
			$('#precio').show();
		}
	}
	function errormayor(){
		if((buscar && seleccionardesde && seleccionarhasta)||!buscar){
			$('#errormayor').text('');
		}else if(buscar && !(seleccionardesde && seleccionarhasta)){
			$('#errormayor').text(' Seleccione sus fechas');
		}
		if(desde>=hasta){
//			$('#errormayor').text(' Error: la fecha de entrada no puede ser igual o mayor que la de salida');
			hasta_=$.datepicker.parseDate('yy-mm-dd',desde);
			hasta_.setDate(hasta_.getDate()+1);
			hasta=$.datepicker.formatDate('yy-mm-dd',hasta_);
			humanhasta=$.datepicker.formatDate('dd/mm/yy',hasta_);
			document.cookie='hasta='+hasta;
			document.cookie='humanhasta='+humanhasta;
			$('#spanhasta').text(humanhasta);
		}
	}
   	$("#accordion").accordion({
		header: ".h3",
		alwaysOpen: false
	}).accordion( 'activate' , -1 );
   	$("#selector").change(function(){
		$('#botonactualizar').css('display', 'inline');
	});
	function validar(){
		$valido=false;
		$(".scantidad").each(function(){
			if (this.value!='nada'){
				$valido=true;
				return false;
			}
		});
		return $valido;
	}
    $("#form1").submit(function() {
      if (validar()){
		  return true;
	  }else{
		 $('#errorhb').css('display', 'inline');
 	       return false; 
	  }
     });
		$(".elmapa").each(function(){
		var url = this.firstChild.textContent;
		$(this).text('');
		$(this).append('<a href="'+url+'&height=450&width=650" class="thickbox enlacedestacado" rel="nofollow">(Mostar Mapa)</a>');
	});

	$('#filtros').find('input').change(function(){
		var thi$ = $(this);
		var name = thi$.attr('name');
		if(thi$.is(':checked')){
			filtros[name] = true;
		}else{
			delete filtros[name];
		}
		
		
		var cookie = [];
for(filtro in filtros){
	cookie.push(filtro);
}
document.cookie='filtros='+cookie.join(':');

	});

	$('#caracteristicas').find('input').change(function(){
		var thi$ = $(this);
		var name = thi$.attr('name');
		if(thi$.is(':checked')){
			caracteristicas[name] = true;
		}else{
			delete caracteristicas[name];
		}			
		
		var cookie = [];
for(caracteristica in caracteristicas){
	cookie.push(caracteristica);
}
document.cookie='caracteristicas='+cookie.join(':');

	});

});

