function showSousGamme(idGamme)
{
    $.getJSON(urlSite+"/index.php?module=searchEngine&action=AjaxGetGammes&idGamme="+idGamme, function(data)
    {
        $("#lastEtape").addClass("etape-active");
        var html = "<select id=\"selectSsGamme\"><option value=\"0\">Pas de sous gammes</option></select>";
        if(data.gamme != null && data.gamme != "" && data.gamme != "error")
        {
            html = "<select id=\"selectSsGamme\">\n<option value=\"0\">TOUTES</option>\n";
            for(i=0; i<data.gamme.length;i++)
            {
                html += "<option value='"+data.gamme[i]['id']+"'>"+data.gamme[i]['value']+"</option>\n";
            }
            html += "</select>\n";
        }
        
        $("#sousGamme").html("");
        $("#sousGamme").html(html);
        $("#sousGamme").show();
    });
}

function searchDoc(page)
{
    var idGamme = 0;
    if($("#selectSsGamme").val() != undefined && $("#selectSsGamme").val() != 0)
    {
        idGamme = $("#selectSsGamme").val();
    }
    else
    {
        idGamme = $("#selectGamme").val();
    }
    
    var type = $("#type").val();
    var txt = $("#txt").val();
    
    $.getJSON(urlSite+"/index.php?module=searchEngine&action=AjaxGetDocs&idGamme="+idGamme+"&type="+type+"&txt="+txt+"&p="+page, function(data)
    {
        $("#recherche_result").show();
        $("#recherche_infos").hide();
        if(data.docs != null && data.docs != "error")
        {
            if(data.nbRes == 1)
            {
                $("#nbresult").html(data.nbRes+" document trouvé pour :");
            }
            else
            {
                $("#nbresult").html(data.nbRes+" documents trouvés pour :");
            }
            
            var html = "<ul>\n";
            for(i=0; i<data.docs.length;i++)
            {
                html += "<li><a href=\""+data.docs[i]['url']+"\" target=\"_blank\" class=\"doc-title\">"+data.docs[i]['type']+" - "+data.docs[i]['label']+"</a></li>\n";
            }
            html += "</ul>\n";

            // Pagination
            var pagination = "";

            if(data.nbPage > 1)
            {
                if(data.currentPage != 1)
                {
                    pagination += "<a onclick=\"searchDoc("+(data.currentPage-1)+")\" href=\"javascript:void(0);\" class=\"precedent\">Précédent</a>\n";
                }

                pagination += "<ul>";
                for(i=1; i <= data.nbPage; i++)
                {
                     if( i == data.currentPage)
                     {
                        pagination += "<li><a onclick=\"searchDoc("+i+")\" href=\"javascript:void(0);\" class=\"s-num-active\">"+i+"</a></li>\n";
                     }
                     else
                     {
                        pagination += "<li><a onclick=\"searchDoc("+i+")\" href=\"javascript:void(0);\" class=\"s-num\">"+i+"</a></li>\n";
                     }
                }
                pagination += "</ul>\n";

                if(data.currentPage != data.nbPage)
                {
                    pagination += "<a onclick=\"searchDoc("+ (parseInt(data.currentPage) + parseInt(1)) +")\" href=\"javascript:void(0);\" class=\"suivant\">Suivant</a>\n";
                }
            }

            $("#pagination").html(pagination);
        }
        else
        {
            $("#pagination").html("");
            $("#nbresult").html("0 document trouvés pour :");
            html = "<p><strong>Aucun résultat</strong></p>";
        }
        
        var recapGamme = "<strong>Domaine :</strong> "+document.getElementById('selectGamme').options[document.getElementById('selectGamme').selectedIndex].text + " | ";
        var recapSsGamme = "<strong>Gamme :</strong> ";
        var recapType = "<strong>Type :</strong> "+document.getElementById('type').options[document.getElementById('type').selectedIndex].text + " | ";
        var recapText = "<strong>Mot clef : </strong>"

        if($('#selectSsGamme').val() != undefined)
        {
            recapSsGamme += document.getElementById('selectSsGamme').options[document.getElementById('selectSsGamme').selectedIndex].text + " | ";
        }
        else
        {
           recapSsGamme += "Aucune | ";
        }

        if($("#txt").val() != "Rechercher dans la documentation")
        {
            recapText += $("#txt").val();
        }
        else
        {
            recapText += "Aucun";
        }
     
        $("#recap").html(recapGamme+recapSsGamme+recapType+recapText);
        $("#res").html(html);
    });
}

function resetSearch()
{
    document.getElementById('txt').value = "Rechercher dans la documentation";
    document.getElementById('type').selectedIndex = 0;
    document.getElementById('selectGamme').selectedIndex = 0;
    document.getElementById('selectSsGamme').selectedIndex = 0;
    $("#sousGamme").hide();
}

$(window).keydown(function(event)
{
		// Enter
		if(event.keyCode == 13)
		{
            searchDoc(1)
		}
});