
(function(container) {

	var $ = jQuery;

	// this is our object
	function ModalForm(formID, options)	{

		var self = {
			formID:		null,
			formName:	null,
			ajaxInProgrss: false,

			loadingBlocks: false,
			submitBlocks: false,


			options:	{}
		};


		// 	inits the form, give formID and options for
		this.init =	function(formID, options)	{
			self.formName = formID;
			self.formID = '#' + formID;

			// mix options
			$.extend(self.options, options);

			// generate jquery selectors for loading and submit blocks
			// we use classes instead of ids because we want to be able to switch on/off multiple
			// submit/loading blocks
			self.loadingBlocks = self.formID + ' .' + self.formName + '_loading';
			self.submitBlocks = self.formID + ' .' + self.formName + '_submit';

			// bind form submit
			if (!self.options.noSubmitBind)	{
				// bind general submit event
				$(self.formID).bind('submit', self.formSubmit);
				// also bind all elements that has the "formSubmit" class (like divs)
				$(self.formID + ' .formSubmit').bind('click', self.formSubmit);
			}
		}

		// event triggered on form submit
		this.formSubmit = function(event)	{
			if (event)	event.preventDefault();

			$(self.formID).trigger('beforeSubmit', [self.formID]);

			// only allow one call a time
			if (self.ajaxInProgress)	return false;
			self.ajaxInProgress = true;

			// switch loading-indicator and submit block?
			$(self.loadingBlocks).show();
			$(self.submitBlocks).hide();

			// submit form via ajax to action url
			var form = $(self.formID);
			var post = form.serialize();
			if (container.jdebug) alert('POST: ' + post);
			post = post +  '&dummy=1';
			var url = form.attr('action');
			if (self.options.url)	url = self.options.url;
			$.ajax({
				type:	'POST',
				url:	url,
				cache:	false,
				data:	post,
				error:	self.ajaxError,
				success:self.ajaxSuccess,
				dataType: 'json'
			});
		};


		// clears up some ajax stuff
		this.ajaxComplete =	function()	{
			self.ajaxInProgress = false;
			// reset loading/submit blocks?
			$(self.loadingBlocks).hide();
			$(self.submitBlocks).show();
		};
		// is called when an error on ajax call happens (e.g. server not reachable, or no json result)
		this.ajaxError = function(xmlHttp, errorMessage, ajaxException)	{
			self.ajaxComplete();
			var data = {ajaxError: true, xmlHttp: xmlHttp, errorMessage: errorMessage, ajaxException: ajaxException};
			$(self.formID).trigger('formError',[data]);
		};

		// called if ajax call was successfull
		this.ajaxSuccess = function(data, textStatus)	{

			// check for application errors
			if (data.error)	{
				self.showError(data.error, data.errorHeader);
			}

			// check for redirect command
			self.redirect = false;
			if (data.redirect)	self.redirect = data.redirect;
			if (data.redirect && !data.showMessage)	location.href = data.redirect;		// redirct of no message should be shown, else redirect after modal close


			// re-enable ajax form, if no error and no showMessage should be displayed
			if (!data.error && !data.showMessage && !data.showHtml)	self.ajaxComplete();

			// check for javascript
			if (data.script)	eval(data.script);

			// check if we should show messages
			if (!data.error && data.showMessage)	self.showMessage(data.showMessage, data.showMessageHeader);

			// trigger custom events
			if (data.error)	{
				$(self.formID).trigger('formError', [data]);
			} else if (data.success) {
				$(self.formID).trigger('formSuccess', [data]);
			}

		};

		// re-enable ajax
		this.modalClose = function()	{
			$(document).unbind('keypress', self.keyPress);
			$.modal.close();
			self.ajaxComplete();
			if (self.redirect)	location.href = self.redirect;
		};

		// displays a div with the given id
		this.showMessage = function(message, messageHeader)	{
			var html = self.getModalHtml(message, messageHeader);
			self.showHtml(html);
		};
		this.showError   = function(message, messageHeader)	{
			if (!messageHeader)	messageHeader = text_there_was_an_error;
			var html = self.getModalHtml(message, messageHeader);
			self.showHtml(html);
		};
		this.getModalHtml = function(message, messageHeader)	{
			// build simple html-code as container for simple text message/error
			if (!messageHeader)	messageHeader = '';
			var html = '<div class="modalFormMessage modalFormMessageFixedHeight"><h3>' + messageHeader + '</h3><p class="modalFormMessageContent">' + message + '</p></div>'
						+ '<div class="modalClose modalFormClose btnSmall btnWidthSmall"><a href="#">' + text_ok + '</a></div>';
			return html;
		};
		this.showHtml = function(html)	{
			// show full html, for special stuff?
			$(self.loadingBlocks).hide();
			$.modal.close();
			$.modal(html, {onClose: self.modalClose, xonOpen: self.fadeIn});

			// add keypress handler
			$(document).bind('keypress',self.keyPress);

		};

		this.fadeIn = function(dialog)	{

		};

		this.keyPress = function(event)	{
			event.preventDefault();
			var keyCode = event.which;
			if (keyCode == 0 || keyCode == 13)	{
				// close this
				self.modalClose();
			}

		};


		// merge self with this, so we can call externally AutoForm.init and internally self.init;
		$.extend(self, this);

		// call constructor
		this.init(formID, options);

	};

	// create and return new ModalForm object. Singleton wrapper for object
	var self = {
		instances:	{},
		init:	function(formID, options)	{
			var obj =  new ModalForm(formID, options);
			self.instances[formID] = obj;				// store in local array, for later use
			return obj;
		}
	}
	container.modalForm = self;


})(_meef);








