/* 
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}


function addParaToList(u, text)
{
    l = createElement("li");
    u.appendChild(l);
    p = createElement("p");
    l.appendChild(p);
    t = document.createTextNode(text);
    p.appendChild(t);
}

function addIDTParaToList(u, text, idtext, title)
{
    l = createElement("li");
    u.appendChild(l);
    p = createElement("p");
    l.appendChild(p);
    l.id = idtext;
    t = document.createTextNode(text);
    p.appendChild(t);
    p.title = title;
}

function addIDParaToList(u, text, idtext)
{
    l = createElement("li");
    u.appendChild(l);
    p = createElement("p");
    l.appendChild(p);
    l.id = idtext;
    t = document.createTextNode(text);
    p.appendChild(t);
}

function addLinkToList(u, url, text)
{
    l = createElement("li");
    u.appendChild(l);
    a = createElement("a");
    l.appendChild(a);
    t = document.createTextNode(text); 
    a.href=url;
    a.appendChild(t);
}

function addIDLinkToList(u, url, text, idtext)
{
    l = createElement("li");
    u.appendChild(l);
    a = createElement("a");
    l.appendChild(a);
    l.id = idtext;
    t = document.createTextNode(text); 
    a.href=url;
    a.appendChild(t);
}


function addTitleLinkToList(u, url, text, title)
{
    l = createElement("li");
    u.appendChild(l);
    a = createElement("a");
    l.appendChild(a);
    t = document.createTextNode(text); 
    a.href=url;
    if(null != title)
	    a.title = title;
    a.appendChild(t);
}

function initNavbar(cr)
{
    if(!document.getElementById)
        return;

    if(document.getElementById('OP'))
         return;

    if(null == cr) cr = "../";

    // about page links
    u = createElement("ul");
    addLinkToList(u, cr+"about.html#a1-nge", "Intro");
    addLinkToList(u, cr+"about.html#a5-explanation", "FAQ"); //Fix this
    addLinkToList(u, cr+"about.html#a3-we", "Contributors");
    document.getElementById('about').appendChild(u);

    // supplemental page links
    u = createElement("ul");
    addLinkToList(u, cr+"supplemental.html#extended", "Expanded Commentary");
//TODO     addLinkToList(u, "template.html", 'References In and To "Evangelion"');
    addIDLinkToList(u, cr+"appendix/geektionary.html", "Geektionary", "geektionary");
//TODO     addLinkToList(u, "template.html", "Blooper Reel");
    addIDParaToList(u, "Revisions", "revisions");
    addLinkToList(u, cr+"supplemental-images.html", "Supplemental Images");
//TODO     addLinkToList(u, "template.html", "Links & References");
    addIDLinkToList(u, cr+"omake/omake.html", "Omake", "omake");
    addLinkToList(u, cr+"appendix/recyclopedia.html", "Recyclopedia");

    document.getElementById('suppl').appendChild(u);
    /* redirect to TOC page*/
    document.getElementById('suppl').firstChild.href=cr+"toc.html#supplement";

    // supplemental page sub-links
    u = createElement("ul");
    addLinkToList(u, cr+"appendix/revisions_oa-to-npc.html", "On-Air to NPC");
    addLinkToList(u, cr+"appendix/revisions_original-to-renewal.html", "Video to Renewal");
    document.getElementById('revisions').appendChild(u);

    u = createElement("ul");
    addLinkToList(u, cr+"omake/die-sterne/die-sterne.html", "Die Sterne");
    addLinkToList(u, cr+"omake/gos2_faq.txt", "Girlfriend of Steel 2 FAQ");
    addLinkToList(u, cr+"omake/omake.html#capdoc", "Capdoc Images");
    document.getElementById('omake').appendChild(u);

    u = createElement("ul");
    addLinkToList(u, cr+"appendix/geektionary_gender.html", "Gender");
    document.getElementById('geektionary').appendChild(u);


    // delete forum link
    u = document.getElementById('navmain').removeChild(document.getElementById('forum'));

    // add episodes
    u = createElement("ul");
    addIDParaToList(u, "OP", "OP");
    addLinkToList(u, cr+"ed/ed.html", "ED");
    addLinkToList(u, cr+"full-op/full-op.html", "Full OP");
    addLinkToList(u, cr+"toc.html#prelim", "Preliminaries");
    document.getElementById('commentary').appendChild(u);


    // add episodes
    addIDTParaToList(
	document.getElementById('navmain'),
	"Prologue Arc", "episodes1", "Episodes 1-7");
    u = createElement("ul");
    addIDParaToList(u, "01", "ep01");
    addIDParaToList(u, "02", "ep02");
    addIDParaToList(u, "03", "ep03");
    addIDParaToList(u, "04", "ep04");
    addIDParaToList(u, "05", "ep05");
    addIDParaToList(u, "06", "ep06");
    addIDParaToList(u, "07", "ep07");
    document.getElementById('episodes1').appendChild(u);

    addIDTParaToList(
	document.getElementById('navmain'),
	"Action Arc", "episodes2", "Episodes 8-13");

    u = createElement("ul");
    addIDParaToList(u, "08", "ep08");
    addIDParaToList(u, "09", "ep09");
    addIDParaToList(u, "10", "ep10");
    addIDParaToList(u, "11", "ep11");
    addIDParaToList(u, "12", "ep12");
    addIDParaToList(u, "13", "ep13");
    document.getElementById('episodes2').appendChild(u);


    addIDTParaToList(
	document.getElementById('navmain'),
	"Descent Arc", "episodes3", "Episodes 14-19");

    u = createElement("ul");
    addIDParaToList(u, "14", "ep14");
    addIDParaToList(u, "15", "ep15");
    addIDParaToList(u, "16", "ep16");
    addIDParaToList(u, "17", "ep17");
    addIDParaToList(u, "18", "ep18");
    addIDParaToList(u, "19", "ep19");
    document.getElementById('episodes3').appendChild(u);


    addIDTParaToList(
	document.getElementById('navmain'),
	"Bitter End", "episodes4", "Episodes 20-26, movies");
    u = createElement("ul");
    addIDParaToList(u, "20", "ep20");
    addIDParaToList(u, "21\u2032", "ep21");
    addIDParaToList(u, "22\u2032", "ep22");
    addIDParaToList(u, "23\u2032", "ep23");
    addIDParaToList(u, "24\u2032", "ep24");
    addIDParaToList(u, "25", "ep25");
    addIDParaToList(u, "26", "ep26");
    addIDParaToList(u, "Death(True)\u00B2", "death");
    addIDParaToList(u, "25\u2032", "air");
    addIDParaToList(u, "26\u2032", "sincerely");
    document.getElementById('episodes4').appendChild(u);

    // add scenes

    // OP
    u = createElement("ul");

    addLinkToList(u, cr+"op/op_1.html", "A");
    addLinkToList(u, cr+"op/op_2.html", "B");

    document.getElementById('OP').appendChild(u);


    // ep#1
    u = createElement("ul");

    addParaToList(u, "=Part A=");
    addLinkToList(u, cr+"episode-01/episode-01A-scene1.html", "Scene 1");
    addLinkToList(u, cr+"episode-01/episode-01A-scene2.html", "Scene 2");
    addLinkToList(u, cr+"episode-01/episode-01A-scene3.html", "Scene 3");
    addLinkToList(u, cr+"episode-01/episode-01A-scene4.html", "Scene 4");
    addParaToList(u, "=Part B=");
    addLinkToList(u, cr+"episode-01/episode-01B-scene5.html", "Scene 5");
    addLinkToList(u, cr+"episode-01/episode-01B-scene6.html", "Scene 6");
    addLinkToList(u, cr+"episode-01/episode-01B-scene7.html", "Scene 7");
    addLinkToList(u, cr+"episode-01/episode-01B-scene8.html", "Scene 8");
    addLinkToList(u, cr+"episode-01/episode-01_credits.html", "Ending");
    addLinkToList(u, cr+"episode-01/episode-01_jikai-yokoku.html", "Preview");

    document.getElementById('ep01').appendChild(u);


    // ep#2
    u = createElement("ul");

    addParaToList(u, "=Part A=");
    addLinkToList(u, cr+"episode-02/episode-02A-scene1.html", "Scene 1");
    addLinkToList(u, cr+"episode-02/episode-02A-scene2.html", "Scene 2");
    addLinkToList(u, cr+"episode-02/episode-02A-scene3.html", "Scene 3");
    addLinkToList(u, cr+"episode-02/episode-02A-scene4.html", "Scene 4");
    addParaToList(u, "=Part B=");
    addLinkToList(u, cr+"episode-02/episode-02B-scene5.html", "Scene 5");
    addLinkToList(u, cr+"episode-02/episode-02B-scene6.html", "Scene 6");
    addLinkToList(u, cr+"episode-02/episode-02B-scene7A.html", "Scene 7A");
    addLinkToList(u, cr+"episode-02/episode-02B-scene7B.html", "Scene 7B");
    addLinkToList(u, cr+"episode-02/episode-02B-scene8.html", "Scene 8");
    addLinkToList(u, cr+"episode-02/episode-02_credits.html", "Ending");
    addLinkToList(u, cr+"episode-02/episode-02_jikai-yokoku.html", "Preview");

    document.getElementById('ep02').appendChild(u);

    // ep#3
    u = createElement("ul");

    addParaToList(u, "=Part A=");
    addLinkToList(u, cr+"episode-03/episode-03A-scene1.html", "Scene 1");
    addLinkToList(u, cr+"episode-03/episode-03A-scene2.html", "Scene 2");
    addLinkToList(u, cr+"episode-03/episode-03A-scene3.html", "Scene 3");
    addLinkToList(u, cr+"episode-03/episode-03A-scene4.html", "Scene 4");
    addParaToList(u, "=Part B=");
    addLinkToList(u, cr+"episode-03/episode-03B-scene5.html", "Scene 5");
    addLinkToList(u, cr+"episode-03/episode-03B-scene6.html", "Scene 6");
    addLinkToList(u, cr+"episode-03/episode-03B-scene7A.html", "Scene 7A");
    addLinkToList(u, cr+"episode-03/episode-03B-scene7B.html", "Scene 7B");
    addLinkToList(u, cr+"episode-03/episode-03B-scene8.html", "Scene 8");
    addLinkToList(u, cr+"episode-03/episode-03B-scene9.html", "Scene 9");
    addLinkToList(u, cr+"episode-03/episode-03_credits.html", "Ending");
    addLinkToList(u, cr+"episode-03/episode-03_jikai-yokoku.html", "Preview");

    document.getElementById('ep03').appendChild(u);

    // ep#4
    u = createElement("ul");

    addParaToList(u, "=Part A=");
    addLinkToList(u, cr+"episode-04/episode-04A-scene1.html", "Scene 1");
    addLinkToList(u, cr+"episode-04/episode-04A-scene2A.html", "Scene 2A");
    addLinkToList(u, cr+"episode-04/episode-04A-scene2B.html", "Scene 2B");
    addLinkToList(u, cr+"episode-04/episode-04A-scene3.html", "Scene 3");
    addLinkToList(u, cr+"episode-04/episode-04A-scene4.html", "Scene 4");
    addParaToList(u, "=Part B=");
    addLinkToList(u, cr+"episode-04/episode-04B-scene5.html", "Scene 5");
    addLinkToList(u, cr+"episode-04/episode-04B-scene6.html", "Scene 6");
    addLinkToList(u, cr+"episode-04/episode-04B-scene7.html", "Scene 7");
    addLinkToList(u, cr+"episode-04/episode-04B-scene8.html", "Scene 8");
    addLinkToList(u, cr+"episode-04/episode-04_credits.html", "Ending");
    addLinkToList(u, cr+"episode-04/episode-04_jikai-yokoku.html", "Preview");

    document.getElementById('ep04').appendChild(u);

