////////////////////////////////////////////////////////////////
// Copyright © 2008-2010 ReFreezed.com
////////////////////////////////////////////////////////////////



var FormUpload = {};

(function(){







/* MEMBERS
********************************************************************************************************************************/

var mLawl = false;







/* PRIVATE METHODS
********************************************************************************************************************************/



function Lawl()
{
}







/* PUBLIC METHODS
********************************************************************************************************************************/



FormUpload.SubmitData = function(action, data, successFunction, errorFunction)
{
	send_http_request(action, data, successFunction, errorFunction);
	return true;
};



FormUpload.SubmitFileInputs = function(action, fileInputs, data, successFunction, timeoutFunction)
{

	// Validate function parameters
	if (action == '')
		return false;
	if ($(fileInputs).length == 0)
		return false;
	if (typeof data != 'object')
		data = {};
	if (typeof successFunction != 'function')
		successFunction = function(){};
	if (typeof timeoutFunction != 'function')
		timeoutFunction = function(){};

	// Create hidden form and iframe
	var hidden_iframe = $('<iframe name="hiddenFormUploadIframe" class="hide" src="about:blank"></iframe>').appendTo('body');
	var hidden_form = $('<form method="post" enctype="multipart/form-data" id="hiddenFormUploadForm"'
		+' target="hiddenFormUploadIframe" class="hide"></form>').appendTo('body');

	// Add data to the hidden form
	for (var name in data)
		hidden_form.append('<div><input type="hidden" name="'+name+'" value="'+data[name].trim().asText()+'" /></div>');

	// Move file inputs to the hidden form
	$(fileInputs).each(function()
	{
		$(this).removeAttr('disabled').after($('<span id="hiddenFormUploadPlaceholder-'+this.name+'"></span>'))
			.appendTo(hidden_form).wrap('<div></div>');
	});

	// Send "request"
	hidden_form.attr('action', '/ajax/'+action+'.php');
	hidden_form.submit();

	// Create an interval call to check if the form has been submitted into the iframe
	(function(){
		var MAX_INTERVAL_CALL_TIME = 90; // How many seconds before we abandon all hopes of a success...
		var INTERVAL_CALL_SPEED = 50; // Delay between calls in milliseconds
		var interval_call_count = 0;
		var interval_id = setInterval(function()
		{
			interval_call_count++;
			if (interval_call_count*INTERVAL_CALL_SPEED > MAX_INTERVAL_CALL_TIME*1000)
			{
				clearInterval(interval_id);
				$(fileInputs).each(function() // Move file inputs out of the hidden form and back to where they came from
				{
					$(this).replaceAll('#hiddenFormUploadPlaceholder-'+this.name);
				});
				hidden_form.remove();
				hidden_iframe.remove();
				timeoutFunction();
				return;
			}
			if (get_iframe_document(hidden_iframe.get(0)).URL == 'about:blank')
				return;
			else
				clearInterval(interval_id);
			$(get_iframe_document(hidden_iframe.get(0))).ready(function() // (The page may not really have loaded yet)
			{
				var data = get_iframe_document(hidden_iframe.get(0)).body.innerHTML;
				$(fileInputs).each(function() // Move file inputs out of the hidden form and back to where they came from
				{
					$(this).replaceAll('#hiddenFormUploadPlaceholder-'+this.name);
				});
				hidden_form.remove();
				hidden_iframe.remove();
				successFunction(data);
			});
		}, INTERVAL_CALL_SPEED);
	})();

	// We're done here for now - the rest is handled by the interval call
	return true;

};



FormUpload.SubmitForm = function(form)
{
	form = $(form).eq(0);
	if (form.hasClass('busy'))
		return false;
	form.submit();
	return true;
};







/* ONLOAD
********************************************************************************************************************************/



$(document).ready(function()
{



	// Lawl



});







})();
