/** Generated by js.php */
/************************************************************************************************************
*	DHTML modal dialog box
*
*	Created:						August, 26th, 2006
*	@class Purpose of class:		Display a modal dialog box on the screen.
*
*	Css files used by this script:	modal-message.css
*
*	Demos of this class:			demo-modal-message-1.html
*
* 	Update log:
*
************************************************************************************************************/

/**
* @constructor
*/

DHTML_modalMessage = function()
{
	this.source								// source du contenu (chargé via Protoype::Ajax.Updater())
	this.content = ''
	this.width = '500'
	this.height = '300'
	this.title
	this.divs = {}
	this.tables = {}
}

DHTML_modalMessage.prototype = {

	setSource : function(source)
	{
		this.source = source;
	}

	,

	setContent : function(content)
	{
		this.content = content;
	}

	,

	setSize : function(width,height)
	{
		if(width)this.width = width;
		if(height)this.height = height;
	}

	,

	setId : function(id)
	{
		this.id = id
	}

	,

	setTitle : function(title)
	{
		this.title = title
	}

	,

	display : function(closeOthers)
	{
		// close all other windows
		if(closeOthers) this.closeOthers();

		// register window
		this.register()

		// load the content (if from URL, permits to retrieve a title in the content instead of as an argument)
		this.loadContent()

		// window nd page informations
		var pageInfos = this.getPageInfos()

		// desactivating scrolling
		// document.body.style.overflow = 'hidden'

		// elements creation
		this.tables.inner = document.createElement('TABLE')
		this.tables.outter = document.createElement('TABLE')

		if(window.dialogsCounter == 1) window.modalBackground = document.createElement('DIV')


		// inner table
		this.tables.inner.className = 'modalDialog_inner'

		this.tables.inner.style.width = this.width+'px'
		this.tables.inner.setAttribute('width',this.width)

		this.tables.inner.style.position = 'relative'

		var row = this.tables.inner.insertRow(0)

		if(this.title)
		{
			var td = row.insertCell(0)
			td.style.height = '20px'
			td.setAttribute('align','left')
			td.innerHTML = '<h2>'+ this.title + '</h2>'

			row = this.tables.inner.insertRow(1)
			var shift = '20'
		}
		else var shift = 0

		td = row.insertCell(0)
		td.setAttribute('valign','top')
		td.setAttribute('align','left')
		var contentTd = td;

		div  = document.createElement('DIV')
		div.style.width = '100%'
		div.style.height = '100%'
		div.style.overflowY = 'auto'
		div.style.overflowX = 'hidden'
		td.appendChild(div)

		// outter table
		this.tables.outter.style.position = 'absolute'
		this.tables.outter.style.width = '100%'
		this.tables.outter.style.height = pageInfos.windowHeight+'px'


		if (document.body && document.body.scrollTop)
		var scrollY = document.body.scrollTop;
		if (document.documentElement && document.documentElement.scrollTop)
		var scrollY = document.documentElement.scrollTop;
		if (window.pageYOffset)
		var scrollY = window.pageYOffset;

		if(!scrollY) var scrollY = 0
		this.tables.outter.className = 'modalDialog_outter'
		this.tables.outter.id = 'modalDialog_outter'
		this.tables.outter.style.top = scrollY+'px'
		this.tables.outter.style.left = '0px'

		// ajout du background grisé
		if(window.dialogs.length == 1)
		{
			window.modalBackground.className = 'modalDialog_background'
			window.modalBackground.style.top = this.tables.outter.style.top
			window.modalBackground.style.width = this.tables.outter.style.width
			window.modalBackground.style.height = this.tables.outter.style.height
		}

		row = this.tables.outter.insertRow(0)
		td = row.insertCell(0)

		td.setAttribute('align','center')
		td.id='greyedBackground'
		td.style.valign='middle'
		td.style.width = pageInfos.pageWidth
		td.style.height = '100%'

		td.appendChild(this.tables.inner)
		//this.tables.inner.setAttribute('ondblclick',"closeDialog('"+this.id+"')")

		document.body.appendChild(window.modalBackground)
		document.body.appendChild(this.tables.outter)

		// interception de la roulette de la souris
		if (window.addEventListener)
		/** DOMMouseScroll is for mozilla. */
		window.addEventListener('DOMMouseScroll', wheel, false);
		/** IE/Opera. */
		window.onmousewheel = document.onmousewheel = wheel;


		// now insert the content
		this.insertContent()

		// ajout des boutons de fermeture
		this.addButtons()
		
		// Ajuste la taille du dialog: si la taille du contenu
		// est inférieure à celle demandée, laisse comme c'est, sinon,
		// on rétrécit...
		if(Element.getHeight(this.tables.inner) > this.height) {
			this.tables.inner.setStyle({
				height: this.height + 'px'
			});
			contentTd.setAttribute('height',this.height-shift)
		}
	}

	,

	addButtons: function()
	{
		// pas de boutons
		if(this.dialogType == 'custom') return

		// ajout de la igne et de la cellule
		row = this.tables.inner.insertRow(this.tables.inner.rows.length)
		td = row.insertCell(0)

		// ajout de boutons
		switch(this.dialogType)
		{

			case 'confirm':
			td.setAttribute('align','center')
			td.innerHTML = "<input type='button' onClick=\"closeDialog('"+this.id+"')\" value=\""+_mlt("CANCEL","Annuler")+"\" /> <input type='button' onclick=\""+this.okAction+"\" value=\""+_mlt("VALIDATE","Valider")+"\" /><br /><br />"
			break

			case 'info':
			case 'infos':
			default:
			td.setAttribute('align','center')
			td.innerHTML = "<input type='button' onClick=\"closeDialog('"+this.id+"')\" value=\""+_mlt("CLOSE")+"\">"
			break
		}
	}
	,

	register : function()
	{
		if(!window.dialogs) {window.dialogs = []; window.dialogsCounter = 0 }

		window.dialogs[window.dialogs.length] = this
		window.activeDialog = this
		window.dialogsCounter++
	}

	,


	unregister : function(id)
	{
		var registered = []
		for(i=0;i< window.dialogs.length;i++)
		{
			if(window.dialogs[i].id != id) registered[registered.length] = window.dialogs[i]
		}
		window.dialogs = registered
		window.dialogsCounter--
		
		if(window.dialogsCounter) window.activeDialog = window.dialogs[window.dialogsCounter-1]
		else window.activeDialog = null

	}

	,

	close : function()
	{
		if(this.tables) Element.remove(this.tables.outter)
		else Element.remove(this.up('TABLE'))
		
		this.unregister(this.id)

		if(window.dialogsCounter == 0)
		{
			// reactivating scrolling
			if (window.addEventListener)
			/** DOMMouseScroll is for mozilla. */
			window.removeEventListener('DOMMouseScroll', wheel, false);

			/** IE/Opera. */
			window.onmousewheel = document.onmousewheel = null;
			
			// remove greyed background
			Element.remove(window.modalBackground)
		}
	}

	,

	closeOthers: function()
	{
		for(i in window.dialogs)
		{
			if(window.dialogs[i].close && i != this.id)  window.dialogs[i].close()
		}
	},
	insertContent: function()
	{
		// récupération de la cellule
		if(this.tables.inner.rows.length == 2) var cell = this.tables.inner.rows[1].cells[0]
		else var cell = this.tables.inner.rows[0].cells[0]
		
		// modifie cell pour pointer sur une div
		cell = Element.down(cell,'div')

		if(!this.silent && typeof(_mlt) != 'undefined') cell.innerHTML = "<center height='100%'>"+_mlt("LOADING","Chargement")+"...</center>"
		Element.update(cell,this.content);
	}
	,
	loadContent : function()
	{
		if(this.source)
		{
			var response = new Ajax.Request(this.source,{asynchronous:false,evalScripts:true,requestHeaders: {'X-Request-Type': 'dialog'}})
			var toto = response.transport.responseText
			this.parseURLResponse(toto)
		}

	},

	parseURLResponse : function(responseText)
	{
		// look for a H1 tag as a title for the modal
		var r = /<h1>(.*?)<\/h1>/img

		var title = r.exec(responseText)

		// dunno why, but i need to perform the regexp twice sometimes (one time in two actually) to catch the title correctly
		if(!title) title = r.exec(responseText)

		if(title)
		{
			this.title = title[1]
			// suppression de la balise h1
			this.content = responseText.replace(/<h1>(.*?)<\/h1>/img,'')
		}
		else this.content = responseText
	}
	,
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageInfos: 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;
		}
		var pageHeight, pageWidth;

		// 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;
		}

		return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight, xScroll:xScroll,yScroll:yScroll};
	}
}


