/**
 * Pork.Iframe V1.3 for Mootools 1.2
 * Now with auto-cleanup for better performance.
 *
 * Automatic object-extension.
 * Usage:
 *	

 * Added easy sendAjax function.
 */

Iframe = new Class({
	Implements: Options,
	initialize: function(element, options)
	{	
		this.element = element;
		this.setOptions(options);
		this.Setup();
	},

	Setup: function() {
		this.sendingFrame = new IFrame({
			options: this.options || {},
			name: 'iframe_'+new Date().getTime(),
			styles: {
				display: 'none'
			},
			events: {	 
				load: function(input) {
					this.onSuccess(this.sendingFrame.contentWindow.document.body.innerHTML);
				}.bind(this)
			}
		});
		this.sendingFrame.injectInside(document.body);
		this.element.target = this.sendingFrame.name;
	},

	Request: function()
	{
		this.element.submit();
		return false;
	},
	
	onSuccess: function(output) {
			if($defined(this.options.update)) $(this.options.update).set('html', output);	
			if($defined(this.options.onComplete)) this.options.onComplete(output);
			this.Cleanup.delay(1500, this);
	},

	Cleanup: function() {
		this.sendingFrame.dispose();
	}

});

Element.implement({
	
	sendIframe: function(options) {		
		return new Iframe(this, options).Request();
	},
	
	sendAjax: function(options) {
		$(this).set("send", $merge({evalScripts:true, onComplete:function(response) { 
		if($defined(options.update) && $(options.update)) $(options.update).set("html", response) }}, options)).send(); 
		return false;
	}
});
