/*--------------------------------------------------------------------------*/
/*	LoadingIndicator	
*	This is a script for creating modal dialog windows (like the ones your operating
*	system uses)
*	
*/

var LoadingIndicator = {
	/* hideAll - ferme toutes les fenetres LoadingIndicator ouvertes */		
	hideAll: function(){				
		if ($('LoadingIndicatorDiv')){
			Element.hide('LoadingIndicatorDiv');
			Element.remove('LoadingIndicatorDiv');
		}	
		if ($('LoadingIndicatorOverlay')){
			Element.remove('LoadingIndicatorOverlay');
		}
	}
}
LoadingIndicator.base = Class.create();
LoadingIndicator.base.prototype = {
    
	initialize: function(options){
		//start by hiding all lightboxes				
		LoadingIndicator.hideAll();	 
		
		var imgUrl;	  
	  var allScripts = document.getElementsByTagName("script"); // Récupère tous les noeuds <script> du document courant 
	  for (var i = 0 ; i < allScripts.length ; i++) { // Pour chacun des noeuds...	  	
	  	var currentScript = allScripts.item(i);
	  	if (currentScript.src	&& /loadingIndicator.js\?imgUrl=(.*)/.test(currentScript.src)) { 	  		
	  		var param = currentScript.src.match(/loadingIndicator.js\?imgUrl=(.*)/); // ...on capture de tout ce qu'il y a après 'imgUrl='	  		
	  		// param[1] contient tout ce qu'il y a après 'imgUrl=' 	  	
	  		imgUrl = param[1];
	  	}
	  } 
			  	  
		this.options = Object.extend({																				
			isModal: true,
			overlayOpacity: 0.0,
			effectDuration: 0.1,
			width: '160px',
			height: '33px',
			bgcolor: '#F0F0F0',
			bordercolor: '#B3B3B3',
			fcolor: '#000000',
			ffamily: 'Arial, Helvetica, sans-serif',
			fsize: '11px',
			message: 'Veuillez patienter...',
			pic: 'ajax-loading.gif'
		}, options || {} )
							
		// cree le div qui contiendra le flux
		this.container = document.createElement('div');
		this.container.style.display = 'none';
		this.container.style.padding = 0;
  	this.container.style.margin = 0;    
  	this.container.style.background = 'transparent';
  	this.container.style.border = 0;	
		this.container.id = 'LoadingIndicatorDiv';			
						
		new Insertion.Top(this.container, '<img id="LoadingIndicatorDivImg" src="' + imgUrl + this.options.pic + '" width="31" height="31" alt="" title="" />&nbsp;&nbsp;&nbsp;' + this.options.message);
					
		Element.setStyle(this.container,'width:'+this.options.width);
		Element.setStyle(this.container,'height:'+this.options.height);
		Element.setStyle(this.container,'background-color:'+this.options.bgcolor);
		Element.setStyle(this.container,'border:1px solid '+this.options.bordercolor);
		Element.setStyle(this.container,'color:'+this.options.fcolor);
		Element.setStyle(this.container,'font-family:'+this.options.ffamily);
		Element.setStyle(this.container,'font-size:'+this.options.fsize);
		Element.setStyle(this.container,'font-weight:normal');		
				
		document.body.appendChild(this.container);
		
		Element.setStyle($('LoadingIndicatorDivImg'),'display:inline;vertical-align:middle;');
						
		//cree un overlay
		if (this.options.isModal)	{
			new Insertion.Before(this.container, "<div id='LoadingIndicatorOverlay' style='display:none;'></div>");
			Element.setStyle($('LoadingIndicatorOverlay'), 'padding:0; margin:0; background:transparent; border:0;position:fixed;');
			Element.setStyle($('LoadingIndicatorOverlay'), 'position:absolute;top:0;left:0;width:100%;height:100%;z-index:90;background-color:#000;');			
		}
								
		Event.observe(window,'resize', this.center.bindAsEventListener(this));		
				
		// L'objet devient déplacable
		/*new Draggable(this.container,{
			// Après un déplacement, l'objet se replace
			revert:true
		});*/
		
		this.show();
														
	},
	
	show : function(){		
	  this.center();	 	   	  	     				     	
 		if (this.options.isModal) {
   		 Effect.Appear($('LoadingIndicatorOverlay'), {	   	
		   	duration:this.options.effectDuration,	
		   	from:0.0,
		   	to:this.options.overlayOpacity,	   	
		   	afterFinish: function() {		  
   				Element.show($('LoadingIndicatorDiv'));   	   	   
		   	},
		   	queue: {position:'end', scope: 'LoadingIndicatorscope'} 	
		   });
 		 }		
	   return false;
	},
	
	hideBox : function(evt){			
		LoadingIndicator.hideAll();		
		return false;
	},
		
	center : function(){
		var my_width  = 0;
		var my_height = 0;		
		if(Prototype.Browser.IE) {			
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;			
		}
		else {
			if ( typeof( window.innerWidth ) == 'number' ){
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
			}else if ( document.documentElement && 
					 ( document.documentElement.clientWidth ||
					   document.documentElement.clientHeight ) ){
				my_width  = document.documentElement.clientWidth;
				my_height = document.documentElement.clientHeight;
			}
			else if ( document.body && 
					( document.body.clientWidth || document.body.clientHeight ) ){
				my_width  = document.body.clientWidth;
				my_height = document.body.clientHeight;
			}
		}				
		
		Element.setStyle(this.container, 'position:absolute; z-index:99;');		
		
		var scrollY = 0;
		
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
		
		var elementDimensions = Element.getDimensions(this.container);

		
		var setX = ( my_width  - elementDimensions.width  ) / 2;
		var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
		
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
		
		Element.setStyle(this.container,'left:'+setX+'px; top:'+setY+'px;');		
		
	}
	
}