$(function() {
	//Fix ratio of Videos
	function fixratio() {
		$("[ratio]").each(function(index) {
			element = $(this);
			element.height(element.width()*element.attr("ratio"));
		});
	}
	
	//Placeholder text
	function elementSupportsAttribute(element,attribute) {
		var test = document.createElement(element);
		if (attribute in test) {
			return true;
		} else {
			return false;
		}
	}
	
	fixratio();
	$(window).resize(function() {
		fixratio();
		//fixlogo();
	});
	
	//Fix Input placeholders for non-HTML5 browsers
	if(!elementSupportsAttribute("input", "placeholder")) {
		$("#author").attr("value","Name*");
		$("#author").focus(function() {
			$(this).attr("value","");
		});
		$("#author").blur(function() {
			if($(this).attr("value") == "") {
				$(this).attr("value", "Name*");
			}
		});
		
		$("#email").attr("value","E-Mail*");
		$("#email").focus(function() {
			$(this).attr("value","");
				});
		$("#email").blur(function() {
			if($(this).attr("value") == "") {
				$(this).attr("value", "E-Mail*");
			}
		});
				
		$("#url").attr("value","Website");
		$("#url").focus(function() {
			$(this).attr("value","");
		});
		$("#url").blur(function() {
			if($(this).attr("value") == "") {
				$(this).attr("value", "Website");
			}
		});
	}
	
	//Load archive pages with ajax
	$('#postPagination a').live('click', function(e){
		e.preventDefault();
		var link = jQuery(this).attr('href');
		var toc = $('#toc');
		var height = toc.height();
		toc.css("height", height);
		$.scrollTo($("#articlesheader"),500,{margin:true});
		$("#toc .container").fadeOut(500, function() {
			toc.load(link+' #toc', function() {
				toc.css("height", "auto");
				var hash = link.match(/\/page\/[0-9]+\/?/);
				if(hash != null) {
					window.location.hash = hash;
				} else {
					window.location.hash = "/page/1/";
				}
			});
		});
	});
	
	//Jump to right page on load
	var hash = window.location.hash;
	hash = hash.substr(1,hash.length);
	var pagehash = /\/page\/[0-9]+\/?/.test(hash);
	if(pagehash) {
		var toc = $("#toc");
		toc.load(hash+' #toc');
	}
	
	//Expand additional information for comment
	$("#comment").focus(function() {
		var addinfo = $("#addinfo");
		if(addinfo.css("display") == "none") {
			$("#addinfo").slideToggle(500);
		}
	});
	
	//Footer navigation
	$(".sectionheader li").click(function() {
		$(this).siblings().removeClass("active");
		$(this).addClass("active");
		var offset = $(this).position().left-$(this).parent().position().left;
		$(this).parent().next().animate({"left":offset+"px"}, 300);
		var content = $(".footer ."+$(this).text().toLowerCase());
		content.siblings().animate({"opacity":"0"},300, function() {
			$(this).removeClass("active")
			content.css("opacity","0").addClass("active").animate({"opacity":"1"},300);
		});
	});
	
	//Enable lettering.js
	$(".lettering").lettering();

});
