cssVars();

var entete_tri = "<form id=\"aeimmoform\"><table style= \"" + tri_table + "\"><tr><td style=\"" + tri_table_td + "\">Prix</td><td style=\"" + tri_table_td + "\">Localit&eacute;</td><td style=\"" + tri_table_td + "\">Nombre de pi&egrave;ces</td><td style=\"" + tri_table_td + "\">Surface</td></tr><tr><td><img src=\"" + arrow_up + "\" style=\"border-style: none;\"  onClick=\"aesort('prix','asc');\"><img src=\"" + arrow_down + "\" style=\"border-style: none;\"  onClick=\"aesort('prix','desc');\"></td><td><img src=\"" + arrow_up + "\" style=\"border-style: none;\"  onClick=\"aesort('code_postal','asc');\"><img src=\"" + arrow_down + "\" style=\"border-style: none;\"  onClick=\"aesort('code_postal','desc');\"></td><td><img src=\"" + arrow_up + "\" style=\"border-style: none;\"  onClick=\"aesort('nombre_de_pieces','asc');\"><img src=\"" + arrow_down + "\" style=\"border-style: none;\"  onClick=\"aesort('nombre_de_pieces','desc');\"></td><td><img src=\"" + arrow_up + "\" style=\"border-style: none;\"  onClick=\"aesort('surface','asc');\"><img src=\"" + arrow_down + "\" style=\"border-style: none;\"  onClick=\"aesort('surface','desc');\"></td></tr></table></form>";

var div_tri = "<div style=\"width:400px;margin-left: auto;margin-right:auto;\">" + entete_tri + "</div>";

function privateAesortAsc(table,champ){



         if((table['code_postal']).length <= 1){

                                return table;
         }

         var petits = new Array();
         var grands = new Array();

         petits['affichage'] = new Array();
         petits['code_postal'] = new Array();
         petits['prix'] = new Array();
         petits['surface'] = new Array();
         petits['nombre_de_pieces'] = new Array();

         grands['affichage'] = new Array();
         grands['code_postal'] = new Array();
         grands['prix'] = new Array();
         grands['surface'] = new Array();
         grands['nombre_de_pieces'] = new Array();

         var pivot = table[champ][0];

         for(var i = 1; i < table[champ].length; i++){

                 if(table[champ][i] < pivot){

                                    petits['affichage'].push(table['affichage'][i]);
                                    petits['code_postal'].push(table['code_postal'][i]);
                                    petits['prix'].push(table['prix'][i]);
                                    petits['surface'].push(table['surface'][i]);
                                    petits['nombre_de_pieces'].push(table['nombre_de_pieces'][i]);
                 }
                 else{
                                    grands['affichage'].push(table['affichage'][i]);
                                    grands['code_postal'].push(table['code_postal'][i]);
                                    grands['prix'].push(table['prix'][i]);
                                    grands['surface'].push(table['surface'][i]);
                                    grands['nombre_de_pieces'].push(table['nombre_de_pieces'][i]);
                 }
          }
          petits['affichage'].push(table['affichage'][0]);
          petits['code_postal'].push(table['code_postal'][0]);
          petits['prix'].push(table['prix'][0]);
          petits['surface'].push(table['surface'][0]);
          petits['nombre_de_pieces'].push(table['nombre_de_pieces'][0]);

          resultat = new Array();

          var petits_trie = privateAesortAsc(petits,champ);
          var grands_trie = privateAesortAsc(grands,champ);

          resultat['affichage'] = petits_trie['affichage'].concat(grands_trie['affichage']);
          resultat['code_postal'] = petits_trie['code_postal'].concat(grands_trie['code_postal']);
          resultat['prix'] = petits_trie['prix'].concat(grands_trie['prix']);
          resultat['surface'] = petits_trie['surface'].concat(grands_trie['surface']);
          resultat['nombre_de_pieces'] = petits_trie['nombre_de_pieces'].concat(grands_trie['nombre_de_pieces']);


          return resultat;
}

function privateAesortDesc(table,champ){



         if((table['code_postal']).length <= 1){

                                return table;
         }

         var petits = new Array();
         var grands = new Array();

         petits['affichage'] = new Array();
         petits['code_postal'] = new Array();
         petits['prix'] = new Array();
         petits['surface'] = new Array();
         petits['nombre_de_pieces'] = new Array();

         grands['affichage'] = new Array();
         grands['code_postal'] = new Array();
         grands['prix'] = new Array();
         grands['surface'] = new Array();
         grands['nombre_de_pieces'] = new Array();

         var pivot = table[champ][0];

         for(var i = 1; i < table[champ].length; i++){

                 if(table[champ][i] < pivot){

                                    petits['affichage'].push(table['affichage'][i]);
                                    petits['code_postal'].push(table['code_postal'][i]);
                                    petits['prix'].push(table['prix'][i]);
                                    petits['surface'].push(table['surface'][i]);
                                    petits['nombre_de_pieces'].push(table['nombre_de_pieces'][i]);
                 }
                 else{
                                    grands['affichage'].push(table['affichage'][i]);
                                    grands['code_postal'].push(table['code_postal'][i]);
                                    grands['prix'].push(table['prix'][i]);
                                    grands['surface'].push(table['surface'][i]);
                                    grands['nombre_de_pieces'].push(table['nombre_de_pieces'][i]);
                 }
          }
          petits['affichage'].push(table['affichage'][0]);
          petits['code_postal'].push(table['code_postal'][0]);
          petits['prix'].push(table['prix'][0]);
          petits['surface'].push(table['surface'][0]);
          petits['nombre_de_pieces'].push(table['nombre_de_pieces'][0]);

          resultat = new Array();

          var petits_trie = privateAesortDesc(petits,champ);
          var grands_trie = privateAesortDesc(grands,champ);

          resultat['affichage'] = grands_trie['affichage'].concat(petits_trie['affichage']);
          resultat['code_postal'] = grands_trie['code_postal'].concat(petits_trie['code_postal']);
          resultat['prix'] = grands_trie['prix'].concat(petits_trie['prix']);
          resultat['surface'] = grands_trie['surface'].concat(petits_trie['surface']);
          resultat['nombre_de_pieces'] = grands_trie['nombre_de_pieces'].concat(petits_trie['nombre_de_pieces']);


          return resultat;
}

function aesort(champ,sens){


         if(sens == 'desc'){

              ads_table = privateAesortDesc(ads_table,champ);

         }
         else{

             ads_table = privateAesortAsc(ads_table,champ);
         }
         aeimmoliste.innerHTML = "<div id=\"aeconteneur\" style=\"" + div_conteneur + "\">" + div_tri + ads_table['affichage'].join("<br><br>") + "</div>";
}
