
var Display = {};

/**
 * Affichage d'un message d'alerte ou d'info
 * Exemple : new Display.Message("Votre message", {cssClass: "adm-alerte"});
 */
Display.Message = Class.create();
Display.Message.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(msg, options) {
		
		// Définition des parametres
		this.msg		= msg; // message HTML
		this.id_div		= 'message_alerte_123';
		this.id_cache	= 'cache-message';
		this.classCache = 'cache';
		this.cache		= false;
		this.id_ancre	= ''; // id de l'element ancre pour le positionnement
		this.plusTop	= 0;
		this.plusLeft	= 0;
		this.width		= 0;
		this.speed		= 0.4;
		this.duration	= 4; // duree d'affichage du message (en sec)
		this.cssClass	= 'alerte';
		this.options	= {};
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		this.open();

	}, // fin du constructeur

	/**
	 * Affichage du message
	 */
	open: function () {
		
		// déclaration des variables
		duration = this.duration;
		speed = this.speed;
		activecache = this.cache;
		//alert(activecache);
		var obj = this;
			
		// on regarde si le div d'alerte existe, si oui on l'efface
		if ($(this.id_div)) {
			$(this.id_div).remove(); 
		}
		
		// on regarde si le div d'alerte existe, si oui on l'efface
		if ($(this.id_cache)) {
			$(this.id_cache).remove();
		}
		
		// création et affichage du cache
		var cache = document.createElement("div");
		document.body.appendChild(cache);
		cache.className = this.classCache;

		if (activecache) {
			// on applique les attributs au div créé
			cache.id = this.id_cache;
			cache.style.display = "block";
			cache.style.height = (document.body.offsetHeight)+"px";
		}

		// on créé le div en DOM et on le place dans le body
		var o = document.createElement("div");
		document.body.appendChild(o);
		
		// on applique les attributs au div créé
		o.id = this.id_div;		
		o = $(this.id_div);		
		o.setStyle({
			display: "none",
			position: "absolute"
		});
		o.update(this.msg);
		
		// choix de la classe en fonction du type d'alerte
		o.className = this.cssClass;
		
		// on close l'alerte qd on clique dessus
		o.observe('click', function(event){
			new Effect.Fade(o, {delay: 0, duration: speed, queue : {position:'front', scope: 'msgscope'},beforeStart:function(){ if(activecache){ new Effect.Fade(cache, {delay: 0, duration: speed*3});}}});
		});
		
		// on place le div par rapport à l'ancre
		if ($(this.id_ancre)) {
			var ancre = $(this.id_ancre);		
			
			var pos = Position.positionedOffset(ancre);
			var top = pos[1];
			var left = pos[0];
			var width = ancre.getWidth();
			
			if (this.plusTop)
				o.style.top = (top + parseInt(this.plusTop)) + "px";
			else
				o.style.top = (top - 5) + "px";
			
			if (this.plusLeft)
				o.style.left = (left + parseInt(this.plusLeft)) + "px";
			else
				o.style.left = left + "px";
			
			if (this.width)
				o.style.width = (width - 34 + parseInt(this.width)) + "px";
			else
				o.style.width = (width - 34) + "px";
		} else {

			// on place au centre de la page
			if (Prototype.Browser.IE) {
				var screenH = document.documentElement.clientHeight;
				var screenW = document.documentElement.clientWidth;
			} else {
				var screenH = window.innerHeight;
				var screenW = window.innerWidth;
			}
			var posTop = (screenH / 2) - (o.getHeight() / 2) + document.documentElement.scrollTop;
			var posLeft = (screenW / 2) - (o.getWidth() / 2) + document.documentElement.scrollLeft;
			o.style.top = posTop + "px";
			o.style.left = posLeft + "px";
			
		} // fin du if
		
		// on affiche le message créé avec scriptaculous (fadeout au bout de 4sec)
		new Effect.Appear(o, {duration: speed, queue : {position:'front', scope: 'msgscope'} });
		new Effect.Fade(o, {delay: this.duration, duration: speed, queue : {position:'end', scope: 'msgscope'},
			beforeStart:function(){
				if (activecache) {
					new Effect.Fade(cache, {delay: duration, duration: speed*3});
				}
			}
		});
		
		
	} // fin de open
	
}; // fin de Display.Message


