// navigation bar stuff

var baselink = 'http://www.nottingham.ac.uk/astronomy/index.php';

// Object navObject

var space  = '&nbsp;&nbsp;';
var baselink1  = space
             + '<b><a href="http://www.nottingham.ac.uk/">University</a>&nbsp;&gt;&nbsp;</b>'
             + '<b><a href="http://www.nottingham.ac.uk/physics/">Physics</a>&nbsp;&gt;&nbsp;</b>';
var baselink2  = baselink1
             + '<b><a href="http://www.nottingham.ac.uk/astronomy/">Astronomy</a>&nbsp;&gt;&nbsp;</b>';
//             + '<b><a href="http://www.nottingham.ac.uk/astronomy/">Astronomy &amp; Particle Theory </a>&nbsp;&gt;&nbsp;</b>';

var title = 'Nottingham Astronomy Group : ';

function navObject(theId,theInfo) {
  this.id     = theId;
  this.href = baselink+'?dire='+theId+'&file=index';
  if (this.id == 'Home') {
    this.link = baselink1 + 
      '<i>Astronomy Home</i>'; 
//    this.link = baselink1 + 
//      '<i>Astronomy &amp; Particle Theory Home</i>';  
  } else {
    if (this.id != 'People') {
      this.link = baselink2+'<i>'+theId+'</i>';
    }
  }
  this.info    = space+theInfo;
}

// arrange the navObjects in an array, so you can loop through them

//var numObjects = 8;
var navObjects = new Array(9);
navObjects[0]  = new navObject('Home','Nottingham Astronomy Group home page');
//navObjects[0]  = new navObject('Home','index','Centre for Astronomy and Particle Theory home page');
navObjects[1]  = new navObject('Research','Find out more about our research interests');
navObjects[2]  = new navObject('PhD','A list of possible PhD projects');
navObjects[3]  = new navObject('People','Staff, research fellows and students in the group');
navObjects[4]  = new navObject('Telescope','The telescope');
navObjects[5]  = new navObject('Events','Information about upcoming events, seminars and talks');
navObjects[6]  = new navObject('Vacancies','Vacancies in the group');
navObjects[7]  = new navObject('Links','Useful research-related links');
navObjects[8]  = new navObject('Contact','How to contact and find us');


// navCurrent is the current active navigation button, will be defined later
var navCurrent = new navObject;

//------------------------------------------------------------------------------------------
// end defining the navigation objects
//------------------------------------------------------------------------------------------

function createPage() {
  createNavbar();  // create the navigation bar and info bar (in main)
  makeCurrent();   // set the current stuffnavigation button 
} // end function createPage

function getId() {
  var start = document.location.search.indexOf("?dire=");
  var name  = 'Home';
  if (start != -1) {
    start += 6;
    var end    = document.location.search.length;
    var ampend = document.location.search.indexOf("&");
    if (ampend != -1) end = ampend;
    name  = document.location.search.substring(start,end);
  }
  return unescape(name);
}

function getFile() {
  var start = document.location.search.indexOf("&file=");
  var name  = 'index';
  if (start != -1) {
    start += 6;
    var end = document.location.search.length;
    name    = document.location.search.substring(start,end);
    end     = name.indexOf("&");
    if (end != -1) name = name.substring(0,end);
  }
  return unescape(name);
}

function createNavbar() {
  // get acces to the navbar element (defined in .html pages)
  //  - style in controlled by the css style sheet
  var y = document.getElementById('navbar');
  var x = y.appendChild(document.createElement('span'));

  // loop through the navObjects
  for (var i=0; i<navObjects.length; i++) {
    // create a new button, which is a link
    var a = y.appendChild(document.createElement('a'));
    a.className   = 'navbuttonoff';        // see css/style.css for layout
    a.id          = navObjects[i].id;      // the identity of the button
    a.href        = navObjects[i].href;    // the page to which the button refers
    a.innerHTML   = navObjects[i].id;      // the text content of the button
//    a.title       = navObjects[i].info;   // the title of the link shows when hover
//        --> I personally don't like this feature...
    a.onmouseover = overbutton;            // button functionality - see below
    a.onmouseout  = outbutton;             // button functionality - see below
  }; // end for

} // end function createNavbar

function overbutton() {
  // on mouseouver, change text & background color
  this.className = 'navbuttonon';

  // get acces to the infobar element (defined in .html pages)
  // in order to change the info text 
  var y = document.getElementById('navinfo');
  for (var i=0; i<navObjects.length; i++) {
    if (this.id == navObjects[i].id) {
       y.innerHTML = space + navObjects[i].info;
    }
  }
} // end function overbutton

function outbutton() {
  // on mouseouver, change text & background color back to normal
  this.className = 'navbuttonoff';
  // and change infobar contents to current link
  document.getElementById('navinfo').innerHTML = navCurrent.link;
} // end function outbutton

function makeCurrent() {
  // get current Id
  var theId = getId();

  // get acces to the current navigation button
  x = document.getElementById(theId);
  x.className = 'navbuttoncurrent';
  x.onmouseover = null;
  x.onmouseout = null;                 // switch off button functionality
  x.onclick = null;
  // set current to the correct navObject
  for (var i=0; i<navObjects.length; i++) {
    if (theId == navObjects[i].id) {
       navCurrent = navObjects[i];
    }
  }

  var file = getFile();
  if (file == 'index') {
    // change infobar contents to current link
    document.getElementById('navinfo').innerHTML = navCurrent.link;
    // change the title content to current id
    if (navCurrent.id == "Home") {
      document.getElementById('title').innerHTML = 'Welcome to the Astronomy Group website';
    } else {
     if (navCurrent.id == "People") {
      document.getElementById('title').innerHTML = "People";
      navCurrent.link = baselink2 + '<i><a href="index.php?dire=People&file=index">People</a></i>';
      document.getElementById('navinfo').innerHTML = navCurrent.link;
//      top.document.title += theId+' - '+file;
     } else {
      document.getElementById('title').innerHTML = navCurrent.id;
     }
    }
    // change the document title to current id
    top.document.title = title + theId;
  } else {
//    if (navCurrent.id !- 'People' && file != 'details') 
    if (file != 'details') 
      {
      navCurrent.link = baselink2 + '<b><a href="index.php?dire='+theId+'&file=index">'+theId+'</a>&nbsp;&gt;&nbsp;</b>' + '<i>'+leftTrim(file)+'</i>';
      document.getElementById('navinfo').innerHTML = navCurrent.link;
      document.getElementById('title').innerHTML = theId+' - '+file;
      top.document.title += theId+' - '+file;
    }
  }

} // end function makeCurrent

function leftTrim(sString) {
  while (sString.substring(0,1) == ' ') {
   sString = sString.substring(1, sString.length);
  }
  return sString;
}

