﻿function initialise() {
    if ($("pagesResultats") && typeof (contenuPage_1) != 'undefined') {
        $("pagesResultats").innerHTML = contenuPage_1;
    }
    if ($("bt_page_1")) {
        $("bt_page_1").className = "link_type1 bouton_page margin_right_type4";
    }
    if ($('numPage')) {
        pageToDisplay = $('numPage').value;
        nbPageTotal = $('nbPages').value;
        cachepagemoteur(nbPageTotal);
        pagemoteur(pageToDisplay, nbPageTotal);
    }
}

function changePage(numPage) {

    nbPages = Math.ceil(nbCamp / nbCampPage);
    $("pagesResultats").innerHTML = eval("contenuPage_" + numPage);
    $("first_camp").innerHTML = ((numPage - 1) * nbCampPage) + 1;
    $("last_camp").innerHTML = eval((numPage - 1) * nbCampPage) + eval(nbCampPage);

    $$(".bouton_page").each(function (elt) {
        elt.className = "link_type2 bouton_page margin_right_type4";
    });

    $("bt_page_" + numPage).className = "link_type1 bouton_page margin_right_type4";

    if (numPage > 1) {
        $("precedent").show();
    } else { $("precedent").hide(); } 

    if (numPage < nbPages) {
        $("suivant").show();
    } else { $("suivant").hide(); } 

    

    var lastPageToDisplay = numPage + 2;
    if (lastPageToDisplay > nbPages) {
        lastPageToDisplay = nbPages;
    }

    var firstPageToDisplay = numPage - 2;
    if (firstPageToDisplay < 1) {
        firstPageToDisplay = 1;
    }

    if ($("span_point") != null) {
        $("pagination").removeChild($("span_point"));
    }

    if ($("span_point_before") != null) {
        $("pagination").removeChild($("span_point_before"));
    }


    $$(".bouton_page").each(function (elt) {
        elt.hide();
    });

    $("bt_page_1").show();
    $("bt_page_" + nbPages).show();


    var i = 0;
    for (i = firstPageToDisplay; i <= lastPageToDisplay; i++) {
        $("bt_page_" + i).show();
    }

    if (lastPageToDisplay != nbPages) {
        var spanPoints = document.createElement('span');
        spanPoints.id = "span_point";
        spanPoints.innerHTML = "...";
        spanPoints.className = "link_type3 bouton_page margin_right_type4";
        Element.insert($("bt_page_" + lastPageToDisplay), { 'after': spanPoints });
    }

    if (firstPageToDisplay != 1) {
        var spanPointsBefore = document.createElement('span');
        spanPointsBefore.id = "span_point_before";
        spanPointsBefore.innerHTML = "...";
        spanPointsBefore.className = "link_type3 bouton_page margin_right_type4";
        Element.insert($("bt_page_" + firstPageToDisplay), { 'before': spanPointsBefore });
    }

}

function pagePrecedente() {
    $$(".bouton_page").each(function (a) {
        if (a.className == "link_type1 bouton_page margin_right_type4") {
            numero_page = a.id.replace("bt_page_", "");
        }
    });
    changePage(parseInt(numero_page, 10) - 1);
}

function pageSuivante() {
    $$(".bouton_page").each(function (a) {
        if (a.className == "link_type1 bouton_page margin_right_type4") {
            numero_page = a.id.replace("bt_page_", "");
        }
    });

    changePage(parseInt(numero_page, 10) + 1);
}

//initialisation suivant precedent
var cptPages = 1;

//cache toutes les pages de resultats
function cachepagemoteur(nbpages) {
    for (i = 1; i <= nbpages; i++) {
        tabResultats = getElementsByClassName('page_' + i);
        for (j = 0; j < tabResultats.length; j++) {
            tabResultats[j].style.display = 'none';
        }
    }

    $$(".bouton_page").each(function (elt) {
        elt.className = "link_type2 bouton_page margin_left_type4 margin_right_type4";
    });
    
}

function pagemoteur(PageaAfficher, nbPages) {
    tabResultatsAffiche = getElementsByClassName('page_' + PageaAfficher);
    for (i = 1; i < tabResultatsAffiche.length; i++) {
        tabResultatsAffiche[i].style.display = 'block';
    }

    $('bt_page_' + PageaAfficher).className = 'link_type1 bouton_page';
    
    if (PageaAfficher == 1) {
        $('precedent').hide();
    } else { $('precedent').show(); }
    if (PageaAfficher == nbPages) {
        $('suivant').hide();
    } else{$('suivant').show();}

    switch (PageaAfficher) {
        case nbPages:
            var first_camp = (PageaAfficher - 1) * nbCampPage;
            $('first_camp').innerHTML = first_camp + 1;
            $('last_camp').innerHTML = nbCamp;
            break;
        case 1:
            var first_camp = PageaAfficher;
            $('first_camp').innerHTML = first_camp;
            $('last_camp').innerHTML = nbCampPage;
            break;
        default:
            var first_camp = (PageaAfficher - 1) * nbCampPage;
            $('first_camp').innerHTML = first_camp + 1;
            $('last_camp').innerHTML = eval(first_camp) + eval(nbCampPage);
    }
    cptPages = PageaAfficher;
}

function precedente() {
    nbPages = $$(".bouton_page").length;
    $$(".bouton_page").each(function (a) {
        if (a.className == "link_type1 bouton_page") {
            numero_page = a.id.replace("bt_page_", "");
            numero_page = parseInt(numero_page, 10);
        }
    });
    cachepagemoteur(numero_page -1 );
    pagemoteur(numero_page - 1, nbPages);
}

function suivante() {
    nbPages = $$(".bouton_page").length;
    $$(".bouton_page").each(function (a) {
        if (a.className == "link_type1 bouton_page") {
            numero_page = a.id.replace("bt_page_", "");
            numero_page = parseInt(numero_page, 10);
        }
    });
    cachepagemoteur(numero_page + 1);
    pagemoteur(numero_page + 1, nbPages);
    
}

