/* Modernizr 2.0.6 (Custom Build) | MIT & BSD
 * Build: http://www.modernizr.com/download/#-cssanimations-csstransforms-csstransforms3d-csstransitions-cssclasses-teststyles-testprop-testallprops-prefixes-domprefixes
 */
;window.Modernizr=function(a,b,c){function C(a,b){var c=a.charAt(0).toUpperCase()+a.substr(1),d=(a+" "+o.join(c+" ")+c).split(" ");return B(d,b)}function B(a,b){for(var d in a)if(k[a[d]]!==c)return b=="pfx"?a[d]:!0;return!1}function A(a,b){return!!~(""+a).indexOf(b)}function z(a,b){return typeof a===b}function y(a,b){return x(n.join(a+";")+(b||""))}function x(a){k.cssText=a}var d="2.0.6",e={},f=!0,g=b.documentElement,h=b.head||b.getElementsByTagName("head")[0],i="modernizr",j=b.createElement(i),k=j.style,l,m=Object.prototype.toString,n=" -webkit- -moz- -o- -ms- -khtml- ".split(" "),o="Webkit Moz O ms Khtml".split(" "),p={},q={},r={},s=[],t=function(a,c,d,e){var f,h,j,k=b.createElement("div");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:i+(d+1),k.appendChild(j);f=["&shy;","<style>",a,"</style>"].join(""),k.id=i,k.innerHTML+=f,g.appendChild(k),h=c(k,a),k.parentNode.removeChild(k);return!!h},u,v={}.hasOwnProperty,w;!z(v,c)&&!z(v.call,c)?w=function(a,b){return v.call(a,b)}:w=function(a,b){return b in a&&z(a.constructor.prototype[b],c)};var D=function(a,c){var d=a.join(""),f=c.length;t(d,function(a,c){var d=b.styleSheets[b.styleSheets.length-1],g=d.cssRules&&d.cssRules[0]?d.cssRules[0].cssText:d.cssText||"",h=a.childNodes,i={};while(f--)i[h[f].id]=h[f];e.csstransforms3d=i.csstransforms3d.offsetLeft===9},f,c)}([,["@media (",n.join("transform-3d),("),i,")","{#csstransforms3d{left:9px;position:absolute}}"].join("")],[,"csstransforms3d"]);p.cssanimations=function(){return C("animationName")},p.csstransforms=function(){return!!B(["transformProperty","WebkitTransform","MozTransform","OTransform","msTransform"])},p.csstransforms3d=function(){var a=!!B(["perspectiveProperty","WebkitPerspective","MozPerspective","OPerspective","msPerspective"]);a&&"webkitPerspective"in g.style&&(a=e.csstransforms3d);return a},p.csstransitions=function(){return C("transitionProperty")};for(var E in p)w(p,E)&&(u=E.toLowerCase(),e[u]=p[E](),s.push((e[u]?"":"no-")+u));x(""),j=l=null,e._version=d,e._prefixes=n,e._domPrefixes=o,e.testProp=function(a){return B([a])},e.testAllProps=C,e.testStyles=t,g.className=g.className.replace(/\bno-js\b/,"")+(f?" js "+s.join(" "):"");return e}(this,this.document);





function validateEmail(email) {   
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}


function Timer(func, delay, start) {
    this.func = func;
    this.delay = delay;
    this.running = false;
    if(start) this.start();
}

Timer.prototype.start = function(delay) {
    this.running = true;
    return this.interval = setInterval(this.func, (delay || this.delay));
}

Timer.prototype.stop = function() {
    this.running = false;
    clearInterval(this.interval);
}


