function Navigate(url){
	window.location=url;
}

function CreateCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

$(function(){
	var panels=$("nav > dl > .sub-pages");
	var goIndicators=$("nav > dl > dt .go");
	var expandIndicators=$("nav > dl > dt .expand");
	panels.hide();
	expandIndicators.show();
	
	$("nav > dl > dt").click(function(e){
		var next=$(this).next();
		if (!next.hasClass("sub-pages") || next.is(":visible")){
			Navigate($(this).children("a").attr("href"));
			return;
		}
		panels.slideUp();
		goIndicators.hide();
		expandIndicators.show();
		next.slideDown();
		$(this).children(".go").show();
		$(this).children(".expand").hide();
	});
	
	$("nav > dl > dt").each(function(){
		if ($(this).next().hasClass("sub-pages"))
			$(this).attr("title", "Click once to expand, click again to go");
	});

	$("nav dd").click(function(e){
		Navigate($(this).children("a").attr("href"));
	});
	
	$("nav a").click(function(e){
		$(this).parent().click();
		return false;
	});
	
	$("#content").css("margin-bottom",window.innerHeight-$("#buttons").offset().top);
	
	$("#cookiePopup button").click(function(){
		$("#cookiePopup").fadeOut();
		CreateCookie("cookiePopupConfirmed",true,3650); // ten years should do it
	});
	
	$("#menu-button").click(function(e){
		$("#menu").show("slide", {direction: "left", easing:'easeOutCirc'});
		$("body>.backdrop").fadeIn();
	});
	
	$("body>.backdrop").click(function(e){
		$("#menu").hide("slide", {direction: "left", easing:'easeInCirc'});
		$("body>.backdrop").fadeOut();
	});
});


$(window).resize(function(e){
	if (window.innerWidth>1023){
		$("#menu").attr("style","");
		$("#menu-button").attr("style","");
		$("body>.backdrop").hide();
	}
});


$(document).delegate('.allowTabInput', 'keydown', function (e) {
	var keyCode = e.keyCode || e.which;
console.log(e);
	if (keyCode == 9) {
		e.preventDefault();
		var start = $(this).get(0).selectionStart;
		var end = $(this).get(0).selectionEnd;

		// set textarea value to: text before caret + tab + text after caret
		$(this).val($(this).val().substring(0, start)
				+ "\t"
				+ $(this).val().substring(end));

		// put caret at right position again
		$(this).get(0).selectionStart =
				$(this).get(0).selectionEnd = start + 1;
	}
});