// Insert 'navbar' 'head' or 'foot' into html document

// Import data as script (Write is more reliable for IE than DOM method)
document.write('<script type="text/javascript" src="/images/navbarData.js"></script>');

function insert(x,n) {
// to be called within the document
if(/n/.test(x)) insert_navbar(n||0); //navbar has fixed position
if(/h/.test(x)) insert_head();  // insert head and foot in this place
if(/f/.test(x)) insert_foot();
}

function include(x,n) {
// to be called after document is loaded
if(/h/.test(x)) include_head();
if(/n/.test(x)) insert_navbar(n||0);
if(/f/.test(x)) include_foot();
if(/m/.test(x)) decode_all();
}

gNavClasses = ['title','link','sub-menu'];

function insert_navbar(n1) {
  var n = (n1)?1:0;
  var navbar = document.createElement("div");
  navbar.id = "navbar";
  insertNav(navData[n], navbar);
  document.body.insertBefore(navbar,document.body.firstChild);
// Allow scrolling over navbar if small window height.
  var hnav = navbar.offsetHeight;
  var hwin = getWindowHeight();
  if (hwin < hnav) navbar.style.position = "absolute";
}

function isArray() {
  if (typeof arguments[0] == 'object')
    return /array/i.test(arguments[0].constructor.toString());
  return false;
}

function isString() {
  if (typeof arguments[0] == 'string') return true;
  if(typeof arguments[0] == 'object') 
    return /string/i.test(arguments[0].constructor.toString());
  return false;
}

function insertNav(navD,navElt) {  
  var newul = document.createElement("ul");
  var newli = '';
  for(var i=0; i<navD.length; i++) {
      if(isArray(navD[i])) {
      insertNav(navD[i],newli||newul);  // call recursively for nested arrays
      continue;
    }
    if(!isString(navD[i])) continue;
    newli = document.createElement("li");
    var s = new String(navD[i]); 
    var t = s.split('|');
    var newLink = 0;
    if(t[1]) {
      var newLink = 1;
      var obj = document.createElement("a");
      obj.href = t[1];
      obj.target = "_top";
      obj.innerHTML = t[0];
      obj.style.color = "black";
      newli.appendChild(obj);
    } else if(t[0]) {
      var newLink = 0;
      var obj = document.createElement("span");
      obj.innerHTML = t[0];
      newli.appendChild(obj);
    } else {
      var newLink = 2;
      obj = newul;
    }
    while(t[2]) { 
      var u = t[2].split(':');
      if(u[1]) obj.style[u[0]] = u[1];		// 'a|b|c:d' style[c] = d;
      else obj.className += ' ' + t[2];		// 'a|b|c' class = c;
      t.shift();				// can repeat eg 'a|b|c:d|e:f'
    } 
    if(newLink==2) continue;
    newli.mousein = 0;
    if((i<navD.length-1)&&((typeof navD[i+1])!="string")) newLink = 2;
    newli.className += ' ' + gNavClasses[newLink];
    if(newLink>0) {
      newli.onmouseover=function() { // Count mouseovers
        if(this.mousein++ == 0) this.className += " sfhover";
      }
      newli.onmouseout=function() {
        var ths=this;
        setTimeout(function() {// introduce 150ms delay
          if(--ths.mousein == 0) ths.className = ths.className.slice(0,-8);
        }, 150);
      }
    }
    newul.appendChild(newli);
  }
  return navElt.appendChild(newul);
}

function getWindowHeight() {
  var windowHeight=0;
  if (typeof(window.innerHeight)=='number') {
    windowHeight=window.innerHeight;
  }
  else if (document.documentElement&&
    document.documentElement.clientHeight) {
    windowHeight=
    document.documentElement.clientHeight;
  }
  else if(document.body&&document.body.clientHeight) {
    windowHeight=document.body.clientHeight;
  }
  else {
    windowHeight=0;
  }
  return windowHeight;
}

function insert_head() {
  document.write('<div id="top">' + headData + '</div>');
}

function include_head() {
  var newdiv = document.createElement("div");
  newdiv.id = "top";
  newdiv.innerHTML = headData;
  document.body.insertBefore(newdiv,document.body.firstChild);
}

function insert_foot() {
  document.write('<div id="foot">' + footData + '</div>');
}

function include_foot() {
  if(document.getElementById("foot")) {
    document.getElementById("foot").innerHTML = footData;
    return;
  }
  var newdiv = document.createElement("div");
  newdiv.id = "foot";
  newdiv.innerHTML = footData;
  document.body.appendChild(newdiv);
}