/**
 * Affichage d'un message d'attente
 * Exemple : new Display.Wait({msg: "Veuillez patienter", cssClass: "adm-wait", cache: true});
 */
Display.Wait = Class.create();
Display.Wait.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(options) {

		// definition des parametres
		this.msg		= 'Veuillez patienter ...'; // message HTML
		this.id_div		= 'adm-wait';
		this.id_cache	= 'cache-wait';
		this.cache		= false;
		this.classCache = 'cache';
		this.id_ancre	= ''; // id de l'element ancre pour le positionnement
		this.plusTop	= 0;
		this.plusLeft	= 0;
		this.width		= 0;
		this.speed		= 0.4;
		this.cssClass	= 'adm-wait';
		this.ajaxImg	= 'images/ajax-loader.gif';
		this.ajaxImgH	= 15;
		this.ajaxImgW	= 15;
		this.onFinish	= Prototype.emptyFunction;
		this.options	= {};

		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		this.open();

	}, // fin du constructeur

	/**
	 * Affichage du message
	 */
	open: function () {
		
		// déclaration des variables
		speed = this.speed;
		activecache = this.cache;
			
		// on regarde si le div d'alerte existe, si oui on l'efface
		if ($(this.id_div)) {
			$(this.id_div).remove(); 
		}
		
		// on regarde si le div de cache existe, si oui on l'efface
		if ($(this.id_cache)) {
			$(this.id_cache).remove();
		}
		
		// création et affichage du cache
		var c = document.createElement("div");
		document.body.appendChild(c); 
		c.className = this.classCache;
		
		if (activecache) {
			// on applique les attributs au div créé
			c.id = this.id_cache;
		}
		
		// on créé le div en DOM et on le place dans le body
		var o = document.createElement("div");
		document.body.appendChild(o);
		
		// on applique les attributs au div créé
		o.id = this.id_div;		
		o = $(this.id_div);
		
		o.setStyle({
			display: 'block',
			opacity: 0.999999,
			position: 'absolute'
		});
		o.update('<img src="' + this.ajaxImg + '" height="' + this.ajaxImgH + '" width="' + this.ajaxImgW + '" align="absbottom" /> ' + this.msg);
		
		// choix de la classe en fonction du type d'alerte
		o.className = this.cssClass;
		
		// on place le div par rapport à l'ancre
		if ($(this.id_ancre)) {
			var ancre = $(this.id_ancre);		
			
			var pos = Position.positionedOffset(ancre);
			var top = pos[1];
			var left = pos[0];
			var width = ancre.getWidth();
			
			if (this.plusTop)
				o.style.top = (top + parseInt(this.plusTop)) + "px";
			else
				o.style.top = (top - 5) + "px";
			
			if (this.plusLeft)
				o.style.left = (left + parseInt(this.plusLeft)) + "px";
			else
				o.style.left = left + "px";
			
			if (this.width)
				o.style.width = (width - 34 + parseInt(this.width)) + "px";
			else
				o.style.width = (width - 34) + "px";
		} else {

			// on place au centre de la page
			if (Prototype.Browser.IE) {
				var screenH = document.documentElement.clientHeight;
				var screenW = document.documentElement.clientWidth;
			} else {
				var screenH = window.innerHeight;
				var screenW = window.innerWidth;
			}
			var posTop = (screenH / 2) - (o.getHeight() / 2) + document.documentElement.scrollTop;
			var posLeft = (screenW / 2) - (o.getWidth() / 2) + document.documentElement.scrollLeft;
			o.style.top = posTop + "px";
			o.style.left = posLeft + "px";
			
		} // fin du if
		
		// on affiche le message créé avec scriptaculous
		//new Effect.Opacity(o, {duration:speed, from:0.0, to:1.0, queue: {position: 'front', scope: 'waitscope'}});

	}, // fin de open

	/**
	 * Effacage du message
	 */
	close: function (options) {

		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		var obj = this;
		
		var o = $(this.id_div);
		var c = $(this.id_cache);
		
		new Effect.Opacity(o, {duration: this.speed, from:1.0, to:0.0, queue: {position: 'end', scope: 'waitscope'},
		//new Effect.Fade(o, {duration: speed, queue : {position:'end', scope: 'waitscope'}, 
			afterFinish: function() {
				if (obj.cache) {
					c.setStyle({display: 'none'});
				}
				if (obj.options.onFinish) {
					obj.options.onFinish();
				}
				o.setStyle({display: 'none'});
			}
		});
	
	} // fin de close

}; // fin de Display.Wait


