// this is a javascript that attempts to be non-intrusive
//- it adds a menu bar to the current page
//- it looks for an element called 'centerme' and centers it on the screen
//- it does this without adding obviously named functions or messing up events

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function enijssen_createMenuBar()
{
	var span = document.createElement("div");
	var span2 = document.createElement("span");
	
	span.className = "homebar";
	
	if(readCookie("menu-position") == "bottom"){
		span.style.bottom = "0px";
		span2.title = "click here to move the menu to the top of the screen";
	}
	else{
		span.style.top = "0px";
		span2.title = "click here to move the menu to the bottom of the screen";
	}
	
	span.id = "enijssen_menubar";
	document.body.appendChild(span);
	
	span2.className = "move";
	
	span2.onclick = function(){
		if(readCookie("menu-position") == "bottom"){
			document.getElementById("enijssen_menubar").style.bottom = "";
			document.getElementById("enijssen_menubar").style.top = "0px";
			this.title = "click here to move the menu to the bottom of the screen";
			createCookie("menu-position","top",999);
		}
		else{
			document.getElementById("enijssen_menubar").style.top = "";
			document.getElementById("enijssen_menubar").style.bottom = "0px";
			this.title = "click here to move the menu to the top of the screen";
			createCookie("menu-position","bottom",999);
		}
	}
	span2.appendChild(document.createTextNode(" "));
	span.appendChild(span2);
	
	//add home link
	var a = document.createElement("a");
	a.href = "http://www.enijssen.com/";
	a.appendChild(document.createTextNode("home"));
	span.appendChild(a);
	span.appendChild(document.createTextNode("   "));
		
	//add all published articles
	for(link in enijssen_links)
	{
		var link_e = enijssen_links[link].split(".");
		var a = document.createElement("a");
		a.href = "http://www.enijssen.com/publish/" + enijssen_links[link];
		a.appendChild(document.createTextNode(link_e[0]));
		span.appendChild(a);
		span.appendChild(document.createTextNode("   "));
	}
}

function enijssen_centerme()
{
	if(document.getElementById("centerme") != null)
	{
		var el = document.getElementById("centerme");
		if(!window.innerWidth) //IE
		{
			
		}
		else
		{
			el.style.position = "fixed";
			el.style.left = (window.innerWidth - el.clientWidth) / 2;
			el.style.top = window.pageYOffset + (window.innerHeight - el.clientHeight) / 2;
		}
	}
}


// now add the above public functions to the onload event, 
// this is of course a self-executing anonymous function.
(function(){ 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') { 
	    window.onload = function() {
			enijssen_createMenuBar(); 
			//enijssen_centerme();
		}
	  } else { 
	    window.onload = function() { 	      
	        oldonload();
			enijssen_createMenuBar();
			//enijssen_centerme();
	    } 
	 } 
	 
	 //and the onresize event
	 var oldonresize = window.onresize; 
	  if (typeof window.onresize != 'function') { 
	    window.onresize = function() {
			//enijssen_centerme(); 
		}
	  } else { 
	    window.onresize = function() { 	      
	        oldonresize();
			//enijssen_centerme();
	    } 
	 } 
}())
