﻿// pagination
var pageTotal;
var pageCourant;
var pageEncadre;
var positionDebut;
var positionFin;
var offset = 4;

// Generation
function initPagination(me, total, courant, encadrement) {
pageTotal = total;
pageCourant = courant;
pageEncadre = encadrement;

setPlageCourante();
afficherPages(me);

setEvents(me, pageTotal, pageCourant);
}

function initPagination_choix(me, total, courant, encadrement, formulaire) {
pageTotal = total;
pageCourant = courant;
pageEncadre = encadrement;

setPlageCourante();
afficherPages_choix(me, formulaire);

if(formulaire) {
	setEvents(me, pageTotal, pageCourant);
}
}



// scroll
var objCourrant = ""; //Id courrant
var valeurs = new Array(); //Informations pour chacun des scrollbar

// url pages
function getPageURL() {
var retour;
var param;
var url;
var position;
var positionParamSuivant;
var lienParam;

	// page
lienParam = "&";
param = "PageNum=";
url = document.URL;
position = url.indexOf(param);

if (position != -1) {
	position--;

	retour = url.substring(0, position);

	positionParamSuivant = url.indexOf(lienParam, position + param.length)
	if (positionParamSuivant != -1) {
		retour = retour + url.substring(positionParamSuivant, url.length);
	}

	if (retour.indexOf("?") == -1) {
		lienParam = "?";
	}

	retour = retour + lienParam + param;
} else {
	position = url.indexOf("?")

	if (position == -1) {
		lienParam = "?";
	}

	retour = url + lienParam + param;
}

return retour;
}

function setPlageCourante() {
	// definir taille encadrement
if(pageCourant < 100) {
	pageEncadre = 3;
} else if(pageCourant >= 100 && pageCourant <= 1000) {
	pageEncadre = 2;
} else {
	pageEncadre = 2;
}

if (true) {
	pageEncadre = pageEncadre - 1;
}

	// definir plage à afficher
if(1 >= (pageCourant - pageEncadre)) {
	positionDebut = 1;

	if(pageTotal <= (2 * pageEncadre)) {
	positionFin = pageTotal;
	} else {
	positionFin = (2 * pageEncadre) + 1;
	}
} else if(pageTotal <= (pageCourant + pageEncadre)) {
	positionFin = pageTotal;

	if(1 >= (pageTotal - (2 * pageEncadre))) {
	positionDebut = 1;
	} else {
	positionDebut = pageTotal - (2 * pageEncadre);
	}
} else {
	positionDebut = pageCourant - pageEncadre;
	positionFin = pageCourant + pageEncadre;
}
}

function afficherPages(me) {
var tabBalises = new Array();

var precedent;
if (1 != pageCourant) {
	precedent = creerBalise("a", "precedent", "<", pageCourant - 1);
} else {
	precedent = creerBalise("a", "precedent", "<");
}
	tabBalises.push(precedent);

if (1 != positionDebut) {
	var premier = creerBalise("a", "premier", "1", "1");
	tabBalises.push(premier);
}

if (2 < positionDebut) {
	var suspensionAvant = creerSuspension(2, positionDebut - 1);

	tabBalises.push(suspensionAvant);
}

for (var n = positionDebut; n <= positionFin; n++) {
	var balise;
	if (n == pageCourant) {
		balise = creerBalise("a", null, n);
		balise.setAttribute("id", "courant");
	} else {
		balise = creerBalise("a", null, n, n);
	}

	tabBalises.push(balise);
}

if (2 <= (pageTotal - positionFin)) {
	var suspensionApres = creerSuspension(positionFin + 1, pageTotal - 1);

	tabBalises.push(suspensionApres);
}

if (pageTotal != positionFin) {
	var dernier = creerBalise("a", "dernier", pageTotal, pageTotal);
	tabBalises.push(dernier);
}

var suivant;
if(pageTotal != pageCourant) {
	suivant = creerBalise("a", "suivant", ">", pageCourant + 1);
} else {
	suivant = creerBalise("a", "suivant", ">");
}
	tabBalises.push(suivant);



if (true) {
	var selection = creerGoTo();
	tabBalises.push(selection);
}

for (var b = 0; b < tabBalises.length; b++) {
	me.append(tabBalises[b]);
}

} 

