var CDMGForm = (function($){
	CDMGForm = function CDMGForm(form_id, params) {
		this.form = checkId(form_id);
		this.onsuccess = new Observer();

		this.params = $.extend({
			send_msg:true,
			success_str:'Thank You',
			success_msg:'Your message has been sent. Thank you!',
			url:'scripts/send-body.php',
			redirect:false,
			redirect_url:'thankyou.html',
			validation:null,
			submit_id:'#submit',
			note_id:'#note'
		}, params);
		
		if (arguments.length>2 && arguments[2]) {
		 	this.params.validation = new CDMGValidator(this.form,arguments[2]); 
		};		
	};
	
	CDMGForm.prototype.init = function init() {
		this.submit = this.build_selector(this.params.submit_id);
		this.note = this.build_selector(this.params.note_id);
		
		/* convienience function to add redirect page */
		if (this.params.redirect) {
			this.onsuccess.add(function() {
				window.location = this.params.redirect_url;
			});
		};
		
		this.ajax();
	};

	CDMGForm.prototype.build_selector = function build_selector(id) {
		return (this.form + ' ' + checkId(id));
	};
	
	CDMGForm.prototype.ajax = function ajax() {
		var _this = this;
		$(this.form).submit(function() { _this.ajax_submit(); return false;});
	};	
	
	CDMGForm.prototype.ajax_submit = function ajax_submit() {
		var _this = this;
		var _submit = this.submit;
		var _note = this.note;

		$(_note).html('');
		$(_submit).hide();
		if (this.validation()) {
			$.ajax(this.build_ajax_params());
		} else {
			$(_submit).show();
		}
	};
	
	CDMGForm.prototype.validation = function validation() {
		if (this.params.validation) {
			return this.params.validation.validate();
		} else {
			return true;
		}
	};
	
	CDMGForm.prototype.build_ajax_params = function build_ajax_params() {
		var ajax_params = {
		   type: this.ajax_type(),
		   url: this.ajax_url(),
		   data: this.ajax_data(),
		   success: this.ajax_success()
		};
		
		return ajax_params;		
	};
	
	CDMGForm.prototype.ajax_type = function ajax_type() {
		return "POST";	
	};
	
	CDMGForm.prototype.ajax_url = function ajax_url() {
		return this.params.url;	
	};
	
	CDMGForm.prototype.ajax_data = function ajax_data() {
		return $(this.form).serialize();
	};
	
	CDMGForm.prototype.ajax_success = function ajax_success() {
		var _this = this;
		var success_function = function(msg){
			if(msg.indexOf(_this.params.success_str) != -1)
			{
				_this.ajax_complete_success(msg);											
			} else {
				_this.ajax_complete_fail(msg);
			};										
		};		
		
		return success_function;
	};
	
	CDMGForm.prototype.ajax_complete_success = function ajax_complete_success(msg) {
		this.ajax_complete_success_msg(msg);
		this.ajax_complete_success_update_view();
	};
	
	CDMGForm.prototype.ajax_complete_success_msg = function ajax_complete_success_msg(msg) {
		if (this.params.send_msg) { alert(this.params.success_msg);}
	};

	CDMGForm.prototype.ajax_complete_success_update_view = function ajax_complete_success_update_view() {
		this.onsuccess.fire(this);
	};
	
	CDMGForm.prototype.ajax_complete_fail = function ajax_complete_fail(msg) {
		this.ajax_complete_fail_msg(msg);
		this.ajax_complete_fail_update_view();
	};
	
	CDMGForm.prototype.ajax_complete_fail_msg = function ajax_complete_fail_msg(msg) {
		var _note = this.note;
		$(_note).html(msg);		
	};
	
	CDMGForm.prototype.ajax_complete_fail_update_view = function ajax_complete_form_update_view() {
		var _submit = this.submit;
		$(_submit).show();		
	};
	
	return CDMGForm;
})(jQuery);


checkId = function checkId(s) {
	if (s.indexOf('#') != -1) {
		return s;	
	} else {
		return ('#' + s);	
	}
}


var Observer = (function() {
	Observer = function Observer() {
	    this.fns = [];
	};
	Observer.prototype = {
	    add : function(fn) {
	        this.fns.push(fn);
	    },
	    del : function(fn) {
	        this.fns = this.fns.filter(
	            function(el) {
	                if ( el !== fn ) {
	                    return el;
	                }
	            }
	        );
	    },
	    fire : function(thisObj) {
	        var scope = thisObj || window;
	        this.fns.forEach(
	            function(el) {
	                el.call(scope);
	            }
	        );
	    }
	};
	return Observer;
})();
