var pause = false;
var navTrigger = false;
$(document).ready(function() {
	//set easing
	$.easing.def = 'easeOutCubic';
	
	//get vimeo players ready
	var vimeoPlayers = $('iframe'), player;
	for (var i = 0, length = vimeoPlayers.length; i < length; i++) {
		player = vimeoPlayers[i];
		$f(player).addEvent('ready', ready);
	}
	function ready(player_id) {
		if(player_id=="vimeo_action") $(".video.action .thumb").fadeOut();
		$f(player_id).addEvent('finish', finish);
	}
	function finish(player_id) {
		$f(player_id).api("unload");
	}
	
	//scrolling navigation
	var scrollFrom = 0;
	$('#nav a').click(function(e){
		if(!$(this).hasClass("active")) {
			pause = true;
			navTrigger = true;
			$("#nav a.active").removeClass("active");
			$(this).addClass("active");
			scrollFrom = $(window).scrollTop();
		} else e.preventDefault();
	});	
	$(window).bind('hashchange',function(e){
		if(navTrigger) {
			$(window).scrollTop(scrollFrom);
			var target = '#' + location.hash.replace(/#\//,'');
			
			//detect iPad/iPhone/iPod
			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') $('html,body').animate({scrollTop: $(target).offset().top-24},1000, function() {pause = false;});
			else $('html,body').animate({scrollTop: $(target).offset().top-177},1000, function() {pause = false;});
		}
	});
	
	//scrolling on load
	var incoming = location.hash.split("#/");
	if(incoming.length>1) {
		pause = true;
		$("#nav a.active").removeClass("active");
		$("#nav a[href=#/"+incoming[1]+"]").addClass("active");
		
		$(window).scrollTop(scrollFrom);
		var target = '#'+incoming[1];
		
		//detect iPad/iPhone/iPod
		if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') $('html,body').animate({scrollTop: $(target).offset().top-24},1000, function() {pause = false;});
		else $('html,body').animate({scrollTop: $(target).offset().top-177},1000, function() {pause = false;});
	}
	
	//change navigation on scroll
	$(window).scroll(function(e) {
		if(!pause) {
			navTrigger = false;
			var scrollTop = $(window).scrollTop();
			
			var reelTop = 0;
			if(scrollTop>=reelTop) {
				$("#nav a.active").removeClass("active");
				$("#nav a[href=#/reel]").addClass("active");
				location.hash = "#/reel";
			}
			
			//detect iPad/iPhone/iPod
			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') var bioTop = $("#bio").offset().top-24;
			else var bioTop = $("#bio").offset().top-177;
			
			if(scrollTop>=bioTop) {
				$("#nav a.active").removeClass("active");
				$("#nav a[href=#/bio]").addClass("active");
				location.hash = "#/bio";
			}
			//detect ipad
			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod'){
				var contactTop = $("#contact").offset().top-24;
			}else{
				var contactTop = $("#contact").offset().top-177;
			}
			
			if(scrollTop>=contactTop) {
				$("#nav a.active").removeClass("active");
				$("#nav a[href=#/contact]").addClass("active");
				location.hash = "#/contact";
			}
		}
	});
	
	//feature scroller
	$("#feature").scrollable({
		circular:true,
		easing:'linear',
		keyboard:false,
		onBeforeSeek: function() {
			$(".video .thumb").show();
		},
		onSeek: function() {
			$(".video.active .thumb").fadeOut();
			$(".cloned iframe").attr("src","").attr("id","");
		}
	}).navigator(".navi");
	
	//feature scroller navigation
	$(".next").click(function() {
		$(".video.active").removeClass("active");
		$(".video .thumb").show();
		$(".category.left").css("width","592px").removeClass("left");
		
		var current = $(".navi a.active");
		var current_vid = current.attr("id");
		
		//stop current video from playing
		$f("vimeo_"+current_vid).api("unload");
		
		if(current.next().length) {
			var next = current.next();
			$(".video."+next.attr("id")).addClass("active").removeClass("hover");
		} else {
			var next = $(".navi a").first();
			$(".video."+next.attr("id")).addClass("active").removeClass("hover");
		}
		
		if(next.next().length) {
			var hover = next.next();
			$(".video."+hover.attr("id")).addClass("hover");
		} else {
			var hover = $(".navi a").first();
			$(".video."+hover.attr("id")).addClass("hover");
		}
		setTimeout('$(".video.'+current.attr("id")+' .category").animate({width: "172"}, {duration: 280,queue: false,easing: "linear",complete: function() {$(this).addClass("left");}})',118);
		return false;
	});
	$(".prev").click(function() {
		$(".video.active").removeClass("active");
		$(".video .thumb").show();
		$(".category.left").animate({
			width: '592'
		  }, {
			duration: 282,
			queue: false,
			easing: 'linear',
			complete: function() {
				$(this).removeClass("left");
			}
		});
		
		var current = $(".navi a.active");
		var current_vid = current.attr("id");
		
		//stop current video from playing
		$f("vimeo_"+current_vid).api("unload");
		
		if(current.prev().length) {
			var prev = current.prev();
			$(".video."+prev.attr("id")).addClass("active").removeClass("hover");
		} else {
			var prev = $(".navi a").last();
			$(".video."+prev.attr("id")).addClass("active").removeClass("hover");
		}
		
		if(prev.prev().length) {
			var left = prev.prev();
			$(".video."+left.attr("id")+" .category").css("width","172px").addClass("left");
			$(".video."+left.attr("id")).addClass("hover");
		} else {
			var left = $(".navi a").last();
			$(".video."+left.attr("id")+" .category").css("width","172px").addClass("left");
			$(".video."+left.attr("id")).addClass("hover");
		}
		return false;
	});
	$(".next").hover(function() {
		$("#feature .video.active").next().addClass("hover");
	}, function() {
		$("#feature .video.active").next().removeClass("hover");
	});
	$(".prev").hover(function() {
		$("#feature .video.active").prev().addClass("hover");
	}, function() {
		$("#feature .video.active").prev().removeClass("hover");
	});
	
	//contact form
	$(".submit").click(function() {
		$("#contact_form").submit(); return false;
	});
	$("#contact_form").submit(function() {
		//reset errors
		$(".required").removeClass("required");
		
		//form validation
		var email = $("input[name=email]").val();
		var message = $("textarea[name=message]").val();
		if(email=="Your Email") {
			$("input[name=email]").addClass("required");
			$("#message").html("Please enter your email address.").fadeIn();
			return false;
		} else if (!validEmail(email)) {
			$("input[name=email]").addClass("required");
			$("#message").html("Please enter a valid email address.").fadeIn();
			return false;
		}
		
		if(message=="Your Message") {
			$("textarea[name=message]").addClass("required");
			$("#message").html("Please enter a message.").fadeIn();
			return false;
		}
		
		//send the ajax request
		var query = "email="+$("input[name=email]").val()+"&message="+$("textarea[name=message]").val();
		$.post('/inc/process.php', query, function(r){
				$('#message').html(r.message).fadeIn();
				setTimeout('$("#message").fadeOut()',5000);
				
				//reset the form fields
				$("input[name=email]").val('Your Email');
				$("textarea[name=message]").val('Your Message');
		 }, 'json');
		return false;
	});
	
	//detect iPad/iPhone/iPod and adjust footer padding
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
		var footerPadding = $(window).height()-$("#contact").outerHeight()-52;
		$("#footer").css("padding-bottom",footerPadding);
	} else {
		var footerPadding = $(window).height()-$("#contact").outerHeight()-204;
		$("#footer").css("padding-bottom",footerPadding);
	}
});

$(window).resize(function() {
	//detect iPad/iPhone/iPod and adjust footer padding
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
		var footerPadding = $(window).height()-$("#contact").outerHeight()-52;
		$("#footer").css("padding-bottom",footerPadding);
	} else {
		var footerPadding = $(window).height()-$("#contact").outerHeight()-204;
		$("#footer").css("padding-bottom",footerPadding);
	}
});

 //validate email address
function validEmail(email) {
	if(email.length>0) {
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (emailPattern.test(email) == false) return false;
		else return true;
	} else return false;
}
