addLoadEvent(prepareAnchors);
addLoadEvent(prepareButtons);
addLoadEvent(prepareContent);
addLoadEvent(prepareHeader);
addLoadEvent(prepareInput);
addLoadEvent(fixBrowserFailures);
addLoadEvent(urlFilter);

function buttonClick(button,show)
{ //shows or hides content upon button click
   var content = getElementsByClass("content",button.parentNode)[0];
   var buttonText = button.firstChild.nodeValue;
   if (show==null)
      var show = /Show/.test(buttonText);
   button.firstChild.nodeValue = show?buttonText.replace(/Show/,"Hide"):buttonText.replace(/Hide/,"Show");
   content.style.display = show?"block":"none";
}

function prepareButtons()
{ //adds button tags to DOM to control dynamic content
   //create contact button and set attributes
   var contactButton = document.createElement("button");
   contactButton.id = "contactButton";
   contactButton.defaultState = "Hide";
   contactButton.onclick = function() {
      buttonClick(this);
   }
   contactButton.appendChild(document.createTextNode("Show Contact Info")); //Firefox bug prevents use of value attribute
   var address =  document.getElementsByTagName("address")[0];
   address.parentNode.insertBefore(contactButton,address);
   address.style.display = "none";

   //create buttons for all h2 headers
   var h2 = document.getElementsByTagName("h2");
   var contentHead = /(^|\s)contentHead(\s|$)/;
   var defaultShow = /(^|\s)defaultShow(\s|$)/;
   var defaultHide = /(^|\s)defaultHide(\s|$)/;
   for (var i=0; i<h2.length; i++)
      if (contentHead.test(h2[i].className)) {
         //create button and set attributes
         var button = document.createElement("button");
         button.id = h2[i].parentNode.id + "Button";
         button.defaultState = "Show";
         button.onclick = function() {
            buttonClick(this);
         }
         if (defaultShow.test(h2[i].className)) {
            button.appendChild(document.createTextNode("Hide")); //Firefox bug prevents use of value attribute
         }
         else if (defaultHide.test(h2[i].className)) {
            button.appendChild(document.createTextNode("Show")); //Firefox bug prevents use of value attribute
            var content = getElementsByClass("content",h2[i].parentNode)[0];
            content.style.display = "none";
         }
         insertAfter(button,h2[i]);
      }
}

function prepareContent()
{ //sets information in the content notice
   document.getElementById("javascriptNotice").style.display = "none";
   if (document.lastModified)
      document.getElementById("lastUpdated").appendChild(document.createTextNode("Last updated " + Date(document.lastModified).toString()));
   
   //insert print link
   var printLink = document.createElement("a");
   printLink.href = "javascript: window.print();";
   printLink.appendChild(document.createTextNode("Print"));
   var printDownload = document.getElementById("printDownload");
   printDownload.insertBefore(document.createTextNode(" \u2014 "),printDownload.firstChild); //em dash
   printDownload.insertBefore(printLink,printDownload.firstChild);
}

function displayControls(show)
{ //toggle display of button and input control elements
   var controls = [document.getElementsByTagName("button"), document.getElementsByTagName("label")];
   for (var i=0; i<controls.length; i++)
      for (var j=0; j<controls[i].length; j++)
         controls[i][j].style.display = show?"inline":"none";
}

