/**
 * Retrieve HTML for injection into the DOM
 *
 * HtmlGet uses GET requests to receive strings (HTML)
 *
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 * @requires Mootools-1.2
 * @requires ByngAjax
 */

Byng.register('byng.ajax.html',
{
	/**
	 * Extend the ByngAjax Class
	 * 
	 */
	Extends : 'byng.ajax',
	
	/**
	 * Holds the options for this class
	 * 
	 * @type Object
	 */
	options : {
		'onComplete' : $empty,
		'onError' 	 : $empty,
		'onResponse' : $empty,
		'container'	 : null,
		'gateway'	 : null,
		'cache'		 : null
	},
	
	/**
	 * HtmlGet prototype constructor
	 * 
	 * @param Object options
	 * @param String gateway
	 */	
	initialize : function (options, gateway)
	{
		// condition: options passed a single DomElement
		if ($type(options) == 'element' || $type(gateway) == 'string') {
			// legacy support for parameterised construct
			options = { 'container' : options, 'gateway' : gateway };
		}
		
		if ($type(options) == 'object') {			
			this.parent(options);			
			if (!options.onComplete) {
				this.addEvent('response', this.show.bind(this));
			} else {
				this.addEvent('response', options.onComplete);
			}			
		}
		
	},
	
	/**
	 * Handle the request to the AJAX dispatcher
	 * 
	 * @param Object Update this DomElement container with fresh HTML
	 */
	update : function (options) 
	{	
		// If we have a request handling function
		if ($type(options) == 'function') {
			options = {'onComplete' : options};
		}
		
		if ($type(options) == 'object') {
			this.setOptions(options);
			if (options.onComplete) {
				this.removeEvents('response');
				this.addEvent('response', options.onComplete);
			}
			
			if (options.onSubmit) {
				this.removeEvents('submit');
				this.addEvent('submit', this.options.onSubmit);
			}
			if (options.onSuccess) {
				this.removeEvents('success');
				this.addEvent('success', options.onSuccess);
			}
		} else {
			
			if (!this.options.onComplete) {
				this.addEvent('response', this.show.bind(this));
			}
			
			if (this.options.onSubmit) {
				this.addEvent('submit', this.options.onSubmit);
			}
		}
					
		return this.get();
	},


	/**
	 * Get a parent and wrappr pair
	 * 
	 * @param Element element Element to insert the HTML into
	 * @param Element wrapper Wrapper container
	 */ 
	set : function (element, wrapper) 
	{
		// set the parent element and container
		this.options.container = element;
		this.options.wrapper   = wrapper;		
	},
	
	/**
	 * Show HTML response in the container
	 * 
	 * @param String html
	 */
	show : function (html) 
	{
		
		// condition: there is a wrapping <div> then show
		if ($type(this.options.wrapper) == 'element' && 
			this.options.wrapper.getStyle('display','none')) {
			this.options.wrapper.setStyle('display','block');
		}
		
		// condition: the mode then update the parent object
		if (this.options.mode == 'outer') {
			this.options.container.set('html', html);
		} else {
		
			if (this.options.container) {
				
				// condition: updating a form field element
				if (['input','select','textarea'].contains(this.options.container.get('tag'))) {
					this.options.container.set('value', html);
				} else {
					this.options.container.set('html', html);	
				}
				// condition: request has onSubmit event
				if (this.hasEvent('submit')) {
					
					this.options.container.getElements('form').addEvent('submit', function( e ) {
						e.stop(); this.fireEvent('submit', [e, this]);
					}.bind(this));
				}

				// condition: success event
				if (this.hasEvent('success')) {					
					this.fireEvent('success', [html]);					
				}
			}
		}
	}
	
});