$(window).bind("load",function(){
		
	// Starts a new class
	timer = new Timer(function() {
		handleSlide();
	}, 5000);
	timer.start();
	
	setTimeout(function(){
		$(".headline .part").removeClass("rollIn rollOut");
	}, 1100);

	function handleSlide(toSelect){
		var toSelect = (toSelect == '') ? false : toSelect,
		 	elem = $(".headline nav a.selected"),
			num = elem.index() + 2,
			num = (num === 4) ? 1 : num;
			if (toSelect) {
				num = toSelect;
			}
			
		if ($("html.cssanimations.csstransforms.csstransforms3d.csstransitions").length === 1) {
		
			$(".headline .part").not(":nth-child(" + num  + ")").addClass("rollOut").delay(1000).stop(true, true).fadeOut(function(){
				$(this).removeClass("rollOut");
			});
			$(".headline .part:nth-child(" + num + ")").stop(true, true).fadeIn();
			$(".headline nav a").not(":nth-child(" + num + ")").removeClass("selected");
			$(".headline nav a").filter(":nth-child(" + num + ")").addClass("selected");
		
		} else {
		
			$(".headline .part").not(":nth-child(" + num  + ")").stop(true, true).fadeOut(1000);
			$(".headline .part:nth-child(" + num + ")").stop(true, true).fadeIn(1000);
			$(".headline nav a").not(":nth-child(" + num + ")").removeClass("selected");
			$(".headline nav a").filter(":nth-child(" + num + ")").addClass("selected");
		
		}
		
		setTimeout(function(){
			$(".headline .part").removeClass("rollIn rollOut");
		}, 1100);
		
	}
	
	function prevItem() {
		var active = $(".headline nav a.selected"),
			last = $(".headline nav a:last"),
			slide = (active.prev("a").length === 1) ? active.prev() : last,
			num = slide.index() + 1;
		handleSlide(num);	
		$(".headline nav").removeClass("timed");
		timer.stop();
	}
	
	function nextItem() {
		var active = $(".headline nav a.selected"),
			first = $(".headline nav a:first"),
			slide = (active.next("a").length === 1) ? active.next() : first,
			num = slide.index() + 1;
		handleSlide(num);
		$(".headline nav").removeClass("timed");
		timer.stop();
	}

	$(".headline nav a").bind("click", function(){	
		var num = $(this).index() + 1;
		handleSlide(num);
		$(".headline nav").removeClass("timed");
		timer.stop();
	});
	
	$(document).keydown(function(e){
	    if (e.keyCode == 37) { // Left key
			prevItem();
	    }
	    if (e.keyCode == 39) { // Right key
	        nextItem();
	    }
	});
	
	$("#contactformwrapper").fadeIn();
	$("#contactform").attr("action","/contact-send.php");
	
	$("#contactform button").bind("click", function(){
		
		//console.log("Button presses");

		var form = $("#contactform"),
			name = form.find("input[name='name']"),
			email = form.find("input[name='email']"),
			budget = form.find("input[name='budget']"),
			button = form.find("button"),
			message = form.find("textarea");
		
		// Validation	
		(name.val() === '') ? name.addClass("error animated shake") : name.removeClass("error animated shake");
		(email.val() === '' || !validateEmail(email.val())) ? email.addClass("error animated shake") : email.removeClass("error animated shake");
		(message.val() === '') ? message.addClass("error animated shake") : message.removeClass("error animated shake");
		
		setTimeout(function(){
			$(".animated.shake").removeClass("animated shake");
		}, 1000);	

		//console.log("Length: " + $("input.error").length);

		// If all good, send away
		if ($(".error").length < 1) {
			
			//console.log("no errors");
			
			$(this).addClass("loading");
			$(this).html("Sending...");
			
			$.ajax({
				type: "POST",
				url: "/contact-send.php",
				data: 'name=' + name.val() + '&email=' + email.val() + '&budget=' + budget.val() + '&message=' + message.val(),
				error: function(){
					button.html("There was an error. Try again please.");
					button.removeClass("loading").addClass("error");
				},
				success: function() {
					setTimeout(function(){
						name.attr("disabled","disabled");
						email.attr("disabled","disabled");
						budget.attr("disabled","disabled");
						message.attr("disabled","disabled");						
						setTimeout(function(){
							form.find("label:nth-child(1)").addClass("fadeOutLeft animated");
						}, (200 * 1));
						setTimeout(function(){
							form.find("label:nth-child(2)").addClass("fadeOutLeft animated");
						}, (200 * 2));
						setTimeout(function(){
							form.find("label:nth-child(3)").addClass("fadeOutLeft animated");
						}, (200 * 3));
						setTimeout(function(){
							form.find("label:nth-child(4)").addClass("fadeOutLeft animated");
						}, (200 * 4));
						setTimeout(function(){
							form.find("label").slideUp(500);
						}, 200 * 6);
						button.attr("disabled","disabled").html("Message Sent. I'll get back to you soon. :)");
						button.removeClass("loading").addClass("sent");
					}, 1500);
				}
			});	
		}

		return false;
	});
	
	$("#contactform .error").bind("keyup blur", function(){
		var val = $(this).val();
		if (val !== '') {
			$(this).removeClass("error");
		}
	});
	
	/* $("#contactform .required").bind("keyup blur", function(){
		var val = $(this).val();
		if (val === '') {
			$(this).addClass("error");
		} else {
			$(this).removeClass("error");
		}
	}); */
		
});
