//------------------------------------------------------------------//
//													 				//
//					 		ajaxPopup			 					//
//													 				//
//------------------------------------------------------------------//
// version							: 2.0.7							//
// Date de création					: 07/08/2008					//
// Dernière modification 			: 02/09/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 						//
//------------------------------------------------------------------//

var aoInstance 		= new Array();
var asTriggersId 	= new Array();
var asPopupStatus 	= new Array();

// lazyInit
function lazyInit(psInstancename, psParams, psTriggerId)
{
	if (asTriggersId.indexOf(psTriggerId) == -1)
	{
		var oParams 						= psParams;
		aoInstance[aoInstance.length] 		= new AjaxPopup(oParams, psInstancename);
		asTriggersId[aoInstance.length]		= psTriggerId;
	}	
}


function closeInstances()
{
	var i=0;
	var nLength = aoInstance.length;
	
	
	for(i=0; i<nLength; i++)
	{	
		if (aoInstance[i].getPopupStatus())
		{
			if (aoInstance[i].m_sId)
			{
				$(aoInstance[i].m_sId).hideElements();
				
				if ($(aoInstance[i].m_sId + '_blackscreen'))
					$(aoInstance[i].m_sId + '_blackscreen').hide();
			}	
				
			asTriggersId.without(aoInstance[i].m_sTrigger);
		}	
	}
}


AjaxPopup = Class.create();

