$(function(){
	var randomImages = ['jz_guitar','guzheng','guitar2','guzheng2','kalimba','jz_closeup',];
	var randomNumber = Math.floor(Math.random() * randomImages.length);
	var imageURL = "http://jeremyzuckerman.com/media/big_images/" + randomImages[randomNumber] + ".jpg";
	$("#big_image").css('background-image','url(' + imageURL + ')');
	
	$.address.change(function(event) {
		if (event.value == "/home"  || event.value == "/#/" || event.value == "/") link_home();
		else if (event.value == "/biography" || event.value == "/#/biography") link_biography();
		else if (event.value == "/audio" || event.value == "/#/audio") link_audio();
		else if (event.value == "/blog" || event.value == "/#/blog") link_blog('');
		else link_blog(event.value);
	});
	
	function handlePaginationLinks() {		
		$("a#show_comment_form").click( function () {
			$(this).hide();
			$("#respond").fadeIn();
			$('#page_content').jScrollPane({animateTo:true, scrollbarWidth:10, scrollbarMargin: 25, dragMaxHeight:100});
			$('#page_content')[0].scrollTo("#submit");
		});
		
		$("#pagination a").click( function (event) {
			event.preventDefault();
			
			// fade out current page if visible
			$("#page").fadeOut("fast");
			
			// get link url
			var url = $(this).attr('href');
			var deep_link_url = url.split("http://jeremyzuckerman.com/mnml/")
			$.address.value(deep_link_url[1]);
					
			// load post in page
			$("#page").load(url, function() {
				$("#page").fadeIn();

				// fade in post and render it in a scrollpane
				$('#page_content').jScrollPane({scrollbarWidth:10, scrollbarMargin: 25, dragMaxHeight:100});
				
				//setup pagination older/newer links to load in page div
				handlePaginationLinks();
			});
		});
	}
	
	$("#nav ul li a").hover(
		function () {
			var nav_text = $(this).attr("id");
			$("li#nav_text").text(nav_text).fadeIn('fast'); 
		},
		function () { $("li#nav_text").hide(); }
	);
	
	function link_home() {
		$("li#nav_text").hide();
		
		var parent = $("a#home").parent();
		
		if ( !$(parent).hasClass("active") ) {
			$("#page").hide();
			
			$("#main").animate({
				width: 950
			}, 1000, function() {
				// Animation complete.
			});
			
			$("#big_image").animate({
				width: 950
			}, 1000, function() {
				// Animation complete.
			});
						
			$('#nav ul li').removeClass("active");
			$(parent).addClass("active");
		}
	}
	
	/*$("a#home").click( function () {
		link_home();
	});*/
	
	function link_biography() {
		var parent = $("a#biography").parent();
		
		if ( !$(parent).hasClass("active") ) {
			// fade out current page if visible
			$("#page").fadeOut("fast");
			
			// show nav text now that mouse is no longer over nav links
			$("li#nav_text").show();
			
			// load biography in page
			$("#page").load('http://jeremyzuckerman.com/mnml/biography/', function() {
				// fade in bio
				$("#page").fadeIn();

				// render bio in a scrollpane
				$('#page_content').jScrollPane({scrollbarWidth:10, scrollbarMargin: 25, dragMaxHeight:100});
			});
			
			$("#main").animate({
				width: 600
			}, 1000, function() {
				// animation complete
			});
			
			$("#big_image").animate({
				width: 600
			}, 1000, function() {
				//$("#page").fadeIn();
			});
			
			// remove active css class from all nav list items
			$('#nav ul li').removeClass("active");
			// add active css class to audio list item
			$(parent).addClass("active");
		}
	}
	
	$("a#biography").click( function () {
		// hide nav text to look less glitchy
		$("li#nav_text").hide();
	});
	
	function link_audio() {
		var parent = $("a#audio").parent();
		
		if ( !$(parent).hasClass("active") ) {
			// fade out current page if visible
			$("#page").fadeOut("fast");
			// show nav text now that mouse is no longer over nav links
			$("li#nav_text").show();
			
			// load music player in page
			$("#page").load("http://jeremyzuckerman.com/mnml/music/", function() {
				$("#page").fadeIn();
			});
			
			$("#main").animate({
				width: 600
			}, 1000, function() {
				// animation complete
			});
			
			$("#big_image").animate({ width: 600 }, 1000, function() {
			});
			
			// remove active css class from all nav list items
			$('#nav ul li').removeClass("active");
			// add active css class to audio list item
			$(parent).addClass("active");
		}
	}
	
	$("a#audio").click( function () {
		// hide nav text to look less glitchy
		$("li#nav_text").hide();
	});
	
	function link_blog(new_url) {
		var parent = $("a#blog").parent();
		
		// fade out current page if visible
		$("#page").fadeOut("fast");
		
		// show nav text now that mouse is no longer over nav links
		$("li#nav_text").show();
		
		// load biography in page
		if (!new_url) new_url = 'http://jeremyzuckerman.com/mnml/blog/';
		else {
			new_url = new_url.replace("%23", "#");
			new_url = 'http://jeremyzuckerman.com/mnml' + new_url;
		}
		
		$("#page").load(new_url, function() {
			$("#page").fadeIn();
			
			// render post in scrollpane
			$('#page_content').jScrollPane({scrollbarWidth:10, scrollbarMargin: 25, dragMaxHeight:100});

			//setup pagination older/newer links to load in page div
			handlePaginationLinks();
		});
		
		$("#main").animate({
			width: 600
		}, 1000, function() {
			// animation complete
		});
		
		$("#big_image").animate({
			width: 600
		}, 1000, function() {
			// fade in post
			//$("#page").fadeIn();
		});
		
		if ( !$(parent).hasClass("active") ) {			
			// remove active css class from all nav list items
			$('#nav ul li').removeClass("active");
			// add active css class to audio list item
			$(parent).addClass("active");
		}
	}
	
	$("a#blog").click( function () {
		// hide nav text to look less glitchy
		$("li#nav_text").hide();
	});
});
