function countInObject(obj) {
var count = 0;
for (var key in obj)
if (obj.hasOwnProperty(key)) count++;
return count;
}
function countTerms() {
var count = 0;
for (var key in search_terms) {
console.log(countInObject(search_terms[key]))
count += countInObject(search_terms[key]);
}
return count;
}
$(function() {
search_terms = [];
search_terms["Identidad"]=[];search_terms["Papel en cadena alimentaria"]=[];search_terms["Temas"]=[];search_terms["Países"]=[]; $("#buscar").click(function(e) {
e.preventDefault();
e.stopPropagation();
$(".tab_content.active_tab_content").removeClass("active_tab_content");
$(".tab.active_tab").removeClass("active_tab");
var url = "https://www.osala-agroecologia.org/wp-content/plugins/osala/ajax.php";
var ajax = $.ajax({
method: "POST",
url: url,
data: {
terms: $.extend({}, search_terms)
}
}).done(function(html) {
result = $("
");
$.map($(html),function(n){
if(! $(n).hasClass('gm-map') ){
result.append(n);
} else {
map = n;
}
});
$("#result").html(result);
$("#map").html(map);
$("#result").effect("highlight", 3000);
}).fail(function() {
$("#result").html("Se produjo un error al realizar la busqueda");
}).always(function() {
$(".result").show();
});
});
$(".experiencia-button").click(function(e) {
e.preventDefault();
e.stopPropagation();
$(this).toggleClass("disable");
//$(this).toggleClass("avia-color-theme-color-subtle");
e_id = $(this).attr("id");
taxonomia = $(this).data("taxonomia");
e_id_index = $.inArray(e_id.substr(6), search_terms[taxonomia]);
if (e_id_index != -1) {
eliminarTeminoBusqueda($(this));
} else {
search_terms[taxonomia].push(e_id.substr(6));
$(this).find("span.avia_button_icon").attr("data-av_icon", ""); // cambia a icono de menos
clone = $(this).clone().attr("id", "s-" + e_id);
clone.addClass("clone");
//clone.find("span.avia_button_icon").attr("data-av_icon","");
clone.appendTo("#terms-selected");
}
if (countTerms() > 0) $("#buscar").show();
else $("#buscar").hide();
});
eliminarTeminoBusqueda = function(t) {
taxonomia = t.data("taxonomia");
e_id = t.attr("id");
e_id_index = $.inArray(e_id.substr(6), search_terms[taxonomia]);
$("#s-" + e_id).remove();
t.find("span.avia_button_icon").attr("data-av_icon", ""); //cambia a icono de mas
search_terms[taxonomia].splice(e_id_index, 1);
};
$("#terms-selected").on("click", ".experiencia-button.clone", function(e) {
e.preventDefault();
e.stopPropagation();
id = $(this).attr("id").substr(2);
orig = $("#" + id);
orig.click();
});
});