// hidden mailto
function sendto() {
  var args = ['webmaster','devonhumanists','org','uk'];
  for (var i=0; i<arguments.length; i++) if(arguments[i]) args[i] = arguments[i];
  var j = (/^\w/.test(args[3]))?4:3; 
  var s = args.slice(1,j).join(".");
  window.location = "mailto:" + String(args[0]) + "@" + s;
}
// hidden print email address
// to omit country, specify as non-word character eg "-"
function sendtoa() {
  var args = ['webmaster','devonhumanists','org','uk'];
  for (var i=0; i<arguments.length; i++) if(arguments[i]) args[i] = arguments[i];
  var j = (/^\w/.test(args[3]))?4:3; 
  var s = args.slice(1,j).join(".");
  document.write(args[0] + "@" + s);
}
// Decode email addresses. Called onload.
// Data from name of "a" tag starting with "!"
function decode(s) {
  var ns = s.length - 2;
  var key0 = new Array(ns);
  var key = new Array();
  for(var i=0; i<ns; i++) key0[i] = i;
  var c = s.charCodeAt(1);
  var k = 2;
  for(var n=ns; n>0; n--) {
    var j = c%n;
    key.push(key0[j]);
    key0.splice(j,1);
    c = s.charCodeAt(k++);
  }
  var t = new Array(ns);
  for(var i=0; i<ns; i++) t[key[i]] = s.charAt(i+2);
  n1 = String(t.shift()).charCodeAt(0)-97;
  t.splice(n1,0,"@");
  return t.join("");     
} 
function decode_all(){
  var as = document.getElementById("content").getElementsByTagName("a");
  for (var i=0; i<as.length; i++) {
    var s = as[i].name;
    if(!/^!/.test(s)) continue;
    var t = decode(s);
    as[i].href = "mailto:" + t;
    if(!as[i].innerHTML) as[i].innerHTML = t;
  }
} 
function sendTo(s) {
  window.location = 'mailto:' + decode(s);
}
// -----------------------DEFAULT NAVBAR DATA ---------------------------


headData = '<img class="right" src="/images/small_logo.gif">' +
'<h1>Devon Humanists</h1>';

footData = '<a href="#top"><img style="float: left" src="/images/uparrow.gif"></a>' +
'<a href="#top"><img style="float: right" src="/images/uparrow.gif"></a>'+
'<b>Humanism</b> is the belief that we can live good lives without ' +
'religious or superstitious beliefs.<br />' +
'<b>Humanists</b> make sense of life using reason, experience and shared human values.';

/*
The menu consists of an unordered list (ul) whose items (li) may include further uls.
navData is an array of strings and (optionally) arrays like navData.
Each string is of the form 'a|b|c|...'
a is a menu label for the li
b is the URL of a link included in li
c (optional) is the class li.className = c
If c is of the form p:q, c defines a style as li.style[c] = d.
c may be repeated.
If the string is of the form '||c' (ie a and b both empty) c is applied to the ul.
*/

navData = new Array(2);

