$(document).ready(function(){
	/* !Cufon text repacement ······················································· */
	Cufon.replace(
		'#main-nav', {
			textShadow: '0 -1px 0 hsla(0, 0%, 0%, .75);',
			hover: true
		});
	
	Cufon.replace(
		'#about h2', {
			textShadow: '0 1px 0 hsla(0, 0%, 0%, .75);'
		});
	
	Cufon.replace(
		'#blog h2', {
			textShadow: '0 1px 0 hsla(0, 0%, 0%, .75);'
		});
		
	Cufon.replace(
		'#blog h3', {
			textShadow: '0 1px 0 hsla(0, 0%, 0%, .75);'
		});
	
	Cufon.replace(
		'button', {
			textShadow: '0 1px 0 hsla(0, 0%, 0%, .75);'
		});
		
	Cufon.replace(
		'#blog button', {
			textShadow: '0 -1px 0 black;'
		});

	
	/* !Goodies Bounce CSS animation ················································ */
	$("#goodies #content .group ul li a").click(function(){
		$(this).parents("li").find("img").addClass("active");
		setTimeout(function() {
			$("img.active").removeClass("active");
		}, 700); //The CSS animation duration
	});
	
	
	/* !Portfolio section navigation ················································ */
	$("#portfolio-sections").tabs("#content > div", {
		tabs: "li"
	});
	
	
	/* !Scrollables ································································· */
	// Custom easing "easeInOutExpo" from George McGinley's jQuery Easing v1.3
	$.easing.custom = 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;
	}
	
	// !Portfolio navigation
	$(".portfolio-nav-wrapper").scrollable({
		vertical: true, 
		size: 6,
		clickable: false,
		nextPage: ".next-page",
		prevPage: ".prev-page"
	}).mousewheel();
	
	// !Portfolio works
	$(".section").scrollable({
		vertical: true,
		size: 1,
		clickable: false,
		keyboard: 'static', // Up/down keys will always control this scrollable
		easing: 'custom', 
		speed: 600,
		
		// Assign left/right keys to the actively viewed scrollable
		onSeek: function(event, i) {
			horizontal.scrollable(i).focus();
		}
	}).navigator(".portfolio-nav");
	
	// !Portfolio works pages
	// Each horizontal one has its own navigator instance
	var horizontal = $("#portfolio .scrollable").scrollable({
		size: 1,
		loop: true,
		easing: 'custom', 
		speed: 800
	}).navigator(".paginator");
	
	// !When page loads setup keyboard focus on the first horzontal scrollable
	horizontal.eq(0).scrollable().focus();
	
	// !Goodies pages
	$("#goodies .scrollable").scrollable({
		size: 1, 
		clickable: false,
		easing: 'custom', 
		speed: 800
	}).mousewheel().navigator(".paginator");
	
	// !Contact Form
	$('form#contactForm').submit(function() {
		$('form#contactForm .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				var labelText = $(this).prev('label').text();
				$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'</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 '+labelText+'</span>');
					hasError = true;
				}
			}
		});
		if(!hasError) {
			$('form#contactForm button').fadeOut('fast', function() {
				$(this).parent().append('<p>Sending...</p>');
			});
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data){
				$('form#contactForm').slideUp("slow", function() {				   
					$(this).before('<div id="thanks"><p class="thanks"><strong>Thanks!</strong> Your email was successfully sent, I will be in touch soon!</p></div>');
				});
			});
		}	
		return false;	
	});

	if ($('.trigger').length) { // If it exists
		$(".trigger").fancybox({
			'padding': 0,
			'margin': 0,
			'speedIn': 600, 
			'speedOut': 200, 
			'overlayShow': true,
			'overlayOpacity': 0.8,
			'overlayColor': '#59436a',
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			'speedIn': 300
		});
	} else { return false; }
});
