/*-----------------------------------------------------------------------------------------------*/
//
// infobulle.js v1.0
//
// Copyright (c) 2008 Agence Clark (http://www.agence-clark.com)
//
/*-----------------------------------------------------------------------------------------------*/

var infoBulle = Class.create();
infoBulle.prototype =
{
	initialize : function(iB_cible,iB_contenu) {
		
		//-- Récupération des variables optionnelles --//
		var options = Object.extend({iB_cible:iB_cible}, arguments[1] || {});
		
		if(!options.iB_dim)			options.iB_dim = ''; // Dimension par defaut
		if(!options.iB_ajax)		options.iB_ajax = ''; // Ajax par defaut
		if(!options.iB_contenu)		options.iB_contenu = ''; // Contenu par defaut
		if(!options.iB_evt)			options.iB_evt = 'mouseover'; // Evenement par defaut
		if(!options.iB_css) 		options.iB_css = 'infobulle'; // CSS par defaut
		if(!options.iB_ancrage) 	options.iB_ancrage = 'LB'; // Ancrage par defaut
		if(!options.iB_fleche)		options.iB_fleche = false; // Fleche true/False
		if(!options.iB_offset)		options.iB_offset = {x:5,y:5}; // Offset par defaut
		if(!options.iB_zIndex)		options.iB_zIndex = 1; // zIndex par defaut
		
		this.iB_cible = iB_cible;
		this.iB_dim = options.iB_dim;
		this.iB_ajax = options.iB_ajax;
		this.iB_contenu = options.iB_contenu;
		this.iB_evt = options.iB_evt;
		this.iB_css = options.iB_css;
		this.iB_ancrage = options.iB_ancrage;
		this.iB_ancrageSave = this.iB_ancrage;
		this.iB_fleche = options.iB_fleche;
		this.iB_offset = options.iB_offset;
		this.iB_zIndex = options.iB_zIndex;
		
		this.iB_dimVP = document.viewport.getDimensions();
		
		this.creerBulle();
		
		this.afficheEvt = new Array(
			{element:this.iB_cible,evt:this.afficheBulle.bindAsEventListener(this, this.iB_cible)}
		);
		this.masqueEvt = new Array(
			{element:this.iB_cible,evt:this.masqueBulle.bindAsEventListener(this, this.iB_cible)}
		);
		
		switch(this.iB_evt){
			case 'click' :
				Event.observe(this.afficheEvt[0].element, 'click', this.afficheEvt[0].evt);
			break;
			default :
				Event.observe(this.afficheEvt[0].element, 'mouseover', this.afficheEvt[0].evt);
				Event.observe(this.masqueEvt[0].element, 'mouseout', this.masqueEvt[0].evt);
			break;
		}
	},
	
	creerBulle : function(){
		if(this.iB_contenu!=''){
			this.tpl='<div class="#{css}" id="etiquette_#{id}" style="display:none;">#{contenu}</div>';
		}
		if(this.iB_ajax!=''){
			this.tpl='<div class="#{css}" id="etiquette_#{id}" style="display:none;"><div id="ajax_#{id}"></div></div>';
		}
		this.valeur_template = {css:this.iB_css, id:this.iB_cible, contenu:this.iB_contenu};
		this.template = new Template(this.tpl);
		this.nouveau = this.template.evaluate(this.valeur_template);
		
		$(this.iB_cible).insert({after:this.nouveau});

		if(this.iB_zIndex!=1){
			$('etiquette_'+this.iB_cible).setStyle({
				zIndex : this.iB_zIndex
			});
		}

		if(this.iB_fleche==true){
			this.creerFleche();
		}
		
		if(this.iB_evt == 'click'){
			this.creerBtFermer();
		}
	},
	
	creerFleche : function(){
		$('etiquette_'+this.iB_cible).insert({bottom:'<div id="fleche_etiquette_'+this.iB_cible+'" class="fleche_'+this.iB_css+'_'+this.iB_ancrage.toLowerCase()+'"></div>'});
		$('fleche_etiquette_'+this.iB_cible).setStyle({
			position:'absolute',
			width:this.iB_offset.x+'px',
			height:this.iB_offset.y+'px'
		});
	},
	
	creerBtFermer : function(){
		$('etiquette_'+this.iB_cible).insert({top:'<a href="#" id="fermer_etiquette_'+this.iB_cible+'" class="fermer_'+this.iB_css+'">Fermer</a>'});
		this.fermerEvt = new Array(
			{element:'fermer_etiquette_'+this.iB_cible,evt:this.masqueBulle.bindAsEventListener(this, 'fermer_etiquette_'+this.iB_cible)}			
		);
		Event.observe(this.fermerEvt[0].element, 'click', this.fermerEvt[0].evt);		
	},
	
	xy : function(e){
		this.pX = Event.pointerX(e);
		this.pY = Event.pointerY(e);
		this.pos();
		//this.viewport();
	},
	
	pos : function(){
		if(this.iB_dim.width){
			$('etiquette_'+this.iB_cible).setStyle({
				width : this.iB_dim.width+'px'								   
			});
		}
		if(this.iB_dim.height){
			$('etiquette_'+this.iB_cible).setStyle({
				height : this.iB_dim.height+'px'
			});
		}
		this.w = $('etiquette_'+this.iB_cible).getWidth();
		this.h = $('etiquette_'+this.iB_cible).getHeight();

		switch(this.iB_ancrage){
			case "LB" : 
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-this.h-this.iB_offset.y-5+'px',
					left: this.pX+5+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						bottom:'-'+this.iB_offset.y+'px',
						top:'auto',
						left: '0px',
						right: 'auto'
					});
				}
			break;
			case "CB" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-this.h-this.iB_offset.y-5+'px',
					left: this.pX-(this.w/2)+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						bottom:'-'+this.iB_offset.y+'px',
						left: (this.w-this.iB_offset.x)/2+'px'
					});
				}
			break;
			case "RB" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-this.h-this.iB_offset.y+'px',
					left: this.pX-this.w+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						bottom:'-'+this.iB_offset.y+'px',
						right: '0px'
					});
				}
			break;
			case "LM" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-(this.h/2)+'px',
					left: this.pX+this.iB_offset.x+5+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						top: (this.h-this.iB_offset.y)/2+'px',
						left: '-'+this.iB_offset.x+'px'
					});
				}
			break;
			case "CM" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-(this.h/2)+'px',
					left: this.pX-(this.w/2)+'px'
				});
			break;
			case "RM" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY-(this.h/2)+'px',
					left: this.pX-this.w-this.iB_offset.x-5+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						top: (this.h-this.iB_offset.y)/2+'px',
						right: '-'+this.iB_offset.x+'px'
					});
				}
			break;
			case "LT" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY+'px',
					left: this.pX+this.iB_offset.x+5+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						top: '0px',
						left: '-'+this.iB_offset.x+'px',
						right: 'auto'
					});
				}
			break;
			case "CT" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY+this.iB_offset.y+15+'px',
					left: this.pX-(this.w/2)+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						top:'-'+this.iB_offset.y+'px',
						left: (this.w-this.iB_offset.x)/2+'px'
					});
				}
			break;
			case "RT" :
				$('etiquette_'+this.iB_cible).setStyle({
					top: this.pY+this.iB_offset.y+'px',
					left: this.pX-this.w+'px'
				});
				if(this.iB_fleche==true){
					$('fleche_etiquette_'+this.iB_cible).setStyle({
						top:'0px',
						right: '-'+this.iB_offset.x+'px',
						left: 'auto'
					});
				}
			break;
		}
	},
	
	ajax : function(){
		if(this.iB_ajax){
			$('ajax_'+this.iB_cible).update('<img src="'+this.iB_ajax.loading+'" alt="">');
			this.pos();
		}
		new Ajax.Request(this.iB_ajax.url,{
			onComplete: function(transport){
				$('ajax_'+this.iB_cible).update(transport.responseText);
				this.pos();
				//this.viewport();
			}.bind(this)
		});	
	},
	
	afficheBulle : function(e){
		switch(this.iB_evt){
			case 'click' :
				if(this.iB_ajax.url != ''){
					this.ajax();
				}
				this.xy(e);
				//this.viewport();
			break;
			default :
				Event.observe(this.iB_cible,'mousemove',function(e){
					this.xy(e);
					//this.viewport();
				}.bind(this));
			break;
		}
		$('etiquette_'+this.iB_cible).show();
	},
	
	masqueBulle : function(){
		//db('masque');
		$('etiquette_'+this.iB_cible).hide();
		this.iB_ancrage = this.iB_ancrageSave;
		if(this.iB_fleche==true){
			$('fleche_etiquette_'+this.iB_cible).className = '';
			$('fleche_etiquette_'+this.iB_cible).addClassName('fleche_'+this.iB_css+'_'+this.iB_ancrageSave.toLowerCase());
		}
		this.pos();
	},
	
	viewport : function(){
		this.alignH = this.iB_ancrage.substr(0,1);
		this.alignV = this.iB_ancrage.substr(1,2);
		
		//db(this.pY);
		//db($('etiquette_'+this.iB_cible).getHeight()+this.iB_offset.y);
		
		if($('etiquette_'+this.iB_cible).offsetTop < 0 && this.pY <  $('etiquette_'+this.iB_cible).getHeight()+this.iB_offset.y){
			this.alignV = 'T';
		}else{
			this.alignV = 'B';
		}
		
		/*else{
			if(this.iB_dimVP.height-$('etiquette_'+this.iB_cible).offsetTop < $('etiquette_'+this.iB_cible).getHeight()){
				this.alignV = 'B';
			}
		}
		if($('etiquette_'+this.iB_cible).offsetLeft<0){
			this.alignH = 'L';
		}
		if($('etiquette_'+this.iB_cible).offsetLeft>this.iB_dimVP.width){
			this.alignH = 'R';
		}
		*/
		this.iB_ancrage = this.alignH+this.alignV;
		if(this.iB_fleche==true){
			$('fleche_etiquette_'+this.iB_cible).className = '';
			$('fleche_etiquette_'+this.iB_cible).addClassName('fleche_'+this.iB_css+'_'+this.iB_ancrage.toLowerCase());
		}
		this.pos();
	}
};