var MOUTH_CLOSE_DUR = 300;
var MOUTH_OPEN_DUR = 400;
var GALLERY_SCROLL_DUR = 200;

var galleryIndex = 0;
var imageIndex = 0;

var activeSection = "";
var mouthOpen = false;

$(document).ready( function(){
	
	$(".nav-link").click( function(e){
		e.preventDefault();
		navigateTo( $(this).attr("href") );
	} );
	
	$("#loading-icon").hide();
	
	if ( document.location.hash != "" ) {
		var goTo = document.location.hash.replace( "#", "" );
		
		if ( goTo.indexOf( "portfolio-full" ) == -1 ) {
			navigateTo( goTo );			
		} else {	
			expandImage( goTo );			
		}
		
	};	
	
} );

function navigateTo( sectionName ) {	
	section = sectionName;
	
	if ( section == "" && mouthOpen || section != activeSection && mouthOpen ) {
		activeSection = section;
		closeMouth();
	} else if ( activeSection == "" && !mouthOpen ){
		activeSection = section;
		loadSection();
	}
	
	//document.location.hash = sectionName.replace( ".html", "" );
}

function closeMouth() {
	mouthOpen = false;
	$("#mouth-top").animate( {"bottom":"50%"}, MOUTH_CLOSE_DUR );
	$("#mouth-bottom").animate( {"top":"50%"}, MOUTH_CLOSE_DUR, loadSection );
	$("#teeth-top").animate( {"top":"50%"}, MOUTH_CLOSE_DUR );
	$("#teeth-bottom").animate( {"bottom":"50%"}, MOUTH_CLOSE_DUR );
}

function loadSection() {
	
	$("#content").html( '<div class="loader"></div>' );
	
	if ( activeSection != "" ) {
		$("#content").load( section, null, onContentLoad );
		$("#loading-icon").show();
	};
	
}

function onContentLoad() {
	registerContent();
	openMouth();
	
	$("#loading-icon").hide();
	document.location.hash = section;
}



function openMouth() {
	mouthOpen = true;
	$("#mouth-top").animate( {"bottom":"90%"}, MOUTH_OPEN_DUR );
	$("#mouth-bottom").animate( {"top":"75%"}, MOUTH_OPEN_DUR );
	$("#teeth-top").animate( {"top":"10%"}, MOUTH_OPEN_DUR );
	$("#teeth-bottom").animate( {"bottom":"25%"}, MOUTH_OPEN_DUR );
}

function openMouthWide() {
	mouthOpen = true;
	$("#mouth-top").animate( {"bottom":"100%"}, MOUTH_OPEN_DUR );
	$("#mouth-bottom").animate( {"top":"100%"}, MOUTH_OPEN_DUR );
	$("#teeth-top").animate( {"top":"0%"}, MOUTH_OPEN_DUR );
	$("#teeth-bottom").animate( {"bottom":"0%"}, MOUTH_OPEN_DUR );
}

function expandImage( viewURL ) {
	openMouthWide();
	activeSection = "fullview";
	
	$("#content").load( viewURL, null, onFullImageLoad );
	$("#content").hide();
	
	$('.image-gallery').hide();
	
	document.location.hash = viewURL;
}

function onFullImageLoad() {
	$("#content").fadeIn();
	$(".portfolio-full .back-link").click( function(e){
		e.preventDefault();
		$("#content").load( $(this).attr("href"), null, onContentLoad );
	} );
	
	$(".portfolio-full .images").attr( "current-image", 0 );
	$(".portfolio-full .images li").hide();
	$(".portfolio-full .images li:eq("+imageIndex+")").show();
		
	$(".portfolio-full .images #nav #prev").click( function(e){
		e.preventDefault();
		navigateProjectImages( -1 );
	} );
	$(".portfolio-full .images #nav #next").click( function(e){
		e.preventDefault();
		navigateProjectImages( 1 );
	} );
	
	if ( $(".portfolio-full .images li").length == 1 ) {
		$(".portfolio-full .images #nav").hide();
	};
	
}

function navigateProjectImages( dir ) {
	var currentImage = parseInt( $(".portfolio-full .images").attr( "current-image" ) );
	var nextImage = ( currentImage + dir ) % $(".portfolio-full .images li").length;
	if ( nextImage == -1 ) {
		nextImage = $(".portfolio-full .images li").length-1;
	};
	
	$(".portfolio-full .images li:eq("+currentImage+")").fadeOut("fast");	
	$(".portfolio-full .images li:eq("+nextImage+")").fadeIn("fast");
	
	$(".portfolio-full .images").attr( "current-image", nextImage );
}



function registerContent(){
	
	if ( $(".image-gallery").length  ) {
		
		// --- BUILD GALLERY
		
		$(".image-gallery").attr( "image-index", galleryIndex );
		$(".image-gallery").attr( "total-images", $(".image-gallery .images li").length );
		
		scrollGallery(0);
		
		$(".image-gallery .images a").each( function(){		
			$(this).click( function(e){
				e.preventDefault();
				imageIndex = $(this).attr('imageid');
				expandImage( $(this).attr("href") );
			});		
		});
		
		$(".image-gallery .nav-text a").click( function(e){
			e.preventDefault();
			galleryIndex = 0;
			navigateTo( $(this).attr("href") );
		});
				
	};
}

function scrollGallery(dir) {
	var imageIndex = parseInt($(".image-gallery").attr( "image-index"));
	var totalImages = parseInt($(".image-gallery").attr( "total-images"));
	
	var curImage = $(".image-gallery .images li:eq("+imageIndex+")");
	galleryIndex = imageIndex + dir;
	galleryIndex = Math.max( galleryIndex, 0 );
	galleryIndex = Math.min( galleryIndex, totalImages-1 );
		
	$(".image-gallery").attr( "image-index",galleryIndex );
	var newImage = $(".image-gallery .images li:eq("+galleryIndex+")");	
	
	var parentWidth = $(".image-gallery").parent().width()/2;
	var margin= parentWidth - Math.max( 200, newImage.width()/2 );
	$(".image-gallery .images li").each( function(i){
		if ( i < galleryIndex ){ margin -= ($(this).width() + 80); }
	} );
	
	$(".image-gallery .images").stop();
	if ( dir != 0 ) {
		$(".image-gallery .images").animate( {"marginLeft":margin+'px'}, GALLERY_SCROLL_DUR );	
	} else {
		$(".image-gallery .images").css( {"margin-left":margin+'px' } );
	}
	
}



