// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());

//Global variables
var moveLock = false;
var moveIncrement = 960;
var itemCount = 1;
var itemWidth = 1;

//Disables the button visually
function disableButton(elem) {
	elem.addClass('disabled');
}

//Enables the button visually
function enableButton(elem) {
	elem.removeClass('disabled');
}

//Checks to see if the button needs to be disabled and will do so
function nextCheck(elem) {
	//Reassign the global variables to the proper values
	itemCount = $('#portfolio LI:visible').length;
	itemWidth = $('#portfolio LI:visible').outerWidth();
	
	if($('#portfolio').position().left <= (0-itemCount*itemWidth+$('.list-container').width()))
	{
		disableButton(elem);
	}
	else
	{
		enableButton(elem);
	}
}

//Checks to see if the button needs to be disabled and will do so
function prevCheck(elem) {
	//Reassign the global variables to the proper values
	itemCount = $('#portfolio LI:visible').length;
	itemWidth = $('#portfolio LI:visible').outerWidth();
	
	if($('#portfolio').position().left >= 0)
	{
		disableButton(elem);
	}
	else
	{
		enableButton(elem);
	}
}

//Slides the list of items to the left
function moveNext(elem) {
	//Checks to see if the list is already moving
	if(!moveLock && !elem.hasClass('disabled')) {
		moveLock = true;
		$('#portfolio').animate({ 
			left: ($('#portfolio').position().left-moveIncrement)
		},
		{	
			duration: 800,
			complete: function() {
				//Unlock the buttons
				moveLock = false;
				enableButton($('.item-prev'));
				
				prevCheck($('.item-prev'));
				nextCheck($('.item-next'));
			}
		});
	}
}

//Slides the list of items to the left
function movePrev(elem) {
	//Checks to see if the list is already moving
	if(!moveLock && !elem.hasClass('disabled')) {
		moveLock = true;
		$('#portfolio').animate({ 
			left: ($('#portfolio').position().left + moveIncrement)
		},
		{	
			duration: 800,
			complete: function() {
				//Unlock the buttons
				moveLock = false;
				
				prevCheck($('.item-prev'));
				nextCheck($('.item-next'));
			}
		});
	}
}

//Moves the list back to the start
function gotoStart() {
	moveLock = true;
	
	$('#portfolio').animate({ 
		left: '0'
	},
	{	
		duration: 800,
		complete: function() {
			//Unlock the buttons
			moveLock = false;
			
			prevCheck($('.item-prev'));
			nextCheck($('.item-next'));
		}
	});
}

//Shows the items in the main content area
function showItems(className){
	var thisDuration = 500;
	
	if(className == "home")
	{
		//If the resume is visible, hide it and go back to the portfolio
		if($('#resume:visible').length > 0)
		{
			$('#resume').children('LI:visible').hide(
				"slide",
				{ direction: "up" },
				thisDuration, 
				function() { 
					$('#resume:visible').hide();
					$('#portfolio:hidden').show();
					$('#portfolio').children('LI:hidden').show("slide", { direction: "up" }, thisDuration);
				}
			);
			
		}
		
		//If nothing is hidden, don't animate
		if($('#portfolio').children('LI:hidden').length > 0)
		{
			//Hide everything then re-display the proper items
			$('#portfolio').children('LI:visible').hide(
				"slide",
				{ direction: "up" },
				thisDuration, 
				function() { 
					$('#portfolio').children('LI').show("slide", { direction: "up" }, thisDuration);
				}
			);
		}
	}
	else if(className == "resume")
	{
		$('#resume:visible').hide();
		//Hide the main items and show the resume
		$('#portfolio').children('LI').hide(
			"slide",
			{ direction: "up" },
			thisDuration, 
			function() { 
				$('#portfolio').hide();
				$('#resume:hidden').show().children('LI').show("slide", { direction: "up" }, thisDuration);
			}
		);
	}
	else
	{
		//If the resume is visible, hide it and go back to the portfolio
		if($('#resume:visible').length > 0)
		{
			$('#resume').children('LI:visible').hide(
				"slide",
				{ direction: "up" },
				thisDuration, 
				function() { 
					$('#resume:visible').hide();
					$('#portfolio:hidden').show();
					$('#portfolio').children('.' + className + ':hidden').show("slide", { direction: "up" }, thisDuration);
				}
			);
		}
		
		//Hide everything then re-display the proper items
		$('#portfolio').children('LI:visible').hide(
			"slide",
			{ direction: "up" },
			thisDuration, 
			function() { 
				$('#portfolio').children('.' + className + ':hidden').show("slide", { direction: "up" }, thisDuration);
			}
		);
	}
}

$(document).ready( function() {
	//Hide the resume section
	$('#resume').children('LI').hide();
	
	//Check to see if the buttons should be disabled
	prevCheck($('.item-prev'));
	nextCheck($('.item-next'));
	
	//Prevent the default behavior
	$('.item-next').click( function (e) {
		e.preventDefault();
		
		moveNext($(this));
	});
	
	$('.item-prev').click( function (e) {
		e.preventDefault();
		
		movePrev($(this));
	});
	
	//If a hash-url is set when the page loads, go directly to it
	var hashname = window.location.hash.replace('#!/', '');
	
	if(hashname && $('.' + hashname).length > 0){
		//Marks the proper nav item as active
		$('.active').removeClass('active');
		$('.nav').find('.' + hashname).parent().addClass('active');
		
		//Shows the appropriate items
		showItems(hashname);
	}
	
	//Creates the nav functionality
	$('A[href^="#!/"]').click( function(e) {
		//Change the currently clicked item to active
		$('.active').removeClass('active');
		$(this).parent().addClass('active');
		
		//Show the proper items in the view area
		var targetClass = $(this).attr('href').replace('#!/', '');;
		showItems(targetClass);
		
		//Move the view area back to the start
		gotoStart();
	});
	
});

