$(function() {
	var nav_alive;
	function Current_Hash() {
		if(window.location.hash.substring(1) !== '') {
			return window.location.hash.substring(1);
		}
	}
	//Track location
	var track_loc = $('#nav ul').position();
	$('#nav ul li').mouseover(function() {
		$('#nav_track').stop(true, true);
		var pos  = $(this).position();
		var width = $(this).width();
		var left = pos.left + 33;
		if(!nav_alive) {
			$('#nav_track').css({left:left, width:width});
			nav_alive = true;
			return;
		}
		$('#nav_track').animate({left:left, width:width}, 400);
		
	});
	
	function Work_In() {
		var cont = $(this);
		$('.work_cont').stop(true, false);
		$('.work_cont').not(cont).animate({opacity:.4}, 200);
	}
	
	function Work_Out() {
		$('.work_cont').stop(true, false);
		$('.work_cont').animate({opacity:1}, 50);
	}
	
	$('.work_cont').hoverIntent(Work_In, Work_Out);
	$('a[rel="lightbox"]').fancybox({overlayColor:'black', overlayOpacity:.8,'transitionIn':'elastic','transitionOut':'elastic'});
	
	if($('#cap_handle')) {
		// Initial load
		if(hash = Current_Hash()) {
			//Move Handle
			var pos = $('ul#cap_list .' + hash).position();
			pos.top += 2;
			$('#cap_handle').css('background', 'url(/images/capabilities/handles/' + hash + '.png)');
			$('#cap_handle').animate({top:pos.top}, 500);
			// Populate content
			var cont = $('#' + hash).html();
			$('#cap_area').html(cont);
		}
		// no hash, display normal
		else {
			var cont = $('#creative').html();
			$('#cap_area').html(cont);
		}
		// click
		$('ul#cap_list li').click(function() {
			$('#cap_handle').stop(true, true);
			var pos = $(this).position();
			pos.top += 2;
			var ele = $(this).attr('class');
			$('#cap_handle').css('background', 'url(/images/capabilities/handles/' + ele + '.png)');
			$('#cap_handle').animate({top:pos.top}, 500);
			var cont = $('#' + ele).html();
			$('#cap_area').html(cont);
		});
	}
	
	$('form input[type=text]').focus(function() {
		$(this).siblings('label').animate({left:'-40px', color:'#111'}, 1000, 'easeOutBounce');
	});
	$('form input[type=text]').blur(function() {
		if($(this).val() == '') {
			$(this).siblings('label').animate({left:'15px', color:'#FFF'});
		}
	});
	
	if($('form#contact_form')) {
		$('.inputs label').each(function() {
			if($(this).siblings('input[type=text]').val() !== '') {
				$(this).animate({left:'-40px', color:'#111'});
			}
		});
	}
	if($.browser.webkit) {
    	$('form input').attr('autocomplete', 'off');
	}
	$('form.validate').validationEngine();
	
});
