
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	= 'adm-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;
		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;
		}
		
		// 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


// 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(); 
	}
}