var DM = window.DM || {};
DM.ContactForm = function(){
	var cform;
	function initialize(formObj){
		cform = $(formObj);
		/*cform.submit(function(e){
			submitForm();
			return false;
		});*/
		prepFields(cform.find('input:text, textarea'));
	};

	function submitForm(){
		var errors = false;
		var url = cform.get(0).action;
		var fName = cform.find("input[name='name']");
		var fEmail = cform.find("input[name='email']");
		var fContent = cform.find("textarea");
		var fFile = cform.find("input[id='file']");
		if(fName.val().length <= 2 || fName.val() == 'Name (required)'){
			fName.next().text('Please enter your name.').show();
			errors = true;
		}
		if(!checkEmailAddress(fEmail.val())) {
			fEmail.next().text('Please enter your email address.').show();
			errors = true;
		}
		if(fContent.val().length <= 5){
			fContent.next().text("Please tell us what you're interested in.").show();
			errors = true;
		}
		if(errors){return};

		var options = {};
		
		$(this).ajaxSubmit({
			success: function(data) {
				console.log(this); // the options for this ajax request
	
				$('<div id="statusMsg">'+data+'</div>').hide().appendTo('body');
				var msg = $(document.createElement("a")).attr({ href: "#statusMsg"}).fancybox({
								'hideOnContentClick': false,
								'frameWidth':300,
								'frameHeight':125,
								'padding':20,
								'overlayShow':false
							});
				console.log("this works");
	
				msg.click(); //some pixie dust required
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				// typically only one of textStatus or errorThrown
				// will have info
				//alert(this); // the options for this ajax request
				$('<div id="statusMsg">'+XMLHttpRequest.responseText+'</div>').hide().appendTo('body');
				var msg = $(document.createElement("a")).attr({
					href: "#statusMsg"
				}).fancybox({
					'hideOnContentClick': false,
					'frameWidth':300,
					'frameHeight':125,
					'padding':20,
					'overlayShow':false
				});
				console.log("this didn't work");

				msg.click(); //some pixie dust required
			},
			// other available options:
			//url:       url,         // override for form's 'action' attribute
			url: cform.get(0).action+'/mode/ajax',
			//type:      type,        // 'get' or 'post', override for form's 'method' attribute
			type: "POST",
			//dataType:  null,        // 'xml', 'script', or 'json' (expected server response type)
			//clearForm: true,        // clear all form fields after successful submit
			resetForm: true        // reset the form after successful submit

			// $.ajax options can be used here too, for example:
			//	timeout:   3000
        });
		/* old form processing
		$.ajax({
			type: "POST",
			url: cform.get(0).action+'/mode/ajax',
			data: cform.serialize(),
			success: function(data) {
				$('<div id="statusMsg">'+data+'</div>').hide().appendTo('body');
				var msg = $(document.createElement("a")).attr({ 
		          href: "#statusMsg"
		        }).fancybox({
					'hideOnContentClick': false,
					'frameWidth':300,
					'frameHeight':125,
					'padding':20,
					'overlayShow':false
				});
				
				msg.click(); //some pixie dust required
				cform.get(0).reset();
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			  // typically only one of textStatus or errorThrown 
			  // will have info
			  //alert(this); // the options for this ajax request
			  $('<div id="statusMsg">'+XMLHttpRequest.responseText+'</div>').hide().appendTo('body');
				var msg = $(document.createElement("a")).attr({ 
		          href: "#statusMsg"
		        }).fancybox({
					'hideOnContentClick': false,
					'frameWidth':300,
					'frameHeight':125,
					'padding':20,
					'overlayShow':false
				});
				
				msg.click(); //some pixie dust required
			}
		});
		*/
		
	};

	function prepFields(fields){
		return fields.each(function () {
			var input = $(this).addClass('out');
			var text = input.val();

			if (text) {
				input.val(text);
				input.blur(function () {
					if (input.val() === '') {
						input.addClass('out').val(text);
					}
				}).focus(function () {
					if (input.val() === text) {
						input.val('');
					}
					input.removeClass('out').next().hide();
				});

				cform.submit(function() {
					if (input.hasClass('out')) {
						input.val('');
					}
				});
				input.blur();
			} else {
				input.focus(function(){
					input.next().hide();
				})
			}
			$('<span class="alert" />').hide().appendTo(input.parent());
		});
	};

	function checkEmailAddress(e) {
		var goodEmail = e.match(/\b(^(\S+@).+((\.com)|(\.biz)|(\.info)|(\.name)|(\.museum)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
		if (goodEmail){
			return true
		} else {
			return false
		}
	};

	return {
		init : initialize
	};
}();
$(function(){
	DM.ContactForm.init('#contactForm');
});