function viewModal(elm, redirection) {
	var redirection = (redirection == undefined || redirection == true) ? true : false;
	if($(elm)) {
		modal.prototype.initialize({
			elm: $(elm).innerHTML,
			parsing: true,
			redirection: redirection
		});
	}
	else {
		modal.prototype.initialize({
			ajax: elm,
			parsing: false,
			redirection: redirection
		});
	}
}
var modal = Class.create();
modal.prototype = {
	initialize: function(value) {
		if(value == undefined) return;
		this.parsing = (value.parsing == true) ? true : false;
		this.redirection = value.redirection;
		this.ie6 = (typeof document.body.style.maxHeight == 'undefined') ? true : false;

		if($('myModal')) {
			$('myModal').hide();
			(value.ajax) ? this.request(value.ajax) : this.updater(value.elm);
			$('myModal').show();
			this.position();
		}
		else {
			this.builder();
			(value.ajax) ? this.request(value.ajax) : this.updater(value.elm);
			this.showModal();
			this.position();
		}
	},
	builder: function() {
		document.modalRedirection = this.redirection;
		Utils.hideSelectBoxes();
		Utils.flash();
		var modal = Builder.node('div', {
			id: 'myModal',
			style: 'display: none'
		},
		[
			Builder.node('div', {
				id: 'myModal-content'
			}),

			Builder.node('a', {
				id: 'myModal-close',
				href: 'javascript: void(0);'
			}, 'X')
		]);

		var loader = Builder.node('div', {
			id: 'myModal-loader',
			style: 'display: none'
		});

		var overlay = Builder.node('div', {
			id: 'pageOverlay',
			style: 'display: none'
		});
		document.body.appendChild(modal);
		document.body.appendChild(loader);
		document.body.appendChild(overlay);
		var pageSize = Utils.getPageSize();
		$('pageOverlay').setStyle({
			width: pageSize[0] + 'px',
			height: pageSize[1] + 'px'
		});
		$('myModal-loader').setStyle({
			top: (pageSize[3] / 2) - 50 + 'px',
			left: (pageSize[2] / 2) - 50 + 'px'
		});
		new Effect.Appear('pageOverlay', {
			duration: 0.2,
			from: 0.0,
			to: 0.7,

			afterFinish: function() {
				$('myModal-loader').show();
			}
		});
	},
	request: function(url) {
		new Ajax.Request(url, {
			method: 'post',
			asynchronous: false,
			encoding: 'utf-8',
			onSuccess: function(transport) {
				this.updater(transport.responseText);
			}.bind(this)
		});
	},
	updater: function(content) {
		$('myModal-content').update(content);
		if(this.parsing == true) {
			$('myModal-content').descendants().each(function(e) {
				if(e.id) e.id = (e.id == 'content' || e.id == 'close') ? 'myModal-' + e.id + '2' : 'myModal-' + e.id;
			});
		}

		if($('modalRedir') != undefined) {
			$('modalRedir').value = 'http://' + window.location.hostname + window.location.pathname;
		}
	},
	position: function() {
		if($('myModal') == undefined) return;
		if(this.ie6 == true) document.location.href = '#';

		var modalSize = $('myModal').getDimensions();
		var pageSize = Utils.getPageSize();
		var x = (pageSize[2] - modalSize.width) / 2;
		var y = (pageSize[3] - modalSize.height) / 2;
		var xLoader = x + (modalSize.width / 2.5);
		var yLoader = y + 50;

		$('myModal').setStyle({
			left: x + 'px',
			top: y + 'px'
		});

		$('myModal-loader').setStyle({
			left: xLoader + 'px',
			top: yLoader + 'px'
		});

		$('pageOverlay').setStyle({
			width: pageSize[0] + 'px',
			height: pageSize[1] + 'px'
		});

		if($('myModal').getElementsByTagName('form')[0] != undefined) {
			var form = $('myModal').getElementsByTagName('form')[0];

			for(var i = 0; i < form.getElementsByTagName('input').length; i++) {
				if(form.getElementsByTagName('input')[i].type != 'hidden') {
					form.getElementsByTagName('input')[i].focus();
					break;
				}
			}
		}
	},

	showModal: function() {
		$('myModal').show();

		Event.observe(window, 'resize', this.position);
		Event.observe($('pageOverlay'), 'click', this.hideModal.bindAsEventListener(this));
		Event.observe($('myModal-close'), 'click', this.hideModal.bindAsEventListener(this));
	},

	hideModal: function() {
		new Effect.Fade('pageOverlay', {
			duration: 0.2,

			beforeStart: function() {
				$('myModal-loader').remove();
				$('myModal').remove();
			},

			afterFinish: function() {
				$('pageOverlay').remove();
			}
		});

		Utils.showSelectBoxes();
		Utils.flash({ display: 'visible' });
	}
};

/**/

/****************
*	Script :			Modal.js (version 2)
*	Auteur :			Steven BUTTARAZZI
*	Date de création :		Juin 2008
*
*	Script testé sous IE v6/v7, Firefox v2/v3, Safari v3, Opera v9.
****************/

