this.validate = function(){
	$("span.error").remove();
	var valid = true;
	
	var name = $("#name").val();	
	var phone = $("#phone").val();		
	var email = $("#email").val();	
	var message = $("#message").val();	
	
	this.checkEmail = function(str){
	  var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
	  return (str.search(regEx) != -1);
	};	
	this.checkPhone = function(str){
	  var regEx = /[\d\s_]{6,}/;
	  return (str.search(regEx) != -1);
	};				
	
	this.error = function(obj,text){
		//var parent = $(obj).parent();
		var parent = $(".msg");
		parent.append("<span class=\"error\">"+ text +"</span>");
		$("span.error",parent).hide().show("fast");
	};	
	
	if(name == "") {
		error($("#name"),"Please tell us your name.")
		valid = false;
	};	
	//email
	if(!checkEmail(email)) {
		error($("#email"),"We need a valid email address.")
		valid = false;
	};			
	//message
	if(message == ""){
		error($("#message"),"Please write your message.")
		valid = false;
	};					
	return valid;

};

this.nav = function(){	

	$("#nav li a").each(
		function(){
			$(this).parent().append("<span>"+ $(this).attr("title") +"</span>");
			$(this).attr("title","");
			}
	);
	
	$("#nav li").hover(
	  function () {
		$("span:first",this).show();
		$("a:first",this).addClass("over");
	  }, 
	  function () {
		$("span",this).hide();
		$("a",this).removeClass("over");
	  }
	);	
};

this.testimonials = function(){
	if($("#testimonialslist").children().length > 1) {
		
		$("#header").append('<ul id="subnav"><li class="prev" style="display:none;"><a href="javascript:void(0);">&laquo; Previous</a></li><li class="next"><a href="javascript:void(0);">Next &raquo;</a></li></ul>')
	
		var temp = 1;
		var total = $("#testimonialslist").children().length;
	
		$("#subnav .prev").click(function(){
				prev();										 
		});
		$("#subnav .next").click(function(){
				next();										 
		});
		
		function showcontent(){
				$("#testimonialslist li").hide();
				$("#testimonialslist li::nth-child(" + temp + ")").fadeIn("fast");
				if (temp == 1) { 
					$("#subnav .prev").hide();
				} else {
					$("#subnav .prev").css("display","inline");
				}
				if (temp == total) {
					$("#subnav .next").hide();
				} else {
					$("#subnav .next").css("display","inline");
				}
		};
		
		function next(){
				temp = (temp == total) ? 1 : temp+1;
				showcontent();
		};
		function prev(){
				temp = (temp == 1) ? total : temp-1;
				showcontent();
		};
	
		showcontent();
	
	};
};

this.homepage = function(){

};

$(document).ready(function(){	
	$("#contactForm button").click(
		function(){
			return validate();
		});	
	nav();
	testimonials();
});