$(document).ready(function() {
	init();
	$("#cont_idiomas").click(function(){
		$("#lista_idiomas").slideToggle();
		return false;
	});
	$('body').click(function(){
		if(!$("#lista_idiomas").is(":hidden")) $("#lista_idiomas").slideUp();
	});
	$("#lista_idiomas").click(function(event){
		event.stopPropagation();
	});
	$("#frmBuscador select").change(function(){
		$(this).selects_buscador(idioma);
	});
	$("#frmBuscador_venta select").change(function(){
		$(this).selects_buscador(idioma,1);
	});
	$("#personas").focus(function(){
		if($(this).attr("value") == personas) $(this).attr("value", ""); 
	}); 
	$("#personas").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", personas); 
	});
	$("#btn_buscar_general").click(function(){
		if ($("#frmBuscador").length) {
			$("#frmBuscador").submit();
		} else {
			$("#frmBuscador_venta").submit();
		}
	});
	$("#btn_buscar_ref").click(function(){
		if(jQuery.trim($("#referencia").val()).length>0) $("#frmBuscadorRef").submit();
	});
	$("#fav_borra").click(function(){
		Dethcuts.clear();
		return false;
	});
});
jQuery.fn.selects_buscador = function(){
	var el = $(this).attr("id");
	var script = path + 'includes_front/json-buscador.asp';
	if (arguments[1]==1) script = path + 'includes_front/json-buscador-venta.asp';
	if(el=='tipo' || el=='poblacion'){
		var params = 'tipo=' + $("#tipo").val() + '&idioma=' + arguments[0];
		if(el=='poblacion') params += '&poblacion=' + $("#poblacion").val();
		$.ajax({
			type: 'GET',
			url: script,
			contentType: 'application/json; charset=utf-8',
			dataType: 'json',
			data: params,
			cache: false,
			success: function(data) {
				if(el=='tipo'){
					$('#poblacion').find('option:gt(0)').remove();
					if(data.poblaciones){
						$.each(data.poblaciones, function(i,item){
							$('#poblacion').append($('<option></option>').attr('value',item.value).text(item.text));
						});
					}
				}
				$('#situacion').find('option:gt(0)').remove();
				if(data.situaciones){
					$.each(data.situaciones, function(i,item){
						$('#situacion').append($('<option></option>').attr('value',item.value).text(item.text));
					});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Error: ' + errorThrown);
			},
			beforeSend: function (XMLHttpRequest) {
				$('#loading_buscador').show();
			},
			complete: function (XMLHttpRequest, textStatus) {
				$('#loading_buscador').hide();
			}
		});
	}
}
var Dethcuts = {
	id : "shortcuts" ,
	rawCuts : "" ,
	extantCuts : "" ,
	name : "" ,
	source : "" ,
	cutsArray : []
}
Dethcuts.add = function(name,source) {
	if (document.layers || document.all || document.getElementById) {
		this.rawCuts = unescape(document.cookie);
		if (this.rawCuts.indexOf("dethcuts")>-1) {
			var start = this.rawCuts.indexOf("dethcuts")+9;
			var end = (this.rawCuts.indexOf(";",start)>-1) ? this.rawCuts.indexOf(";",start) : this.rawCuts.length;
			this.extantCuts = this.rawCuts.substring(start,end);
			if (this.extantCuts.indexOf(name)>-1) {
				return;
			} else {
				document.cookie = "dethcuts=" + this.extantCuts + "#" + name + "@" + source + "; expires=Tue, 28 Dec 2010 00:00:00; path=/;"
			}
		} else {
			document.cookie = "dethcuts=" + name + "@" + source + "; expires=Tue, 28 Dec 2070 00:00:00; path=/;"
		}
		this.compile();
	}
}
Dethcuts.valores = function() {
this.rawCuts = unescape(document.cookie);
	if (this.rawCuts.indexOf("dethcuts")>-1) {
		var start = this.rawCuts.indexOf("dethcuts")+9;
		var end = (this.rawCuts.indexOf(";",start)>-1) ? this.rawCuts.indexOf(";",start) : this.rawCuts.length;
		this.extantCuts = this.rawCuts.substring(start,end)
		this.cutsArray = this.extantCuts.split("#")
		var temp = "";
		for (i=0;i<this.cutsArray.length;i++) {
			var name = this.cutsArray[i].substring(0,this.cutsArray[i].indexOf("@"))
			temp +=  "##" + name;
		} 
		return temp;
	}
}
Dethcuts.compile = function() {
	if (document.layers || document.all || document.getElementById) {
		this.rawCuts = unescape(document.cookie);
		if (this.rawCuts.indexOf("dethcuts")>-1) {
			var start = this.rawCuts.indexOf("dethcuts")+9;
			var end = (this.rawCuts.indexOf(";",start)>-1) ? this.rawCuts.indexOf(";",start) : this.rawCuts.length;
			this.extantCuts = this.rawCuts.substring(start,end)
			this.cutsArray = this.extantCuts.split("#")
			var temp = ""
			for (i=0;i<this.cutsArray.length;i++) {
				var name = this.cutsArray[i].substring(0,this.cutsArray[i].indexOf("@"))
				var src = this.cutsArray[i].substring(this.cutsArray[i].indexOf("@")+1,this.cutsArray[i].length)
				temp += "<a href='" + src + "' class='lnk_menu_lateral'><span>" + name + "</span></a>";
			}
			this.write(temp);
		} else return
	}
}
Dethcuts.write = function(str) {
	if (document.layers) {
		document.layers[this.id].document.open();
		document.layers[this.id].document.write(str);
		document.layers[this.id].document.close();
	} else {
		document.getElementById(this.id).innerHTML = str
	}
}
Dethcuts.clear = function() {
	document.cookie = "dethcuts=; expires=Thu, 01 Jan 1970 00:00:01; path=/;";
	$("#shortcuts").html("<span>" + elementos + "</span>");
}
function init() {
	Dethcuts.compile();
}
