(function($) {
$.fn.dynamicForm = function() {
	doPause = function(el) {
		$(el).find(':input').attr('disabled','disabled');
		el.pause = true;
		$(el).find('.loading').fadeIn( 300 );
	};
	undoPause = function(el, lock) {
		lock = lock || false;
		if ( !lock )
			$(el).find(':input').attr('disabled','');
		el.pause = false;
		$(el).find('.loading').fadeOut( 300 );
		
	};
	updateStatus = function(el, message, status) {
		status = status || 'info';
		if ( $('.message', el).length == 0 )
		{
			$(el).prepend( $( '<div/>').addClass('message').addClass(status).html( '<p>' + message + '</p><span class="close" title="Dismiss"></span>' )
			.fadeIn( 500 ).click(function(){ 
				$(this).fadeOut(500, function(){ $(this).remove(); });
			}));
		}
		else
		{
			$('.message', el).fadeOut( 300,function() {
				$(this).fadeIn( 500 ).removeClass().addClass('message').addClass(status).find('p').html( message );
			});
		}
	};
	handleReturn = function( el, data ) {
		data = data || [];
		data.notice = data.notice || 'An error occured!';
		data.status = data.status || 0 ;
		$( '.Save', el ).find( 'span' ).removeClass().addClass( statusCodes[ data.status ] ).html( data.notice );
		if ( !data.block )
			undoPause( el );
		if ( data.reload )
			setTimeout(function() { window.location.reload( true ); }, 1000 );
	};
	initForm = function(el) {
		if( $(el).attr('action').substr(0,2) != '#!' )
			return false;
		$(':input:disabled').not('.disabled').each(function(i,n) {
			$(this).attr('disabled','');
		});
		$(':input.focus').focus();
		
		$(el).submit(function(){
			if(el.pause)
				return;
			if ( $(el).hasClass('static') )
				return true;
			var data = [];
			var location = $(this).attr('action').substr(2);
			
			$(':input', this).each(function(i,d){
				if ( this.name == 'confirm' )
					return;
				if ( this.type == "checkbox" || this.type == "radio" )
				{
					if ( this.checked == true )
						data.push($(this).attr('name') + '=' + escape($(this).val()));
				} else if ( $(this).attr('name').length > 0 )
					data.push($(this).attr('name') + '=' + escape($(this).val()));

			});
			if ( data.length == 0 )
				return false;
			
			if ( $("input[name='confirm']", this).length > 0 )
				if ( !confirm( $("input[name='confirm']", this).val() ) )
					return false;
		
			doPause(el);
			
			$.ajax({
				url: '/dynamic/'+location+'/?callback=?',
				cache:false,
				type: "POST",
				data: data.join('&'),
				dataType: 'jsonp',
				timeout:9000,
				error: function()
				{
					undoPause(el);
					updateStatus( el, 'An error occured submitting this form.', 'fail' );
				},
				success: function( data )
				{
					undoPause(el, (data.lock || data.refresh));
					if ( data.refresh )
						setTimeout( "window.location.reload( true );", 1000 );
					if ( data.location )
						setTimeout(function() { window.location = data.location; }, 1000 );
					updateStatus( el, data.message, data.status );
				}
			});
			return false;
		});
	};
	this.each(function () {
		initForm(this);
	});
	return this;
};
})(jQuery);
$(document).ready(function() {
	$('form').not('.static').dynamicForm();
});