/**
 * Formulaire en superposition
 */
Display.DivForm = Class.create();
Display.DivForm.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(options) {

		// Définition des paramètres
		this.cacheId	= 'cache-form'; // id du cache
		this.classCache = 'cache';
		this.formId		= 'adm-cms-form'; // id du div contenant le form
		this.options	= {};

		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}
		
		//this.open();
		
	}, // fin du constructeur
	
	open: function () {
		// on se place en haut de la page
		self.scrollTo(0,0);
		
		// on créé un cache
		if ($(this.cacheId)) {
			$(this.cacheId).remove();
		}
		var cache = document.createElement("div");
		cache.id = this.cacheId;
		document.body.appendChild(cache);
		cache.className = this.classCache;

		cache = $(this.cacheId);
		
		// on créé le div du formulaire
		if ($(this.formId)) {
			$(this.formId).remove();
		}
		var form = document.createElement("div");
		form.id = this.formId;
		document.body.appendChild(form);
		
		form = $(this.formId);
		
		form.style.display = "block";
		form.style.opacity = 0.5;

		cache.style.height = document.body.offsetHeight + "px";
		cache.style.display = 'block';
		
		// on cache les elements qui passent au dessus du div
		this.hideTags(1);

		var obj = this;
		
		//new Effect.Scale(form.id, 100, {duration:0.3 ,scaleContent:false, scaleFrom:70, scaleFromCenter:true, afterFinish: function() {
		new Effect.Opacity(form, {duration:0.5, fps:25, from:0.5, to:1.0, afterFinish: function() {
			if (obj.options.onFinish) {
				obj.options.onFinish();
			}
		}});
		
	}, // fin de open
	
	/**
	 * Fermeture du div superposé
	 */
	close: function () {

		var cache = $(this.cacheId);
		var form = $(this.formId);
		
		form.update("");
		
		//new Effect.Shrink(formId, {duration: 0.7});
		new Effect.Opacity(form, {duration:0.5, fps:25, from:1, to:0.5, afterFinish: function() {
			form.style.display = "none";
			cache.style.display = 'none';			
		}});
	
		// on remet les elements effacés 
		this.hideTags(0);
		
	}, // fin de close
	
	/**
	 * Cachage ou affichage d'elements pour l'affichage d'une div en superposition
	 */
	hideTags: function (t) {
		var tags = new Array("applet", "iframe", "select");
	
		for (var k = tags.length; k > 0; ) {
			var ar = document.getElementsByTagName(tags[--k]);
			var cc = null;
	
			for (var i = ar.length; i > 0;) {
				cc = ar[--i];
	
				if (t==0) {
					cc.style.visibility = "visible";
				} else {
					cc.style.visibility = "hidden";
				}
			} // fin du for
		} // fin du for
	}
}; // fin de DivForm

/**
 * Affichage d'une popup
 */
