(function() {

	var crudForm = window.crudForm = {
		inited : false,
		init : function() {
			if (this.inited)return;
			this.processing = false;
			inited = true;
		},
		setup : function(opts) {
			this.init();
			
			$(opts.crudId).submit(crudForm.submitform);
			formFeedback.setup(opts.crudId);
		},
		submitform : function() {
			if (crudForm.processing)return false;

			if ($('.mceEditor').length) {
				tinyMCE.triggerSave();
			}

			var formID = '#' + $(this).attr('id');
			var submitBtn = '#' + $(formID + ' [type=submit]').attr('id');
			var queryString = $(this).formSerialize();

			formFeedback.clearAllErrors();
			crudForm.processing = true;

			// i'd recommend to put these two in a callback
			if ($.cssButton != undefined)
				$.cssButton.setState(submitBtn, 'process');

			$.post($(this).attr('action'), queryString, function(response) {
					
						crudForm.processing = false;
						if ($.cssButton != undefined)
							$.cssButton.reset(submitBtn);

						if (response.redirect != undefined) {
							$(formID).clearForm();
							return window.location.href = response.redirect;
						}

						if (!response.success) {
							formFeedback.showErrors(response);
						} else {
							$(formID).trigger({
										type : "process",
										response : response
									});
						}

					}, "json");

			return false;
		}
	};
})();


//

(function() {

	var formFeedback = window.formFeedback = {
		speed : 500,
		inited : false,
		init : function() {
			if(this.inited)
				return;

			/* grab CSS colour values to highlight fields and flag errors */
			this.defaultColor = $("form input").css("background-color");
			$("body").append("<div id='remove' class='highlight'></div>");
			this.highlightColor = $('#remove').css("background-color");
			$('#remove').removeClass('highlight').addClass('focus');
			this.focusColor = $('#remove').css("background-color");
			$('#remove').remove();
			inited = true;
		},
		setup : function(formID) {

			this.init();

			var inputFields = '#' + formID + ' :input:not([type=submit])';

			// discovered feb 23 2010, using 'lable' as a field name in crud form will break the
			// this errors class removal functionality, make sure to name your fields
			// something like 'page_label' instead of 'label'
			
			$(inputFields).focus(function(){
				var idClass = '.' + $(this).attr('id');
				//console.log(idClass);
				if ($(idClass).is('ul.errors')) $(idClass).remove();
				formFeedback.setState(this, 'focus');
				$(this).removeClass("errors");
			});
			
			$(inputFields).blur(function(){
				formFeedback.setState(this, 'blur');
			});
		},
		setState : function(target, event) {
			
			if ($(target).hasClass('displaygroup'))return;

			var f = formFeedback;
			var color;
			var cb;
			
			switch (event) {
				case 'focus' :
					color = f.focusColor;
					break;
				case 'blur' :
					color = f.defaultColor;
					break;
				case 'highlight' :
					color = f.highlightColor;
					break;
				default :
					color = f.defaultColor;
					
			}
			/*
			cb = function() {
				$('.errors').removeClass("errors");
			}
			*/
			
			//$(target).animate({'backgroundColor':color},f.speed,cb);
			$(target).animate({'backgroundColor':color},f.speed);
		},
		clearAllErrors : function() {
			$('ul.errors').remove();
			formFeedback.setState('.errors');
		},
		showErrors : function(response) {
			
			$.each(response.errors, function(i, val) {

						var el = "#" + i;
						$(el).not('.displaygroup').addClass("errors");
						formFeedback.setState(el, 'highlight');
						/*
						 * add ul.errors after the field the errors are
						 * associated with
						 */
						if ($(el).hasClass("richtext"))el += '_parent';

						if(val)
							$(el).after($(val).hide().fadeIn(formFeedback.speed));
					});
		}
	};
})();
