if (window.Popup)
{
	Popup = window.Popup;
}
else
{

	// create the class popup
	Popup = Class.create()
	
	Popup.prototype.includedModulesJs = new Array();
	
	Popup.prototype.win = window;
	
	Popup.prototype.isDisplayed = false;
	
	// Popup constructor
	Popup.prototype.initialize = function(overlayId, options, win, force)
	{
		if (win != null)
			this.win = win;
		if (!this.win.top)
			return null;
		//this.includedModulesJs = new Array();
		if (!overlayId || overlayId == '')
			return;
			
		// comment to force if already exist
	/*	if (!force && top.$(overlayId))
			return;*/
	
		this.overlayId = overlayId;
		this.options =	{
							zIndex: 10000,
							toOpacity: 0.5,
							effectsDuration: 0.0,
							url: '',
							method: 'get',
							width: 400,
							maxHeight: '80%', // mustn't change
							height: '100%',
							top: '5%',
							parameters: {
								'ms': new Date().getTime()
							}
						};
		
		var displayClose = true;
		
		if (options && typeof (options) == 'object')
			for (var property in options)
			{
				if(property != "displayClose")
				{
					//alert(property);
					this.options[property] = options[property];
				}
				else
				{
					if(options[property] == false)
						displayClose = false;
				}
			}
				
		this.createOverlay();
		if(displayClose)
			this.createPopup();
		else
			this.createPopup(displayClose);
		this.createInfo();
	}
	
	// create the overlay
	Popup.prototype.createOverlay = function()
	{
		var overlay = null;
		if ((overlay = $(this.overlayId)) != null)
		{
			this.overlay = overlay;
			return;
		}
		overlay = new Element('div');
		overlay.writeAttribute('id', this.overlayId);
		overlay.setStyle({
			'position': 'fixed',
			'width': '100%',
			'height': '100%',
			'left': '0px',
			'top': '0px',
			'backgroundColor': 'black',
			'opacity': '0.3',
			'zIndex': this.options.zIndex
		});
		overlay.hide();
		
		this.overlay = overlay;
		
		$(document.body).insert(overlay);
	}
	
	Popup.prototype.createPopup = function(displayClose)
	{
		var popupBlock = null;
		var popupOverlay = null;
		var popupClose  = null;
		var popupBody = null;
		
		this.popupBlockId = this.overlayId + 'PopupBlock';
		if ((popupBlock = $(this.popupBlockId)) != null)
		{
			popupOverlay = $(this.overlayId + 'popupOverlay');
			popupBody = popupBlock.childElements()[2].down(1);
		}
		else
		{
			// popupBubbleTop
			var popupTop = new Element('div');
			var popupTopMiddle = new Element('div');
			popupTopMiddle.setStyle({
				'background': 'url("template/popin/top_middle.png") repeat-x',
				'position': 'relative',
				'margin': '0 11px',
				'height': '10px'
			});
			popupTop.insert(popupTopMiddle);
			var popupTopLeft = new Element('div');
			popupTopLeft.setStyle({
				'background': 'url("template/popin/top_left.png") no-repeat',
				'width': '11px',
				'height': '10px',
				'position': 'absolute',
				'left': '-11px'
			});
			popupTopMiddle.insert(popupTopLeft);
			var popupTopRight = new Element('div');
			popupTopRight.setStyle({
				'background': 'url("template/popin/top_right.png") no-repeat',
				'width': '11px',
				'height': '10px',
				'position': 'absolute',
				'right': '-11px'
			});
			popupTopMiddle.insert(popupTopRight);

			// popupMiddle
			var popupMiddleLeft = new Element('div');
			popupMiddleLeft.setStyle({
				'background': 'url("template/popin/middle_left.png") repeat-y',
				'height': '93%' // TODO: some hack, must find something better
			});
			var popupMiddleRight = new Element('div');
			popupMiddleRight.setStyle({
				'background': 'url("template/popin/middle_right.png") repeat-y right',
				'marginLeft': '11px',
				'height': '100%'
			});
			popupMiddleLeft.insert(popupMiddleRight);

			// popupBubbleBottom
			var popupBottom = new Element('div');
			var popupBottomMiddle = new Element('div');
			popupBottomMiddle.setStyle({
				'background': 'url("template/popin/bottom_middle.png") repeat-x',
				'position': 'relative',
				'margin': '0 11px',
				'height': '10px'
			});
			popupBottom.insert(popupBottomMiddle);
			var popupBottomLeft = new Element('div');
			popupBottomLeft.setStyle({
				'background': 'url("template/popin/bottom_left.png") no-repeat',
				'width': '11px',
				'height': '10px',
				'position': 'absolute',
				'left': '-11px'
			});
			popupBottomMiddle.insert(popupBottomLeft);
			var popupBottomRight = new Element('div');
			popupBottomRight.setStyle({
				'background': 'url("template/popin/bottom_right.png") no-repeat',
				'width': '11px',
				'height': '10px',
				'position': 'absolute',
				'right': '-11px'
			});
			popupBottomMiddle.insert(popupBottomRight);
			
			popupOverlay = new Element('div');
			if(Object.isUndefined(displayClose))
				popupClose = new Element('div');
			popupBlock = new Element('div');
			popupBody = new Element('div');
			popupBody.setStyle({
				'backgroundColor': 'white',
				'margin': '0 11px 0 0',
				'padding': '0',
				'height': '100%',
				'overflow': 'auto',
				'position': 'relative'
			});
			popupBlock.insert(popupTop);
			popupBlock.insert(popupMiddleLeft);
			popupMiddleRight.insert(popupBody);
			popupBlock.insert(popupBottom);
			popupBlock.insert(popupClose);
			popupOverlay.insert(popupBlock);
			$(document.body).insert(popupOverlay);
			
		}
		popupOverlay.writeAttribute('id', this.overlayId + 'popupOverlay');
		popupOverlay.style.position = 'fixed';
		popupOverlay.style.width = '100%';
		popupOverlay.style.height = '100%';
		popupOverlay.style.left = '0px';
		popupOverlay.style.top = '0px';
		popupOverlay.style.display = 'none';
		popupOverlay.style.opacity = '1.0';
		popupOverlay.style.zIndex = this.options.zIndex + 1;
		
		if(popupClose !== null)
		{
			popupClose.style.position = 'absolute';
			popupClose.style.top = '-10px';
			//popupClose.style.left = '0px';
			popupClose.style.right = '-10px';
			//popupClose.style.marginLeft = 'auto';
			//popupClose.style.marginRight = '0px';
			popupClose.style.width = '24px';
			popupClose.style.height = '24px';
			//popupClose.style.backgroundColor = 'red';
			popupClose.style.backgroundImage = 'url(template/popin/close.png)';
			popupClose.style.cursor = 'pointer';
			popupClose.onclick = function ()
			{
				this.hide();
			}.bind(this);
		}
		
		popupBlock.writeAttribute('id', this.popupBlockId);
		popupBlock.style.position = 'relative';
		popupBlock.style.marginLeft = 'auto';
		popupBlock.style.marginRight = 'auto';
		popupBlock.style.top = (Object.isString(this.options.top) ? this.options.top : this.options.top +'px');
		popupBlock.style.maxHeight = (Object.isString(this.options.maxHeight) ? this.options.maxHeight : this.options.maxHeight +'px');
		//popupBlock.style.width = (Object.isString(this.options.width) ? this.options.width : this.options.width +'px');
		//popupBlock.style.height = (Object.isString(this.options.height) ? this.options.height : this.options.height +'px');
		popupBlock.setStyle({
			'width': (Object.isString(this.options.width) ? this.options.width : this.options.width +'px'),
			'height': '60px'
		});
		popupBlock.hide();
		popupBody.update(
			new Element('div').setStyle({
				'width': '32px',
				'height': '32px',
				'margin': '10px auto'
			}).insert(new Element('img', {
				'width': 32,
				'height': 32,
				'src': 'img/loading.gif',
				'alt': 'chargement'
			}))
		);
		
		this.popupOverlay = popupOverlay;
		this.popupBlock = popupBlock;
		this.popupBody = popupBody;
	}
	
	Popup.prototype.createInfo = function()
	{
		if ($(this.overlayId +'InfoBlock') != null)
		{
			this.popupInfoBlock = $(this.overlayId +'InfoBlock');
			this.popupInfoBody = this.popupInfoBlock.down('div', 1);
			return;
		}
		var infoBlock = new Element('div');
		infoBlock.setAttribute('id', this.overlayId +'InfoBlock');
		infoBlock.style.padding = '5px 15px 5px 5px';
	
		var infoTop = new Element('b');
		infoTop.style.fontSize = '11px';
		infoTop.style.display = 'block';
		infoTop.style.overflow = 'hidden';
		
		var infoTop1 = new Element('b');
		infoTop1.style.background = '#ffde2a';
		infoTop1.style.display = 'block';
		infoTop1.style.overflow = 'hidden';
		infoTop1.style.height = '1px';
		infoTop1.style.margin = '0px 5px';
	
		var infoTop2 = new Element('b');
		infoTop2.style.background = '#ffd';
		infoTop2.style.display = 'block';
		infoTop2.style.borderColor = '#ffde2a';
		infoTop2.style.borderStyle = 'solid';
		infoTop2.style.borderWidth = '0px 2px';
		infoTop2.style.margin = '0px 3px';
		infoTop2.style.height = '1px';
		
		var infoTop3 = new Element('b');
		infoTop3.style.background = '#ffd';
		infoTop3.style.display = 'block';
		infoTop3.style.borderColor = '#ffde2a';
		infoTop3.style.borderStyle = 'solid';
		infoTop3.style.borderWidth = '0px 1px';
		infoTop3.style.margin = '0px 2px';
		infoTop3.style.height = '1px';
	
		var infoTop4 = new Element('b');
		infoTop4.style.background = '#ffd';
		infoTop4.style.display = 'block';
		infoTop4.style.borderColor = '#ffde2a';
		infoTop4.style.borderStyle = 'solid';
		infoTop4.style.borderWidth = '0px 1px';
		infoTop4.style.margin = '0px 1px';
		infoTop4.style.height = '2px';
		
		var infoBody = new Element('div');
		infoBody.style.background = '#ffd';
		infoBody.style.display = 'block';
		infoBody.style.borderColor = '#ffde2a';
		infoBody.style.borderStyle = 'solid';
		infoBody.style.borderWidth = '0px 1px';
		infoBody.style.padding = '10px';
		
		var infoBottom = new Element('b');
		infoBottom.style.fontSize = '11px';
		infoBottom.style.display = 'block';
		infoBottom.style.overflow = 'hidden';
		
		var infoBottom1 = new Element('b');
		infoBottom1.style.background = '#ffd';
		infoBottom1.style.display = 'block';
		infoBottom1.style.borderColor = '#ffde2a';
		infoBottom1.style.borderStyle = 'solid';
		infoBottom1.style.borderWidth = '0px 1px';
		infoBottom1.style.margin = '0px 1px';
		infoBottom1.style.height = '2px';
		
		var infoBottom2 = new Element('b');
		infoBottom2.style.background = '#ffd';
		infoBottom2.style.display = 'block';
		infoBottom2.style.borderColor = '#ffde2a';
		infoBottom2.style.borderStyle = 'solid';
		infoBottom2.style.borderWidth = '0px 1px';
		infoBottom2.style.margin = '0px 2px';
		infoBottom2.style.height = '1px';
	
		var infoBottom3 = new Element('b');
		infoBottom3.style.background = '#ffd';
		infoBottom3.style.display = 'block';
		infoBottom3.style.borderColor = '#ffde2a';
		infoBottom3.style.borderStyle = 'solid';
		infoBottom3.style.borderWidth = '0px 2px';
		infoBottom3.style.margin = '0px 3px';
		infoBottom3.style.height = '1px';
		
		var infoBottom4 = new Element('b');
		infoBottom4.style.background = '#ffde2a';
		infoBottom4.style.display = 'block';
		infoBottom4.style.overflow = 'hidden';
		infoBottom4.style.height = '1px';
		infoBottom4.style.margin = '0px 5px';
	
	
		this.popupInfoBlock = infoBlock;
		this.popupInfoBody = infoBody;
		
		infoTop.insert(infoTop1);
		infoTop.insert(infoTop2);
		infoTop.insert(infoTop3);
		infoTop.insert(infoTop4);
		infoBottom.insert(infoBottom1);
		infoBottom.insert(infoBottom2);
		infoBottom.insert(infoBottom3);
		infoBottom.insert(infoBottom4);
		infoBlock.insert(infoTop);
		infoBlock.insert(infoBody);
		infoBlock.insert(infoBottom);
		this.popupBlock.insert({'top': infoBlock});
		
		infoBlock.hide();
	}
	
	Popup.prototype.fillPopup = function(popupBody)
	{
		if (this.options.url && this.options.url != '')
		{
			
			new Ajax.Request(this.options.url, {
												asynchronous: false,
												parameters: this.options.parameters,
												method: this.options.method,
												evalJSON: false,
												evalJS: false,
												onSuccess: function(xhr)
												{
													//popupBody.innerHTML = xhr.responseText;
													try 
													{
														this.popupBlock.setStyle({
															'height': (Object.isString(this.options.height) ? this.options.height : this.options.height +'px')
														});
														this.setInnerHTML(popupBody, xhr.responseText);
													}
													catch(e)
													{
														alert("error in Popup.prototype.fillPopup:\n"+ Object.toJSON(e));
														this.cancel = true;
													}
												}.bind(this),
												onFailure: function(xhr)
												{
													this.cancel = true;
												}.bind(this)
											});
			
			//new Ajax.Updater(this, this.options.url);
		}
		else
			popupBody.innerHTML = 'Rien a afficher';
	}
	
	// show the popup.
	Popup.prototype.show = function()
	{
		this.isDisplayed = true;
		if (this.popupInfoBlock !== null)
			this.popupInfoBlock.hide();
		this.createPopup();
		this.overlay.show();
		this.popupOverlay.show();
		this.popupBlock.show();
		this.popupBody.show();
		this.cancel = false;
		this.fillPopup(this.popupBody);
		if (this.cancel)
			this.hide();
	}
	
	Popup.prototype.onEndFade = function(effect)
	{
		this.popupBody.innerHTML = '';
	}
	
	// hide the popup
	Popup.prototype.hide = function()
	{
		this.isDisplayed = false;
		this.popupOverlay.hide();
		this.overlay.hide();
		this.popupBody.update();
	}
	
	
	// 
	Popup.prototype.getContent = function(src)
	{
		if(!this.includedModulesJs[src])
		{
			var result = '';
			new Ajax.Request(src, {
				method: 'get',
				asynchronous: false,
				evalJS: false, 
				onSuccess: function(transport) {
					result = transport.responseText;
					return transport.responseText
				}
			});
			this.includedModulesJs[src] = true;
			return result;
		}
	}
	
	// SetInnerHTML Sécurisé
	
Popup.prototype.setInnerHTML = function(popupBody, /** String */HTML)
{
  popupBody.innerHTML = HTML;
  var AllScripts = popupBody.getElementsByTagName("script");
  for (var i = 0; i < AllScripts.length; i++) {
	 var s = AllScripts[i];
	 if (s.src && s.src != "")
	 {
	 	/*
	 	try
	 	{
	 		temp = this.getContent(s.src);
			this.win.eval(temp);
	 	}
	 	catch (e)
	 	{
	 		alert('src:'+ s.src +'\nerror:'+ Object.toJSON(e));
	 	}*/
	 	//loader.addJS(s.src);
	 }
	 else {
		try
		{
			this.win.eval(s.innerHTML);
		}
		catch(e)
		{
			//alert('error:'+ e +'\n'+ s.innerHTML);
			alert('error:'+ Object.toJSON(e));
		}
	 }
  }
}

Popup.prototype.fillInfo = function(datas)
{
	if (datas.blank())
	{
		if (this.popupInfoBlock.visible())
			new Effect.BlindUp(this.popupInfoBlock, { duration: this.options.effectsDuration, afterFinish: function(){ this.popupInfoBody.innerHTML = ''; }.bind(this)});
		else
			this.popupInfoBody.innerHTML = '';
	}
	else
	{
		this.popupInfoBody.innerHTML = datas;
		if (!this.popupInfoBlock.visible())
		{
//			alert('coucou');
			new Effect.BlindDown(this.popupInfoBlock, { duration: this.options.effectsDuration});
		}
	}
}

}
