/*
	Confirm
*/

var textOk = 'Oui';
var textCancel = 'Annuler';

var Confirm = {

	init: function(options){
		this.options = options || {};

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^confirm/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div', {'id': 'cOverlay'}).injectInside(document.body);

		this.container = new Element('div', {'id': 'cContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.cConfirm = new Element('div', {'id': 'cConfirm'}).injectInside(this.container);

		this.cAction = new Element('div', {'id': 'cAction'}).injectInside(this.cConfirm);
		
		this.cInputs = new Element('div', {'id': 'cInputs'}).injectInside(this.cConfirm);
		this.cAccept = new Element('a', {'id': 'cAccept', 'href': '#'}).injectInside(this.cInputs);
		this.cCancel = new Element('a', {'id': 'cCancel', 'href': '#'}).injectInside(this.cInputs);
		this.cCancel.onclick = this.overlay.onclick = this.close.bind(this);

		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.cConfirm);

		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide()
		};
	},

	click: function(link){
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 4);
		this.container.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.8);
		this.open(link.href, link.title);
		return false;
	},

	open: function(href, title){
		this.cAction.setHTML(title);
		this.cAccept.href = href;
		this.cAccept.setHTML(textOk);
		this.cCancel.setHTML(textCancel);
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.extend(document.getElementsByTagName('iframe'));
		elements.each(function(el){
			if (open) el.cBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.cBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
	},

	close: function(){
		for (var f in this.fx) this.fx[f].stop();
		this.container.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

window.addEvent('domready', Confirm.init.bind(Confirm));
