function liste_ville(color,cat,rub,region,departement,ville) {
    $.post(url_site+'/ajax/liste_ville.php', {rub:rub,region:region,departement:departement}, function(docXML){
        var items = docXML.getElementsByTagName("ville");
        var tab_id = new Array;
	var tab_titre = new Array;
	var tab_url = new Array;
        for (i=0; i<items.length ; i++) {
                for (j=0;j<items[i].childNodes.length;j++) {
                        if (items[i].childNodes[j].nodeName == 'id') {
                                tab_id[i]=items[i].childNodes[j].childNodes[0].nodeValue;
                        }
                        if (items[i].childNodes[j].nodeName == 'titre') {
                                tab_titre[i]=items[i].childNodes[j].childNodes[0].nodeValue;
                        }
                        if (items[i].childNodes[j].nodeName == 'url') {
                                tab_url[i]=items[i].childNodes[j].childNodes[0].nodeValue;
                        }
                }
        }
        var nb_ville=items.length;
        var str='';
        var str_2='';
        var classe='';
        var style='';

        if (nb_ville>0) {
                var is_temp=false;
                for (i=0;i<nb_ville;i++) {
                        str=str+'<li><a class="gris fleche_cat'+cat+'" href="'+tab_url[i]+'"><b style="font-size:13px;">'+tab_titre[i]+'</b> : toutes les piscines</a>';

                        if (ville==tab_id[i]) {classe="fleche_cat"+cat+" ";style="color:#"+color+";margin-left:-10px; ";} else {classe="blanc ";style="";}
                        str_2=str_2+'<li><a style="'+style+'" class="'+classe+'font-12" href="'+tab_url[i]+'">'+tab_titre[i]+'</a></li>';
                }

                if (region=='0') {
                    $("#villes_2").html(str_2);
                    $("#villes_2" ).show( 'blind', {}, 1000 );
                }
                if (rub=='piscine') {
                    if (region!='0' || (departement!='0' && ville=='0')) $("#villes").html(str);
                    $('#villes_affiche_liste').click(function() {
                        $( "#villes" ).show( 'blind', {}, 1000 );
                        $('#villes_affiche_liste').hide();
                        $('#villes_masque_liste').show();
                    });
                    $('#villes_masque_liste').click(function() {
                        $("#villes" ).hide( 'blind', {}, 1000 );
                        $('#villes_masque_liste').hide();
                        $('#villes_affiche_liste').show();
                    });
                }
        }
    });
}
function cherche(champ,table,id_categorie) {
    $.post(url_site+'/ajax/cherche.php', {table:table,id_categorie:id_categorie}, function(docXML){
        var data = $( "item", docXML ).map(function() {
            return {
                value: $( "titre", this ).text(),
                id: $( "id", this ).text(),
                url: $( "url", this ).text()
            };
        }).get();
        $( "#"+champ).autocomplete({
            source: data,
            minLength: 0,
            select: function( event, ui ) {
                $( "#"+champ+"_valide").click(function() {
                    $(location).attr('href',ui.item.url);
                });
            }
        });
    });
}
function cherche_ville(champ) {
    $( "#"+champ ).autocomplete({
        open: function(event, ui) { $(".ui-slider-handle").css("z-index", -1); },
        close: function(event, ui) { $(".ui-slider-handle").css("z-index", 2); },
        source: function( request, response ) {
            $.ajax({
                url: url_site+'/ajax/cherche.php',
                dataType: "xml",
                data: {
                    table: "ville",
                    recherche: request.term
                },
                success: function( docXML ) {
                    response($( "item", docXML ).map(function() {
                        return {
                            value: $( "titre", this ).text(),
                            id: $( "id", this ).text(),
                            url: $( "url", this ).text()
                        };
                    }).get());
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            $('#installateur_cherche_id_ville').val(ui.item.id);
        }
    });
}
function cherche_installateur(marque,ville,distance,region,departement) {
    $.post(url_site+'/ajax/cherche.php', {table:'installateur',marque:marque,ville:ville,distance:distance,region:region,departement:departement}, function(docXML){
        var data = $( "item", docXML ).map(function() {
            return {
                value: $( "titre", this ).text(),
                id: $( "id", this ).text(),
                url: $( "url", this ).text(),
                ville: $( "ville", this ).text(),
                code_postal: $( "code_postal", this ).text(),
                distance: $( "distance", this ).text()
            };
        }).get();
        var str='';
        if (data.length==0) {
            if (marque!='0') str='<li class="marginleft_10">Pas d\'installateur répertorié pour cette marque</li>';
            else str='<li class="marginleft_10">Pas d\'installateur répertorié</li>';
        }
        else {
            for (var i = 0; i<data.length; i++) {
                str=str+'<li class="marginleft_10"><a class="fleche_bleu bleu gras" href="'+data[i]['url']+'">'+data[i]['value']+'</a><span class="marginleft_10">'+data[i]['code_postal']+' - '+data[i]['ville'];
                if (distance!='0') str=str+' ('+data[i]['distance']+' km)';
                str=str+'</span></li>';
            }
        }
        $('#cherche_liste_installateur').html(str);
    });
}
function les_plus(champ,id_categorie) {
    $.post(url_site+'/ajax/les_plus.php', {id_categorie:id_categorie}, function(docXML) {
        $("#"+champ).html(docXML);
        $("#"+champ+' ul.titre li:first').addClass("active");
        $("#"+champ+' ul.liste:first').addClass("active");
        $("#"+champ+' ul.titre li').hover(function() {
            var i = $("#"+champ+' ul.titre li').index(this);
            $("#"+champ+' ul.titre li').removeClass("active");
            $("#"+champ+' ul.liste').removeClass("active");
            $("#"+champ+' ul.titre li:eq('+i+')').addClass("active");
            $("#"+champ+' ul.liste:eq('+i+')').addClass("active");
        });
    });
}