function loadLink(quickLink)
{ //show content based on the selected quick link
   //establish quick link settings
   var showContact, showExperience, showResearch, showConsulting, showTeaching, showAfter, showEducation, showDegrees, showCertifications, showExpertise, showPublications, showArticles, showTheses, showAbstracts, showPersonal, showControls;
   switch (quickLink.id) {
      case "researchLink": showContact = false; showExperience = true; showResearch = true; showConsulting = false; showTeaching = false; showAfter = "1998"; showEducation = true; showDegrees = true; showCertifications = false; showExpertise = true; showPublications = true; showArticles = true; showTheses = true; showAbstracts = true; showPersonal = true; showControls = false; break;
      case "consultingLink": showContact = false; showExperience = true; showResearch = false; showConsulting = true; showTeaching = false; showAfter = "2004"; showEducation = true; showDegrees = true; showCertifications = false; showExpertise = true; showPublications = true; showArticles = true; showTheses = false; showAbstracts = false; showPersonal = true; showControls = false; break;
      case "teachingLink": showContact = false; showExperience = true; showResearch = true; showConsulting = false; showTeaching = true; showAfter = "2000"; showEducation = true; showDegrees = true; showCertifications = false; showExpertise = true; showPublications = true; showArticles = true; showTheses = true; showAbstracts = false; showPersonal = true; showControls = false; break;
      case "defaultLink": showContact = false; showExperience = true; showResearch = true; showConsulting = true; showTeaching = false; showAfter = "2000"; showEducation = true; showDegrees = true; showCertifications = false; showExpertise = true; showPublications = true; showArticles = true; showTheses = false; showAbstracts = false; showPersonal = true; showControls = false; break;
      case "customLink": showControls = true; break;
      case "unabridgedLink": showContact = true; showExperience = true; showResearch = true; showConsulting = true; showTeaching = true; showAfter = "1994"; showEducation = true; showDegrees = true; showCertifications = true; showExpertise = true; showPublications = true; showArticles = true; showTheses = true; showAbstracts = true; showPersonal = true; showControls = false; break;
   }
   
   //toggle buttons
   var buttons = document.getElementsByTagName("button");
   for (var i=0; i<buttons.length; i++)
      switch (buttons[i].id) {
         case "contactButton": if (showContact!=null) buttonClick(buttons[i],showContact); break;
         case "experienceButton": if (showExperience!=null) buttonClick(buttons[i],showExperience); break;
         case "educationButton": if (showEducation!=null) buttonClick(buttons[i],showEducation); break;
         case "expertiseButton": if (showExpertise!=null) buttonClick(buttons[i],showExpertise); break;
         case "publicationsButton": if (showPublications!=null) buttonClick(buttons[i],showPublications); break;
         case "personalButton": if (showPersonal!=null) buttonClick(buttons[i],showPersonal); break;
      }
   
   //toggle controls
   var controls = document.getElementsByTagName("input");
   for (var i=0; i<controls.length; i++)
      switch (controls[i].id) {
         case "research": if (showResearch!=null) controls[i].checked = showResearch; break;
         case "consulting": if (showConsulting!=null) controls[i].checked = showConsulting; break;
         case "teaching": if (showTeaching!=null) controls[i].checked = showTeaching; break;
         case "cutoffYear": if (showAfter!=null) controls[i].value = showAfter; break;
         case "degree": if (showDegrees!=null) {controls[i].checked = showDegrees; controls[i].onclick();} break;
         case "certification": if (showCertifications!=null) {controls[i].checked = showCertifications; controls[i].onclick();} break;
         case "article": if (showArticles!=null) {controls[i].checked = showArticles; controls[i].onclick();} break;
         case "thesis": if (showTheses==null) {controls[i].checked = showTheses; controls[i].onclick();} break;
         case "abstract": if (showAbstracts!=null) {controls[i].checked = showAbstracts; controls[i].onclick();} break;
      }
   updateExperience();
   displayControls(showControls);
   
   //shade quick links based on active view
   var allQuickLinks = getElementsByClass("quickLink",quickLink.parentNode,"a");
   for (var i=0; i<allQuickLinks.length; i++)
      allQuickLinks[i].style.color = "#FFFFFF";
   quickLink.style.color = "#CCCCCC";
   quickLink.blur();
}