Display.Popup = Class.create();
Display.Popup.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(url, options) {
		
		// Définition des paramètres
		this.url		= url;
		this.name		= 'popup'; 
		this.win		= null;
		this.pos		= 'center'; // postion centrée ou random
		this.width		= 500;
		this.height		= 500;
		this.scroll		= 'yes';
		this.resize		= 'yes';
		this.options	= {};
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		this.open();

	}, // fin du constructeur

	/**
	 * Affichage de la popup
	 */
	open: function () {
				
		if (this.pos=="random") {
			LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-this.width)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-this.height)-75)):100;
		}
		if (this.pos=="center") {
			LeftPosition=(screen.width)?(screen.width-this.width)/2:100;TopPosition=(screen.height)?(screen.height-this.height)/2:100;
		} else if ((this.pos!="center" && this.pos!="random") || this.pos==null){
			LeftPosition=0;
			TopPosition=20;
		}
		settings = 'width='+this.width;
		settings+= ',height='+this.height;
		settings+= ',top='+TopPosition;
		settings+= ',left='+LeftPosition;
		settings+= ',scrollbars='+this.scroll;
		settings+= ',location=no';
		settings+= ',directories=no';
		settings+= ',status=no';
		settings+= ',menubar=no';
		settings+= ',toolbar=no';
		settings+= ',resizable='+this.resizable;
		
		this.win = window.open(this.url, this.name, settings);
		this.win.focus(this.name);
		
	} // fin de open
	
}; // fin de Display.Popup

/**
 * Affichage d'une liste d'autocomplete sur un champ texte
 * Exemple : new Display.AutoComplete("search", {cssClass: "adm-alerte"});
 */
