/* Javascript coded by Anthony van Winkle exclusively for Night Zero */
/* You are freely welcome to reimplement this code for your own use  */
/* Contact director at nightzero.com to show me what you've done     */

var page;var random = Math.floor(Math.random()*3)+1; var oldrand = random;var oldcontent = "blurb";


/* On loading, parse the url string for page and content declarations */
function checkPage() {
	var url = window.location.href;
	/* If there is no page variable passed */
	if (url.indexOf("?") < 0) {
		/* Take the variable from the html file, if possible */
		if (url.indexOf(".htm") > 0) {page=String(url.slice(url.lastIndexOf("/")+1,url.indexOf(".htm")));}
		/* Otherwise assume it's index (i.e. documentroot) */
		else {page = "index"}
		contentHide();
		/* Check for a content declaration, display it if found */
		if (url.indexOf("#") > 0) {changeContent(String(url.slice(url.indexOf("#")+1)));}
		}
	/* If there is a page variable passed */
	else if (url.indexOf("?") > 0 ){
		/* If there is both a page and content declaration */
		if (url.indexOf("#") > 0) {
			var autoload = String(url.slice(url.indexOf("#")+1));
			page = String(url.slice(url.indexOf("?")+1,url.indexOf("#")));
			fetchContent(autoload);
		}
		/* Otherwise assume a page declaration only */
		else {page = String(url.slice(url.indexOf("?")+1));fetchContent();}
	}
	/* If something unexpected happens, just go with index */
	else {page = "index"}
}


/* Summon a new page via AJAX */
function fetchContent(autocontent) { 

	/* Choose a random background image for the forthcoming page */
   backgroundDiv = document.getElementById('background');
   oldrand = backgroundDiv.src.slice(backgroundDiv.src.indexOf(".jpg")-1,backgroundDiv.src.indexOf(".jpg"));
   while (oldrand == random) {random = Math.floor(Math.random()*3);random++;};   oldrand = random;
   document.getElementById('content').style.display = "none";
   backgroundDiv.style.display = "none"; 
   backgroundDiv.src = "images/b_"+page+"_"+random+".jpg";
   backgroundDiv.style.display = "block";
   
   /* Initialize the AJAX request */
   if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();} else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
   xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4) {
			parseDump(xmlhttp.responseText);
			/* Hide content if not the index page, then position the blurb above the navbar */
			if (page != "index") {contentHide();}
			if (window.innerHeight) {document.getElementById("details").style.paddingTop = parseInt(window.innerHeight-250)+"px";}
			if (document.body.offsetHeight) {document.getElementByID("details").style.paddingTop = parseInt(document.body.offsetHeight-250)+"px";}
			$(document.getElementById('content')).fadeIn(800);
			/* Load a declared content page, if one was requested */
			if (autocontent != null) {changeContent(autocontent);}
		}
	};
   xmlhttp.open("GET",page+".htm",true);
   xmlhttp.send(null);
  
}

/* Strip down the AJAX results to only the <content> headers, and append the background image */
function parseDump(dump) {
	document.getElementById('content').innerHTML = dump.slice(dump.indexOf("<content>"),dump.indexOf("</content>")+10);
	$(document.getElementById('content')).append($("<div class=\"img\"><img id=\"background\" src=\""+backgroundDiv.src+"\"></div>"));
}

/* Rotate through each of the three background images for the current page */
function changeImage() {
  random++; if (random > 3) {random = 1}; 
  oldrand = random;
  backgroundDiv = document.getElementById('background'); 
  $(function () {
        var img = new Image();
        $(img).load(function(){
            $(backgroundDiv).fadeOut(1800,function(){backgroundDiv.src = "images/b_"+page+"_"+random+".jpg"; $(backgroundDiv).fadeIn(1800);});
        }).attr('src', "images/b_"+page+"_"+random+".jpg");
    });
 }


/* Listen for mouse events to trigger the navbar popin */
function mouseListen() {
  if (document.addEventListener){document.addEventListener('mousemove',getCursorXY, false); }
  else if (document.attachEvent){document.attachEvent('mousemove',getCursorXY);}
}

/* If the mouse is in the bottom 90 pixels, activate the navbar */
function getCursorXY(e) {
	var pos = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	var mousePos = window.innerHeight + window.pageYOffset- pos;
	if (mousePos <= 90){ $(".navbar").fadeIn(300); return false;}
	else {$(".navbar").fadeOut(300);}
	
}

/* Fade out the current content box and replace with the chosen one */
function changeContent(content) {
	var deets = document.getElementById("details");
	/* If the same content is chosen again, fade it out (toggle) and reset tracker */
	if (oldcontent == content) {$(deets).fadeOut(300,contentHide());oldcontent="blurb";}
	else{
	$(deets).fadeOut(300, function() {
		/* To determine height, display must be "block", so we secretly switch to visibility "hidden" and measure it */
		deets.style.visibility = "hidden";
		deets.style.display = "block";
		document.getElementById("p_"+oldcontent).style.display = "none";
		document.getElementById("p_"+content).style.display = "block";
		/* Calculate top padding based on window height and content height */
		var difference = window.innerHeight-document.getElementById("p_"+content).clientHeight;
		/* If there's room, place the details just above the navbar, otherwise at the bottom of the page, otherwise 100px from the top */
		if (difference > 110) {deets.style.paddingTop = difference-110+"px"}
		else if (difference <=110 && difference > 0) {deets.style.paddingTop = difference-2+"px"}
		else {deets.style.paddingTop = "100px"}
		/* Swap back display and visibility settings for jQuery fadeIn effect and track current content */
		deets.style.display = "none";
		deets.style.visibility = "visible";
		oldcontent = content;
		$(deets).fadeIn(300); 
	});
	}
}

/* Hide all distinct content sections on pageload or content selection */
function contentHide() {
	/* Build an array of all list items in the menu */
	var menuArray = document.getElementById("content_menu").getElementsByTagName("li");
	/* For each list item, hide the paragraph of the same name */
	for (i=0;i<menuArray.length;i++) {
	var target = menuArray[i].getElementsByTagName("a")[0].href;
	document.getElementById("p_"+target.slice(target.indexOf("#")+1)).style.display = "none";
	}
	/* Reset the current tracker */
	oldcontent = "blurb";
}

/* On page loadup, prepare for JS functionality */
function initializePage() {
	/* Replace static navigation links with AJAX commands */
	var linkarray = document.getElementsByName('navlink');
	for (i=0;i<linkarray.length;i++) {
		url = linkarray[i].href;
		url = url.slice(url.indexOf(".com/")+5,url.indexOf(".htm"));
		linkarray[i].href = "/index.htm?"+url;
	}
	/* Position content panel based on window height, with IE-specific catch*/
	if (window.innerHeight) {document.getElementById("details").style.paddingTop = parseInt(window.innerHeight-250)+"px";}
	else if (document.body.offsetHeight) {document.getElementByID("details").style.paddingTop = parseInt(document.body.offsetHeight-250)+"px";}
	/* Initialize rotating background image */
	setInterval("changeImage();",10000);
}

