/**
 * @author Rob Borgersen
 */

/*************
 * includeJS(str)
 * 
 * This function forces the current page to include script_filename
 * as a java include.
 * 
 * usage: 
 * 		include("layout.js"); 
 * 		redirect("http://www.google.ca");
 * 
 * @author Robert Borgersen
 *  
 */
function includeJS(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}

/*************
 * redirect(str)
 * 
 * This function redirects the window to the site str.
 * 
 * usage: 
 * 		redirect("../index.htm"); 
 * 		redirect("http://www.google.ca");
 * 
 * @author Robert Borgersen
 *  
 */
function redirect(str)
{
	window.location = str;
}


/*************
 * The following code adds the ability to put
 * "newWindow" in the class of a link, and
 * the link will automatically open in a new
 * window.
 * 
 * @author Unknown
 *  
 */

addEvent(window, 'load', setUpNewWindowLinks); 

function setUpNewWindowLinks()
{
	var a = document.getElementsByTagName("a");
	this.check = function(obj)
	{
		return jscss("check",obj,"newWindow","");
	}
	this.set = function(obj)
	{
		obj.target = "_blank";
	}
	for(var i=0; i<a.length; i++)
	{
		if(check(a[i]))
		{ 
			set(a[i]);
		}
	};
}


/*************
 * The following code adds the ability to put
 * "popup" in the class of a link, and
 * the link will automatically open in a new
 * window.
 * 
 * @author Unknown
 *  
 */
addEvent(window, 'load', setUpPopupLinks); 

function openPopup(url){
	strWidth = screen.availWidth;
	strHeight = screen.availHeight;
	
	var tools="";
	tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


function setUpPopupLinks()
{
	var a = document.getElementsByTagName("a");
	this.check = function(obj)
	{
		return jscss("check",obj,"Popup","");
	}
	this.set = function(obj)
	{
		addEvent(obj, 'onclick', function()
		{
			window.open(obj.href, 'newWin', 'resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,top=0,left=0');
		})
	}
	for(var i=0; i<a.length; i++)
	{
		if(check(a[i]))
		{ 
			set(a[i]);
		}
	};
}

function printRandomQuoteHere()
{
	var quote = getRandomQuote();
	document.write(quote);
}

function getRandomQuote()
{
	var randomQuote="FAIL";
	var randNum = getRandomNumber(14);
	switch(randNum)
	{
		case 1:
			randomQuote = "Mathematics is like love; a simple idea, but it can get complicated";
			break;
		case 2:
			randomQuote = "Obvious is the most dangerous word in mathematics. -- E.T. Bell";
			break;
		case 3:
			randomQuote = "Mathematics should be fun. -- Peter J. Hilton";
			break;
		case 4:
			randomQuote = "Time is a great teacher, but unfortunately it kills all its pupils - Louis Hector Berlioz";
			break;
		case 5:
			randomQuote = "Old mathematicians never die, they just tend to infinity.";
			break;
		case 6:
			randomQuote = "Never take life seriously. Nobody gets out alive anyway.";
			break;	
		case 7:
			randomQuote = "There are three kinds of mathematicians; those who can count and those who can't.";
			break;	
		case 8:
			randomQuote = "To a mathematician, real life is a special case.";
			break;
		case 9:
			randomQuote = "2 is not equal to 3 - not even for very large values of 2. ";
			break;
		case 10:
			randomQuote = "Pure mathematics is, in its way, the poetry of logical ideas. ";
			break;
		case 11:
			randomQuote = "Black holes result from God dividing the universe by zero. ";
			break;
		case 12:
			randomQuote = "A mathematician is a device for turning coffee into theorems. ";
			break;
		case 13:
			randomQuote = "Sometimes it is useful to know how large your zero is.";
			break;
		case 14:
			randomQuote = "Mathematics is the supreme judge; from its decisions there is no appeal. ";
			break;
		
	} 
										
	return randomQuote;
	
}

/*
 * Date Format 1.2.3
 * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
 * MIT license
 *
 * Includes enhancements by Scott Trenda <scott.trenda.net>
 * and Kris Kowal <cixar.com/~kris.kowal/>
 *
 * Accepts a date, a mask, or a date and a mask.
 * Returns a formatted version of the given date.
 * The date defaults to the current date/time.
 * The mask defaults to dateFormat.masks.default.
 */

var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};


function printLastMod()
{
	//document.write("Last Modified: "+date_lastmodified()+"");
	document.write("Last Modified: "+dateFormat(document.lastModified, "yyyy-mm-dd h:MM:ss TT Z")+"");
		
}


function insertFooterHere()
{
	document.writeln('<div id="Footer">');
	document.writeln('Copyright Robert Borgersen, 2009.');
	document.writeln('</div>');
}

// Returns a random number between 1 and max.
function getRandomNumber(max)
{
	var randomnumber=Math.floor(Math.random()*max)+1;
	return randomnumber;
}

function toggleVisible(divID) 
{
	if (document.getElementById(divID).style.display == "none")
		document.getElementById(divID).style.display = "";
	else if (document.getElementById(divID).style.display == "")
		document.getElementById(divID).style.display = "none";
	else alert("An error occured in the menu. Contact the web master.");
	
	headerName = divID + "_header";	
	obj = document.getElementById(headerName);	
	
	if(jscss("check",obj,"expandable",""))
	{
		jscss("swap",obj,"expandable","shrinkable");
	
	}else if(jscss("check",obj,"shrinkable",""))
	{
		jscss("swap",obj,"shrinkable","expandable");
	}
}

function cycleImage()
{
	if (document.getElementById('Picture') && document.getElementById('Picture').getAttribute)
	{
    	currentImage = parseInt(document.getElementById('Picture').getAttribute('bannerID'));
		currentImage = currentImage + 1;
		if(currentImage > 6) currentImage=1;
		document.getElementById('Picture').setAttribute('bannerID',currentImage.toString());
	}
}

/*************
 * addEvent
 * 
 * @author Scott Andrew LePera
 * 
 * Adds a function fn to object obj in event evType
 * 
 * Example:
 *   addEvent(window, 'load', foo); 
 * 
  * 
 */

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   //alert("Handler could not be attached");
   return false; 
 } 
}


/**********************************************************************
a   defines the action you want the function to perform.
o   the object in question.
c1  the name of the first class
c2  the name of the second class

Possible actions are:
swap   replaces class c1 with class c2 in object o.
add    adds class c1 to the object o.
remove removes class c1 from the object o.
check  test if class c1 is already applied to object o and returns true or false.

courtesy of http://onlinetools.org
**********************************************************************/ 
function jscss(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}
