/* Podcast Navigation */

(function (window, document, undefined) {

	var navi = document.getElementById('podcast-navi');
	var audio = document.getElementById('podcast');
	if (!navi || !audio || !audio.play) {
		return;
	}
	navi.onclick = function (e) {
		var target = e.target;
		if (target.nodeName != 'A') {
			return;
		}
		var content = target.textContent || target.innerText;
		var matches = content.match(/\((\d+):(\d+)\)/);
		if (matches.length != 3) {
			return;
		}
		var seekTime = (matches[1] * 60) + +matches[2];
		audio.currentTime = seekTime;
		audio.play();
		return false;
	};
	
})(window, document);


/* Github projects */

(function (window, document, undefined) {
	var username = 'molily';
	var targetId = 'github-projects';
	
	var target = document.getElementById(targetId);
	if (!target) {
		return;
	}
	
	// Try to read cached HTML from sessionStorage
	try {
		if (window.sessionStorage && window.sessionStorage.getItem) {
			var html = window.sessionStorage.getItem(targetId);
			if (html) {
				target.innerHTML = html;
				return;
			}
		}
	} catch (e) {}
	
	// Set global callback function
	var callbackName = 'githubProjects_' + +new Date;
	window[callbackName] = callback;
	
	// Create JSONP script
	var script = document.createElement('script');
	script.src = 'http://github.com/api/v2/json/repos/show/' + username + '?callback=' + callbackName;
	document.getElementsByTagName('head')[0].appendChild(script);
	
	function callback (o) {
		// Create output
		var repos = o.repositories;
		var html = '<dl>';
		for (var i = 0, repo; repo = repos[i]; i++) {
			html += '<dt><a href="' + repo.url + '">' + repo.name + '</a></dt><dd>' + repo.description + '</dd>'
		}
		html += '</dl>';
		target.innerHTML = html;
		
		// Save HTML in sessionStorage
		try {
			if (window.sessionStorage && window.sessionStorage.setItem) {
				window.sessionStorage.setItem(targetId, html);
			}
		} catch (e) {}
		
		// Remove global callback
		try {
			delete window[callbackName];
		} catch (e) {
			window[callbackName] = undefined;
		}
		
		// Remove inserted script
		script.parentNode.removeChild(script);
	}
	
})(window, document);


/* Read more link */

(function (window, document, undefined) {
	
	if (!document.getElementById('prev-posts') || !document.getElementsByClassName) {
		return;
	}
	
	// Shorten post body
	var postBody = document.getElementsByClassName("post-body")[0];
	if (!postBody) {
		return;
	}
	postBody.className += " shorten";
	
	// Insert more link
	var moreLink = document.createElement("p");
	moreLink.className = "moreLink";
	moreLink.onclick = showWholeArticle;
	var aEl = document.createElement("a");
	aEl.href = "#";
	aEl.appendChild(document.createTextNode("Diesen Artikel weiterlesen"));
	moreLink.appendChild(aEl);
	postBody.parentNode.appendChild(moreLink, postBody);
	
	function showWholeArticle (e) {
		e.preventDefault();
		postBody.className = postBody.className.replace(/(?:^|\s)shorten(?:\s|$)/, "");
		moreLink.parentNode.removeChild(moreLink);
	}
	
})(window, document);