var MW = (MW)?MW:{};
//Popup
MW.Popup_Class = function(){
	this.isOpen = false;
	this.oldContent = null;
	this.popupHistory = [];
	this.afterClosingFunction = null;
	this.isOppening = false;
	this.displayScroll = true;
	this.lastOverFlow = "";

	//OBSOLeTE / NE PAS UTILISER
	this.open = function (pPageID, pParams, pFunction)
	{
		MW.ScrollBar.hide();
		document.getElementById("PopupBack").style.top = document.body.scrollTop;
		document.getElementById("Popup").style.top = document.body.scrollTop+20;
		document.getElementById("PopupBack").style.display = "block";
		document.getElementById("PopupContent").innerHTML ="";

		document.getElementById("Popup").style.display = "block";
		this.afterClosingFunction = pFunction;
		MWMyPageService.popup(OwnerID, pPageID, pParams, this.onOpen);
	};
	
	
	
	//OBSOLeTE / NE PAS UTILISER
	this.showWithIFrame = function(iFrame, pFunction)
	{
		this.openWithContent(iFrame, pFunction);
	};
	
	//OBSOLeTE / NE PAS UTILISER
	this.openWithContent = function (pContent, pFunction)
	{
		
		//MW.ScrollBar.hide();
			
		document.getElementById("PopupBack").style.top = document.body.scrollTop;
		document.getElementById("Popup").style.top = document.body.scrollTop;
		document.getElementById("PopupBack").style.display = "block";
		document.getElementById("Popup").style.display = "block";
			
		this.afterClosingFunction = pFunction;
		this.onOpen(pContent);
	};
	
	//OBSOLeTE / NE PAS UTILISER	
	this.onOpen = function (pResult)
	{
		if(pResult.isInfoMsg){
			alert(pResult.infoMsg);
			if (!this.isOpen)
				MW.Popup.close();
		}
		else
		{
			var popupContent = jQuery("#PopupContainer");
			if(!popupContent) popupContent = jQuery("#PopupContent");
			
			// Gestion des plusieurs popup : l'ancien contenu est sauvegardé
			if (this.isOpen) this.oldContent = popupContent.html();
			else this.oldContent = null;
			this.isOpen = true;
			
			if(pResult.moduleInnerHTMLContent)
				popupContent.html(pResult.moduleInnerHTMLContent);
			else
			{
				popupContent.empty();
				if(pResult.indexOf)
					popupContent.html(pResult);
				else
				{
					pResult.show();
					popupContent.append(pResult);
				}
			}
			if(MW.Display) MW.Display.executeScript(popupContent);
		}
	};
	
	
	
	this.showWithHref = function(pHref, pFunction, pContainCloseButton)
	{
		pContent = jQuery("<iframe src='"+pHref+"' frameborder='0' width ='850px' height='500px'/>")
		this.show(pContent, pFunction, null);
	};
	

	
	this.show = function(pJQElement, pParams, pCaller, pContainCloseButton)
	{
		pJQElement = jQuery(pJQElement);
		if(!pJQElement || !pJQElement[0]);
			//alert('%DEVERROR_NO_POPUP_ELEMENT%');
		else if (!this.isOppening)
		{
			if (!this.displayScroll)
			{
				this.lastOverFlow = jQuery('body').css('overflow');
				jQuery('body').css('overflow','hidden');
			}
			this.isOppening = true;
			
			// Gestion des plusieurs popup : l'ancien contenu est sauvegardé
			var popupContent = jQuery("#PopupContainer");
			if (this.isOpen) this.popupHistory.push(popupContent.children());
			else this.popupHistory = [];
			this.isOpen = true;

			pJQElement = jQuery(pJQElement);
			//jQuery('#PopupBack').css({height:window.document.body.scrollHeight+'px'});
			jQuery('#PopupBack').css({height:'100%'});
			jQuery("#Popup").css("top", document.body.scrollTop);
			//document.getElementById("Popup").style.top = document.body.scrollTop;
			jQuery("#PopupBack").show();
			jQuery("#Popup").show();
			// Hide specific elements on page
			jQuery(".PopupHideOnShow").hide();
			
			if (jQuery("#Popup_CloseButtonLink").length > 0)
			{
				if (pContainCloseButton != null && pContainCloseButton == false)
					jQuery("#Popup_CloseButtonLink")[0].style.display = "none";
				else
					jQuery("#Popup_CloseButtonLink")[0].style.display = "block";
			}
						
			//WARNING !!!!! MW.Map VERY BAD
			if(MW.Map)
				document.getElementById("PopupBack").height = MW.Map.visibleHeight;
			
			var cloneJQElement = pJQElement;//.clone();
			cloneJQElement[0].params = pParams;
			cloneJQElement[0].initialPopup = pJQElement;
			cloneJQElement[0].initialPopupParent = pJQElement.parent();
			jQuery("#PopupContainer").empty().append(cloneJQElement);
			// ERRRRROOOOORRRRRR
			//pJQElement.remove();
			
			if(pParams){
				for(var id in pParams){
					var element;
					element = cloneJQElement.find("#"+id)[0];

					if(element){
						if(element.tagName.toUpperCase() == 'INPUT') element.value = pParams[id];
						else element.innerHTML = pParams[id];
					}
				}
			}
			jQuery("#PopupContainer").empty().append(cloneJQElement);
			this.isOppening = false;
			
			//INIT THE POPUP
			window.currentPopupInit = cloneJQElement;
			window.currentPopupCaller = pCaller;
			window.currentPopupParams = pParams;
			
			jQuery(cloneJQElement).show();

			
			
			if (cloneJQElement.find(".popupInitScript").length > 0)
				eval(cloneJQElement.find(".popupInitScript").text());
			if (pJQElement.find(".popupInitScript").length > 0)
				pJQElement.find(".popupInitScript").remove();
			if (cloneJQElement.find(".popupLoadScript").length > 0)
				eval(cloneJQElement.find(".popupLoadScript").text());
		}
	};
	
	
	
	this.close = function ()
	{
		var content = jQuery("#PopupContainer").children()[0];
		if(content){
			var initialPopupParent = content.initialPopupParent;
			var initialPopup = content.initialPopup;
			if (initialPopupParent)
			{
			 //   initialPopupParent.hide(); <- A ETE AJOUTER PAR JLE POUR FIXER BUG IE MAIS DONNE UNE PAGE BLANCHE
				initialPopupParent.append(initialPopup);
			}
			//Execute closing function		
			if(content.params && content.params.closeFunction) content.params.closeFunction();
		}				
		if(this.afterClosingFunction) this.afterClosingFunction();
		
		
		//if (this.oldContent == null)
		if (this.popupHistory.length == 0)
		{
			this.isOpen = false;
			//MW.ScrollBar.show();
			jQuery("#PopupBack").hide();
			jQuery("#Popup").hide();

			// Hide specific elements on page
			jQuery(".PopupHideOnShow").show();
			if (!this.displayScroll)
				jQuery('body').css('overflow',this.lastOverFlow);
		}
		else
		{
			this.isOpen = false;
			this.onOpen(this.popupHistory[0]);
			this.popupHistory.pop();
			this.isOpen = (this.popupHistory.length == 0);

			//this.isOpen = false;
			//this.onOpen(this.oldContent);
			//this.oldContent = null;
		}
	};
	this.hide = this.close;
	this.onValidation = function (pResult) {};
};
MW.Popup = new MW.Popup_Class();

