var Msgbox = new Class({
	
	Implements: [Options, Events],
	
	options: {
		back	: null,
		window	: null,
		content	: null,
		close	: null
	},
	
	initialize: function(){
		
		var mythis = this;
		
		this.setOptions({
			'back'		: $('msgbox-back'),
			'window'	: $('msgbox'),
			'content'	: $('msgbox-content'),
			'close'		: $('msgbox-close')
		});
		
		$$('a[rel=msgbox]').addEvent('click', function(event){
			event.stop();
		});
		
		window.addEvent('keyup', function(event){
			if( event.key == 'esc' ){
				event.stop();
				mythis.close();
			}
		});
		
		$$('form[rel=msgbox]').each(function(form){
			form.adopt(new Element('input', { 'type': 'hidden', 'name': 'mode', 'value': 'dynamic' }));
		});
		
		$$('form[rel=msgbox]').addEvent('submit', function(event){
			event.stop();
			var el = this.getElement('input[type=hidden][name=onsuccess]');
			if( $defined(el) ){
				eval(el.value);
			}
		});
		
		$$([this.options.close, this.options.back]).addEvent('click', function(event){
			event.stop();
			mythis.close();
		});
		
		if( new RegExp(/msie 6/i).test(navigator.userAgent) ){
			this.options.back.setStyle('height', window.getScrollSize().y);
		}
		
		this.close();
	},
	
	setContent: function( _content ){
		this.options.content.set('html', _content);
	},
	
	load: function( _template ){
		this.options.content.load(_template);
	},
	
	updatePosition: function(){
		var size = this.options.content.measure(function( _elem ){
			return this.getSize();
		});
		var fullSize = size.x + 42; // 20 px de padding + 1px de border
		this.options.window.setStyle('margin-left', -1 * (fullSize / 2));
	},
	
	reveal: function( _fnSuccess ){
		var mythis = this;
		this.options.window.setStyle('display', 'block');
		this.options.back.setStyle('display', 'block');
		this.updatePosition();
		var fxBack = new Fx.Morph(this.options.back, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		var fxWindow = new Fx.Morph(this.options.window, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		fxBack.start({
			'opacity': [0, 0.5]
		}).chain(function(){
			fxWindow.start({
				'opacity': [0, 1]
			}).chain(function(){
				if( $defined( _fnSuccess ) ){
					_fnSuccess.run();
				}
			});
		});
	},
	 
	close: function(){
		this.options.window.fade(0);
		this.options.window.setStyle('display', 'none');
		this.options.back.fade(0);
		this.options.back.setStyle('display', 'none');
	},
	
	// --
	
	show_Wait: function( _fnSuccess ){
		this.options.close.setStyle('visibility', 'hidden');
		this.setContent('<div class="msgbox-wait"></div>');
		this.reveal(_fnSuccess);
	},
	
	show_ConnectionForm: function( _mode_full ){
		this.show_Wait(function(){ 
			var q = new Request.HTML({
				'onSuccess'	: function( _tree, _elem, _html, _js ){
					this.options.close.setStyle('visibility', 'visible');
					this.setContent(_html);
					this.updatePosition();
					eval(_js);
				}.bind(this)
			}).get('/tpl/Js/ConnectionForm.php?mode=' + ($defined(_mode_full) && _mode_full ? 'full' : ''));
		}.bind(this));
	},

	// -- Submit Avis
	show_ConfirmAvis: function( _mode_full ){
		this.options.back.fade(0.5);
		this.show_Wait(function(){ 
			var q = new Request.HTML({
				'onSuccess'	: function( _tree, _elem, _html, _js ){
					this.options.close.setStyle('visibility', 'visible');
					this.setContent(_html);
					this.updatePosition();
					eval(_js);
				}.bind(this)
			}).get('/tpl/Js/ConfirmAvis.php?mode=' + ($defined(_mode_full) && _mode_full ? 'full' : ''));
		}.bind(this));
	},	
	
	show_AddProductBasket: function( _form ){
		this.show_Wait(function(){
			new Request.JSON({
				'url'		: $(_form).get('action'), 
				'onSuccess'	: function( _retour ){
					var nbItemsBasket = _retour.panier.toInt();
					var s = nbItemsBasket > 1 ? 's' : '';
					$('nombre-articles-panier').set('html', nbItemsBasket + ' article' + s);
					new Request.HTML({
						'url': '/tpl/Js/AddProductBasket.php?' + $(_form).toQueryString(),
						'onSuccess': function( _tree, _elem, _html, _js ){
							this.options.close.setStyle('visibility', 'visible');
							this.setContent(_html);
							this.updatePosition();
						}.bind(this)
					}).get();
				}.bind(this)
			}).post($(_form));
		}.bind(this));
	},
	
	show_DelProductBasket: function( _form ){
		this.show_Wait(function(){
			new Request.HTML({
				'url': '/tpl/Js/DelProductBasket.php?' + $(_form).toQueryString(),
				'onSuccess': function( _tree, _elem, _html, _js ){
					this.options.close.setStyle('visibility', 'visible');
					this.setContent(_html);
					this.updatePosition();
				}.bind(this)
			}).get();
		}.bind(this));
	},
	
	show_DeleteAddress: function( _delete_link ){
		this.show_Wait(function(){
			new Request.HTML({
				'url': '/tpl/Js/DeleteAddress.php',
				'onSuccess': function( _tree, _elem, _html, _js ){
					this.options.close.setStyle('visibility', 'visible');
					this.setContent(_html);
					this.updatePosition();
				}.bind(this)
			}).post({ 'delete-link' : _delete_link });
		}.bind(this));
	}
	
});
