$(function(){

	/* Navigation */
	$('#nav li a').click(function(){
		speed = 800;
		elem = $(this).parent();
		
		$('#nav li').removeClass('current');
		elem.addClass('current');
		
		if (elem.hasClass('home')){
			$('#main').animate({"margin-left":"0"},{queue:false,duration:speed});
			$('#nav').animate({"background-position":"17px 48px"},{queue:false,duration:speed});
		}
		else if (elem.hasClass('folio')){
			$('#main').animate({"margin-left":"-840px"},{queue:false,duration:speed});
			$('#nav').animate({"background-position":"89px 48px"},{queue:false,duration:speed});
		}
		else if (elem.hasClass('about')){
			$('#main').animate({"margin-left":"-1680px"},{queue:false,duration:speed});
			$('#nav').animate({"background-position":"162px 48px"},{queue:false,duration:speed});
		}
		else {
			$('#main').animate({"margin-left":"0"},{queue:false,duration:speed});
			$('#nav').animate({"background-position":"17px 48px"},{queue:false,duration:speed});
		}
		
		return false;
	});
	
	/* Testimonials */
	$('#scroll').cycle({
		fx:'scrollLeft',
		speed:800,
		timeout:10000,
		prev:'#scrollprev',
		next:'#scrollnext'
	});

	/* Forms */
	$.fn.cleanUp = function(){
		return this.each(function(){
			var type = this.type, tag = this.tagName.toLowerCase();
			
			if (tag == "form"){
				return $(":input", this).cleanUp();
			}
			if (type == "text" || tag == "textarea"){
				this.value = this.defaultValue;
				$("input, textarea").removeClass("error").addClass("success").blur();
				setTimeout(function(){$("input, textarea").removeClass("success");}, 4000);
			}						  
		});
	};

	$("#testimonialform").submit(function(){
		var errors = 0, reg = /^[_a-z0-9A-Z-+]+(\.[_a-z0-9A-Z-+]+)*@[a-z0-9A-Z-]+(\.[a-z0-9A-Z-]+)*(\.[a-z]{2,4})$/;		
		$("input, textarea").removeClass("error");
		$("#send").val("true");

		if ($("#name").val() == "" || $("#name").val() == "Name"){
			$("#name").addClass("error"); 
			errors++;
		}
		
		if (!reg.test($("#email").val())){
			$("#email").addClass("error"); 
			errors++;
		}

		if ($("#testimonial").val() == "" || $("#testimonial").val() == "Testimonial"){
			$("#testimonial").addClass("error");
			errors++;
		}
		
		if (errors == 0){		
			jQuery.ajax({
				type:"POST",
				data:$(this).serialize(),
				url:this.action,
				timeout:5000,
				success:function(data){
					$("#testimonialform").cleanUp();
				}
			});
		}
		
		return false;
	});
	
});

$(function(){
	$(function(){
		$.fn.onFocus = function(){
			return this.focus(function(){
				this.value = this.value === this.defaultValue ? "" : this.value;
			}).blur(function(){
				this.value = this.value === "" || /^\s+$/.test(this.value) ? this.defaultValue : this.value;
			});
		};
		$(function(){
			$("input, textarea").onFocus()
			.each(function(){
				var input = $(this);
				input.parents("form:eq(0)").submit(function(){
					return input[0].value !== input[0].defaultValue;
				});
			});
		});
	});
});
