//Lancement du script

if(typeof Prototype == 'undefined') {
	throw('IdepTool_infoBulle : Veuillez importer la classe "Prototype"');
}

Event.observe(window, 'load', idepTool_InfoBulleInit);
function idepTool_InfoBulleInit(){
	new IdepToolInfoBulle();
}

//Début de la classe

var IdepToolInfoBulle = Class.create();
IdepToolInfoBulle.prototype = {
	
	//Liste des variables
	var_ClassName 		: 'idepToolBulle',
	var_IdBulle			: 'idepToolBulle',
	var_Container		: 'container',
	var_x				: 0,
	var_y				: 0,
	image_dossier		: 'images/ideptool/',
	image_top			: 'bulle-top.png',
	image_bg			: 'bulle-bg.png',
	image_bottom		: 'bulle-bottom.png',
	image_rtop			: 'bulle-rtop.png',
	image_rbottom		: 'bulle-rbottom.png',	
	
	//Constructeur
	initialize : function(){
		var BulleElement = $$('.' + this.var_ClassName);
		if(BulleElement.length){
			BulleElement.each(
				function(item, index){
					Event.observe(item, 'mouseover'	, this.montrer.bind(this, item)	, false);
					Event.observe(item, 'mouseout'	, this.cacher.bind(this, item)	, false);
				}.bind(this)
			);
		}
	},
	
	//Fonction d'affichage
	
	montrer : function(item){
		this.checkPresenceDiv();
		new Element.update($(this.var_IdBulle + '_content'), item.getAttribute('title'));
		item.removeAttribute('title');
		document.onmousemove = this.suivre.bind(this, item);
		$(this.var_IdBulle).show();
		return false;
	},
	
	//Fonction de masquage
	
	cacher : function(item){
		item.setAttribute('title', $(this.var_IdBulle + '_content').innerHTML);
		$(this.var_IdBulle).hide();
	},
	
	//Création de la div
	
	checkPresenceDiv : function(){
		if(!$(this.var_IdBulle)){
			new Insertion.Before($(this.var_Container), '<div id="'+ this.var_IdBulle +'"><div id="'+ this.var_IdBulle +'_top">&nbsp;</div><div id="'+ this.var_IdBulle +'_content">&nbsp;</div><div id="'+ this.var_IdBulle +'_bottom">&nbsp;</div></div>');
			
			$(this.var_IdBulle).style.position 							= 'absolute';
			$(this.var_IdBulle).style.width 							= '107px';
			$(this.var_IdBulle).style.zIndex		 					= '99999999';
			$(this.var_IdBulle).style.margin		 					= '0';
			$(this.var_IdBulle).style.padding		 					= '0';
			$(this.var_IdBulle).style.fontWeight	 					= 'bold';
			$(this.var_IdBulle).style.color		 						= '#000000';
			$(this.var_IdBulle).style.fontSize		 					= '12px';
			$(this.var_IdBulle).style.fontFamily						= 'Arial';
			$(this.var_IdBulle).style.textAlign							= 'center';
			
			$(this.var_IdBulle + '_top').style.backgroundPosition		= 'top';
			$(this.var_IdBulle + '_top').style.backgroundRepeat			= 'no-repeat';
			
			$(this.var_IdBulle + '_content').style.backgroundImage		= 'url('+ this.image_dossier + this.image_bg +')';

			$(this.var_IdBulle + '_bottom').style.backgroundPosition	= 'bottom';
			$(this.var_IdBulle + '_bottom').style.backgroundRepeat		= 'no-repeat';
		}
	},
	
	//Fonction qui va suivre la souris
	
	suivre : function(item, e){
		
		var DocRef;
		if(e){
			this.var_x = e.pageX;
			this.var_y = e.pageY;
		}else{
			this.var_x = event.clientX;
			this.var_y = event.clientY;
			
			if( document.documentElement && document.documentElement.clientWidth){
				DocRef = document.documentElement;
			}else{
				DocRef = document.body;
			}
			
			this.var_x += DocRef.scrollLeft;
			this.var_y += DocRef.scrollTop;
		}
		
		var hauteur 	= Element.getHeight($(this.var_IdBulle));
		var position 	= Position.page(item);
	
		if((position[1] - hauteur -10) < 0){
			$(this.var_IdBulle).style.left							= parseInt(this.var_x) - 53 + 'px';
			$(this.var_IdBulle).style.top							= parseInt(this.var_y) + 20 + 'px';
			$(this.var_IdBulle + '_top').style.backgroundImage		= 'url('+ this.image_dossier + this.image_rtop +')';			
			$(this.var_IdBulle + '_top').style.height				= '16px';
			$(this.var_IdBulle + '_bottom').style.backgroundImage	= 'url('+ this.image_dossier + this.image_rbottom +')';
			$(this.var_IdBulle + '_bottom').style.height			= '7px';
			$(this.var_IdBulle + '_content').style.padding			= '5px 0px 0 0px';
		}else{	
			$(this.var_IdBulle).style.left							= parseInt(this.var_x) - 53 + 'px';
			$(this.var_IdBulle).style.top							= parseInt(this.var_y) - hauteur - 10 + 'px';
			$(this.var_IdBulle + '_top').style.backgroundImage		= 'url('+ this.image_dossier + this.image_top +')';
			$(this.var_IdBulle + '_top').style.height				= '7px';
			$(this.var_IdBulle + '_bottom').style.backgroundImage	= 'url('+ this.image_dossier + this.image_bottom +')';
			$(this.var_IdBulle + '_bottom').style.height			= '16px';
			$(this.var_IdBulle + '_content').style.padding			= '0 0px 5px 0px';
		}
	}	
}