$(document).ready(function() {

	// set default easing equation
	jQuery.easing.def = "easeInOutExpo";

// ----------------------------------------------------------------
// back and forth arrows for homepage
// ----------------------------------------------------------------
	var totalProjects 	= $('.project').length;
	var projectCount	= 1;
	
	$('.moveRight').click( function() {
		if (projectCount < totalProjects) {
			$('#project_container').animate({left: "-=900px"}, { duration: 800, queue: true, complete:checkNums });
		  	projectCount++;
			$('.moveLeft').fadeIn('fast');
		}
		return false;
	});
	
	
	$('.moveLeft').click( function() {
		if (projectCount > 1) {
			$('#project_container').animate({left: "+=900px"}, { duration: 800, queue: true, complete:checkNums });
			projectCount--;
			$('.moveRight').fadeIn('fast');
		}
		return false;
	});
	
	function checkNums() {
		if (projectCount >= totalProjects) {
			$('.moveRight').fadeOut('fast');
		} else if (projectCount <= 1) {
			$('.moveLeft').fadeOut('fast');
		}
		
		var textNum = "0" + projectCount;
		$('.currentProject').text(textNum);
	};



// ----------------------------------------------------------------
// work page animations
// ----------------------------------------------------------------
	$('a[rel="external"]').click( function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	
	//----------------------------------------------------------------------
	//-- Work Page Slider
	//----------------------------------------------------------------------
	// set first thumb's opacity:
	$('.project_thumbs img').eq(0).addClass('active_thumb');
	
	// set init height
	if ($('.project_images img').get(0) != undefined) {
		
		var firstImgHeight = $('.project_images img').get(0).height;
		$('#image_gallery').animate({height: firstImgHeight + "px"}, 800);
		
		
		if (firstImgHeight > $('#col2').height() - 40) {
			$('#col1').animate({'height': parseInt(firstImgHeight + 40) + "px"}, 800);
		}		
		
		
		// store all heights
		var height_arr = [];
		for (var i=0; i < $('.project_images img').length; i++) {
			height_arr.push($('.project_images img').get(i).height);
		}
		
		
	}
	
	
	// thumb click function
	$('.thumb').click( function() {
		var num 			= $(this).parent().index();
		var amtToMove 		= 0;
		var slice_arr		= height_arr.slice(0,num);
		
		for (x in slice_arr) {
			amtToMove += parseInt(slice_arr[x]);
		}
		
		var moveTo = (0 - amtToMove);
		
		// animate all
		$('.project_images').animate({top: moveTo + "px"}, {duration:800, queue:true});
		$('#image_gallery').animate({height: height_arr[num] + "px"}, 800);
		
		$('#col1').animate({'height': parseInt(height_arr[num] + 40) + "px"}, 800);

		
		$('.project_thumbs img').removeClass('active_thumb');
		$(this).find('img').addClass('active_thumb');
		
		return false;
	});


// ----------------------------------------------------------------
// fancybox stuff
// ----------------------------------------------------------------
	$(".fancyLink").fancybox({
		"transitionIn":'elastic',
		"transitionOut" : 'fade',
		"speedIn" : 300,
		"overlayOpacity" : 0.8,
		"overlayColor" : '#000',
		"centerOnScroll" : true
	});


// ----------------------------------------------------------------
// tipsy stuff
// ----------------------------------------------------------------
	$('#project_nav li a').tipsy({gravity:'s', fade:true});
	

// ----------------------------------------------------------------
// contact form validation
// ----------------------------------------------------------------
	$("#contact_form").validate({
		errorClass: "invalid",
		validClass: "valid",
		
		rules: {
			contact_name:	{ required: true, minlength: 2 },
			contact_email: 	{ required: true, email: true },
			contact_message:{ required: true, minlength: 2 }
		},
		
		messages: {
			contact_name: "Your name is required.",
			contact_email: "A valid email is required.",
			contact_message: "A message is required."
		},
		
		highlight: function(element, errorClass, validClass) {
			$(element).addClass(errorClass).removeClass(validClass);
		},
		
		errorContainer: "#errorMessage",
		
		errorElement: "li",
		
		errorPlacement: function(error, element) {
			error.replaceAll($('#errorMessage li')).addClass('error');
		},
		
		invalidHandler: function(form, validator) {
			$("#errorMessage").slideDown('fast');
		},
		
		submitHandler: function(form) {
			form.submit();
		}
		
	});	
	

});
