﻿ShTri = function() {
    //Tableau des établissements à trier
    this.arrayOfEtablissement = new Array();

    this.addEtablissement = function(_id, _nom, _isAnwb, _pays, _region, _departement, _ville, _note) {
        this.arrayOfEtablissement.push(new etablissement(_id, _nom, _isAnwb, _pays, _region, _departement, _ville, _note));
    }

    //Objet Etablissement
    function etablissement(_id, _nom, _isAnwb, _pays, _region, _departement, _ville, _note) {
        this.Id = _id;
        this.Nom = _nom;
        this.IsANWB = _isAnwb;
        this.Pays = _pays;
        this.Region = _region;
        this.Departement = _departement;
        this.Ville = _ville;

        if ((_note == null) || (_note == "")) {
            this.Note = 0.0;
        }
        else {
            this.Note = _note;
        }
    }

    //Noms des champs sur lesquels seront potentiellement effectués les tris
    var nomChamp1 = "";
    var nomChamp2 = "";
    var nomChamp3 = "";

    //Fonction tri sur un champ de type string
    function sortByString(a, b) {
        var x = eval("a." + nomChamp1 + ".toLowerCase()");
        var y = eval("b." + nomChamp1 + ".toLowerCase()");

        //Si le champ2 est rempli on tri sur le 2eme champ
        if (nomChamp2 != "") {
            return ((x < y) ? -1 : ((x > y) ? 1 : sortByString2(a, b)));
        }

        return ((x < y) ? -1 : ((x > y) ? 1 : 0));

    }
    //Fonction de tri sur un champ de type string (appellé uniquement en cas de triage sur 2 champs et que 2 champs du premier tri sont égaux)
    function sortByString2(a, b) {
        var x = eval("a." + nomChamp2 + ".toLowerCase()");
        var y = eval("b." + nomChamp2 + ".toLowerCase()");

        if (nomChamp3 != "") {
            return ((x < y) ? -1 : ((x > y) ? 1 : sortByString3(a, b)));
        }

        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }
    //Fonction de tri sur un champ de type string (appellé uniquement en cas de triage sur 3 champs et que 2 champs du deuxième tri sont égaux)
    function sortByString3(a, b) {
        var x = eval("a." + nomChamp3 + ".toLowerCase()");
        var y = eval("b." + nomChamp3 + ".toLowerCase()");
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }


    //Fonction tri sur un champ de type booléen
    function sortByBool(a, b) {
        var x = eval("a." + nomChamp1 + ".toLowerCase()");
        var y = eval("b." + nomChamp1 + ".toLowerCase()");
        return ((x > y) ? -1 : ((x < y) ? 1 : 0));
    }

    //Fonction tri sur un champ de type Float
    function sortByFloat(a, b) {
        var x = parseFloat(eval("a." + nomChamp1));
        var y = parseFloat(eval("b." + nomChamp1));
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    // "chaineDeTri" attendu est du genre : "Note@1@float" ou "Region,Departement,Ville@0@string"
    // "idDiv" est le nom du div qui contient tout les div à trier
    this.choixDuTri = function(chaineDeTri, idDiv, modulo) {
        if (!modulo) {
            modulo = 10;
        }
        if ((chaineDeTri != null) && (chaineDeTri != "")) {
            var tabSplit = chaineDeTri.split('@');
            var champs = tabSplit[0];

            var tabChamps = champs.split(',');

            //Si c'est un tri simple
            if (tabChamps.length == '1') {
                nomChamp1 = tabChamps[0];
                nomChamp2 = "";
                nomChamp3 = "";

                if (tabSplit[2] == 'string') {
                    this.arrayOfEtablissement.sort(sortByString);
                }
                else {
                    if (tabSplit[2] == 'bool') {
                        this.arrayOfEtablissement.sort(sortByBool);
                    }
                    else {
                        if (tabSplit[2] == 'float') {
                            this.arrayOfEtablissement.sort(sortByFloat);
                        }
                    }
                }
            }
            else {   //Si c'est un tri double
                if (tabChamps.length == '2') {
                    nomChamp1 = tabChamps[0];
                    nomChamp2 = tabChamps[1];
                    nomChamp3 = "";

                    this.arrayOfEtablissement.sort(sortByString);
                }
                else //Si c'est un tri triple
                {
                    nomChamp1 = tabChamps[0];
                    nomChamp2 = tabChamps[1];
                    nomChamp3 = tabChamps[2];

                    this.arrayOfEtablissement.sort(sortByString);
                }
            }

            //Si l'ordre doit être décroissant
            if (tabSplit[1] == '0') {
                this.arrayOfEtablissement.reverse();
            }

            //Suppression du contenu du div général des résultats
            document.getElementById(idDiv).innerHTML = "";

            //Récupération dans l'ordre tri des div correspondants
            var chaine = "";
            var numPage = 0;
            var pageMoteurNb = "";
            var resultMoteurNb = "";
            for (var cpt = 0; cpt < this.arrayOfEtablissement.length; cpt++) {
                if ((cpt % modulo) == 0) {
                    //Si on est sur la premiere page, les 10 premiers résultats sont visibles
                    if (cpt == 0) {
                        numPage++;
                        pageMoteurNb = "page_moteur_" + numPage;
                        chaine += '<div ID=' + pageMoteurNb + '  class="page_moteur">';
                    }
                    else {
                        numPage++;
                        pageMoteurNb = "page_moteur_" + numPage;
                        chaine += '<div ID="' + pageMoteurNb + '" style="display:none" class="page_moteur">';
                    }
                }

                resultMoteurNb = "result_moteur" + (cpt + 1);
                chaine += "<div id='" + resultMoteurNb + "' class='result_moteur'>" + document.getElementById(this.arrayOfEtablissement[cpt].Id).innerHTML + "</div>";

                if ((cpt % modulo) == (modulo - 1)) {
                    chaine += "</div>";
                }
            }

            //Si on a pas fermé le div avant, on le fait...
            if ((cpt % modulo) != (modulo - 1)) {
                chaine += "</div>";
            }

            //Insertion du div trié dans le div général des résultats
            document.getElementById(idDiv).innerHTML = chaine;

            //Partie pour gerer des problèmes sur les numéros et les changements de page de résultats
            if (document.getElementById('bt_page_1')) {
                var cpt2 = 1;
                while (cpt2 <= numPage) {
                    if (document.getElementById('bt_page_' + cpt2)) {
                        document.getElementById('bt_page_' + cpt2).className = "boutonPageDispo";
                    }
                    if (document.getElementById('bt_page_B' + cpt2)) {
                        document.getElementById('bt_page_B' + cpt2).className = "boutonPageDispo";
                    }
                    cpt2 = cpt2 + 1;
                }

                if (document.getElementById('bt_page_1')) {
                    document.getElementById('bt_page_1').className = "boutonPageEnCours";
                }
                if (document.getElementById('bt_page_B1')) {
                    document.getElementById('bt_page_B1').className = "boutonPageEnCours";
                }
                if (document.getElementById("bouton_precedent"))
                    document.getElementById("bouton_precedent").className = "bouton_navigationInactif";
                if (document.getElementById("bouton_precedentB"))
                    document.getElementById("bouton_precedentB").className = "bouton_navigationInactif";
                if (document.getElementById("flechegauche"))
                    document.getElementById("flechegauche").className = "flecheGrise";
                if (document.getElementById("flechegaucheB"))
                    document.getElementById("flechegaucheB").className = "flecheGrise";
            }
        }
    }
}