function prepareHeader()
{ //adds header links to DOM to control dynamic content
   //create header and set attributes
   var headerDiv = document.createElement("div");
   headerDiv.setAttribute("id","header");
   var contactDiv = document.getElementById("contact");
   addClass(contactDiv,"headerRoom");
   document.body.insertBefore(headerDiv,contactDiv);
   
   //create quick link and set attributes
   var quickLinkLabel = document.createElement("span");
   quickLinkLabel.appendChild(document.createTextNode("Quick Links:"));
   quickLinkLabel.id = "quickLinkLabel";
   headerDiv.appendChild(quickLinkLabel);

   //create research link and set attributes
   var researchLink = document.createElement("a");
   researchLink.appendChild(document.createTextNode("Research"));
   researchLink.id = "researchLink";
   researchLink.className = "quickLink";
   researchLink.href = "?research";
   researchLink.onclick = function() {
      loadLink(this);
   }
   headerDiv.appendChild(researchLink);

   //create consulting link and set attributes
   var consultingLink = document.createElement("a");
   consultingLink.appendChild(document.createTextNode("Consulting"));
   consultingLink.id = "consultingLink";
   consultingLink.className = "quickLink";
   consultingLink.href = "?consulting";
   consultingLink.onclick = function() {
      loadLink(this);
   }
   headerDiv.appendChild(consultingLink);

   //create teaching link and set attributes
   var teachingLink = document.createElement("a");
   teachingLink.appendChild(document.createTextNode("Teaching"));
   teachingLink.id = "teachingLink";
   teachingLink.className = "quickLink";
   teachingLink.href = "?teaching";
   teachingLink.onclick = function() {
      loadLink(this);
   }
   headerDiv.appendChild(teachingLink);

   //create default link and set attributes
   var defaultLink = document.createElement("a");
   defaultLink.appendChild(document.createTextNode("Default"));
   defaultLink.id = "defaultLink";
   defaultLink.className = "quickLink";
   defaultLink.href = "?default";
   defaultLink.onclick = function() {
      loadLink(this);
   }
   defaultLink.style.color = "#CCCCCC";
   headerDiv.appendChild(defaultLink);

   //create custom link and set attributes
   var customLink = document.createElement("a");
   customLink.appendChild(document.createTextNode("Custom"));
   customLink.id = "customLink";
   customLink.className = "quickLink";
   customLink.href = "?custom";
   customLink.onclick = function() {
      loadLink(this);
   }
   headerDiv.appendChild(customLink);
   
   
   //create unabridged link and set attributes
   var unabridgedLink = document.createElement("a");
   unabridgedLink.appendChild(document.createTextNode("Unabridged"));
   unabridgedLink.id = "unabridgedLink";
   unabridgedLink.className = "quickLink";
   unabridgedLink.href = "?unabridged";
   unabridgedLink.onclick = function() {
      loadLink(this);
   }
   headerDiv.appendChild(unabridgedLink);
   
   //create francais link and set attributes
   var francaisLink = document.createElement("a");
   francaisLink.appendChild(document.createTextNode("Français"));
   francaisLink.id = "francaisLink";
   francaisLink.className = "langLink";
   francaisLink.href = "mar_cv.html";
   headerDiv.appendChild(francaisLink);
   
   //create english link and set attributes
   var englishLink = document.createElement("a");
   englishLink.appendChild(document.createTextNode("English"));
   englishLink.id = "englishLink";
   englishLink.className = "langLink";
   englishLink.href = "mar_cv.html";
   headerDiv.appendChild(englishLink);
   
   //create deutsch link and set attributes
   var deutschLink = document.createElement("a");
   deutschLink.appendChild(document.createTextNode("Deutsch"));
   deutschLink.id = "deutschLink";
   deutschLink.className = "langLink";
   deutschLink.href = "mar_cv.html";
   headerDiv.appendChild(deutschLink);
   
   //shade language link based on loaded document
   switch (document.URL.substring(document.URL.lastIndexOf("/")+1,document.URL.lastIndexOf(".")+5)) {
      case "mar_lebenslauf.html":
         deutschLink.style.color = "#CCCCCC";
         break;
      case "mar_cv.html":
         englishLink.style.color = "#CCCCCC";
         break;
      case "mar_resume.html":
         francaisLink.style.color = "#CCCCCC";
         break;
      default:
   }
}

function highlightPublications()
{ //sets list item styles for displayed publications
   var publications = document.getElementById("publications").getElementsByTagName("li");
   for (var i=0, j=0; i<publications.length; i++)
      if (publications[i].style.display=="list-item") {
         publications[i].style.background = (j%2==0)?"#FFFFFF":"#EEEEEE";
         j++;
      }
}

function checkboxClick(checkBox,division)
{ //alters <li class="content"> display values upon checkbox state change
   var listItems = document.getElementById(division).getElementsByTagName("li");
   for (var i=0; i<listItems.length; i++)
      if (RegExp(checkBox.value).test(listItems[i].className)) //value must be alphanumeric (inadvertently hides <li> with "multiple" classes)
         listItems[i].style.display = (checkBox.checked)?"list-item":"none";
   if (division=="publications")
      highlightPublications();
}