function afficherPages_choix(me, formulaire) {
var tabBalises = new Array();

var precedent;
if (1 != pageCourant) {
	precedent = creerBalise("a", "precedent", "<", pageCourant - 1);
} else {
	precedent = creerBalise("a", "precedent", "<");
}
	tabBalises.push(precedent);

if (1 != positionDebut) {
	var premier = creerBalise("a", "premier", "1", "1");
	tabBalises.push(premier);
}

if (2 < positionDebut) {
	var suspensionAvant = creerSuspension(2, positionDebut - 1);

	tabBalises.push(suspensionAvant);
}

for (var n = positionDebut; n <= positionFin; n++) {
	var balise;
	if (n == pageCourant) {
		balise = creerBalise("a", null, n);
		balise.setAttribute("id", "courant");
	} else {
		balise = creerBalise("a", null, n, n);
	}

	tabBalises.push(balise);
}

if (2 <= (pageTotal - positionFin)) {
	var suspensionApres = creerSuspension(positionFin + 1, pageTotal - 1);

	tabBalises.push(suspensionApres);
}

if (pageTotal != positionFin) {
	var dernier = creerBalise("a", "dernier", pageTotal, pageTotal);
	tabBalises.push(dernier);
}

var suivant;
if(pageTotal != pageCourant) {
	suivant = creerBalise("a", "suivant", ">", pageCourant + 1);
} else {
	suivant = creerBalise("a", "suivant", ">");
}
	tabBalises.push(suivant);



if (formulaire) {
	var selection = creerGoTo();
	tabBalises.push(selection);
}

for (var b = 0; b < tabBalises.length; b++) {
	me.append(tabBalises[b]);
}

} 

function creerSuspension(debut, fin) {
var retour;

	retour = creerBalise("a", "break", "[...]");

return retour;
}

function creerBalise(tag, classe, texte, idClic) {
var balise = document.createElement(tag);

if(classe != undefined) {
	balise.setAttribute("class", classe);
}

if(tag == "a" && idClic != undefined) {
	balise.setAttribute("href", getPageURL() + idClic);  
}  

if(texte != undefined) {
	var texteBalise = document.createTextNode(texte);
	balise.appendChild(texteBalise);
}

return balise;
}



function creerGoTo() {
var conteneur = creerBalise("div", "goto");
	conteneur.setAttribute("id", "goto");

var texteGoto = document.createTextNode("Page : " );
var spanGoto = document.createElement("span");
	spanGoto.appendChild(texteGoto);

var txtBoxGo = document.createElement("input");
	txtBoxGo.setAttribute("type", "text");
	txtBoxGo.setAttribute("size", "1");
	txtBoxGo.setAttribute("name", "deplacer");
	txtBoxGo.setAttribute("class", "txtBoxGo");
	txtBoxGo.setAttribute("value", pageCourant);


var btnGo = document.createElement("input");
	btnGo.setAttribute("type", "button");
	btnGo.setAttribute("value", "");
	btnGo.setAttribute("id", "btnGo");

var courant = document.createElement("input");
	courant.setAttribute("type", "hidden");
	courant.setAttribute("value", pageCourant);
	courant.setAttribute("class", "hCourant");
var total = document.createElement("input");
	total.setAttribute("type", "hidden");
	total.setAttribute("value", pageTotal);
	total.setAttribute("class", "hTotal");


conteneur.appendChild(spanGoto);
conteneur.appendChild(txtBoxGo);
conteneur.appendChild(btnGo);
conteneur.appendChild(courant);
conteneur.appendChild(total);
return conteneur;
}	

// Events
function setEvents(me, varTotal, varCourant) 
{
var total = varTotal;
var courant = varCourant;

me.find("input").click(function() {
	if($(this).attr("type") == "text") {
		if(isNaN($(this).val()) || $(this).val() == courant) {
			$(this).val("");
		}
	} else if ($(this).attr("type") == "button") {
		var valeur = $(this).prev().val();
		if(!isNaN(valeur)) {
			if(valeur >= 1 && valeur <= total && valeur != courant) {
				location.href = getPageURL() + valeur;
			} else {
				$(this).prev().val(pageCourant);
			}
		} else {
			$(this).prev().val("");
		}
	}
});

me.find("input").keypress(function(e) {
	if (e.keyCode == 13) {
		var valeur = $(this).val();
		if(!isNaN(valeur)) {
			if(valeur >= 1 && valeur <= total && valeur != courant) {
				location.href = getPageURL() + valeur;
			} else {
				$(this).val(pageCourant);
			}
		} else {
			$(this).val("");
		}
	}
});
}
