//-------------------------------
// Class
//-------------------------------

function ContactApp() {

	var _self = this;
	
	//-------------------------------
	// Properties
	//-------------------------------
	
	var post = "includes/mail/send.php";

	//-------------------------------
	// Constructor
	//-------------------------------
	
	function init() {
		$("ul.contact .email a").bind("click", onContactBtnClick);
		$("form.send #btn-submit").live("click", onSendFormSubmit);
		
		$("form input, form textarea").each(function(i, item) {
			if($(this).val().length) $("label[for=" + $(this).attr("id") + "]").hide();
		}).live("click focus select", function(event) {
			$("label[for=" + $(this).attr("id") + "]").hide();
			
		}).live("blur", function(event) {
			if(!$(this).val().length) $("label[for=" + $(this).attr("id") + "]").show();
		});
	}
	
	//-------------------------------
	// Public Methods
	//-------------------------------
	
	//-------------------------------
	// Private Methods
	//-------------------------------
	
	var validated = function() {
		var invalid = 0;
		
		$("form.send li[type=required]").each(function(i, item) {
			if($("input[type=text]", item).val().length == 0) {
				$("label.required", item).show();
				invalid++;
			} else {
				$("label.required", item).hide();
			}
		});
		
		return invalid;
	};

	//-------------------------------
	// Listeners
	//-------------------------------
	
	var onContactBtnClick = function(event) {
		$("form.send").toggle();
		
		return false;
	};
	
	var onSendFormSubmit = function(event) {
		if(validated()) return false;
	
		var message = "";
						
		$("form.send input:not([type=hidden][type=button]), form.send textarea, form.send ul li.linear").each(function(i, item) {
			
			if($(item).is("[type=radio]") && $(item).is(":checked")) {
				message		+= "<strong>" + $(".radiotitle[for=" + $(item).attr("name") + "]").text() + "</strong><br/>";
				message		+= $(item).val() + "<br/>\r\n";
			} else if($(item).is("[type=checkbox]") && $(item).is(":checked")) {
				message		+= "<strong>" + $(".checkboxtitle[for=" + $(item).attr("name") + "]").text() + "</strong><br/>";
				message		+= $(item).val() + "<br/>\r\n";
			} else if($(item).hasClass("linear")) {
				message		+= "<h2>" + $("h5", item).text() + "</h2>\r\n";
			
			} else if($(item).attr("type") != "radio" && $(item).attr("type") != "checkbox" && $(item).attr("class") != "linear") {
				message		+= "<strong>" + $("label[for=" + $(item).attr("id") + "]").text() + "</strong>: ";
				message		+= $(item).val() + "<br/>\r\n";
			}
		});
		
		$.post(post,
		   { from: $("#txtEmailFrom").val(), subject: $("form #subject").val(), message: message },
		    function(data){
		  		$("form.send label.submitted").fadeIn();
		 	}
		);
	
		return false;
	};
	
	//-------------------------------
	// Constants
	//-------------------------------
	


	init();
}
