$(document).ready(function(){
	orig_doc_height = $(document).height();
	function scrollLeft(px) {
		var width = $(window).width();
		var pad = (width - 960) / 2;
		var left = px + pad;
		return left + 'px';
	}
	function js_array_to_php_array (a) {
		var a_php = "";
		var total = 0;
		for (var key in a)
		{
			++ total;
			a_php = a_php + "s:" +
					String(key).length + ":\"" + String(key) + "\";s:" +
					String(a[key]).length + ":\"" + String(a[key]) + "\";";
		}
		a_php = "a:" + total + ":{" + a_php + "}";
		return a_php;
	}
	
	function Move(coord, content_name) {
		$('#scroller').css('left', scrollLeft(-coord));
		$('#content_blocks').css('left', scrollLeft(-coord));
		$('#grad').css('left', scrollLeft(-coord));
		var booga = $('.' + content_name).position();
		$('#footer_interior').css('left', booga.left);
		footer_height = (orig_doc_height - 380) - ($('#grad').height() + $('.' + content_name).height());
		if(footer_height <= 250) { footer_height = 250; }
		footer_pos = $('.' + content_name).height() + 150;
		$('#footer').css({'height': footer_height, 'top': footer_pos});
		
		// Push footer to bottom
		var push_footer = orig_doc_height - ($('#grad').height() + $('.' + content_name).height() + $('#footer').height());
		var t = push_footer + footer_height + footer_height;
		$('#footer').css('height', t);
	}
	
	function Move_Animate(coord, content_name) {
		$('#grad').animate({left:"-" + coord + "px"}, {duration:1000});
		$('#scroller').animate({left:scrollLeft(-coord)}, {duration:1000});
		$('#content_blocks').animate({left:scrollLeft(-coord)}, {duration:1000});
		var booga = $('.' + content_name).position();
		$('#footer_interior').css('left', booga.left);
		footer_height = (orig_doc_height - 380) - ($('#grad').height() + $('.' + content_name).height());
		if(footer_height <= 250) { footer_height = 250; }
		footer_pos = $('.' + content_name).height() + 150;
		$('#footer').css({'height': footer_height, 'top': footer_pos});
		
		// Push footer to bottom
		var push_footer = orig_doc_height - ($('#grad').height() + $('.' + content_name).height() + $('#footer').height());
		var t = push_footer + footer_height + footer_height;
		$('#footer').css('height', t);
	}
	
	$("#formID").validationEngine();
	
	var max_height = $('.about_content').height();
	var max_height = (max_height + 375);
	$('#content_blocks').css('min-height', max_height);
	
	if(document.location.hash == '#about') {
		Move(1500, 'about_content');
	}
	else if(document.location.hash == '#work_nav') {
		Move(6934, 'work_content');
	}
	else if(document.location.hash == '#contact_nav') {
		Move(8992, 'contact_content');
	}
	else {
		Move(4250, 'home_content');
	}

	$('.nav_home').click(function() {
		Move_Animate(4250, 'home_content');
	});
	$('.nav_about').click(function() {
		Move_Animate(1500, 'about_content');
		
	});
	$('.nav_work').click(function() {
		Move_Animate(6934, 'work_content');
	});
	$('.nav_contact').click(function() {
		Move_Animate(8992, 'contact_content');
	});
	
	$('form.validate button').click(function() {
		if($("form.validate").validationEngine('validate') == false) {
			return;
		}
		var inputs = $('form.validate').serialize();
		$.ajax({
			type: "POST",
			url: "/",
			dataType: 'json',
			data: inputs,
			success: function(j) {
				$('#form_box').slideUp('fast').html(j.message).slideDown('fast');
			}
		});
	});
	
	$('form.validate input, form.validate textarea').each(function() {
		if($(this).val() !== '') {
			$(this).siblings('label').animate({top:-18}, 200);
		}
	});
	
	$('form.validate input, form.validate textarea').focus(function() {
		$(this).siblings('label').animate({top:-18}, 200);
	});
	
	$('form.validate input, form.validate textarea').blur(function() {
		if($(this).val() == '') {
			$(this).siblings('label').animate({top:7}, 200);
		}
	});
});
