/* This script displays a banner for a webpage and a sidebar containing 
a list of links on a webpage.  The links are read from a file on the 
server.  The HTML page is expected to contain a banner table and sidebar 
table with tbody ids "banner" and"idSidebar" respectively 
*/

window.onload = createBanner ;  

var xhr = false ;

var updateTag = "Updated October 2011";

var fileLocation = "sidebar1.txt" ;   //url for text file
var bannerId = "banner" ;
var homepageUrl = "http://stanford.edu/~walbot/index.html";
var contactUrl = "http://stanford.edu/~walbot/contact.html";
var cornImgPath = "http://stanford.edu/~walbot/images/top_corn.gif" 

var tableId = "idSideBar" ;          //html id for table
var linkList = new Array() ;         //array of link names and urls


function createBanner() {
	
   var bannerTBody = document.getElementById(bannerId);
   
   var row1 = document.createElement("tr"); 
   row1.className = "r1";
   
   //three elements in row1 for image and two title sections
   
   var imageCell = document.createElement("td");
   imageCell.rowspan = "2";
   imageCell.className = "imgCell";
   //  TODO get the image to spread into second row, maybe imageCell.rowspan = 2;
   var cornImg = document.createElement("img");
   cornImg.className = "cornImg";
   cornImg.src = cornImgPath;
   imageCell.appendChild(cornImg);
   row1.appendChild(imageCell);
  
  
   var titleCell1 = document.createElement("td");
   titleCell1.className = "tCell1";
   var mainTitle = document.createElement("span");
   mainTitle.className = "title";
   mainTitle.appendChild(document.createTextNode("The Walbot Lab"));
   titleCell1.appendChild(mainTitle);
   titleCell1.appendChild(document.createElement("br"));
   titleCell1.appendChild(document.createTextNode("Maize anther development:  specification of cell")); 
   titleCell1.appendChild(document.createElement("br"));
   titleCell1.appendChild(document.createTextNode("types and activation of ")); 
   var mudr_name = document.createElement("span");
   mudr_name.innerHTML = "<em>MuDR</em>/<em>Mu</em>";
   titleCell1.appendChild(mudr_name);
   titleCell1.appendChild(document.createTextNode(" transposons"));
   row1.appendChild(titleCell1);
   
   
  
   var titleCell2 = document.createElement("td");
   titleCell2.className = "tCell2";
   titleCell2.appendChild(document.createTextNode("at Stanford University"));
   row1.appendChild(titleCell2);

	bannerTBody.appendChild(row1);



	//Now build the thin navigation bar with HOME and CONTACT links
	var row2 = document.createElement("tr");
	row2.className = "r2";
	
	var navBarCell1 = document.createElement("td");
	navBarCell1.appendChild(document.createTextNode(updateTag));
	//navBarCell1.classname = "bannerBottomRow";
    row2.appendChild(navBarCell1);
    
    var navBarCell2 = document.createElement("td");
    //navBarCell2.classname = "bannerBottomRow";
    row2.appendChild(navBarCell2);
    
    var navBarCell3 = document.createElement("td");
    
    buildNavBarLink("HOME    ", homepageUrl, navBarCell3);
    //navBarCell3.appendChild(document.createTextNode("\t")); // TODO needs fine-tuning
    buildNavBarLink("    CONTACT", contactUrl, navBarCell3);
	row2.appendChild(navBarCell3);
	
	
	bannerTBody.appendChild(row2);
	
    //NB: for effective CSS formatting, we need the following classes:
   //image (img element?)
   //main title (font element)
   //subtitles (font element)
   //bannerTopRow (tr or td) AND we need a way to specify different row widths
   //bannerBottomRow (tr or td)
   //navBarLink
   readFile();
   return false;
}


function buildNavBarLink(linkTitle, linkUrl, parent)
{
    var navBarLink= document.createElement("a");
    navBarLink.className = "navBarLink";
    navBarLink.href = linkUrl;
    navBarLink.appendChild(document.createTextNode(linkTitle));
    parent.appendChild(navBarLink);
    return false;
}

//Much of this code taken from John Fernandes' genrcds_famtree10 script and Javascript and Ajax Visual Quick Start Guide 2007

function readFile() {

   if (window.XMLHttpRequest)
   {  
      xhr = new XMLHttpRequest() ;
   }
   else 
   {
      if (window.ActiveXObject)
      {
         try {
            xhr = new ActiveXObject(Microsoft. XMLHTTP);
            }
            catch(e)
            {
               alert("Your browser is too old.");
            }
      }
   }
   if (xhr) 
   {
      xhr.onreadystatechange = makeList;
      xhr.open("GET", fileLocation, true);
      xhr.send(null);
   }
   else
   {
      alert("Sorry, couldn't request file.");
   }
   return false;
}


function makeList() {
   if (xhr.readyState == 4) 
   {
    if (xhr.status == 200) 
    {
      //expected file format: link name and link value on the same line and tabbed, each link with its own line      
      linkList = xhr.responseText.split(/\n/); 
      
      //debugging code
      //alert("Found " + linkList.length + " links.");
      buildTable();
    
    }
   }
   return false;

}

function buildTable() {

   var tablebody = document.getElementById(tableId) ;
   
   var line ;
   var newRow ;
   var newCell ;
   
   
   var newLink;

   //alert("document.location: " + document.location);
   //alert("document.URL: " + document.URL);
   for(i = 0; i < linkList.length; i++)
   {
      line = linkList[i].split(/\t/);
      var newRow = document.createElement("tr")
      var newCell = document.createElement("td");
      if(line.length > 1)
      {
         //create a link to appropriate url
         //newLink = document.createElement("a", {"href": line[1], "text": line[0]}) ; 
         newLink = document.createElement("a") ; 
         newLink.href = line[1] ;
         //alert("newLink.href: " + newLink.href);
         //NB: below line works for Chrome, but to support Explorer we need
         //to manually build the link URL from the href before comparing
         if(newLink.href == document.URL) continue;
         var re = new RegExp( "/" + newLink.href + "$");
         if(document.URL.match(re)) continue;
         newLink.appendChild(document.createTextNode(line[0])) ;
      }
      else
      { 
        newLink = document.createTextNode(line[0]);
        newRow.className = "NonLinkingText";
      }
      newCell.appendChild(newLink) ;
      newRow.appendChild(newCell) ;
      tablebody.appendChild(newRow) ;
   }

   return false;
}