AjaxPopup.prototype = 
{
	initialize: function(params, psInstancename)
	{
		var self = this;
		
		// Trigger
		this.m_sTrigger						= params.sTrigger;
		this.m_sTriggerAction				= params.sTriggerAction 			|| 'click';					
		
		// Global component ID
		this.m_sId							= params.sPopupId 					|| psInstancename;
		
		// Popup options
		this.m_sPopupCSS 					= params.sPopupCSS 					|| 'ajax_popup_basic';
		this.m_nPopupWidth					= params.nPopupWidth 				|| 350;
		this.m_nPopupHeight					= params.nPopupHeight 				|| 250;

		// Popup apparition options
		this.m_nPopupEffectDuration			= params.nPopupEffectDuration 		|| 1.5;
		this.m_nPopupOpacityFrom			= params.nPopupOpacityFrom 			|| 0.0;
		this.m_nPopupOpacityTo				= params.nPopupOpacityTo 			|| 1.0;
		
		// Top Bar options
		this.m_bHaveTopBar					= params.bHaveTopBar 				|| false;
		this.m_sTopBarCSS					= params.sTopBarCSS 				|| 'ajax_popup_tbar';
		this.m_sTopBarLabel					= params.sTopBarLabel 				|| 'Ajax popup';
		this.m_sTopBarLabelCSS				= params.sTopBarLabelCSS 			|| 'ajax_popup_tbar_title';
		
		// Close button options
		this.m_bHaveCloseButton				= params.bHaveCloseButton			|| true;
		this.m_sCloseButtonCSS				= params.sCloseButtonCSS			|| 'ajax_popup_close';
		
		// Content options
		this.m_bHaveAjaxContent				= params.bHaveAjaxContent			|| false;
		this.m_sContentURL					= params.sContentURL				|| 'undefined';
		this.m_sContentHTML					= params.sContentHTML				|| '';
		this.m_sContentCSS					= params.sContentCSS 				|| 'ajax_popup_content';
		
		// BlackScreen options
		this.m_bHaveBlackScreen				= params.bHaveBlackScreen			|| false;
		this.m_sBlackScreenCSS				= params.sBlackScreenCSS 			|| 'ajax_popup_blackscreen';
		this.m_nBlackScreenEffectDuration 	= params.nBlackScreenEffectDuration || 1.5;		// en secondes
		this.m_nBlackScreenOpacityFrom 		= params.nBlackScreenOpacityFrom 	|| 0.0;		// de 0.0 à 1.0
		this.m_nBlackScreenOpacityTo 		= params.nBlackScreenOpacityTo 		|| 0.75;	// de 0.0 à 1.0
		
		// Options usability
		this.m_bIsDraggable		 			= params.bIsDraggable 				|| false;
		this.m_bLoadOnStartup 				= params.bLoadOnStartup 			|| false;
		this.m_bCloseOnEscape 				= params.bCloseOnEscape 			|| true;
		this.m_bCloseOnExternalClick		= params.bCloseOnEscape 			|| true;
		
		// Loading 
		this.m_sLoadingCSS 					= params.sLoadingCSS				|| 'ajax_popup_loading';
		this.m_sLoadingPicture 				= params.sLoadingPicture			|| '';
		this.m_sLoadingText	 				= params.sLoadingText				|| 'Loading';
		
		this.m_bDebug	 					= params.bDebug						|| false;
		
		// Custom Ids (elements in content)
		this.m_sCustomCloseButtonId 		= params.sCustomCloseButtonId;

		
		// Elements IDs
		this.m_sIdTopBar					= this.m_sId + '_tbar';
		this.m_sIdBlackScreen				= this.m_sId + '_blackscreen';
		this.m_sIdCloseButton				= this.m_sId + '_close';
		this.m_sIdLoading					= this.m_sId + '_loading';
		this.m_sIdBody						= this.getBodyId();
		
		this.m_bMSIE6						= false /*@cc_on || @_jscript_version < 5.7 @*/;
		
		// DOM Elements to create			
		this.m_oBlackScreen;
		this.m_oPopup;
		this.m_oTopBar;
		this.m_oCloseButton;
		this.m_oContent;
		this.m_oLoading;
		
		// Element Body
		this.m_oBody						= document.body;

		// Element Creation tracking
		this.m_bIsCreatedBlackScreen		= false;
		this.m_bIsCreatedPopup				= false;
		this.m_bIsCreatedTopBar				= false;
		this.m_bIsCreatedCloseButton		= false;
		this.m_bIsCreatedContent			= false;
		this.m_bIsCreatedLoading			= false;
		
		// Element Appended tracking
		this.m_bIsAppendedBlackScreen		= false;
		this.m_bIsAppendedPopup				= false;
		this.m_bIsAppendedTopBar			= false;
		this.m_bIsAppendedCloseButton		= false;
		this.m_bIsAppendedContent			= false;
		this.m_bIsAppendedLoading			= false;
		
		// Popup displayed
		this.m_bIsDisplayedBlackScreen		= false;
		this.m_bIsDisplayedPopup			= false;
		
		this.oHideObject 					= function() { self.hideElements(); };
		this.oShowObject 					= function() { self.showElements(); };
		
		this.buildElements();
		this.appendElements();
		
		this.initEvents();
		
		if (this.m_bLoadOnStartup)
			this.showElements();
			
	},
	

	getBodyId: function()
	{
		this.m_sIdBody = document.body.id;

		if (!this.m_sIdBody)
		{
			document.body.id = 'body';
			this.m_sIdBody = document.body.id;
		}	
	},

	
	getPopupStatus: function()
	{
		return this.m_bIsDisplayedPopup;
	},
	
	
	buildElements: function()
	{
		if (this.m_bDebug)
			alert('buildElements : start');
			
		this.buildBlackScreen();
		this.buildPopup();
		this.buildTopBar();
		this.buildContent();
		this.buildLoading();
		
		if (this.m_bDebug)
			alert('buildElements : ok');
	},
	
	
	buildBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen && !this.m_bIsCreatedBlackScreen)
		{		
			if (this.m_bDebug)
				alert('buildBlackScreen : start');
			
			var anDimension = this.getPageSize();
			
			
			// creation de l'element DOM BlackScreen
			this.m_oBlackScreen = new Element('div', {'class': this.m_sBlackScreenCSS, 'id': this.m_sIdBlackScreen, 'style': 'width: ' + anDimension[0] + 'px; height: ' + anDimension[1] + 'px;'}).hide();
			this.m_oBlackScreen.style.zIndex = 500;
			
			// tracker create
			this.m_bIsCreatedBlackScreen = true;
			
			if (this.m_bDebug)
				alert('buildBlackScreen : ok');
		}	
	},
	
	
	buildPopup: function()
	{
		if (this.m_bDebug)
			alert('buildPopup : start');
		
		this.m_oPopup 				= new Element('div', {'id': this.m_sId, 'class': this.m_sPopupCSS}).hide();
		this.m_oContent 			= new Element('div', {'class': this.m_sContentCSS});	
	
		// tracker create
		this.m_bIsCreatedPopup 		= true;
		this.m_bIsCreatedContent 	= true;
		
		this.applyPopupPosition();
		
		
		if (this.m_bDebug)
			alert('buildPopup : ok');
	},
	
	
	buildTopBar: function()
	{
		// bouton fermer
		var oCloseButton 		= new Element('div', {'class':  this.m_sCloseButtonCSS, 'id': this.m_sIdCloseButton}).update('&nbsp;');
		
		if (this.m_bHaveTBar)
		{
			this.m_oTopBar	 	= new Element('div', {'class': this.m_sTopBarCSS, 'id': this.m_sIdTopBar});
			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_sTopBarLabelCSS}).update(this.m_sTopBarLabel);
			
			// assemblage
			oCellButton.appendChild(oCloseButton);
			oTr.appendChild(oCellButton);
			oCellTitle.appendChild(oBarTitle);
			oTr.appendChild(oCellTitle);
			oTBody.appendChild(oTr);
			oTable.appendChild(oTBody);

			this.m_oTopBar.appendChild(oTable);
			
			// tracker create
			this.m_bIsCreatedTopBar 		= true;
			this.m_bIsCreatedCloseButton 	= true;
		}
		else if (this.m_bHaveCloseButton)
		{
			this.m_oCloseButton 			= oCloseButton;
			
			// tracker create
			this.m_bIsCreatedCloseButton 	= true;
		}
		
		if (this.m_bDebug)
			alert('buildTopBar : ok');
	},
	
	
	buildContent: function(pbReload)
	{
		this.m_oContent 					= new Element('div', {'class': this.m_sContentCSS});	
		
		// tracker create
		this.m_bIsCreatedContent 			= true;
		
		
		if (this.m_bDebug)
			alert('buildContent : ok');
	},
	
	
	buildLoading: function()
	{
		this.m_oLoading 					= new Element('div', {'class': this.m_sLoadingCSS, 'id': this.m_sIdLoading});
		this.m_oLoading.hide();
		
		
		// Tracker Create
		this.m_bIsCreatedLoading			= true;
		
		if (this.m_sLoadingPicture.length > 0)
		{
			var oPicture 					= new Element('img', {'src': this.m_sLoadingPicture, 'alt': 'image Loading...'});
			this.m_oLoading.appendChild(oPicture);
		}	
		else
			this.m_oLoading.update(this.m_sLoadingText);
			
		this.applyLoadingPosition();	
		
		if (this.m_bDebug)
			alert('buildLoading : ok');
	},
	
	
	appendElements: function()
	{
		if (this.m_bDebug)
			alert('appendElements : start');
		
		if (this.m_bHaveBlackScreen)
		{
			this.m_oBody.appendChild(this.m_oBlackScreen);
			this.m_bIsAppendedBlackScreen = true;
		}	
		
		this.m_oBody.appendChild(this.m_oLoading);
		
		
		if (!this.m_bIsCreatedPopup)
		{
			alert('La popup n\'a pas été créée');
			exit;
		}
		
		if (this.m_bHaveTBar)
		{
			this.m_oPopup.appendChild(this.m_oTopBar);
			this.m_bIsAppendedTopBar 		= true;
			this.m_bIsAppendedCloseButton 	= true;
		}
		else if (this.m_bHaveCloseButton)
		{
			this.m_oPopup.appendChild(this.m_oCloseButton);
			this.m_bIsAppendedCloseButton 	= true;
		}
		
		this.m_oPopup.appendChild(this.m_oContent);
		
		//tracker append
		this.m_bIsCreatedLoading			= true;
		this.m_bIsAppendedContent 			= true;
		
		this.m_oBody.appendChild(this.m_oPopup);
		
		
		if (this.m_bDebug)
			alert('appendElements : ok');
	},
	
	
	showElements: function()
	{
		if (this.m_bDebug)
			alert('showElements : start');
		
		if (this.m_bDebug)
			alert('trigger cliqué');
			
		this.showLoading();
		this.showBlackScreen();
		this.loadContent();
		
		
		if (this.m_bDebug)
			alert('showElements : terminé');
	},
	
	
	showBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen)
		{
			if (this.m_bDebug)
				alert('showBlackScreen : start');
				
			if (!this.m_bMSIE6)
				Effect.Appear(this.m_sIdBlackScreen, { duration: this.m_nBlackScreenEffectDuration, from: this.m_nBlackScreenOpacityFrom, to: this.m_nBlackScreenOpacityTo});	
			else
			{
				$(this.m_sIdBlackScreen).style.display = 'block';	
				$(this.m_sIdBlackScreen).style.backgroundColor = 'transparent';
			}	
			
			// Tracker Display
			this.m_bIsDisplayedBlackScreen 		= true;
			
			if (this.m_bDebug)
			alert('showBlackScreen : ok, idDisplayed : ' + this.m_bIsDisplayedBlackScreen);
		}
	},
	
	
	showLoading: function()
	{
		if (!this.m_bMSIE6)
			Effect.Appear(this.m_sIdLoading, { duration: this.m_nBlackScreenEffectDuration, from: this.m_nBlackScreenOpacityFrom, to: this.m_nBlackScreenOpacityTo});	
		else
			$(this.m_sIdLoading).style.display = 'block';
		
		if (this.m_bDebug)
			alert('showLoading : start');
	},
		
	
	
	showPopup: function()
	{
		if (!this.m_bIsDisplayedPopup)
		{
			var nTopPosition 			= window.pageYOffset || document.documentElement.scrollTop 	|| 0; 
			var nLeftPosition 			= window.pageXOffset || document.documentElement.scrollLeft || 0;
			
			var nScreenWidth 			= document.viewport.getDimensions().width;
			var nScreenHeight 			= document.viewport.getDimensions().height;
			
			this.m_oPopup.style.top 	= nTopPosition + (nScreenHeight / 2) + 'px';
			this.m_oPopup.style.left 	= nLeftPosition + (nScreenWidth / 2) + 'px';
			
			
			if (!this.m_bMSIE6)
				Effect.Appear(this.m_sId, { duration: this.m_nPopupEffectDuration, from: this.m_nPopupOpacityFrom, to: this.m_nPopupOpacityTo});	
			else
				$(this.m_sId).style.display = 'block';
				
				
			this.m_bIsDisplayedPopup	= true;
		}
		
		
		if (this.m_bDebug)
			alert('showPopup : end');
	},
	
	loadContent: function()
	{
		if (this.m_bDebug)
			alert('loadContent : start');
		
		if (this.m_bHaveAjaxContent)
			this.getAjaxContent();
		else
			this.getHTMLContent();	
			
			
		if (this.m_bDebug)
			alert('loadContent : ok');
	},

	
	getAjaxContent: function()
	{
		
		if (this.m_bDebug)
			alert('getAjaxContent : start');
		
		var self = this;
		
		if (!this.m_bHaveAjaxContent)
			alert('Aucune URL spécifiée !');
		else
		{
			new Ajax.Request
			(	
				this.m_sContentURL, 
				{
					onSuccess: function(transport) 
					{
						self.getHTMLContent(transport.responseText);
			  		},
			  		asynchronous: true,
			  		evalJS: true
				}
			); 
		}			
		
		if (this.m_bDebug)
			alert('getAjaxContent : ok');
	},
	
	
	getHTMLContent: function(psContent)
	{
		if (psContent)
			this.m_sContentHTML = psContent;
		
		if (!this.m_bIsAppendedContent)
			alert('Contenu non appended');
			
		this.m_oContent.update(this.m_sContentHTML);
		
		this.hideLoading();
		this.showPopup();
		
		
		// Initialise l'eventobserve sur le bouton close dans le contenu
		var self = this;
		if ($(this.m_sCustomCloseButtonId))
		{
			Event.observe(
				self.m_sCustomCloseButtonId,
				'click',
				this.oHideObject
			);
		}
		
		if (this.m_bDebug)
			alert('getHTMLContent : ok');
	},
	
	
	hideElements: function()
	{
		this.hideBlackScreen();
		this.hidePopup();
		
		if (this.m_bDebug)
			alert('hideElements : end');
	},
	
	
	hideBlackScreen: function()
	{
		if (this.m_bHaveBlackScreen && this.m_bIsDisplayedBlackScreen)
		{
			this.m_oBlackScreen.hide();
			
			// Tracker Display
			this.m_bIsDisplayedBlackScreen 	= false;
		}	
		
		if (this.m_bDebug)
			alert('hideBlackScreen : ok');
	},
	
	
	hidePopup: function()
	{
		if (this.m_bIsDisplayedPopup)
		{
			$(this.m_sId).hide();
			
			// Tracker Display
			this.m_bIsDisplayedPopup	= false;
		}	
		
		if (this.m_bDebug)
			alert('hidePopup : end');
	},
	
	
	hideLoading: function()
	{
		this.m_oLoading.hide();
		
		if (this.m_bDebug)
			alert('hideLoading : end');
	},
	
	
	initEvents: function()
	{
		//draggable
		if (this.m_bIsDraggable)
		{
			new Draggable(this.m_sId);
		}	
		
		
		var self = this;
		Event.observe(
			self.m_sTrigger,
			self.m_sTriggerAction,
			function()
			{
				self.showElements();
			}	
		);
		
		
		if (this.m_bIsCreatedCloseButton)
		{
			Event.observe(
				self.m_sIdCloseButton,
				'click',
				this.oHideObject
			);
		}
		
		if(this.m_bCloseOnEscape)
		{
			this.observeKeyboard();
		}
		
		
		if (this.m_bCloseOnExternalClick)
		{
			Event.observe(
				self.m_sIdBlackScreen,
				'click',
				function()
				{
					if (self.m_bIsDisplayedPopup)
						this.oHideObject
				}
			);
		}	
		
		
		if (this.m_bDebug)
			alert('initEvents : ok');
		
	},
	
	closeOnExternalClick: function()
	{
		
	},
	
	
	observeKeyboard: function()
	{
		var self = this;
		Event.observe(
			window,
			'keypress',
			function(poEvent)
			{
				var sKeyCode = poEvent.keyCode;
				if (sKeyCode == 27)
				{
					self.hideElements();
				}
			}	
		);
		
		if (this.m_bDebug)
			alert('observeKeyboard : end');
	},
	
	
	applyPopupPosition: function()
	{
		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 + 'px';
		this.m_oPopup.style.marginTop		= (this.m_nPopupHeight / 2) * -1 + 'px';
		this.m_oPopup.style.width			= this.m_nPopupWidth + 'px';
		this.m_oPopup.style.height			= this.m_nPopupHeight + 'px';
		this.m_oPopup.style.zIndex			= 1000;
	},
	
	
	applyLoadingPosition: function()
	{
		var nLoadingWidth;
		var nLoadingHeight;
		
		nLoadingWidth 	= this.m_oLoading.getWidth();
		nLoadingHeight 	= this.m_oLoading.getHeight();
		
		this.m_oLoading.style.position 		= 'absolute';
		this.m_oLoading.style.left 			= '50%';
		this.m_oLoading.style.top 			= '50%';
		this.m_oLoading.style.marginLeft	= (nLoadingWidth / 2) * -1 + 'px';
		this.m_oLoading.style.marginTop		= (nLoadingHeight / 2) * -1 + 'px';
		this.m_oLoading.style.zIndex		= 800;
	},
	
		//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	getPageSize: function()
	{
	
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
		return arrayPageSize;
	}
}