/* ### [JAVASCRIPT INFO] ###############################################################
########################################################################################
	
	Name: Website functionality
	Author: Alberto Calvo - contact@intemperie.me
	Author URI: http://intemperie.me
	Version: 1.0
	Description: Website functionality using jQuery + jQuery tools
	
########################################################################################
*/

$(document).ready(function(){
	// !Accessibility
	$("body").addClass("js-enabled");
	if ($("#home").length) {
		//Remove the tabindex for preventing bad behavior with the scrollable
		$("#home #items a").attr("tabindex","-1");
	}
	
	// !Custom easing from George McGinley's jQuery Easing v1.3
	$.easing.easeInOutExpo = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
	
	// !Home Scrollable
	$("#home #items-wrapper").scrollable({
		size: 1,
		clickable: false,
		keyboard: 'static',
		easing: 'easeInOutExpo', 
		speed: 1200,
		loop: true
	}).navigator({
		navi: "#navigator"
	}).autoscroll({
		interval: 12000,
		autopause: true
	});
	
	// !Portfolio Navigation
	$(".works #content nav").scrollable({
		size: 10,
		clickable: false,
		keyboard: false,
		nextPage: "#next",
		prevPage: "#prev"
	}).mousewheel();
	
	// !Portfolio Works
	$(".works #content nav").tabs("#items li", {
		effect: 'fade',
		fadeOutSpeed: 700,
		fadeInSpeed: 500,
		current: 'active'
	});
	
	// !Portfolio Keyboard Navigation
	$(document).keydown(function (e) {
		var apiTabs = $(".works #content nav").tabs(0);
		var apiScrollable = $(".works #content nav").scrollable();
		
		// Assign Left/Right keys to Prev./Next events on the Tabs & Scrollable
		var keyCode = e.keyCode || e.which,
		arrow = { left: 37, right: 39 };	
		
		switch (keyCode) {
			case arrow.left:
				apiTabs.prev();
				apiScrollable.prev();
			break;
		case arrow.right:
				apiTabs.next();
				apiScrollable.next();
			break;
		}
	});
	
	// !Contact Form
	$('#contact-us').submit(function() {
		$('#contact-us .error').remove();
		var hasError = false;
		$('[required]').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				var labelText = $(this).prev('label').text();
				$(this).parent().append('<span class="error">This is a required field</span>');
				hasError = true;
			} else if($(this).hasClass("email")) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					var labelText = $(this).prev('label').text();
					$(this).parent().append('<span class="error">You entered an invalid email address</span>');
					hasError = true;
				}
			}
		});
		if(!hasError) {
			$('#contact-us button').fadeOut('fast', function() {
				$(this).parent().append('Sending...');
			});
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data){
				$('#contact-us').slideUp("slow", function() {				   
					$(this).before('<div id="thanks"><strong>Thanks!</strong> Your email was successfully sent. We\'ll be in touch soon.</div>');
				});
			});
		}
		return false;	
	});
	
	// !Scroll the Portfolio Nav to the current element selected (for incoming links)
	if ($('.works').length) { // If it exists
		$(function(){
			// Get the current element
			var currentTabIndex = $(".works #content nav").tabs(0).getIndex();
			var apiScrollToIndex = $(".works #content nav").scrollable();
			
			// Pass the current Tab index to the Scrollable and scroll to it
			apiScrollToIndex.seekTo(currentTabIndex);
		});
	} else { return false; }
});