if (typeof($p) == 'undefined') $p = {};
if (typeof($p.valConfig) == 'undefined') $p.valConfig = {};

$p.notify = function(){
	var charCount = doc.getElementById('charCount');
	var form = doc.getElementById('notify');
	var submitBtn = doc.getElementById('submit');
	var container = doc.getElementById('formContainer');
	var customerName = doc.getElementById('customerName'); 
	var customerEmail = doc.getElementById('customerEmail'); 
	
	if(doc.getElementsByTagName('textarea')){
		var msg = true;
		var textarea = doc.getElementsByTagName('textarea')[0];
	}
	
	// Validates the count of the text area
	function validateCount(count){
		if(count < 0){
			$(charCount).addClass('red').addClass('bold');
			//disable submit button
			$(submitBtn).unbind('click').addClass('inactive');
			
		} else if($(charCount).hasClass('red')){
			$(charCount).removeClass('red').removeClass('bold');
			//enable submit
			$(submitBtn).click(function(){form.submit();}).removeClass('inactive');
		}		
	}
	// initiate text areas
	function initTextareas(){
		$(textarea).focus(function(){
			if($(this).hasClass('default')){
				this.value = '';
				$(this).removeClass('default');
			}
		});
		$(textarea).keyup(function(){
			var charsLeft = $p.getCharsLeft(this, 500);
			charCount.innerHTML = charsLeft;
			validateCount(charsLeft, charCount);
		});
		$(textarea).blur(function(){
			if($p.trim(this.value).split('').length == 0){
				// Remove the message paragraph if it's there and empty
				$('#messagePar').remove();
				resetMessage();
			} else {
				// create a new paragraph about the one containing the user's email address if there isn't one
				if(!doc.getElementById('messagePar')) {
					$('<p id="messagePar">A personal note from your patient:</p>').insertBefore('#emailPar');
				}
				// append the message (or replace it
				if(!doc.getElementById('userMessage')) {
					$('<span id="userMessage" class="block">' + this.value + '</span>').appendTo('#messagePar');
				} else {
					doc.getElementById('userMessage').innerHTML = this.value;
				}
			}
		});		
	}
	// Reset the customer message 
	function resetMessage(){
		textarea.value = 'Enter a personal note here';
		$(textarea).addClass('default');
		charCount.innerHTML = '500';		
	}
	// Clear the form
	function clearForm(){
		$(form).find('input:text').each(function(){
			this.value = '';
		});
		
		if(msg){
			resetMessage(textarea);
		}
		
		$('#errors').remove();
		$('.error').removeClass('error');
	}
	// Submit the form via AJAX
	function submitNotify(){
		// If the text area is the default message clear it
		if($(textarea).hasClass('default')){
			textarea.value = '';
		}
		$.post(
			'/physicians/notify',
			$(form).serialize(),
			// on success clear the form and show a message
			function(msg){
				$(form).remove();
				$('#errors').remove();
				$(msg).appendTo(container);
			},
			'HTML'
		);
	}
	// Auto fill values into form email from user input
	function autofill(field){
		field.value = $p.trim(field.value);
		if(field.value.length > 0){
			$('.' + $(field).attr('id')).each(function(){
				this.innerHTML = field.value;
			});
		}		
	}
	// set it up
	function init(){
		// Textarea awesomeness
		if(msg){
			initTextareas();
		}
		// After they submit make this happen
		//$p.valConfig.formAction = submitNotify;
		
		// Customer name slickness
		// If there's something there onload
		if(customerName.value != '') {
			autofill(customerName);
		}
		// When they do fill it in
		$(customerName).blur(function(){
			autofill(this);
		});
		
		// Same thing for email
		// If there's something there onload
		if(customerEmail.value != '') {
			autofill(customerEmail);
		}
		// When they do fill it in
		$(customerEmail).blur(function(){
			autofill(this);
		});		
		
		// And perhaps clear it
		$('#cancel').click(function(){
			// Make sure they actually want to
			var clear = confirm("Clear the form?")
			if(clear) clearForm();
		});
	}
	
	return {
		init:init,
		submitNotify:submitNotify
	};
}();


$(doc).ready(function(){
	$p.notify.init();
})