function updateExperience() {
   var jobs = ["research","consulting","teaching"];
   var checked = [document.getElementById(jobs[0]).checked,document.getElementById(jobs[1]).checked,document.getElementById(jobs[2]).checked];
   var cutoffYear = parseInt(document.getElementById("cutoffYear").value);
   var experience = document.getElementById("experience").getElementsByTagName("li");
   for (var i=0, date; i<experience.length; i++) {
      date = parseInt(getElementsByClass("dateRange",experience[i])[0].firstChild.nodeValue.split("\u2013").pop()); //en dash
      if ( (isNaN(date)) || (date>cutoffYear) ) //if last date of experience is greater than or equal to the year requested ("now"=NaN)
         experience[i].style.display =
            ( (RegExp(jobs[0]).test(experience[i].className)&&checked[0]) ||
              (RegExp(jobs[1]).test(experience[i].className)&&checked[1]) ||
              (RegExp(jobs[2]).test(experience[i].className)&&checked[2]) ) ? "list-item" : "none";
      else
         experience[i].style.display = "none";
   }
}

function prepareInput()
{ //adds input tags to DOM to control dynamic content
   //add text input box to experience division
   var content = getElementsByClass("content",document.getElementById("experience"))[0];
   var textBox = document.createElement("input");
   textBox.type = "text";
   textBox.id = "cutoffYear";
   textBox.value = "2000";
   textBox.size = textBox.maxLength = "4";
   textBox.onkeyup = function() {
      if (this.value.length==4)
         updateExperience();
   }
   var textBoxLabel = document.createElement("label");
   textBoxLabel.appendChild(document.createTextNode("Showing experience after"));
   textBoxLabel.appendChild(textBox);
   content.insertBefore(textBoxLabel,content.firstChild);
   
   //set text box label styles
   textBoxLabel.style.left = "570px";
   
   //add checkboxes to experience division
   var labels = ["Teaching","Consulting","Research"];
   var values = ["teaching","consulting","research"];
   for (var i=0; i<values.length; i++) {
      //create checkboxes and set attributes
      var checkBox = document.createElement("input");
      checkBox.type = "checkbox";
      checkBox.id = checkBox.value = values[i];
      checkBox.onclick = function() {
         updateExperience();
      }
      var checkBoxLabel = document.createElement("label");
      checkBoxLabel.appendChild(checkBox);
      checkBoxLabel.appendChild(document.createTextNode(labels[i]));
      content.insertBefore(checkBoxLabel,content.firstChild);
      if ( (values[i]=="research") || (values[i]=="consulting") ) {
         checkBox.checked = checkBox.defaultChecked = "checked"; //bug in IE requires explicity setting defaultChecked
      }
      
      //set checkbox label position
      checkBoxLabel.style.left = 270 + (values.length-1-i)*100 + "px"; //convoluted to get proper tab order
   }
   
   //set experience styles
   updateExperience();
   
   //add checkboxes to education division
   var content = getElementsByClass("content",document.getElementById("education"))[0];
   var labels = ["Certifications","Degrees"];
   var values = ["certification","degree"];
   for (var i=0; i<values.length; i++) {
      //create checkboxes and set attributes
      var checkBox = document.createElement("input");
      checkBox.type = "checkbox";
      checkBox.id = checkBox.value = values[i];
      checkBox.onclick = function() {
         checkboxClick(this,"education");
      }
      var checkBoxLabel = document.createElement("label");
      checkBoxLabel.appendChild(checkBox);
      checkBoxLabel.appendChild(document.createTextNode(labels[i]));
      content.insertBefore(checkBoxLabel,content.firstChild);
      if (values[i]=="degree") {
         checkBox.checked = checkBox.defaultChecked = "checked"; //bug in IE requires explicity setting defaultChecked
      }
      
      //set checkbox label position
      checkBoxLabel.style.left = 270 + (values.length-1-i)*100 + "px"; //convoluted to get proper tab order
   }
   
   //set education styles
   var education = document.getElementById("education").getElementsByTagName("li");
   for (var i=0; i<education.length; i++)
      education[i].style.display = RegExp(values[1]).test(education[i].className)?"list-item":"none";

   //add checkboxes to publications division
   var content = getElementsByClass("content",document.getElementById("publications"))[0];
   var labels = ["Abstracts","Theses","Articles"];
   var values = ["abstract","thesis","article"];
   for (var i=0; i<values.length; i++) {
      //create checkboxes and set attributes
      var checkBox = document.createElement("input");
      checkBox.type = "checkbox";
      checkBox.id = checkBox.value = values[i];
      checkBox.onclick = function() {
         checkboxClick(this,"publications");
      }
      var checkBoxLabel = document.createElement("label");
      checkBoxLabel.appendChild(checkBox);
      checkBoxLabel.appendChild(document.createTextNode(labels[i]));
      content.insertBefore(checkBoxLabel,content.firstChild);
      if (values[i]=="article") {
         checkBox.checked = checkBox.defaultChecked = "checked"; //bug in IE requires explicity setting defaultChecked
      }
      
      //set checkbox label position
      checkBoxLabel.style.left = 270 + (values.length-1-i)*100 + "px"; //convoluted to get proper tab order
   }
   
   //set publications styles
   var publications = document.getElementById("publications").getElementsByTagName("li");
   for (var i=0; i<publications.length; i++)
      publications[i].style.display = RegExp(values[2]).test(publications[i].className)?"list-item":"none";
   highlightPublications();
}