/*
	Méthode : viewModal
		viewModal(elm, redirection);

	Fonction : affiche la modal selon le contenu injecté
		- Elm : est une url ou un id
		- redirection : est un boolean. Selon que redirection est à true ou false, la redirection de la dite modal  (si tant est qu'il y en est une) devient inactive
*/
function voirModale(elm, redirection) {
	var redirection = (redirection == undefined || redirection == true) ? true : false;

	if($(elm)) {
		modale.prototype.initialize({
			elm: $(elm).innerHTML,
			parsing: true,
			redirection: redirection
		});
	}
	else {
		modale.prototype.initialize({
			ajax: elm,
			parsing: false,
			redirection: redirection
		});
	}
}

var modale = Class.create();

modale.prototype = {
	initialize: function(value) {
		if(value == undefined) return;
		this.parsing = (value.parsing == true) ? true : false;
		this.redirection = value.redirection;
		this.ie6 = (typeof document.body.style.maxHeight == 'undefined') ? true : false;

		if($('myModal')) {
			$('myModal').hide();
			(value.ajax) ? this.request(value.ajax) : this.updater(value.elm);
			$('myModal').show();
			this.position();
		}
		else {
			this.builder();
			(value.ajax) ? this.request(value.ajax) : this.updater(value.elm);
			this.showModal();
			this.position();
		}
	},

	builder: function() {
		document.modalRedirection = this.redirection;
		Utils.hideSelectBoxes();
		Utils.flash();

		var modal = Builder.node('div', {
			id: 'myModal',
			style: 'display: none'
		},
		[
			Builder.node('div', {
				id: 'myModal-content'
			}),

			Builder.node('a', {
				id: 'myModal-close',
				href: 'javascript: void(0);'
			}, 'X')
		]);

		var loader = Builder.node('div', {
			id: 'myModal-loader',
			style: 'display: none'
		});

		var overlay = Builder.node('div', {
			id: 'pageOverlay',
			style: 'display: none'
		});

		document.body.appendChild(modal);
		document.body.appendChild(loader);
		document.body.appendChild(overlay);

		var pageSize = Utils.getPageSize();

		$('pageOverlay').setStyle({
			width: pageSize[0] + 'px',
			height: pageSize[1] + 'px'
		});

		$('myModal-loader').setStyle({
			top: (pageSize[3] / 2) - 50 + 'px',
			left: (pageSize[2] / 2) - 50 + 'px'
		});

		new Effect.Appear('pageOverlay', {
			duration: 0.2,
			from: 0.0,
			to: 0.7,

			afterFinish: function() {
				$('myModal-loader').show();
			}
		});
	},

	request: function(url) {
		new Ajax.Request(url, {
			method: 'post',
			asynchronous: false,
			encoding: 'utf-8',
			onSuccess: function(transport) {
				this.updater(transport.responseText);
			}.bind(this)
		});
	},

	updater: function(content) {
		$('myModal-content').update(content);

		if(this.parsing == true) {
			$('myModal-content').descendants().each(function(e) {
				if(e.id) e.id = (e.id == 'content' || e.id == 'close') ? 'myModal-' + e.id + '2' : 'myModal-' + e.id;
			});
		}

		if($('modalRedir') != undefined) {
			$('modalRedir').value = 'http://' + window.location.hostname + window.location.pathname;
		}
	},

	position: function() {
		if($('myModal') == undefined) return;
		if(this.ie6 == true) document.location.href = '#';

		var modalSize = $('myModal').getDimensions();
		var pageSize = Utils.getPageSize();
		var x = (pageSize[2] - modalSize.width) / 2;
		var y = (pageSize[3] - modalSize.height) / 2;
		var xLoader = x + (modalSize.width / 2.5);
		var yLoader = y + 50;

		$('myModal').setStyle({
			left: x + 'px',
			top: y + 'px'
		});

		$('myModal-loader').setStyle({
			left: xLoader + 'px',
			top: yLoader + 'px'
		});

		$('pageOverlay').setStyle({
			width: pageSize[0] + 'px',
			height: pageSize[1] + 'px'
		});

		if($('myModal').getElementsByTagName('form')[0] != undefined) {
			var form = $('myModal').getElementsByTagName('form')[0];

			for(var i = 0; i < form.getElementsByTagName('input').length; i++) {
				if(form.getElementsByTagName('input')[i].type != 'hidden') {
					form.getElementsByTagName('input')[i].focus();
					break;
				}
			}
		}
	},

	showModal: function() {
		$('myModal').show();

		Event.observe(window, 'resize', this.position);
		Event.observe($('pageOverlay'), 'click', this.hideModal.bindAsEventListener(this));
		Event.observe($('myModal-close'), 'click', this.hideModal.bindAsEventListener(this));
	},

	hideModal: function() {
		new Effect.Fade('pageOverlay', {
			duration: 0.2,

			beforeStart: function() {
				$('myModal-loader').remove();
				$('myModal').remove();
			},

			afterFinish: function() {
				$('pageOverlay').remove();
			}
		});

		Utils.showSelectBoxes();
		Utils.flash({ display: 'visible' });
	}
};
