//------------------------------------------------------------------//
//													 				//
//					 		ajaxPopup			 					//
//													 				//
//------------------------------------------------------------------//
// Date de création					: 13/03/2008					//
// Dernière modification 			: 13/03/2008					//
// Auteur 							: Johan Leche					//
// Propriétaire						: Keyrio SARL					//
// Contact							: j.leche@keyrio.fr				//
// Web								: http://www.keyrio.fr			//
//------------------------------------------------------------------//
//																	//
// Dépendances :			- prototype.js 		version 1.6			//
// 							- scriptaculous.js						//
// 							- ajaxpopup.css 						//
//------------------------------------------------------------------//

AjaxPopup = Class.create();

AjaxPopup.prototype = 
{
	initialize: function(params)
	{
		// declencheur
		this.m_sTrigger						= params.sTrigger;
		this.m_sTriggerAction				= params.sTriggerAction 			|| 'click';					
		
		// paramètres du composant
		this.m_sId							= params.sPopupId 					|| 'id_' + Math.ceil(Math.random());
		
		// parametres popup principale
		this.m_sPopupStyle 					= params.sPopupStyle 				|| 'ajax_popup_basic';
		this.m_nPopupWidth					= params.nPopupWidth 				|| 350;
		this.m_nPopupHeight					= params.nPopupHeight 				|| 500;
		this.m_bForceFit					= params.bForceFit 					|| false;
		this.m_nPopupOpacityFrom			= params.nPopupOpacityFrom 			|| 0.0;
		this.m_nPopupOpacityTo				= params.nPopupOpacityTo 			|| 1.0;
		this.m_nPopupEffectDuration			= params.nPopupEffectDuration 		|| 1.5;
		
		// parametres barre de titre
		this.m_sTBarStyle					= params.sBarStyle 					|| 'ajax_popup_tbar';
		this.m_bHaveTBar					= params.bHaveTitleBar 				|| false;
		this.m_sTBarLabel					= params.sTitleLabel 				|| 'Ajax popup';
		this.m_sTBarLabelStyle				= params.sBarTitleStyle 			|| 'ajax_popup_tbar_title';
		this.m_sCloseButtonStyle			= params.sCloseButtonStyle			|| 'ajax_popup_close';
		this.m_bHaveCloseButton				= params.bHaveCloseButton			|| true;
		
		this.m_bCloseonButtonOnly		 	= params.bCloseonButtonOnly 		|| false;
		
		this.m_bIsDraggable		 			= params.bIsDraggable 			|| false;
		
		// parametres contenu popup
		this.m_sContentStyle				= params.sContentStyle 				|| 'ajax_popup_content';
		this.m_sContent						= params.sContent;
		
		// parametres blackScreen
		this.m_sBlackScreenStyle			= params.sBlackScreenStyle 			|| 'ajax_popup_blackscreen';
		this.m_bHaveBlackScreen				= params.bHaveBlackScreen			|| false;
		this.m_nBlackScreenOpacityFrom 		= params.nBlackScreenOpacityFrom 	|| 0.0;	// de 0.0 à 1.0
		this.m_nBlackScreenOpacityTo 		= params.nBlackScreenOpacityTo 		|| 0.75;	// de 0.0 à 1.0
		this.m_nBlackScreenEffectDuration 	= params.nBlackScreenEffectDuration || 1.5;	// en secondes
		
		
		// IDs
		this.m_sIdTitleBar					= this.m_sId + '_tbar';
		this.m_sIdBlackScreen				= this.m_sId + '_blackscreen';
		this.m_sIdCloseButton				= this.m_sId + '_close';
		this.m_sIdBody						= document.body.id;

		if (!this.m_sIdBody)
		{
			document.body.id = 'body';
			this.m_sIdBody = document.body.id;
		}	
		
		this.m_oPopup;
		this.m_oTopBar;
		this.m_oContent;
		this.m_oBlackScreen;
		this.m_oCloseButton;
		
		this.m_bPopupIsActive				= false;
		this.m_bPopupIsBuilt				= false;
		this.m_nFalseClick					= 0;
		
		// on construit les éléments 	
		if (!this.m_bPopupIsBuilt)	
			this.buildPopup();
		
		// on lance les listeners
		this.initEvents();
	},
	
	
	buildPopup: function()
	{
		this.m_oPopup 			= new Element('div', {'id': this.m_sId, 'class': this.m_sPopupStyle}).hide();
		this.m_oContent 		= new Element('div', {'class': this.m_sContentStyle}).update(this.m_sContent);
		this.buildTopBar();
		this.buildBlackScreen();
		
		this.collatePopup();
		
		this.applyPositionement();
	},
	
	
	collatePopup: function()
	{
		if (this.m_bHaveTBar)
		{
			this.m_oPopup.appendChild(this.m_oTopBar);
		}
		else if (this.m_bHaveCloseButton)
		{
			this.m_oPopup.appendChild(this.m_oCloseButton);
		}
		
		this.m_oPopup.appendChild(this.m_oContent);
		
		var oBody = document.body;
		
		if (this.m_bHaveBlackScreen)
		{
			oBody.appendChild(this.m_oBlackScreen);
		}	
		
		oBody.appendChild(this.m_oPopup);
		
		//draggable
		if (this.m_bIsDraggable	)
			new Draggable(this.m_sId);
	},
	
	
	buildTopBar: function()
	{
		// bouton fermer
		
		var oCloseButton 	= new Element('div', {'class':  this.m_sCloseButtonStyle, 'id': this.m_sIdCloseButton});
		
		if (this.m_bHaveTBar)
		{
			this.m_oTopBar	 	= new Element('div', {'class': this.m_sTBarStyle, 'id': this.m_sIdTitleBar});
			var oTable 			= new Element('table', {'width': '100%', 'cellspacing': 0, 'cellpadding': 0});
			var oTBody			= new Element('tbody');
			var oTr				= new Element('tr');
			var oCellTitle		= new Element('td');
			var oCellButton		= new Element('td');
			
			// titre de la popup
			var oBarTitle		= new Element('div', {'class': this.m_sTBarLabelStyle}).update(this.m_sTBarLabel);
			
			// assemblage
			
			oCellButton.appendChild(oCloseButton);
			oTr.appendChild(oCellButton);
			
			oCellTitle.appendChild(oBarTitle);
			oTr.appendChild(oCellTitle);
			
			oTBody.appendChild(oTr);
			
			oTable.appendChild(oTBody);
			
			this.m_oTopBar.appendChild(oTable);
		}
		else if (this.m_bHaveCloseButton)
		{
			this.m_oCloseButton = oCloseButton;
		}
		
	},
	
	
	buildBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen)
		{
			this.m_oBlackScreen = new Element('div', {'class': this.m_sBlackScreenStyle, 'id': this.m_sIdBlackScreen}).hide();
			this.m_oBlackScreen.style.zIndex = 500;
		}	
	},
	
	
	changePopupStatus: function(pbStatus)
	{
		this.m_bPopupIsActive = pbStatus;
	},
	
	
	runBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen)
		{
			Effect.Appear(this.m_sIdBlackScreen, { duration: this.m_nBlackScreenEffectDuration, from: this.m_nBlackScreenOpacityFrom, to: this.m_nBlackScreenOpacityTo});	
		}
	},
	
	
	removeBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen)
		{
			Effect.Fade(this.m_sIdBlackScreen);
		}	
	},
	
	
	runPopup: function()
	{
		Effect.Appear(this.m_sId, { duration: this.m_nPopupEffectDuration, from: this.m_nPopupOpacityFrom, to: this.m_nPopupOpacityTo});	
	},
	
	
	removePopup: function()
	{
		$(this.m_sId).hide();
	},
	
	
	launchPopup: function()
	{
		this.runPopup();
		this.runBlackScreen();
		this.m_bPopupIsActive = true;
	},
	
	
	hidePopup: function()
	{
		this.m_bPopupIsActive = false;
		this.removeBlackScreen();
		this.removePopup();
		this.m_nFalseClick = 0;
	},
	
	
	initEvents: function()
	{
		var self = this;
		Event.observe(
			self.m_sTrigger,
			self.m_sTriggerAction,
			function()
			{
				self.launchPopup();
			}	
		);
		
		if(this.m_bCloseOnOutsideClick)
		{
			Event.observe(
				this.m_sIdBody,
				'click',
				function()
				{
					if (self.m_bPopupIsActive)
					{
						if (self.m_nFalseClick > 1)
						{
							self.hidePopup();
						}
						
						self.m_nFalseClick++;
					}
				}
			);
		}
		
		
		if (this.m_bHaveTBar || this.m_bHaveCloseButton)
		{
			Event.observe(
				this.m_sIdCloseButton,
				'click',
				function()
				{
					if (self.m_bPopupIsActive)
					{
						self.hidePopup();
					}
				}
			);
		}
	},
	
	
	applyPositionement: function()
	{
		this.m_nPopupWidth;
		this.m_nPopupHeight;
		
		this.m_oPopup.style.position 		= 'absolute';
		this.m_oPopup.style.left 			= '50%';
		this.m_oPopup.style.top 			= '50%';
		this.m_oPopup.style.marginLeft		= (this.m_nPopupWidth / 2) * -1;
		this.m_oPopup.style.marginTop		= (this.m_nPopupHeight / 2) * -1;
		this.m_oPopup.style.width			= this.m_nPopupWidth;
		this.m_oPopup.style.height			= this.m_nPopupHeight;
		this.m_oPopup.style.zIndex			= 1000;
	}
}