
$(document).ready(function(){
    var what = $('#what');
    if(what.length > 0)
    {
       what[0].ajaxTimeout = null;

       what.focusin(function(){
           if (what.val() == 'che cosa') what.val('');
       });
       what.focusout(function(){
           if (what.val() == '') what.val('che cosa');
       });
       what.keyup(function(){
           var input_search = what.val();

           if(input_search.length > 0 ){
             clearTimeout(what[0].ajaxTimeout);
             what[0].ajaxTimeout = setTimeout(function(){
                   var id_argomento_glossario = $('#ref_id_argomento_glossario_voci').val();
                   $.ajax({
                       type: "POST",
                       url: ABSOLUTE_SITE_URL + 'ricerca/suggestion_glossario_ajax/',
                       data: ({
                            argomento_glossario_id: (id_argomento_glossario > 0 ? id_argomento_glossario : null),
                            searched_string: jQuery.trim(input_search)
                       }),
                       dataType: "text",
                       success: function(res){
                            $('#suggestions_voci_glossario').show();
                            $('#voci_assegnabili_placeholder').html(res);
                        },
                       error:function(){
                            alert("errore chiamata ajax");
                       }
                    });
                 }, 600);
            }
        });
    }

    $("#vociGlossariocloseButton").click(function() {
      $('#suggestions_voci_glossario').hide();
    });

    $(document).bind('click', function(e) {
        
        var el_clicked = $(e.target);
        //var el_kids = el_clicked.children();
        var el_parents = el_clicked.parents();
        
        if (!el_clicked.is('#suggestions_voci_glossario')  && el_parents.filter("#suggestions_voci_glossario").length == 0  ) {
            $('#suggestions_voci_glossario').hide();
        }
    });

    var who = $('#who');
    if(who.length > 0)
    {
       who[0].ajaxTimeout = null;

       who.focusin(function(){
           if (who.val() == 'chi') who.val('');
       });
       who.focusout(function(){
           if (who.val() == '') who.val('chi');
       });
       who.keyup(function(){
           var input_search = who.val();

           if(input_search.length > 0 ){
             clearTimeout(who[0].ajaxTimeout);
             who[0].ajaxTimeout = setTimeout(function(){
                   $.ajax({
                       type: "POST",
                       url: ABSOLUTE_SITE_URL + 'professionisti/suggestion_categorie_ajax/',
                       data: ({
                            searched_string: jQuery.trim(input_search)
                       }),
                       dataType: "text",
                       success: function(res){
                            $('#suggestions_voci_chi').show();
                            $('#vociChi_assegnabili_placeholder').html(res);
                        },
                       error:function(){
                            alert("errore chiamata ajax");
                       }
                    });
                 }, 600);
            } else {
                $('#who_cid').val('');
            }
        });
    }

    var where = $('#where');
    where.focusin(function(){
       if (where.val() == 'dove') where.val('');
    });
    where.focusout(function(){
       if (where.val() == '') where.val('dove');
    });



    $("#vociChicloseButton").click(function() {
      $('#suggestions_voci_chi').hide();
    });

    $(document).bind('click', function(e) {

        var el_clicked = $(e.target);
        //var el_kids = el_clicked.children();
        var el_parents = el_clicked.parents();

        if (!el_clicked.is('#suggestions_voci_chi')  && el_parents.filter("#suggestions_voci_chi").length == 0  ) {
            $('#suggestions_voci_chi').hide();
        }
    });

    //where (versioni locali)
    
    if (true)//(IS_LOCAL_VERSION)
    {
        if(where.length > 0)
        {
           where[0].ajaxTimeout = null;
/*
           where.focusin(function(){
               if (where.val() == LOCAL_VERSION+' (Provincia)') where.val('');
           });
           where.focusout(function(){
               if (where.val() == '') what.val(LOCAL_VERSION+' (Provincia)');
           });
*/
           where.keyup(function(){
               var input_search = where.val();

               if(input_search.length > 0 ){
                 clearTimeout(where[0].ajaxTimeout);
                 where[0].ajaxTimeout = setTimeout(function(){
                       //var id_where = $('#ref_id_luogo').val();
                       $.ajax({
                           type: "POST",
                           url: ABSOLUTE_SITE_URL + 'professionisti/suggestion_luoghi_ajax/',
                           data: ({
                                //where_id: (id_where > 0 ? id_where : null),
                                searched_string: jQuery.trim(input_search)
                           }),
                           dataType: "text",
                           success: function(res){
                                $('#suggestions_luoghi').show();
                                $('#luoghi_placeholder').html(res);
                            },
                           error:function(){
                                alert("errore chiamata ajax");
                           }
                        });
                     }, 600);
                }
            });
        }
    }

    $("#luoghicloseButton").click(function() {
      $('#suggestions_luoghi').hide();
    });

});

function seleziona_suggerimento_chi(sender,cid){
    $('#who').val($(sender).html());
    if ( cid !== undefined ) {
        $('#who_cid').val(cid);
    }

    //alert($(sender).html());
    $('#suggestions_voci_chi').hide();
}

function seleziona_suggerimento_luogo(sender){
    $('#where').val($(sender).html());
    $('#suggestions_luoghi').hide();
}


function check_cercap(){
    if ($('#who').val() == 'chi') $('#who').val('');
    if ($('#where').val() == 'dove') $('#where').val('');
    return true;
}

function check_cercae(){
    if ($('#what').val() == 'che cosa') $('#what').val('');
    return true;
}

function goto_contenitore(){
    if ($('#container_url').val() != '') window.location.href=$('#container_url').val();
    return true;
}