Display.AutoComplete = Class.create();
Display.AutoComplete.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(id, options) {
		
		// Définition des parametres
		this.id_container 	= id + '_container';
		this.id_id		 	= id + '_id';
		this.id_value		= id + '_value';
		this.id_loading		= id + '_loading';
		this.cssClass 		= 'autocomplete';
		this.url			= '_ajax/autoComplete.php';
		this.params 		= ''; // &param1=1&param2=2 ...
		this.minChars 		= 2;
		this.pe 			= null;
		this.index 			= -1;
		this.current 		= null;
		this.duration		= 0.25; // duree d'affichage (en sec)
		this.delaySearch	= 0.2; // temps maximum entre deux recherche
		this.peDelaySearch 	= null;
		this.appearContainer= null;
		this.onSearch		= Prototype.emptyFunction;
		this.onSelect		= Prototype.emptyFunction;
		this.options		= {};
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		this.elt = $(id);
		this.container = $(this.id_container);

		this.elt.observe('keyup', this.keyEvent.bind(this));
		this.elt.observe('focus', this.focusEvent.bind(this));
		this.elt.observe('blur',  this.blurEvent.bind(this));
		
		// on bloque la soumission du formulaire s'il y en a un
		if (this.elt.form) {
			var obj = this;			
			$(this.elt.form.id).observe('submit', function(event) {
				if (obj.container.visible()) {
					Event.stop(event);
				}
			});
		}

	}, // fin du constructeur

	/**
	 * 
	 */
	focusEvent: function(event)
	{
		this.search();
	},
	blurEvent: function(event)
	{
		if (this.current) {
			this.select(this.current.readAttribute("ac_id"), this.current.readAttribute("ac_value"));
			this.close();
		}
		
		this.pe = new PeriodicalExecuter(this.peEvent.bind(this), 0.15);
	},
	peEvent: function(pe)
	{
		this.close();
		this.pe = pe;
		this.pe.stop();
	},
	keyEvent: function(event)
	{
		switch (event.keyCode) {
			case Event.KEY_DOWN:
				this.upDown(1);
			break;
			case Event.KEY_UP:
				this.upDown(-1);
			break;
			case Event.KEY_RETURN:
				if (this.current) {
					this.select(this.current.readAttribute("ac_id"), this.current.readAttribute("ac_value"));
					this.close();
				}
			break;
			default:
				if (this.delaySearch != 0) {
					// on lance le delay entre 2 keydown
					if (this.peDelaySearch != null) {
						this.peDelaySearch.stop();
					}
					this.peDelaySearch = new PeriodicalExecuter(this.peDelaySearchEvent.bind(this), this.delaySearch);
				} else {
					this.search();
				}
		}
	},
	peDelaySearchEvent: function(pe)
	{
		this.search();
		this.peDelaySearch.stop();
		this.peDelaySearch = null;
	},

	upDown: function(cpt)
	{
		this.index += cpt;
		var cResult = this.container.select("a");
		if (this.index > cResult.size() -1 ) {
			this.index --;
		} else if (this.index < 0) {
			this.index = 0;
		}
		if (this.current) {
			this.current.removeClassName("current");
		}
		this.current = cResult.toArray()[this.index];
		this.current.addClassName("current");

		//this.select(this.current.readAttribute("ac_id"), this.current.readAttribute("ac_value"));
	},
	search: function()
	{
		if (this.options.onSearch) {
			this.params = this.options.onSearch();
		}
		
		if (this.pe) { this.pe.stop(); this.pe = null; }
		if ($(this.id_id)) { $(this.id_id).value = ""; }
		
		var s = this.elt.value;
		var obj = this;
		if (s.length >= this.minChars) {
			var url = this.url +  '&' + this.elt.id + '=' + encodeURIComponent(s) + this.params;
			//alert(url);
			if ($(this.id_loading)) { new Effect.Appear(this.id_loading, {duration: this.duration}); }
			new Ajax.Request(url, {
				method: 'get',
				asynchronous: false,
				onSuccess: function(transport) {
					obj.container.show();
					obj.container.update(transport.responseText);
					if ($(obj.id_loading)) { new Effect.Fade(obj.id_loading, {duration: obj.duration}); }
					
					// on compte le nb de résultats
					var cResult = obj.container.select("a");
					if (cResult.size() > 0) {
						var first = cResult.toArray()[0];
						if (cResult.size() == 1) {
							obj.index = 0;
							obj.upDown(0);
						}
						cResult.each( function(o) {
							o.observe('click',  obj.clickItem.bindAsEventListener(obj, o));
						});
					}
					
					// nb de lignes
					/*var cLine = this.container.select("div");
					var h = cLine.toArray()[0].getHeight();
					this.container.style.height = (cLine.size() * h) + "px";*/
					
					if (obj.appearContainer) { obj.appearContainer.cancel(); }					
					obj.appearContainer = new Effect.Opacity(obj.container, {from:0, to:1, duration: obj.duration, afterFinish: function() { obj.appearContainer = null; } });
					//this.container.show();
				}
			});			
			
		} else {
			this.close();
		}
	},
	clickItem: function(event)
	{
		if (this.pe) { this.pe.stop(); this.pe = null; }
		var data = $A(arguments);
		data.shift();
		var a = data[0];
		this.select(a.readAttribute('ac_id'), a.readAttribute('ac_value'));
		this.close();
	},
	select: function(id, value)
	{
		if (this.pe) { this.pe.stop(); this.pe = null; }
		if ($(this.id_id)) { $(this.id_id).value = id; }
		this.elt.value = value;
		if (this.options.onSelect) {
			this.options.onSelect();
		}
	},
	close: function()
	{
		new Effect.Fade(this.container, {duration: this.duration});//container.hide();
		this.index = -1;	
		this.current = null;
	}
	
}; // fin de Display.AutoComplete



// affiche une infobulle
function infoBulle(input,texte,classname,x,y)
{		
	// ****** gestion de la creation d'un div dynamique
	if (!$("infobulle_123")) {
		var o = document.createElement("div");
		//mon_div = $("div_phantomexpinfobulle");
		document.body.appendChild(o);
		o.id = "infobulle_123";
	}
	
	var o = $('infobulle_123');
	
	o.style.opacity = 0.1;
	o.style.position='absolute';
	
	new Effect.Opacity(o, {duration:0.5, fps:25, from:0, to:1.0});
	//new Effect.Appear(nouveauDiv.id, {duration:0.5});

	var pos = Position.positionedOffset(input);
	var top = pos[1];
	var left = pos[0];

	o.style.top = top + y + 'px';
	o.style.left = left + x + 'px';
	
	o.innerHTML = texte;
	o.className = classname;

	o.onmouseover=function(){
		o.style.display='block';
	}
}

function clearInfoBulle()
{
	if ($("infobulle_123")) {
		$("infobulle_123").remove(); 
	}
}