// boite modale
dialog = function(id,options)
{
	var dlg = new DHTML_modalMessage()
	dlg.setId(id)
	if(options.size) dlg.setSize(options.size.split(',')[0],options.size.split(',')[1])
	else dlg.setSize(300,300)
	dlg.setTitle(options.title || '')
	if(options.source) dlg.setSource(options.source)
	else if(options.url) dlg.setSource(options.url)
	else if(options.content) dlg.setContent(options.content)
	dlg.silent = options.silent || false
	dlg.dialogType = options.type || 'custom'
	dlg.okAction = options.okAction || 'closeDialog('+id+')'
	return dlg
}

closeDialog = function(id)
{
	if(window.dialogs[id]) window.dialogs[id].close()
	else if(window.activeDialog) window.activeDialog.close()
}

closeAllDialogs = function()
{
	for(i in window.dialogs)
	{
		if(window.dialogs[i].close) window.dialogs[i].close()
	}
}

handle = function(delta) {}

/** Event handler for mouse wheel event.
*/

wheel = function(event)
{

	if (!event)	event = window.event;

	

	// plus de modale, on laisse filer l'événement
	if(!window.activeDialog)
	{
		event.returnValue = true;
		return true;
	}

	// obtention du sens de la roulette
	if (event.wheelDelta)
	{
		delta = event.wheelDelta/120;
		if (window.opera) delta = -delta;
	}
	else if (event.detail)
	{
		delta = -event.detail/3;
	}

	if(delta > 0) { up = true;down = false }
	else { up = false; down = true }

	// si on est au-dessus de la modale, on laisse filer l'évènement
	if(src = Event.element(event))
	{
		if(src.id == 'greyedBackground')
		{
			if (event.preventDefault) event.preventDefault();
			event.returnValue = false;
			return false
		}
		else
		{
			if(window.activeDialog.tables.inner.childNodes[0].rows.length > 1)
			{
				activeDiv = window.activeDialog.tables.inner.childNodes[0].rows[1].cells[0].childNodes[0]
			}
			else
			{
				activeDiv = window.activeDialog.tables.inner.childNodes[0].rows[0].cells[0].childNodes[0]
			}
			
			if(((activeDiv.scrollTop+activeDiv.offsetHeight - delta < activeDiv.scrollHeight) && down) || (up && (activeDiv.scrollTop > 0)))
			{
				event.returnValue = true;
				return true;
			}
			else
			{
				if (event.preventDefault) event.preventDefault();
				event.returnValue = false;
				return false
			}
		}
	}

	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
	return false;

}