// More goes here

    // ep#12
    u = createElement("ul");
    addLinkToList(u, cr+"episode-12/episode-12A-scene1.html", "Scene 1");
    document.getElementById('ep12').appendChild(u);


// More goes here

    // ep#14
    u = createElement("ul");
    addLinkToList(u, cr+"episode-14/episode-14B-scene7.html", "Scene 7");
    document.getElementById('ep14').appendChild(u);

// More goes here

    // ep#19
    u = createElement("ul");
    addLinkToList(u, cr+"episode-19/episode-19B-scene12A.html", "Scene 12A");
    document.getElementById('ep19').appendChild(u);

// More goes here

    // ep#21
    u = createElement("ul");
    addLinkToList(u, cr+"episode-21/episode-21A-scene1.html", "Scene 1");
    addLinkToList(u, cr+"episode-21/episode-21A-scene4.html", "Scene 4");
    addLinkToList(u, cr+"episode-21/episode-21B-scene6.html", "Scene 6");
    document.getElementById('ep21').appendChild(u);

// More goes here

    // ep#23
    u = createElement("ul");
    addLinkToList(u, cr+"episode-23/episode-23A-scene4.html", "Scene 4");
    addLinkToList(u, cr+"episode-23/episode-23B-scene6.html", "Scene 6");
    addLinkToList(u, cr+"episode-23/episode-23B-scene7.html", "Scene 7");
    addLinkToList(u, cr+"episode-23/23_reiquarium.html", "Reiquarium");
    document.getElementById('ep23').appendChild(u);


