var AjaxLoader = new Class({

	initialize: function(options){

		this.options = new Hash({
				'wrapper_class':'ajax_loader_wrapper',
				'wrapper_id':'ajax_loader_wrapperId',
		    'waiting_element':new Element('div', {'html':'Chargement en cours...'})
			}
		);
	  this.options.extend($defined(options) ? options : new Hash());
	},


	load: function(element, url, post, waiting_element){

		var wrapper = new Element('div', {'class':this.options.get('wrapper_class'),'id':this.options.get('wrapper_id')});
		$(element).grab(wrapper, 'bottom');
		
		var target = new Element('div');
		wrapper.grab(target, 'bottom');

		if ($chk(waiting_element) == false)
			waiting_element = this.options.get('waiting_element').clone(true, true);
			
		wrapper.grab(waiting_element, 'bottom');

		new Fx.Slide(waiting_element, {'link':'cancel'})
			.hide().slideIn();

		new Request.HTML({'url':url, 'update':target, 'evalScripts':true})
		  .addEvent('complete', function(responseTree, responseElements, responseHTML, responseJavaScript){
		  
		    var target = responseElements[0].getParent();
		    if (target != null)
		    {
					if (Browser.Engine.name == "trident")
					{	
          target.set('html', target.get('html'));
            }
		      
		      	
          var waiting_element = target.getNext();
			    if (waiting_element != null)
			    {
				    waiting_element.get('slide').slideOut()
								.addEvent('complete', function(element){
										element.getParent().destroy();
								});

				    //new Fx.Slide(target, {'link':'chain'}).hide().slideIn();
			    }
				}
			})
			.send(post);
			
		return wrapper;
	},


	destroy: function(wrapper){

	  wrapper.getChildren()[0].get('slide')
			.slideOut()
			.addEvent('complete', function(){this.destroy()}.bind(wrapper));
	}
});


/*	Name:	DivStack
 *
 *	Constructor::
 *    // Instanciate a DivStack element
 *		$initialize(element_root[, options])
 *			>element_root: (String || Element) [The name of]the node which will receive the added divs.
 *      >options: (Hash) The list of optional parameters.
 *        >>divs_id_prefix: (String) The id of the container element under the element_root element.
 *        >>waiting_element: (Element) The element which is displayed while the div is loaded.
 *
 *	Methods::
 *    // Add a new div at the bottom of the div stack
 *		$pushForm(url[, post])
 *      >url: (String) The url to retrieve the div
 *      >post: (String) The optional extra post values
 *
 *    // Extract the div from the bottom
 *		$popForm()
 */

var DivStack = new Class({

	initialize: function(element_root, options){

		this.options = new Hash({
		    'divs_wrapper_id':'divs_stack',
		    'div_wrapper_class':'div_stack_entry',
		    'waiting_element':null
			}
		);
	  this.options.extend($defined(options) ? options : new Hash());

	  this.element_root = $(element_root);

	  this.element_divs = new Element('div', {'id':this.options.get('divs_wrapper_id')});

	  this.element_root.grab(this.element_divs, 'bottom');
	  
	  this.ajaxLoader = new AjaxLoader({
			'wrapper_class':this.options.get('div_wrapper_class'),
			'waiting_element':this.options.get('waiting_element')
		});

		this.divsAdded = new Array();
	},


	pushForm: function(url, post){
	  var form = this.ajaxLoader.load(this.element_divs, url, post);
		this.divsAdded.push(form);
		
		return form;
	},


	popForm: function(){
	  if (this.divsAdded.length > 0)
			this.ajaxLoader.destroy(this.divsAdded.pop());
	},
	
	getLast: function(){
		return this.divsAdded.getLast();
	}
});



