/**
* Choix langue par défaut si aucune n'est définie
**/
if(!window.lang){
	window.lang="fr_FR";
}

/**
 * Initialisation des constantes d'affichage multilingues
 */
var TXT_FERMER="Fermer";
if(window.lang=="es_ES"){
	TXT_FERMER="Cerrar";
}
if(window.lang=="it_IT"){
	TXT_FERMER="Chiudere";
}
if(window.lang=="en_GB"){
	TXT_FERMER="Close";
}
/** 
 * Dimension du iframe pour les MSIE<=6.9
 */
var MSIE6_WIDTH=206;
var MSIE6_HEIGHT=328;

/**
* L'objet Calendar lorsqu'il est créé ajoute dans le body la structure HTML suivante:
* <div class="calendar">
*   <!-- Le div et la table suivants se trouvent en deux exemplaires (2 mois) puisqu'on est en calendrier double -->
*   <div class="title"><a href="">&lt;&lt;</a> fev. 2007 <a href="">&gt;&gt;</a></div>
*	<table>
*		<thead>
*			<tr>
*				<td>lun.</td>
*				<td>mar.</td>
*				<td>mer.</td>
*				<td>jeu.</td>
*				<td>ven.</td>
*				<td>sam.</td>
*				<td>dim.</td>
*			</tr>
*		</thead>
*		<tbody>
*			<tr>
*				<td></td>
*				<td></td>
*				<td></td>
*				<td><a href="">1</a></td>
*				<td><a href="">2</a></td>
*				<td><a href="">3</a></td>
*				<td><a href="">4</a></td>
*			</tr>
*			<tr>
*				<td><a href="">5</a></td>
*				<td><a href="">6</a></td>
*				<td><a href="">7</a></td>
*				<td><a href="">8</a></td>
*				<td><a href="">9</a></td>
*				<td><a href="">10</a></td>
*				<td><a href="">11</a></td>
*			</tr>
*					.
*					.
*					.
*		</tbody>
*	</table>
*	<div class="close"><a href="javascript:">Fermer</a><div>
* <div>
*
* L'apparence de cette structure peut être facilement modifiée à l'aide de feuilles de style (se référer aussi à la feuille de style par défaut QUI NE
* DOIT PAS ETRE MODIFIEE calendar.css, on peut être ammené à surcharger les styles de cette dernière)
*
* Pour utiliser ce calendrier, on doit impérativement utiliser des sélecteurs jour et mois qui contiennent respectivement les valeurs date codée sur 
* deux digits 00 01 ... 31 et le mois sur deux digits concaténé avec l'année sur 4 digits séparés par "/" 01/2007 par exemple. Ces sélecteurs
* sont transmis à l'instance Calendar en paramètre sous forme de nodes elements:
* 	new Calendar(jourElement,moisElement);
* On doit de plus s'assurer d'inclure dans la page utilisant calendar.js, la feuille de style calendar.css
*
* Paramètres du constructeur:
* - jourElement l'élément selecteur de jour (tel que décrit au dessus) 
* - moisElement l'élément selecteur de mois (tel que décrit au dessus) 
* Méthodes utiles:
* - show()
* - hide()
* La fonction de ces méthodes est évidente, et il n'est d'ailleurs pas nécessaire d'utiliser hide() qui est déjà wrappé
* sur les liens du calendrier.
**/
function Calendar(jourElement,moisElement,div) {
	// ATTENTION: Le sélecteur mois contient à la fois le mois et l'année au format mm/aaaa et souvent on fait 
	// l'amalgame entre mois et mois+année, parfois on fait la différence entre mois et année, il faut suivre...
	
	// On place le nouveau calendrier créé dans le pool (variable globale calendarPool)
	this.poolIndex=calendarPool.add(this);
	// On référence les sélecteurs jour et mois qui vont être affectés par ce calendrier
	this.jourElement=jourElement;
	this.moisElement=moisElement;
	// On définit la date actuellement affichée par le sélecteur dans les propriétés associées
	this.selectedJour=this.jourElement.value;
	this.selectedMois=this.moisElement.value.replace(/\/[0-9]{4}$/,"");
	this.selectedAn=this.moisElement.value.replace(/^[0-9]{2}\//,"");
	
	// On définit les bornes minimale et maximale des mois et années définies dans le sélecteur mois
	this.minSelectableMois=this.moisElement.options[0].value.replace(/\/[0-9]{4}$/,"");
	this.minSelectableAn=this.moisElement.options[0].value.replace(/^[0-9]{2}\//,"");
	this.maxSelectableMois=this.moisElement.options[this.moisElement.options.length-1].value.replace(/\/[0-9]{4}$/,"");
	this.maxSelectableAn=this.moisElement.options[this.moisElement.options.length-1].value.replace(/^[0-9]{2}\//,"");

	// On commence maintenant à créer l'arborescence DOM contenant le calendrier
	// D'abord on crée les propriétés qui doivent référencer ces éléments
	// Le div principal (cf. structure décrite plus haut)
	this.divCalendar=div;
	this.divCalendar=document.createElement("DIV");
	this.divCalendar.className="calendar";
	this.divCalendar.style.display="none";

	// Les éléments se trouvant dans le div interne qui contient le titre du calendrier 1
	this.a1Down=document.createElement("A");
	this.title1=document.createTextNode("Title1");
	this.a1Up=document.createElement("A");

	// La table et le tbody du calendrier 1
	// NOTA: IE ne parvient pas à afficher une table DOM sans tbody!
	this.table1=document.createElement("TABLE");
	this.tbody1=document.createElement("TBODY");
	
	// Les éléments se trouvant dans le div interne qui contient le titre du calendrier 2
	this.title2=document.createTextNode("Title2");

	// La table et le tbody du calendrier 2
	// NOTA: IE ne parvient pas à afficher une table DOM sans tbody!
	this.table2=document.createElement("TABLE");
	this.tbody2=document.createElement("TBODY");
	
	// On modifie maintenant les propriétés DOM créées plus haut et on organise le tout hiérarchiquement
	// Dans le div title
	this.a1Down.href="javascript:;";
	this.a1Down.appendChild(document.createTextNode("<<"));
	this.a1Up.href="javascript:;";
	this.a1Up.appendChild(document.createTextNode(">>"));
	var divTitle1=document.createElement("DIV");
	divTitle1.className="title";
	divTitle1.appendChild(this.a1Down);
	divTitle1.appendChild(this.title1);
	divTitle1.appendChild(this.a1Up);
	this.divCalendar.appendChild(divTitle1);

	// Dans table on place un thead qui va contenir les noms de jour, puis le tbody vide
	var thead=document.createElement("THEAD");
	var tr=document.createElement("TR");
	thead.appendChild(tr);
	for(var i=1;i<=7;i++){
		var jourTexte=getJourTexteCourt(i%7);
		var td=document.createElement("TD");
		td.appendChild(document.createTextNode(jourTexte));
		tr.appendChild(td);
	}
	this.table1.appendChild(thead);
	this.table1.appendChild(this.tbody1);
	this.divCalendar.appendChild(this.table1);

	// On recommence pour le calendrier 2
	var divTitle2=document.createElement("DIV");
	divTitle2.className="title";
	divTitle2.appendChild(this.title2);
	this.divCalendar.appendChild(divTitle2);

	thead=document.createElement("THEAD");
	tr=document.createElement("TR");
	thead.appendChild(tr);
	for(var i=1;i<=7;i++){
		var jourTexte=getJourTexteCourt(i%7);
		var td=document.createElement("TD");
		td.appendChild(document.createTextNode(jourTexte));
		tr.appendChild(td);
	}
	this.table2.appendChild(thead);
	this.table2.appendChild(this.tbody2);
	this.divCalendar.appendChild(this.table2);
	
	// Div de fermeture
	var divClose=document.createElement("DIV");
	divClose.className="close";
	var aClose=document.createElement("A");
	aClose.appendChild(document.createTextNode(TXT_FERMER));
	aClose.href="javascript:calendarPool.get("+this.poolIndex+").hide();cerrado();";
	divClose.appendChild(aClose);
	//divTitle1.appendChild(divClose);
	this.divCalendar.appendChild(divClose);

	// Finalement on met le calendrier dans le body
//	var body=document.getElementsByTagName("BODY")[0];
//	body.appendChild(this.divCalendar);
	div.appendChild(this.divCalendar);
	

	// Affiche le calendrier
	this.show=function() {
		calendarPool.closeAll();
		this.selectedJour=this.jourElement.value;
		this.selectedMois=this.moisElement.value.replace(/\/[0-9]{4}$/,"");
		this.selectedAn=this.moisElement.value.replace(/^[0-9]{2}\//,"");
		this.setMonth();
		this.divCalendar.style.display="block";
		
		if(genericNavigator.navigator.id==MSIE&&genericNavigator.navigator.version<=6.9) {
			var iframe=document.createElement("IFRAME");
			iframe.style.display="none";
			iframe.style.display="block";
			iframe.style.position="absolute";
			iframe.style.top="0";
			iframe.style.left="0";
			iframe.style.zIndex="-1";
			iframe.style.filter="mask()";
			iframe.style.width=(MSIE6_WIDTH+10)+"px";
			iframe.style.height=(MSIE6_HEIGHT+10)+"px";
			iframe.style.background="none";
			iframe.frameBorder=0;
			iframe.scrolling="no";
			this.divCalendar.appendChild(iframe);
		}
	}
	// Efface le calendrier
	this.hide=function() {
		this.divCalendar.style.display="none";
	}
	// Modifie les propriétés du calendrier avec un nouveau mois (et année)
	this.setMonth=function(mois,an) {
		this.setTitle(mois,an);
		this.setTable(mois,an);
		this.setUp(mois,an);
		this.setDown(mois,an);
	}
	// Modifie les textes dans les div title en fonction du nouveau mois (et année)
	this.setTitle=function(mois,an) {
		if(mois==undefined||an==undefined) {
			mois=this.selectedMois;
			an=this.selectedAn;
		}
		this.title1.nodeValue=" "+getMoisTexteLong(mois)+" "+an+" ";
		mois++;
		if(mois>12){
			mois=1;
			an++;
		}
		this.title2.nodeValue=" "+getMoisTexteLong(mois)+" "+an+" ";
	}
	// Modifie les tables (les tbody pour être précis) en analysant le mois (et année du calendrier 1)
	this.setTable=function(mois,an) {
		if(mois==undefined||an==undefined) {
			mois=this.selectedMois;
			an=this.selectedAn;
		}
		var DAY=24*60*60*1000;
		this.table1.removeChild(this.tbody1);
		this.tbody1=document.createElement("TBODY");
		this.table1.appendChild(this.tbody1);
		var date=new Date();
		date.setDate(1);
		date.setMonth(mois-1);
		date.setFullYear(an);
		while(date.getDay()!=1){// while(date.getDay()!=LUNDI)
			date.setTime(date.getTime()-DAY);
		}
		while(date.getMonth()!=(mois-1)){
			this.initTd1(date,mois,an);
		}
		while(date.getMonth()==(mois-1)){
			this.initTd1(date,mois,an);
		}
		mois++;
		if(mois>12){
			mois=1;
			an++;
		}
		if(this.isSelectable(mois,an)){
			this.table2.removeChild(this.tbody2);
			this.tbody2=document.createElement("TBODY");
			this.table2.appendChild(this.tbody2);
			this.table2.style.display="";
			var date=new Date();
			date.setDate(1);
			date.setMonth(mois-1);
			date.setFullYear(an);
			while(date.getDay()!=1){// while(date.getDay()!=LUNDI)
				date.setTime(date.getTime()-DAY);
			}
			while(date.getMonth()!=(mois-1)){
				this.initTd2(date,mois,an);
			}
			while(date.getMonth()==(mois-1)){
				this.initTd2(date,mois,an);
			}
		}
		else{
			this.title2.nodeValue="";
			this.table2.style.display="none";
		}
	}
	// Crée un td contenant la date passée en paramètre dans le tbody 1
	this.initTd1=function(date,mois,an) {
		var DAY=24*60*60*1000;
		var tr=document.createElement("TR");
		this.tbody1.appendChild(tr);
		for(var i=1;i<=7;i++){
			var td=document.createElement("TD");
			if(date.getMonth()==(mois-1)){
				var a=document.createElement("A");
				var formatedJour=date.getDate();
				if(formatedJour<10) formatedJour="0"+formatedJour;
				var formatedMois=date.getMonth()+1;
				if(formatedMois<10) formatedMois="0"+formatedMois;
				a.href="javascript:document.getElementById('"+this.jourElement.id+"').value='"+formatedJour+"';document.getElementById('"+this.moisElement.id+"').value='"+formatedMois+"/"+date.getFullYear()+"';calendarPool.get("+this.poolIndex+").hide();cerrado();";
				a.appendChild(document.createTextNode(date.getDate()));
				td.appendChild(a);
			}
			else{
				td.appendChild(document.createTextNode(date.getDate()));
			}
			if(date.getFullYear()==this.selectedAn&&date.getMonth()==(this.selectedMois-1)&&date.getDate()==this.selectedJour){
				addClass(td,"selected");
			}
			else{
				removeClass(td,"selected");
			}
			tr.appendChild(td);
			date.setTime(date.getTime()+DAY);
		}
	}
	// Crée un td contenant la date passée en paramètre dans le tbody 2
	this.initTd2=function(date,mois,an) {
		var DAY=24*60*60*1000;
		var tr=document.createElement("TR");
		this.tbody2.appendChild(tr);
		for(var i=1;i<=7;i++){
			var td=document.createElement("TD");
			if(date.getMonth()==(mois-1)){
				var a=document.createElement("A");
				var formatedJour=date.getDate();
				if(formatedJour<10) formatedJour="0"+formatedJour;
				var formatedMois=date.getMonth()+1;
				if(formatedMois<10) formatedMois="0"+formatedMois;
				a.href="javascript:document.getElementById('"+this.jourElement.id+"').value='"+formatedJour+"';document.getElementById('"+this.moisElement.id+"').value='"+formatedMois+"/"+date.getFullYear()+"';calendarPool.get("+this.poolIndex+").hide();";
				a.appendChild(document.createTextNode(date.getDate()));
				td.appendChild(a);
			}
			else{
				td.appendChild(document.createTextNode(date.getDate()));
			}
			if(date.getFullYear()==this.selectedAn&&date.getMonth()==(this.selectedMois-1)&&date.getDate()==this.selectedJour){
				addClass(td,"selected");
			}
			else{
				removeClass(td,"selected");
			}
			tr.appendChild(td);
			date.setTime(date.getTime()+DAY);
		}
	}
	// Modifie le lien up dans le div title en fonction du nouveau mois (et année)
	this.setUp=function(mois,an) {
		if(mois==undefined||an==undefined) {
			mois=this.selectedMois;
			an=this.selectedAn;
		}
		mois= Number(mois) + 1;
		if(mois>12){
			an++;
			mois=1;
		}
		if(this.isSelectable(mois,an)){
			this.a1Up.style.display="";
			this.a1Up.href="javascript:calendarPool.get("+this.poolIndex+").setMonth("+mois+","+an+")";
		}
		else{
			this.a1Up.style.display="none";
		}
	}
	// Modifie le lien down dans le div title en fonction du nouveau mois (et année)
	this.setDown=function(mois,an) {
		if(mois==undefined||an==undefined) {
			mois=this.selectedMois;
			an=this.selectedAn;
		}
		mois=Number(mois)-1;
		if(mois<1){
			an--;
			mois=12;
		}
		if(this.isSelectable(mois,an)){
			this.a1Down.style.display="";
			this.a1Down.href="javascript:calendarPool.get("+this.poolIndex+").setMonth("+mois+","+an+")";
		}
		else{
			this.a1Down.style.display="none";
		}
	}
	// Indique si le mois (et année) existe dans le sélecteur mois affecté au calendrier
	this.isSelectable=function(mois,an) {
		return ((an>this.minSelectableAn||(an==this.minSelectableAn&&mois>=this.minSelectableMois))&&(an<this.maxSelectableAn||(an==this.maxSelectableAn&&mois<=this.maxSelectableMois)));
	}
}


/**
* L'objet CalendarPool contient tous les calendriers créés sur la page, il est référencé dans la variable globale
* calendarPool
**/
function CalendarPool() {
	this.pool=new Array();
	this.add=function(calendar) {
		this.pool.push(calendar);
		return this.pool.length-1;
	}
	this.get=function(poolIndex) {
		return this.pool[poolIndex];
	}
	this.closeAll=function() {
		for(var i=0;i<this.pool.length;i++){
			this.pool[i].hide();
		}
	}
}
var calendarPool=new CalendarPool();

