////////////////////////////////////////////////////////////////////////////
//
// © PMP CONCEPT 2007 :: GESTION DES POPUPS
//
////////////////////////////////////////////////////////////////////////////

/* inclusion des dépendances */
PMP.include("librairiesjs/popup.js");
PMP.include("librairiesjs/ajax.js");


// Classe PopupStack
// 
//
PMP.util.pmpPopupStack = function(parameters) 
{
	if(!PMP.common.isObject(parameters))
	{
		if(!PMP.common.isUndefined(parameters))
			var parameters = {content:parameters};
		else
			var parameters = {};
	}

	PMP.util.pmpPopupStack.baseConstructor.call(this, parameters.content); // ou PMP.util.pmpPopupStack.superclass.constructor.call(this, element);
	
	this.items = new Array();
	this.index = 0;
	this.width = 0;
	this.height = 0;
	this.minWidth = 0;
	this.minHeight = 0;
	this.maxWidth = 0;
	this.maxHeight = 0;
	this.scrolling = false;

	for(var param in parameters)
	{
		switch(param)
		{
			case 'content':
				this.addContent(parameters.content);
				break;

			case 'contentURL':
				this.addContentURL(parameters.contentURL);
				break;

			case 'url':
				this.addURL(parameters.url);
				break;

			case 'width':
				this.width = parameters.width;
				break;

			case 'height':
				this.height = parameters.height;
				break;

			case 'minWidth':
				this.minWidth = parameters.minWidth;
				break;

			case 'minHeight':
				this.minHeight = parameters.minHeight;
				break;

			case 'maxWidth':
				this.maxWidth = parameters.maxWidth;
				break;

			case 'maxHeight':
				this.maxHeight = parameters.maxHeight;
				break;

			case 'scrolling':
				this.scrolling = parameters.scrolling;
				break;

			default:
				break;
		}
	}
}

