/* Simplify adding on load events */
function addLoadEvent(func,arg){
	if (!arg){
		var oldonload = window.onload;
	  	if (typeof window.onload != 'function') { window.onload = func; } 
		else { window.onload = function() { oldonload(); func(); } }
	}
  	else{/*if the onload event has an argument/parameter cater for that*/
  		if (arg){
  			oldonload = window.onload;
  			if (typeof window.onload != 'function') { window.onload = func(arg); }
     		window.onload = function() { oldonload(); func(arg); }
  		}
	}	
}

function sidebar(page) {
    var pages = new Array(
		"Home", 
		"Research",
		"CV",
		"Courses", 
		"Music", 
		"Contact");

    var links = new Array(
		"./index.html", 
		"./research.html",
		"./cv.html",
		"./courses.html", 
		"./music.html",
		"./contact.html");

	// write header
    document.write('<div id="sidebar">');
    document.write('	<ul>');
    document.write('		<li>');
    document.write('			<h2>' + page + '</h2>');
    document.write('			<ul>');

	// trim page to have only prefix
	var ps = page.search('[.]');
	if (ps != -1) {	page = page.substring(0, ps) }

	// write nav
    for (var i in pages) {
        if (pages[i] == page) {
            document.write('<li><b><a href=\"' + links[i] + '\">' + pages[i] + '</a></b></li>');
        } else {
            document.write('<li><a href=\"' + links[i] + '\">' + pages[i] + '</a></li>');
        }
    }
    document.write('			</ul>');
    document.write('		</li>');
    document.write('	</ul>');
    document.write('</div>');
}

function header() {
    document.write('<div id="header">');
    document.write('<h1>David L Millman</h1>');
    document.write('<p>UNC-Chapel Hill Computer Science</p>');
    document.write('</div>');
}

function beginBody() {
    document.write('<div id="wrapper">');
    document.write('<div class="top">');
    document.write('    <div id="page">');
    document.write('    	<div class="bgtop">');
    document.write('            <div class="bgbtm">');
}

function endBody() {
    document.write('                <div style="clear:both">&nbsp;</div>');
    document.write('            </div>');
    document.write('        </div>');
    document.write('    </div>');
    document.write('</div>');
    document.write('</div>');
}

function copyright() {
    document.write('<div id="footer">');
    document.write('<p>&copy;2011 All Rights Reserved &nbsp;&bull;&nbsp; Template design by <a href="http://www.freecsstemplates.org/">Free CSS Templates</a></p>');
    document.write('</div>');
}

function email() { dave(); at(); uncCS(); }
function dave() { document.write('dave'); }
function at() {	document.write('@'); }
function uncCS() {	document.write('cs.unc.edu'); }


/** Google Analytics */
function loadGAScript(){
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
} 
loadGAScript();

function callGA(){
	var pageTracker = _gat._getTracker("UA-13112192-1");
	pageTracker._trackPageview();
}
addLoadEvent(callGA);