navData[0] = [		//Regular navbar
    '<b>Click here to update this menu</b>|javascript:window.location.reload()|color:red|font-variant:small-caps',
    'Recently changed||color:red|',
    [
      'On-line Forum|/zisp/discussion?id=01',
      'Events|/events.html',
      'News|/news/media.html',
      'BHA news|http://www.humanism.org.uk/site/cms/newscategoryview.asp?Chapter=22&Type=2009',
      'NSS news|http://www.secularism.org.uk/newsline.html',
      'Review|/book_reviews/physics_of_christianity.html'
     ],
    'Home page|/index.html',
    'Humanism &amp; Devon Humanists',
    [
      'More about Humanism|/more_about.html',
      'What is Humanism, Secularism, &#133; ?|/what_is_humanism.html',
      'The Devon Humanists|/devon_humanists.html',
      'Humanist Forum|/humanist_forum.html',
      'Activities|/activities.html',
      'Past events|/past_events.html',
      'Reading list|/reading_list.html'
    ],
    'For Schools|/schools/index.html',
    'Members only|/members_only/index.html',
    'Contact us|/zisp/mailer',
    'News',
    [
      'In the media|/news/media.html',
      'Other items',
      [
        'Devonhumanists in Totnes|/news/totnes_dixon.html',
        'Exeter Council prayers|/news/exeter_council_prayers.html',
        'Grayling in Devon|/news/grayling_at_dartington.html',
        'BHA Local Volunteers|/news/local_rep.html',
        'Naming ceremony in a Devon school|/news/naming_in_school.html',
        'Support secular government|/news/secular_government.html',
        'Prayers scrapped at Totnes Council|/news/totnes_council_prayers.html',
        'Wedding on Slapton beach|/news/slapton_wedding.html'
      ],
      '&nbsp; &#133; more',
      [ '||top:-5em',
        'In the media|/more_news/more_media.html',
        'Religion &amp; government|/more_news/religion_and_government.html',
        'Faith Schools|/more_news/faith_schools.html',
        'International Humanist|/more_news/international_humanist.html',
        'New Age drivel|/more_news/new_age_drivel.html',
        'RE in Schools|/more_news/re_in_schools.html',
        'NHS prison visitors|/more_news/prison_visitors.html',
        'Naming Ceremonies|/more_news/naming_ceremomnies.html'
      ],
      '&nbsp; &#133; more',
      [ '||top:-10em',
        'Atheism &amp; RE|/more_news/atheism_and_re.html',
        'Torbay Crematorium|/more_news/torbay_crematorium.html',
        'Chemistry v Creationism|/more_news/chemistry_v_creationism.html',
        'Faith schools &amp; Ofsted|/more_news/faith_schools_and_ofsted.html',
        'Secular fundamentalists|/more_news/secular_fundamentalists.html',
        'National RE syllabus|/more_news/national_re_syllabus.html',
        'Humanism at Dartingto|/more_news/humanism_at_dartington.html',
        'Tsunami whose fault?|/more_news/tsunami_whose_fault.html'
      ]
    ],
    'Events|/events.html',
    'On-line Forum|/zisp/discussion?id=01',
    'Links|/links.html',
    'Interviews, TV, radio',
    [
      'Dawkins talks with McGrath|http://video.google.com/videoplay?docid=6474278760369344626',
      'Dawkins  on <i>The Hour</i>|http://cosmicafterthoughts.blogspot.com/2007/05/richard-dawkins-on-hour.html',
      'Grayling <i>Beyond Belief</i> BBC 16jul|http://www.bbc.co.uk/religion/programmes/beyond_belief/'
    ],
    'Quotes|/quotes.html',
    'Book Reviews',
    [ '||up',
      'Grass Roots Humanism|/book_reviews/grass_roots_humanism.html',
      'Physics of Christianity|/book_reviews/physics_of_christianity.html',
      'The God Delusion|/book_reviews/god_delusion.html',
      'Letter to a Christian Nation|/book_reviews/christian_nation.html',
      'Breaking the Spell|/book_reviews/breaking_the_spell.html',
      'History of Disbelief|/book_reviews/history_of_disbelief.html',
      '&nbsp; &#133; more',
      [ '||up',
        'Who do we vote for?|/book_reviews/who_do_we_vote_for.html',
        'Gospel Truths|/book_reviews/gospel_truths.html',
        'End of Faith|/book_reviews/end_of_faith.html',
        'Twilight of Atheism|/book_reviews/twilight_of_atheism.html',
        'Philosopher at End of Universe|/book_reviews/philosopher_end_universe.html',
        'The Da Vinci Code|/book_reviews/da_vinci_code.html',
        'Our Final Century|/book_reviews/our_final_century.html',
        'Straw Dogs|/book_reviews/straw_dogs.html',
        'Guide to Atheism|/book_reviews/guide_to_atheism.html'
      ]
    ],
    'Media activities',
    [ '||up',
      'Press releases',
      [
        'Express and Echo 20/06/07|/articles/express_echo_20_06_07.html'
      ],
      'Radio broadcasts',
      [ '||up',
        'Radio Devon June 06|/articles/pft_roger_jun.html',
        'Radio Devon August 06|/articles/pft_roger_aug_rej.html',
        'Radio Devon November 06|/articles/pft_roger_nov.html',
        'Radio 4 March 07|/articles/scouting_hazel.html'
      ],
      'Letters',
      [ '||up',
        'Secular Government|/articles/letter_gordon_secular_government.html',
        'Voluntary assemblies 07/06/06|/articles/letter_roger_070606.html',
        'Faith Schools-1|/articles/letter_gordon_faith_schools1.html',
        'Faith Schools-2|/articles/letter_gordon_faith_schools2.html'
      ]
    ],
    'Page top|#top'
  ];

navData[1] = [		//Schools navbar
    'Devon Humanists Home page|/index.html',
    'Schools Home page|/schools/index.html',
    'About Humanism',
    [
      'I&nbsp;&nbsp;&nbsp;Philosophical<br /> &nbsp;&nbsp;&nbsp;&nbsp;background|/schools/philosophical.html',
      'II&nbsp;&nbsp;Morality|/schools/morality.html'
    ],
    'Letter to Devon Schools|/schools/april07schoolsletter.html',
    'Contact us (Schools)|/zisp/mailer?ID=01',
    'Non-statutory framework for RE|/schools/framework.html',
    'Page top|#top'
  ];


