var WindowModal = Class.create();

WindowModal.prototype = {
	
	sClassName: 'WindowModal',
	
	// ----- PROPERTIES -----
	
	iWidth: 	0,
	iHeight: 	0,
	sURL: 		null,
	
	oWindow: null,
	oWindowIFrame: null,
	
	// ----- CONSTRUCTOR -----
	
	initialize: function(iWidth, iHeight, sURL)
	{
		this.trace('CONSTRUCTOR()');
		
		//
		this.iWidth 	= iWidth;
		this.iHeight 	= iHeight;
		this.sURL 		= sURL;
		
		//
		this.oWindow = new Control.Modal(
			null,
			{
				afterOpen: this.onWindowAfterOpen.bind(this),
				className: 'modal',  
				fade: true,
				height: iHeight,
				iframe: true,
				overlayOpacity: 0.7,
				width: iWidth
			}
		);
		this.oWindow.href = sURL;
	},
	
	addEvent: function()
	{
		this.trace('addEvent()');
		
		var oWindowsIframeBody = Element.extend(this.oWindowIFrame.contentWindow.document.body);
		
		//
		if(oWindowsIframeBody == null)
		{
			this.trace('ERROR - NO BODY BUTTON');
			return(false);
			
			var oCloseBtnElement = null;
		}
		else
		{
			var oCloseBtnElement = oWindowsIframeBody.select('.lightbox-fermer')[0];
		}
		
		//
		if(oCloseBtnElement == null)
		{
			this.trace('ERROR - NO CLOSE BUTTON');
			return(false);
		}
		
		//
		oCloseBtnElement.observe('click', this.onCloseBtnClick.bind(this));
	},
	
	// ----- METHODS -----
	
	open: function()
	{
		this.trace('open()');
		
		this.oWindow.open();
	},
	
	close: function()
	{
		this.trace('close()');
		
		this.oWindow.close();
	},
	
	// ----- EVENTS -----
	
	//
	onWindowAfterOpen: function(event)
	{
		this.trace('onWindowAfterOpen()');
		
		//
		if(this.oWindow.container.select('iframe')[0] == null)
		{
			this.trace('ERROR - NO IFRAME');
			return(false);
		}
		
		this.oWindowIFrame = this.oWindow.container.select('iframe')[0];
		
		if(this.oWindowIFrame.contentWindow.document.body == null)
		{
			this.oWindowIFrame.observe('load', this.addEvent.bind(this));
		}
		else
		{
			this.addEvent();
		}
		
	},
	
	// 
	onCloseBtnClick: function(event)
	{
		this.trace('onCloseBtnClick()');
		
		//
		this.close();
	},
	
	// ----- MISC -----
	
	trace: function(objToTrace)
	{
		if(typeof(console) != 'undefined')
		{
			if(typeof(objToTrace) == 'string')
			{
				console.log('['+ this.sClassName +'] '+ objToTrace);
			}
			else
			{
				console.log('['+ this.sClassName +'] >');
				console.log(objToTrace);
			}
		}
	}
};
