// JavaScript Document

/**
 * This function is nothing but syntactic sugar. It is intended to be called
 * by the browser's onLoad() event and can be used to list all functions that
 * shall be called when the document is loading. Similar to rc.local scripts
 * or your Windows' StartUp-Folder.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-05-10
 */
function startUp() {
	addJSitems();
	qotd();
}

/**
 * This function adds some links and other JS-related items to your website.
 * The idea is to hide certain items from the user, if JavaScript is not enabled.
 * For instance, if the user doesn't ave JS enabled, it would not make sense to
 * have bookmarking links on the page that require JS, as they would simply
 * not do anything when clicked, than clutter screen space.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-05-10
 */
function addJSitems() {
	document.getElementById("pageOptions").innerHTML = "<a href='javascript:printMe();' target='_self' title='Print paper-friendly version'><img src='http://www.tenbergen.org/img/print_ico.gif' alt='Print paper-friendly version' /></a>&nbsp;<a href='javascript:printMe();' target='_self' title='Print paper-friendly version'>Printer friendly</a> | <a href='javascript:bookmarkMe();' target='_self' title='Bookmark this page'>Bookmark</a>";
}

/**
 * This function is used for adding a quote of the day to a specified
 * element in the DOM. In more detail, this function is responsible for loading the
 * script file containing an array with quotes and adding it to the
 * document.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-05-21
 */
function qotd() {
	//load the quote DB script
	var head= document.getElementsByTagName('head')[0];
	var script= document.createElement('script');
	script.language = "javascript"
	//add the event handler
	script.onreadystatechange= function () {
		if (this.readyState == 'loaded' || this.readyState == 'complete') printQOTD();
   	}
	script.onload = printQOTD;
	script.type = "text/javascript";
	script.src = "http://www.tenbergen.org/res/rules.js";
	//add the script to the page
	head.appendChild(script);
}

/**
 * This function is used for adding a quote of the day to a specified
 * element in the DOM. In more detail, this function is responsible for
 * extracting a rule from the loaded quote DB script and adding the quote
 * nicely formatted into the page to the element where it belongs.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-05-21
 */
function printQOTD() {
	//find a rule from the rule DB
	var amountOfRules = rules.length;
	var randomNumber = Math.floor(Math.random() * amountOfRules);
	var rule = rules[randomNumber];
	//print the rule
	document.getElementById("quote").innerHTML = '"' + rule[1] + '" -- Star Trek\'s Ferengi ' + rule[0];
}

/**
 * This function constructs an email address and provides some marginal
 * protection against spam bots. Not very powerful, but good enough for most users.
 * @param user The user name of the email address.
 * @param domain The domain name of the mail server, including top level domain.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2008-12-12
 */
function encryptMailAddress(user, domain) {
	location.href="mailto:" + user + "@" + domain;
}

/**
 * This function adds a bookmark of a URL to the browser's bookmark list.
 * It should work for Chroma, Firefox, and IE, but some IE versions act funny.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-03-29
 */
function bookmarkMe() {
	//modify these two to whatever you like
	var url   = window.location.href;		//"http://www.tenbergen.org";
	var title = document.title;				//"Basti's Homepage";
	
	if (window.sidebar) { 					//Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} 
	else if (document.all) { 				//IE Favorite
		window.external.AddFavorite(url, title);
	}
	else if (window.opera && window.print) { 		//Opera Hotlist
		//sorry, I can't...
	}
}

/**
 * This function prints the current page. The HTML document should have a
 * stylesheet for printing for best results.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-03-22
 */
function printMe() {
	window.print();
}

/**
 * This function finds an element by the ID lastModified in the DOM Tree and
 * appends the last modification date of the current document to the inner HTML.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2008-12-23
 */
function lastModified() {
	document.getElementById("lastModified").innerHTML = "Page Last Modified: " + document.lastModified;	
}

/**
 * This function opens a file in a sleek pop up window.
 * @author B. Tenbergen (http://www.tenbergen.org)
 * @version 2009-05-10
 */
function popUpWin(page, w, h, sb) {
	if ((w == -1) || (h == -1)) {
		prop = 'fullscreen, scrollbars=No';
	}
	else {
		l = (screen.width) ? (screen.width-w) / 2 : 100;
		t=(screen.height) ? (screen.height-h) / 2 : 100;
		prop = 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ',scrollbars='+ sb +',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	}
	popUpWinWindow=window.open(page, '', prop);
	popUpWinWindow.focus();
}
