/**
 * @author Joachim Fraatz
 */
$(function() {
	if(typeof window.console !== 'object') {
		console = {};
		console.log = function() {};
	}
	new Application(window.document);
});
		
function Application(doc) {
	var document = doc;
	var navigation;
	var contentRequestResult;
	var contentView;
	var contentId = "";
	var PRETTY_URL_PATTERN = "!";
	var TITLE_SEPERATOR = " | ";
	var FALLBACK_TITLE_PREFIX = "Joachim Fraatz" ;
	var BACKGROUND_SRC = {
		home: "images/projects/adidas-green-s0.jpg",
		projects: "images/projects/adidas-green-s1.jpg",
		resume: "images/projects/adidas-green-s0.jpg",
		blog: "images/projects/adidas-green-s2.jpg",
		contact: "images/projects/adidas-green-s3.jpg"
	};
	initialize();
	
	function initialize() {
		navigation = new Navigation("#navigation");
		$(window).hashchange(window_hashchange);
		var hash = document.location.hash.length ? document.location.hash : "#" + PRETTY_URL_PATTERN + "home";
		navigateToContentForHash(hash);
	}
	
	function window_hashchange() {
		console.log("window_hashchange", document.location.hash);
		navigateToContentForHash(document.location.hash);
	}
	
	function navigateToContentForHash(hash) {
		var hashParts = hash.replace("#" + PRETTY_URL_PATTERN , "").split("/");
		if(hashParts[0] !== contentId) {
			contentId = hashParts[0];			
			if(document.location.hash.indexOf(contentId) === -1) {
				document.location.hash = PRETTY_URL_PATTERN + contentId;
			}
			navigation.selectItemForURI(document.location.hash);
			$.get(contentId + ".html")
			.success(contentRequest_success)
			.error(contentRequest_error);
		} else {
			document.location.hash = hash.replace("#", "");
			if(contentView) {
				contentView.navigateToContentForHash(hash);
			}
		}
	}
	
	function contentRequest_error() {		
		console.log("load_error");
	}

	function contentRequest_success(data) {
		contentRequestResult = data;
    	$("#contentContainer").fadeOut(500, "easeInOutSine", showContent);
	}
	
	function showContent() {
		var resultDoc = $.htmlDoc(contentRequestResult);
		contentRequestResult = "";
		var title = resultDoc.find("head title").html();
		if(!title) {
			title = FALLBACK_TITLE_PREFIX + TITLE_SEPERATOR + contentId.substring(0, 1).toUpperCase() + contentId.substring(1);
		}
		document.title = title;
		var contentViewElement = resultDoc.find(".contentView")[0];
		$("#contentContainer").html(contentViewElement);
		$("#contentContainer").fadeIn(500, "easeInOutSine");
		
		if(contentView) {
			contentView.destroy();
		}
		contentView = new ContentView(title + TITLE_SEPERATOR);
		var hash = document.location.hash;
		if(hash.indexOf("/") !== -1) {
			contentView.navigateToContentForHash(hash);
		}
		setTimeout(updateBackground, 400);
	}
	
	function updateBackground() {
		window.addImageToBackground(BACKGROUND_SRC[contentId]);
	}
}

function Navigation(id) {
	var elementId = id;
	var activeLink;
	initialize();
	
	function initialize() {
		$(elementId).fadeIn(700, "easeInOutSine");
		$(elementId + " a").each(function() {
			$(this).click(function() {
				resetActiveLink();
				setLinkActive(this);
			});
			var width = $(this).outerWidth() - 2;
			$(this).parent().css("max-width", width);//windows font-render workaround
		});
	}
	
	function resetActiveLink() {
		$(activeLink).show();
		$(elementId + " .active").remove();
	}
	
	function setLinkActive(link) {
		activeLink = link;
		$(link).hide();
		$(link).parent().append("<span class='active'>" + $(link).text() + "</span>");
	}
	
	this.selectItemForURI = function(uri) {
		if(uri === "#!home") {
			resetActiveLink();
		}
		$(elementId + " a").each(function() {
			var href = $(this).attr("href");
			if(uri.indexOf(href) !== -1 && activeLink !== this) {
				resetActiveLink();
				setLinkActive(this);
			}
		});
	};
}
