$.jqwidont.auto(false);

function removeRandom() {
	$("#zougen").remove();
}

$(document).ready(function() {
	
	// prevent widows
	//$("p, blockquote").widont();
	$("*")
	.contents()
	.filter(function() {
		return this.nodeType == 3; // text node
	}).each(function(index) {
		$(this).replaceWith($.jqwidont.transform($(this).text()));
	});
	
	
	// random surprise!
	if(Math.random() < 0.3) {
		var randomImages = 11;
		var randomImage = Math.floor(Math.random() * randomImages + 1);
		var preloader = new Image();
		preloader.src = "/img/random" + randomImage + ".jpg";
				
		preloader.onload = function() {
			$("body").append('<img src="' + preloader.src + '" id="zougen" style="position:absolute; z-index: 9000; left: ' + Math.floor(Math.random() * 40) + '%; top: ' + Math.floor(Math.random() * 30) + '%;" onclick="$(this).remove()" />');
			window.setTimeout("removeRandom();", 750);
		}
	}
	
	
	// show/hide dev grid
	$("#dev").click(function() {
		$("body").toggleClass("dev-grid");
		//$("header, nav, article, article div").toggleClass("dev-block");
		if($("body").hasClass("dev-grid") == false && window.location.hash == "#devmode") {
			window.location.hash = "";
		} else {
			window.location.hash = $(this).attr("href");
		}
		return false;
	});
	if(window.location.hash == "#devmode") {
		$("#dev").click();
	}
	
	
	// external links in new window
	$("a[rel='external']").attr("target", "_blank");


	// navigation

		// get currently active element
		var active = $("nav li.active");	
		var timer;
	
		window.reset_menu = function() {
			if(typeof active != "undefined" && $("nav .active").length == 0) {
				$(active).addClass("active");
			}
		}
		
		$("nav a").hover(function() {
		
			clearTimeout(timer);
				
			if(typeof active != "undefined") {
				$(active).removeClass("active");
			}
		
			$(this).parent("li").addClass("active");
		
		}, function() {
		
			$(this).parent("li").removeClass("active");
		
			// reset active element if there isn't one
			// but give time to allow hovering over other menu link
			timer = window.setTimeout("reset_menu()", 10);
		
			/*
			if(typeof active != "undefined" && $("nav .active").length == 0) {
				$(active).addClass("active");
			}
			*/
				
		});
	
		$("nav div").mouseover(function() {
			timer = window.setTimeout("reset_menu()", 10);
			$(this).parent("li").removeClass("active");
		});
		
	// end navigation
	
	
	function superJoshOverlay(sjoTitle, sjoContents) {

		$(".sjo").remove();

		$("body").append('<div class="sjo sjoOverlay"></div><div class="sjo sjoContents"><h3>' + sjoTitle + '</h3><p>' + sjoContents + '</p><p class="sjoLinks"><a href="#" class="sjoOk">OK</a></p></div>');
		$(".sjoOverlay").css({height:$(document).height()}).fadeTo(0, 0).fadeTo(300, 0.8);
		$(".sjoContents").hide().fadeIn(300);
		
		$(".sjoLinks a, .sjoOverlay").click(function(event) {
			event.preventDefault();
			$(".sjo").fadeOut(300, function() {
				$(this).remove();
			});
		});		
		
	}
	
	
	/*
	// dynamically load pages 
	
	function load_content(content_name, fade_duration) {
		// load html
		$("article").fadeOut(fade_duration, function() {
			$(this).load("/content/" + content_name + ".php", function() {
				$(this).show();
				
				// load js
				$.getScript("/js/" + content_name + ".js");

				// unload and load css
				$('style[title="' + content_name + '"]').remove();
				$.ajax({
					url: "/css/" + content_name + ".css",
					success: function(data){
						 $("<style type=\"text/css\" title=\"" + content_name + "\"></style>").appendTo("head").html(data);
					}
				});
				
			});
		});
	}
	
	$("nav a").click(function(event) {
		
		event.preventDefault();
		
		$("nav li.active").removeClass("active");
		$(this).parent("li").addClass("active");
		
		var content_name = $(this).attr("href").replace("?content=", "");
		
		window.location.hash = "#" + content_name;
		
		load_content(content_name, 500);
		
	});
	
	// catch reloads / direct links 
	var content_name = window.location.hash;
	if(typeof content_name != "undefined" && content_name.replace("#", "") != "") {
		load_content(content_name.replace("#", ""), 0);
	}
	*/
	
	
});
