/*
based on prototype's && moo.fx's ajax class
to be used with prototype.lite, in conjunction with moo.AJAX
this submits an iframe invisibly to the server, and expects a JSON object in return
handy, so that you do not have to care about posting forms, urlencoding, file uploads etc.
usage: <form method='post' onsubmit='new iframe(this); return false;'>

changelog:
17-01-06: initial release
18-01-06: added options initiator
          added IE5 and (hopefully) safari support.
		  added Opera support.
*/

document.iframeLoaders = {};

iframe = Class.create();
iframe.prototype = {
	initialize: function(form, options){
		if (!options) options = {};
		this.form = form;
		this.uniqueId = new Date().getTime();
		this.transport = this.getTransport();
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.updateMultiple = options.multiple || false;
		this.msgElm = false;
		document.iframeLoaders[this.uniqueId] = this;
		form.target = 'frame_'+this.uniqueId;
		form.submit();
	},

	onStateChange: function(){
		this.transport = $('frame_'+this.uniqueId);
		try {  // For NS6
			 var doc = this.transport.contentDocument.document.body.innerHTML;
			 this.transport.contentDocument.document.close();
		}
		catch (e){ 
			try{ // For IE5.5 and IE6
				var doc = this.transport.contentWindow.document.body.innerHTML;
				this.transport.contentWindow.document.close();
			}
			catch (e){
				try	{	// for IE5
					var doc = this.transport.document.body.innerHTML;
					this.transport.document.body.close();
				}
				catch (e) { 
					try	{ // for Opera (and Safari?)
						var doc = window.frames['frame_'+this.uniqueId].document.body.innerText;
						//window.frames['frame_'+this.uniqueId].document.close();
					}
					catch (e) {
						// forget it.	
					}
				}
			}
		}
		this.transport.responseText = doc;
		
		if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			if (this.updateMultiple){// JSON support!
				setTimeout(function(){ 
					try	{
						var hasscript = false; 
						eval("var inputObject = "+this.transport.responseText);	// we're expecting a JSON object, eval it to inputObject
						for (var i in inputObject)	{
							if (i == 'script') { hasscript = true; } // check if we passed some javascript along too
							else {
								if ( elm = $(i)) { elm.innerHTML = inputObject[i]; } // it's not script, update the corresponding div
								else { alert("element "+i+" not found!");	} // or not.
							}
						}
						if (hasscript) eval(inputObject['script']); // some on-the-fly-javascript exchanging support too
					}
					catch (e) {
						alert('There was an error processing: '+this.transport.responseText); // in case of an error, alert and fix.
					}						
				}.bind(this), 10);
			}	
	},

	getTransport: function() 
	{
		var divElm = document.createElement('DIV');
		divElm.style.display = 'none';
		document.body.appendChild(divElm);
		divElm.innerHTML = '<iframe name=\"frame_'+this.uniqueId+'\" id=\"frame_'+this.uniqueId+'\" src=\"about:blank\" onload=\"setTimeout(function(){document.iframeLoaders['+this.uniqueId+'].onStateChange()},20);"></iframe>';
		//						^->  Think tháts nasty? Try creating an iframe (x-browser) through DOM and hooking some events to it ;)
	}
};