// More goes here

    // ep#25
    u = createElement("ul");
    addLinkToList(u, cr+"episode-25/episode-25B-scene5.html", "Scene 5");
    document.getElementById('ep25').appendChild(u);

// More goes here

    // ep#26'
    u = createElement("ul");
    addLinkToList(u, cr+"episode-m26/episode-m26-scene08B.html", "Scene 8B");
    addLinkToList(u, cr+"episode-m26/episode-m26-scene11.html", "Scene 11");
    addLinkToList(u, cr+"episode-m26/episode-m26-scene12.html", "Scene 12");
    document.getElementById('sincerely').appendChild(u);
//------------------------------------------

    // forum suppression part 2
    addIDTParaToList(
	document.getElementById('navmain'),
	"\u00A0", "", "");


    // footer stuff
    year = new Date().getFullYear();


    f = document.getElementById('currentyear');
    if(f)
    {
	t = document.createTextNode("####"+year);
	while(f.firstChild) {f.removeChild(f.firstChild);}
	f.appendChild(t);
    }

    f = document.getElementById('footer');
    if(f)
    {
	while(f.firstChild) {f.removeChild(f.firstChild);}

	p = createElement("p");
	t = document.createTextNode("Original content copyright \u00A9 Rachel K. Clark/The NGE Fan-Geeks Commentary Project, 2004 ~ "+year);
	p.appendChild(t);

	p.appendChild(createElement("br"));
	t = document.createTextNode("Website design \u00A9 Steve \u201CMr. Tines\u201D Gilham.");
	p.appendChild(t);
	p.appendChild(createElement("br"));
	t = document.createTextNode("Neon Genesis Evangelion (and all sub-properties thereof) \u00A9 GAINAX/Project Eva.");
	p.appendChild(t);
	p.appendChild(createElement("br"));
	t = document.createTextNode("All other non-original content are copyrighted by their respective owners.");
	p.appendChild(t);
	p.appendChild(createElement("br"));

	p.style.fontStyle = "italic";
	p.style.fontSize = "80%";
	p.style.textAlign = "center"

    
	f.appendChild(p);

    }

    for (link=0; link<document.links.length; ++link)
    {
	if(document.links[link].target != null && document.links[link].firstChild.src != null
         && null == document.links[link].onclick)
      {
             document.links[link].onclick = doPopup;
	}
    }

}

function doPopup()
{
        // doing better than this will require pre-fetching the image
        // this works 80%+ of the time
        h = "3px";
        w = "4px";
        if(this.firstChild.currentStyle)
        { //ie
            h = this.firstChild.currentStyle["height"];
            w = this.firstChild.currentStyle["width"];
        } else if (window.getComputedStyle) {
        	h = window.getComputedStyle(this.firstChild, null).height;
        	w = window.getComputedStyle(this.firstChild, null).width;
        }
        
        h = h.slice(0,-2);
        w = w.slice(0,-2);
        width = (w*480)/h;
        return pop(width, 480, this.href, this.firstChild.currentStyle); 
}

function popOnClick(w,h, url)
{
        return pop(w, h, url, document.firstChild.currentStyle); 
}

function pop(w, h, url, flag)
{
  dw = 20;
  dh = 20;
  if(flag) //ie
  { 
     dw = 60; // = const + scrollwidth?
     dh = 30;
  }
  width = w+dw;
  height = h+dh;
  var openoptions = 'width='+width+',height='+height+',location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes, status=yes';
  window.open(url, "_blank",openoptions );
  return false;
}

