var hudIsDisplaying = 0;

jQuery(function( $ ){
	//$('a').click(function () {return false;}); //For debug

	$('#camediaweb, .headerbutton').hover(
		function () {
			$(this).stop().animate({'opacity': 0.6});
		},
		function () {
			$(this).stop().animate({'opacity': 1});
		}
	);
	
	$('#categories li a').click(
		function () {
			$('#categories li.currentcat').removeClass('currentcat');
			$(this).parent().addClass('currentcat');
			
			loadWithURL($(this).attr('href'), 'Left');
			return false;
		}
	);

	initnav();
	
	$(document).keydown(function (eventObj){
		//alert(eventObj.keyCode);

		if (eventObj.keyCode == 73 && hudIsDisplaying == 0) showHud();
		else if (eventObj.keyCode == 73 && hudIsDisplaying == 1) hideHud();

		if (eventObj.keyCode == 37 && $('#previousproject a').click().length == 0)
			hoNoes('hero');

		if (eventObj.keyCode == 38 && $('a', $('#categories li.currentcat').next()).click().length == 0)
			hoNoes('categories');

		if (eventObj.keyCode == 39 && $('#nextproject a').click().length == 0)
			hoNoes('hero');

		if (eventObj.keyCode == 40 && $('a', $('#categories li.currentcat').prev()).click().length == 0)
			hoNoes('categories');
		
		if (eventObj.keyCode == 40 || eventObj.keyCode == 38) return false;
	});

});

function initnav()
{
	$('#insidecontentframe').hover(
		function () {
			showHud();
		},
		function () {
			hideHud();
		}
	);

	$('#dots').hover(
		function () {
			$('#dotshelp').stop().animate({'opacity': 0});
		},
		function () {
		}
	);
	
	$('#nextproject a').click(
		function () {
			loadWithURL($(this).attr('href'), 'Left');
			return false;
		}
	);

	$('#previousproject a').click(
		function () {
			loadWithURL($(this).attr('href'), 'Right');
			return false;
		}
	);
}

function loadWithURL(url, direction)
{
	var currentURL = url;
	var easeOut = 300;
	var easeIn = 1000;

	var firstMove = {};
	firstMove['margin' + direction] = '-200px';

	var resetMove = {};
	resetMove['margin' + direction] = '0px';

	$('#insidecontentframe').animate(firstMove, easeOut);
	$('#contentframe').animate({opacity: 0}, easeOut, function() {
		$('#contentframe').load(currentURL + ' #contentframe > *', function () {
			$('#insidecontentframe').animate(resetMove, 0);
			$('#contentframe').animate({opacity: 1}, easeIn, function () {
				hudIsDisplaying = 0;
				initnav();
			});
		});
	});
}

function hoNoes(id)
{
	var letsMove = 10;
	
	while (letsMove > 0)
	{
		$("#" + id).animate({ marginLeft: letsMove + "px" }, 50).animate({ marginLeft: -letsMove + "px" }, 50);
 
		letsMove = letsMove - 2;
	}
	
	return 1;
}

function showHud()
{
	$('#hud').stop().animate({'opacity': 1}, 600);
	$('#hero').stop().animate({'opacity': 0.4}, 600);
	$('.sidenavigation').stop().animate({'opacity': 0.4}, 200);
	hudIsDisplaying = 1;
}

function hideHud() {
	$('#hud').stop().animate({'opacity': 0});
	$('#hero').stop().animate({'opacity': 1});
	$('.sidenavigation').stop().animate({'opacity': 1});
	hudIsDisplaying = 0;
}