// FontSizeToolbox.js
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Historique de mise à jour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 2008-01-14 : Vincent
//				Création du script




// Éditer selon les langues disponibles
function FontSizeToolbox_getContenus () {
	var contenus = new Object();
	
	// Éditer ces propriétés selon les contenus linguistiques
	contenus.fr = {
						petit:  "Petite",
						normal: "Normale",
						grand:  "Grande",
						html:   "{FontSizeToolbox}"
					};
	contenus.en = {
						petit:  "Small",
						normal: "Normal",
						grand:  "Big",
						html:   "{FontSizeToolbox}"
					};
	// Fin de l'édition
	
	return contenus[this.langue];
}





function FontSizeToolbox (sElementID, argumentObject) {
	if ( this.eToolbox = document.getElementById(sElementID) ) {
		
		
		// Propriétés paramétrables
		this.langueDefaut = "fr";
		this.sizeDefaut   = "normal";
		
		this.petit  = .8;
		this.normal = 1;
		this.grand  = 1.5;
		
		this.class_box          = "FontSizeToolBox";
		this.class_spacer       = "FontSizeToolBox_Spacer";
		this.class_petit        = "FontSizeToolBox_Petit";
		this.class_petit_actif  = "FontSizeToolBox_PetitActif";
		this.class_normal       = "FontSizeToolBox_Normal";
		this.class_normal_actif = "FontSizeToolBox_NormalActif";
		this.class_grand        = "FontSizeToolBox_Grand";
		this.class_grand_actif  = "FontSizeToolBox_GrandActif";
		
		this.expiration;
		
		
		// Ajoute ou redéfini les propriétés de l'objet FontSizeToolox
		if ( String(typeof(argumentObject)).toLowerCase() == "object" ) {
			for ( propriete in argumentObject) {
				// Valide si la propriété peut être redéfinie avant d'être ajoutée
				if ( propriete != "eToolbox" &&
						propriete != "cookieObject" &&
						propriete != "langue" &&
						propriete != "choixUsager" &&
						propriete != "contenus" &&
						propriete != "boutons" &&
						propriete != "writeToolbox" &&
						propriete != "setFontSize" &&
						propriete != "getContenus" ) {
					this[propriete] = argumentObject[propriete];	
				}
			}
		}
		
		
		// Méthodes
		this.getContenus  = FontSizeToolbox_getContenus;
		this.writeToolbox = FontSizeToolbox_writeToolbox;
		this.setFontSize = FontSizeToolbox_setFontSize;
		
		
		// Propriétés qu'il ne faut pas redéfinir
		this.cookieObject = new CookieObject();
		
		this.langue = document.documentElement.getElementsByTagName("body")[0].className.substring(0,2); 
			if (!this.langue) this.langue = this.langueDefaut;
		
		if (!this.cookieObject.get("FontSize_Usager")) {
			this.choixUsager = this.sizeDefaut;
			this.cookieObject.set("FontSize_Usager", this.choixUsager, this.expiration, "/");
		} else
			this.choixUsager = this.cookieObject.get("FontSize_Usager");
		
		this.contenus = this.getContenus();
		this.boutons  = new Object();
		
		
		// Lance l'écriture du FontSizeToolBox
		this.writeToolbox();
	}
}





function FontSizeToolbox_writeToolbox () {
	var contenuToolBox = this.contenus.html.replace(/{FontSizeToolbox}/g, '<p class="' + this.class_box + '"></p>');
	this.eToolbox.innerHTML = contenuToolBox;
		
	var box;
	var aParagraphes = this.eToolbox.getElementsByTagName("p");
	for ( var cParagraphes = 0; cParagraphes < aParagraphes.length; cParagraphes++ ) {
		var paragraphe = aParagraphes[cParagraphes];
		if ( paragraphe.className == this.class_box ) {
			box = paragraphe;
			cParagraphes = aParagraphes.length;
		}
	}
	
	
	
	if ( box ) {
		var spacer = document.createElement("span");
			spacer.className = this.class_spacer;
		var txSpacer = document.createTextNode(" | ");
			spacer.appendChild(txSpacer);
		
		// Bouton Petit
		var petit = document.createElement("a");
			petit.href      = "#";
			petit.title     = this.contenus.petit;
			petit.className = this.class_petit;
			petit.refSize   = "petit";
			petit.refObject = this;
			petit.onclick   = function () { this.refObject.setFontSize(this); return false; };
		
		var spanPetit = document.createElement("span");
		var txPetit   = document.createTextNode(this.contenus.petit);
		
		this.boutons.petit = petit;
		
		spanPetit.appendChild(txPetit);
		petit.appendChild(spanPetit);
		box.appendChild(petit);
		box.appendChild(spacer.cloneNode(true));
		
		
		
		// Bouton Normal
		var normal = document.createElement("a");
			normal.href      = "#";
			normal.title     = this.contenus.normal;
			normal.className = this.class_normal;
			normal.refSize   = "normal";
			normal.refObject = this;
			normal.onclick   = function () { this.refObject.setFontSize(this); return false; };
		
		var spanNormal = document.createElement("span");	
		var txNormal   = document.createTextNode(this.contenus.normal);
		
		this.boutons.normal = normal;
		
		spanNormal.appendChild(txNormal);
		normal.appendChild(spanNormal);
		box.appendChild(normal);
		box.appendChild(spacer.cloneNode(true));
		
		
		
		// Bouton Grand
		var grand = document.createElement("a");
			grand.href      = "#";
			grand.title     = this.contenus.grand;
			grand.className = this.class_grand;
			grand.refSize   = "grand";
			grand.refObject = this;
			grand.onclick   = function () { this.refObject.setFontSize(this); return false; };
		
		var spanGrand = document.createElement("span");
		var txGrand   = document.createTextNode(this.contenus.grand);
		
		this.boutons.grand = grand;
		
		spanGrand.appendChild(txGrand);
		grand.appendChild(spanGrand);
		box.appendChild(grand);
		
		
		this.setFontSize( this.boutons[this.choixUsager] );
	}
	
}





function FontSizeToolbox_setFontSize ( eElement ) {
	if ( eElement != this.boutons.actif ) {
		if (this.boutons.actif)
			this.boutons.actif.className = this["class_" + this.boutons.actif.refSize];
		
		this.boutons.actif = eElement;
		this.boutons.actif.className = this["class_" + this.boutons.actif.refSize + "_actif"];
		
		this.choixUsager = eElement.refSize;
		this.cookieObject.set("FontSize_Usager", this.choixUsager, this.expiration, "/");
		
		document.body.style.fontSize = this[this.choixUsager] + "em";
	}
}

