var rcb = {
	currentPage: "home",

	htmlentities: function (html) {
		html = html.replace(/&/g, '&amp;');
		html = html.replace(/</g, '&lt;');
		html = html.replace(/>/g, '&gt;');
		
		return html;
	},
	
	modules: [
		{
			name: "photos",
			title: '<strong>Favorite Photos </strong> via <a href="http://rboyce.tumblr.com/tagged/photos/">Tumblr</a>',
			jsonURL: "http://rboyce.tumblr.com/api/read/json?tagged=photos&callback=?",
			itemLimit: 8,
			displayModule: function(data) {
				$("#rcb-module-photos").append("<ol></ol>");
			
				var limit = this.itemLimit;
				var displayed = 0;
				// iterate over array of links
				jQuery.each(data.posts, function() {
					// break if we have displayed enough
					if(displayed >= limit) {
						return false;
					}
					// continue if not a photo
					if(this.type != "photo") {
						return true;
					}
					// append item html
					$("#rcb-module-photos ol").append('<li id="rcb-module-photos-'+this['id']+'"><a href="'+this['url-with-slug']+'"><img src="'+this['photo-url-75']+'" alt="Photo from Tumblr" /></a></li>');
					
					displayed++;
				});
				
				// Failed to display enough links
				if(displayed <= 0) {
					// Remove list
					$("#rcb-module-photos ol").remove();
					$("#rcb-module-photos").append("Oh, it looks like I don't have any photos right now.");
				}
			}
		},
		{
			name: "links",
			title: '<strong>Favorite Links </strong> via <a href="http://delicious.com/rboyce">Delicious</a>',
			jsonURL: "http://feeds.delicious.com/v2/json/rboyce?callback=?",
			itemLimit: 3,
			displayModule: function(data) {
				$("#rcb-module-links").append("<ol></ol>");
			
				var limit = this.itemLimit;
				var displayed = 0;
				// iterate over array of links
				jQuery.each(data, function() {
					// break if we have displayed enough
					if(displayed >= limit) {
						return false;
					}
					// append item html
					$("#rcb-module-links ol").append('<li><a href="'+this.u+'">'+rcb.htmlentities(this.d)+'</a> '+rcb.htmlentities(this.n)+'</li>');
					displayed++;
				});
				
				// Failed to display enough links
				if(displayed <= 0) {
					// Remove list
					$("#rcb-module-links ol").remove();
					$("#rcb-module-links").append("Oh, it looks like I don't have any links right now.");
				}
			}
		}
	],
	
	loadPage: function(target) {
		// return if we are already there
		if(target == rcb.currentPage) {
			return;
		}
		
		// set page hash & window title
		if(target != "home") {
			if(window.history && window.history.pushState) {
				window.history.pushState({}, target+' - rboyce', '/'+target);
			}
			else {
				window.location.hash = target;
			}
			document.title = target+' - rboyce';
		}
		else {
			if(window.history && window.history.pushState) {
				window.history.pushState({}, 'rboyce', '/');
			}
			else {
				window.location.hash = "";
			}
			document.title = 'rboyce';
		}
		// set navbar selected
		$('#rcb-nav li').each(function() {
			if($(this).children('a').attr('href') == target) {
				$(this).addClass('selected');
			}
			else {
				$(this).removeClass('selected');
			}
		});

		// slide up most content	
		$("#rcb-more-content").slideUp(700, function() {
			// remove all dom specific to this page
			$(".rcb"+rcb.currentPage).remove();
			
			// hide parts of first line if coming from home page
			if(rcb.currentPage == "home") {
				// remove paragraph padding
				$("#first-para").animate({"padding-bottom": 0}, 500);
			
				// fade and slide up second line
				rcb.secondLineHeight = $("#second-line").height();
				$("#second-line").css({"overflow": "hidden", "width": $("#second-line").width()});
				$("#second-line").animate({
					"height": 0, 
					"opacity": 0
				}, 500, function() {
					// fade and slide over first line
					rcb.firstPhraseWidth = $("#first-phrase").width();
					$("#first-phrase").css({"overflow": "hidden", "height": $("#first-phrase").height()});
					$("#first-phrase").animate({
						"width": 0, 
						"opacity": 0
					}, 700);
				});
			}
			
			// show parts if returning to home page
			if(target == "home") {
				// add paragraph padding
				$("#first-para").animate({"padding-bottom": "15px"}, 500);
				// fade in and slide over first line
				$("#first-phrase").animate({
					"width": rcb.firstPhraseWidth,
					"opacity": 1
				}, 700, function() {
					$("#second-line").animate({
						"height": rcb.secondLineHeight, 
						"opacity": 1
					}, 500);
				});
			}
			
			// display loading indicator
			$("#rcb-more-content").html('<img class="rcb-content-loading" src="img/ajax-loader.gif" alt="Loading..." />');
			
			// load target page
			$.get('content/'+target, function(data, textStatus) {
				$("#rcb-more-content .rcb-content-loading").remove();
				$("#rcb-more-content").html(data);
				$("#rcb-more-content").slideDown(1400);
				
				rcb.currentPage = target;
			});
		});
	},
	
	loadInitialPage: function(target) {
		if(target == 'home') {
			return;
		}
		
		// set page hash & window title
		if(target != "home") {
			if(window.history && window.history.pushState) {
				window.history.replaceState({}, target+' - rboyce', '/'+target);
			}
			else {
				window.location.hash = target;
			}
			document.title = target+' - rboyce';
		}
		else {
			if(window.history && window.history.pushState) {
				window.history.replaceState({}, 'rboyce', '/');
			}
			else {
				window.location.hash = "";
			}
			document.title = 'rboyce';
		}
		// set navbar selected
		$('#rcb-nav li').each(function() {
			if($(this).children('a').attr('href') == target) {
				$(this).addClass('selected');
			}
			else {
				$(this).removeClass('selected');
			}
		});
		
		rcb.secondLineHeight = $("#second-line").height();
		$("#second-line").css({
			"overflow": "hidden", 
			"width": $("#second-line").width(),
			"height": 0, 
			"opacity": 0
		});
		
		rcb.firstPhraseWidth = $("#first-phrase").width();
		$("#first-phrase").css({
			"overflow": "hidden", 
			"height": $("#first-phrase").height(),
			"width": 0, 
			"opacity": 0
		});
		
		// display loading indicator
		$("#rcb-more-content").html('<img class="rcb-content-loading" src="img/ajax-loader.gif" alt="Loading..." />');
		
		// load target page
		$.get('content/'+target+'.html', function(data, textStatus) {
			$("#rcb-more-content .rcb-content-loading").remove();
			$("#rcb-more-content").html(data);
			
			rcb.currentPage = target;
		});
	},
	
	subPages: ["home", "resume", "contact"],
	
	displayModules: function() {
		// Load & display modules
		jQuery.each(rcb.modules, function() {
			var module = this;
			$("#rcb-foot-extras").append('<div id="rcb-module-'+module.name+'" class="rcb-foot-module"></div>');
			$("#rcb-module-"+module.name).html('<h4>'+module.title+'</h4><img class="rcb-foot-module-loading" src="img/ajax-loader.gif" alt="Loading..." />');
		
			$.getJSON(module.jsonURL, function(data) {
				// Remove loading icon
				$("#rcb-module-"+module.name+" .rcb-foot-module-loading").remove();
				
				// error loading JSON
				if(!data) {
					$("#rcb-module-"+module.name).append("Sorry, I'm having some trouble getting "+module.name+" to show.");
				}
				
				// display module items
				else {
					module.displayModule(data);
				}
			});
		});
	},
	
	initialize: function() {
		// Event handlers for navigation and inital page
		jQuery.each(rcb.subPages, function() {
			var target = this;
			$("a[href='"+target+"']").live("click", function(event) {
				event.preventDefault();
				rcb.loadPage(target);
			});
			
			// go to inital page, if set
			if(window.location.hash == '#'+target) {
				rcb.loadInitialPage(target);
			}
		});
		
		if(window.onhashchange) {
			window.onhashchange = function() {
				alert('hashchange!');
				jQuery.each(rcb.subPages, function() {
					if(window.location.hash == '#'+target) {
						rcb.loadPage(target);
					}
				});
			}
		}
		
		rcb.displayModules();
	}
};