var pmpPopupStackExtendTentatives = 0;
function pmpPopupStackExtend()
{
	if(!PMP.util.pmpPopup)
	{
		if(pmpPopupStackExtendTentatives<10)
			window.setTimeout("pmpPopupStackExtend();", 500);
		pmpPopupStackExtendTentatives++;
		return;
	}
		
	PMP.common.extend(	PMP.util.pmpPopupStack, 
						PMP.util.pmpPopup,
						{
							addContent : function(content)
							{
								var popupItem = {content:content.toString()};
								this.items.push(popupItem);
							},
							
							addContentURL : function(contentURL)
							{
								var popupItem = {contentURL:contentURL.toString()};
								this.items.push(popupItem);
							},
							
							addURL : function(url)
							{
								var popupItem = {url:url};
								this.items.push(popupItem);
							},
							
							gotoURL : function(url, index)
							{
								if(PMP.common.isUndefined(index))
									var index = this.index;
								else if(!PMP.common.isNumber(index) || index>=this.items.length)
									return;
								
								if(!PMP.common.isString(url) || url.trim()=="")
									url = this.items[index]['url'];
								
								// ajout du paramètre ajax dans l'url
								url = PMP.url.rewrite({ajax:1}, url);
								
								var loader = document.createElement("div");
								loader.setAttribute("id", "popupLoader" + index);
								loader.setAttribute("class", "pmpPopupLoader");
								loader.id = "popupLoader" + index;
								loader.className = "pmpPopupLoader";
								
								var iFrameContainer = document.createElement("div");
								iFrameContainer.setAttribute("style", "background-color:#ffffff;");
								iFrameContainer.style.backgroundColor = "#ffffff";
								
								var iFrame = document.createElement("iframe");
								var iFrameId = this.popup.id+"Iframe"+this.index;
								iFrame.setAttribute("name", iFrameId);
								iFrame.name = iFrameId;
								iFrame.setAttribute("id", iFrameId);
								iFrame.id = iFrameId;
								iFrame.setAttribute("style", "visibility:hidden; display:none;");
								iFrame.style.visibility = "hidden";
								iFrame.style.display = "block";
								iFrame.setAttribute("scrolling", this.scrolling ? "yes" : "no");
								iFrame.style.scrolling = this.scrolling ? "yes" : "no";
								//iFrame.setAttribute("allowTransparency", "true");
								//iFrame.allowTransparency = true;
								iFrame.setAttribute("src", url);
								
								var evt = new PMP.util.pmpEvent();
								var __popup = this;

								// ajustement des dimensions de la popup
								if(this.width!=0 && this.height!=0)
								{
									iFrame.style.width = this.width+"px";
									iFrame.style.height = this.height+"px";
								}
								
								evt.addEventListener(	iFrame, 
														"load", 
														function()
														{
															// masque le loader
															loader.style.visibility = "hidden";
															
															// ajustement des dimensions de la popup
															if(__popup.width!=0 && __popup.height!=0)
															{
																iFrame.style.width = __popup.width+"px";
																iFrame.style.height = __popup.height+"px";
															}
															else
															{
																PMP.common.autoSize(iFrameId);
																if(__popup.minWidth>0 && PMP.common.getElementWidth(iFrame)<__popup.minWidth)
																	iFrame.style.width = __popup.minWidth+"px";
																if(__popup.minHeight>0 && PMP.common.getElementHeight(iFrame)<__popup.minHeight)
																	iFrame.style.height = __popup.minHeight+"px";
																if(__popup.maxWidth>0 && PMP.common.getElementWidth(iFrame)>__popup.maxWidth)
																	iFrame.style.width = __popup.maxWidth+"px";
																if(__popup.maxHeight>0 && PMP.common.getElementHeight(iFrame)>__popup.maxHeight)
																	iFrame.style.height = __popup.maxHeight+"px";
															}

															// positionnement de la popup à l'écran (par défaut centrée)
															__popup.updatePosition();
															
															// affichage de l'iFrame
															iFrame.style.visibility = "visible";
															iFrame.style.display = "block";
															//iFrame.style.overflow = "visible";
															iFrame.style.overflow = "auto";

														}	);
								

								this.content.appendChild(loader);
								iFrameContainer.appendChild(iFrame);
								this.content.appendChild(iFrameContainer);
								iFrame.src = url;

								this.items[index] = {url:url, content:""};
							},
							
							show : function(popupItem)
							{
								this.renderItem(popupItem);
								
								PMP.util.pmpPopupStack.superclass.show.call(this);
							},
							
							prev : function ()
							{
								this.gotoIndex(this.index - 1);
							},
							
							next : function ()
							{
								this.gotoIndex(this.index + 1);
							},
							
							gotoIndex : function (index)
							{
								if(index>=0 && index<this.items.length)
								{
									this.index = index;
									this.renderItem(this.items[this.index]);
									/*
									if(this.items[this.index].content && this.items[this.index].content.trim()!="")
										this.popup.innerHTML = this.items[this.index].content;
									else if(this.items[this.index].url && this.items[this.index].url.trim()!="")
									{
										this.gotoURL();
									}
									*/
								}
							},
							
							renderItem : function (popupItem)
							{
								if(PMP.common.isUndefined(popupItem))
								{
									if(this.index>=0 && this.index<this.items.length)
										var popupItem = this.items[this.index];
									else
										return;
								}

								if(popupItem.content && popupItem.content.trim()!="")
									this.popup.innerHTML = popupItem.content;
								else if(popupItem.contentURL && popupItem.contentURL.trim()!="")
								{
									var ajax = new PMP.util.pmpAjax(popupItem.contentURL, "GET");
									ajax.appendData("ajax", "1");
									var retour = ajax.send();
									this.popup.innerHTML = retour.responseText;
								}
								else if(popupItem.url && popupItem.url.trim()!="")
								{
									this.gotoURL(popupItem.url);
								}
							}
						}
					 );
}

pmpPopupStackExtend();