function fixBrowserFailures()
{ //add generated content and fix text input controls for browsers that just don't get it
   //add CSS generated content to IE
   if (window.navigator.appName=="Microsoft Internet Explorer") {
      //span.jobTitle:after, span.boss:after, span.conference:after, span.institution:after, span.department:after, span.degree:after, span.program:after {content: ",";}
      var needsCommaAfter = getElementsByClass("jobTitle boss conference institution department degree program",null,"span");
      for (var i=0; i<needsCommaAfter.length; i++)
         if (needsCommaAfter[i].lastChild.nodeName=="#text")
            needsCommaAfter[i].lastChild.nodeValue += ",";
         else
            needsCommaAfter[i].appendChild(document.createTextNode(","));
      //div#personal li.leisure:after {content: ",";}
      needsCommaAfter = getElementsByClass("leisure",null,"li");
      for (var i=0; i<needsCommaAfter.length; i++)
         if (needsCommaAfter[i].lastChild.nodeName=="#text")
            needsCommaAfter[i].lastChild.nodeValue += ",";
         else
            needsCommaAfter[i].appendChild(document.createTextNode(","));
      //span.location:after, span.journal:after, span.title:after, span.volumeIssuePages:after {content: ".";}
      var needsPeriodAfter = getElementsByClass("location journal title volumeIssuePages",null,"span");
      for (var i=0; i<needsPeriodAfter.length; i++)
         if (needsPeriodAfter[i].lastChild.nodeName=="#text")
            needsPeriodAfter[i].lastChild.nodeValue += ".";
         else
            needsPeriodAfter[i].appendChild(document.createTextNode("."));
      //span.date:before {content: "(";} span.date:after {content: ")";}
      var needsParentheses = getElementsByClass("date",null,"span");
      for (var i=0; i<needsParentheses.length; i++) {
         if (needsParentheses[i].firstChild.nodeName=="#text")
            needsParentheses[i].firstChild.nodeValue = "(" + needsParentheses[i].firstChild.nodeValue;
         else
            needsParentheses[i].insertBefore(document.createTextNode("("),needsParentheses[i].firstChild);
         if (needsParentheses[i].lastChild.nodeName=="#text")
            needsParentheses[i].lastChild.nodeValue += ")";
         else
            needsParentheses[i].appendChild(document.createTextNode(")"));
      }
      //a.download:before {content: "[";} a.download:after {content: "]";}
      var needsBrackets = getElementsByClass("download",null,"a");
      for (var i=0; i<needsBrackets.length; i++) {
         if (needsBrackets[i].firstChild.nodeName=="#text")
            needsBrackets[i].firstChild.nodeValue = "[" + needsBrackets[i].firstChild.nodeValue;
         else
            needsBrackets[i].insertBefore(document.createTextNode("["),needsBrackets[i].firstChild);
         if (needsBrackets[i].lastChild.nodeName=="#text")
            needsBrackets[i].lastChild.nodeValue += "]";
         else
            needsBrackets[i].appendChild(document.createTextNode("]"));
      }
   }
}

function urlFilter()
{ //uses URL attributes to filter the default presentation
   var urlAttribute = document.URL.substr(document.URL.lastIndexOf("?")+1);
   if (/(research)|(consulting)|(teaching)|(default)|(custom)|(unabridged)/.test(urlAttribute))
      loadLink(document.getElementById(urlAttribute + "Link"));
}