//<![CDATA[

if (!window.NanoM) {
	Type.registerNamespace("NanoM");
	window.NanoM.log = function(p_arg) {
		if (window.console) {
			window.console.log(p_arg);
		}
	}
}

	/************************************************
	/ NanoM.Contact
	/************************************************/
	NanoM.Contact = function(p_el) {
		NanoM.Contact.constructBase(this, [p_el]);
	};

	NanoM.Contact.prototype = {
		handle_click: function(p_evt){
			if($(p_evt.target).hasClass('js-send')){
				var self = this;
				var invalidMsg = '';
				$('.js-required').each(function(){
					if(!$(this).val()){
						invalidMsg += $(this).attr('name') + ' required\n';
					}
				});
				if(invalidMsg === ''){
					$.ajax({
						type: "POST",
						url: p_evt.target.href,
						data: $(':input', this.element).serialize(),
						success: Function.createDelegate(this._onSuccess, this),
						error: Function.createDelegate(this._onError, this)
					});
					this._showView('sending');
				}
				else{
					alert(invalidMsg);
				}
				return false;
			}
		},
		_onSuccess: function(p_result, p_status){
			if(p_result == '0'){
				this._showView('sent');
			}
			else{				
				this._onError(null, null, null, p_result);
			}
		},
		_onError: function(p_xhr, p_status, p_error, p_msg){
			alert(p_msg || "We're sorry. There was a problem sending your message.");
			this._showView('form');
		},
		_showView: function(p_name){
			this._viewParser = /\s?js-view-(\w+)/i;
			var self = this;
			this.jq.find("*[class*='js-view-']").each(function() {
				var matches = self._viewParser.exec(this.className);
				if(matches[1] == p_name){
					$(this).show();
				}
				else{
					$(this).hide();
				}
			}).end();
		}
	}
	
	NanoM.Contact.extend("NanoM.Contact", True.Behavior);

//]]>
