// $Date: 2004/04/19 03:30:39 $
// $Revision: 1.6 $

var link_strs   = new Array(); // onclick attribute.
var link_titles = new Array(); // visible link title.
var link_groups = new Array(); // group name => index
var group_list  = new Array(); // list of link group objs.
var iframe_hist = new Array(); // document history.
var dl_links    = new Array(); // download links.

var content_divs   = new Array(); // divs with class="content"
var the_iframe_div = null;
var frame_controls = null;

var first_div = ""; // first item to show.

// text of the iframe for url links.
var iframe_text = "<iframe name=\"CONTENT\" src=\"@URL@\" id=\"CONTENT\" width=\"100%\" height=\"400\"></iframe>";

// frame height manipuation parameters.
var iframe_dh = 40;
var iframe_ht  = 400;

// images for the one-sided surface section.
var image_list = new Array("img/moebius_grid_00000.jpg",
			   "img/moebius_grid_00001.jpg",
			   "img/moebius_pens_00000.jpg",
			   "img/moebius_pens_00001.jpg",
			   "img/moebius_solid_00000.jpg",
			   "img/moebius_solid_00001.jpg",
			   "img/moebius_solid_00002.jpg",
			   "img/klein_00000.jpg",
			   "img/klein_00001.jpg",
			   "img/klein_00002.jpg",
			   "img/klein_00003.jpg",
			   "img/hanoi_00000.jpg",
			   "img/hanoi_00001.jpg",
			   "img/hanoi_00002.jpg"
			   );

var img_idx = 0;

function prev_img() { goto_image(-1); }
function next_img() { goto_image(1); }

function goto_image(d) {
    img_idx += d;
    img_idx = (img_idx + image_list.length) % image_list.length;
    var i = document.getElementById("snapshot");
    i.src = image_list[img_idx];

    i = document.getElementById("snaplink");
    i.href = image_list[img_idx];
    i.innerHTML = image_list[img_idx];
}
//

// frame manipuation: begin
function change_height(dh) {
    var f = document.getElementById("CONTENT");
    if (!f) return;
    var h = parseInt(f.height, 10);
    h += dh;
    f.height = "" + h;
}

function bigger() { change_height(iframe_dh); }
function smaller() { change_height(-iframe_dh); }
function reset_size() {
    var f = document.getElementById("CONTENT");
    if (!f) return;
    f.height = "" + iframe_ht;
}
// frame manipuation: end.


// download links:
// - write the link, and push it onto the list of links.
function make_dl_link(fname) {
    var s = "<a href=\""+ fname + "\">" + fname + "</a>\n";
    document.write(s);
    dl_links.push(s);
}

// document history navigation.
function go_back() {
    if (iframe_hist.length <= 1) {
	load_content(first_div, false);
    }
    else {
	var i = iframe_hist.length -2;
	var last = iframe_hist[i];
	iframe_hist.pop();

	var t = last.substr(0,2);
	var u = last.substr(2);
	if (t == "c:") load_content(u, false);
	else load_url(u, false);
    }
}

// create a link group.
function create_group(title) {
    var g = new Array();
    g.title = title;
    return g;
}

// add a link group; replaces the title if the group is already in the
// list.
function add_link_group(gid, title) {
    var idx = link_groups[gid];
    if (idx != null) {
	group_list[idx].title = title;
	return;
    }
    group_list.push(create_group(title));
    link_groups[gid] = group_list.length - 1;
}

// content link: adds a div (class=content) to the links.
// checks if the div really exists; if so, adds it.
function add_content_link(div_id) {
    var div = document.getElementById(div_id);
    if (!div) return;
    add_div_link(div);
}


// actually does the addition:
// parse the div id for the grouplist.
// The div id is of the form: <groups>:<name>
// where groups is a list of dot-separated identifiers.
function add_div_link(div) {
    var t = div.title;
    var s = "onclick=\"load_content('" + div.id + "');\"";
    link_strs.push(s);
    link_titles.push(t);

    var i = div.id.indexOf(':');
    var gidlist = div.id.substr(0,i).split('.');
    for (i = 0; i < gidlist.length; i++)
	add_link_to_group(link_titles.length-1, gidlist[i]);
}

// adds a link to a group.
function add_link_to_group(idx, gid) {
    if (gid == null) gid = "Links";
    var g = link_groups[gid];
    if (g == null) {
	add_link_group(gid, "Link Group");
	g = link_groups[gid];
    }
    group_list[g].push(link_titles.length-1);
}

// adds a URL link to be opened in an iframe.
function add_url_link(url, title, group) {
    link_strs.push("onclick=\"load_url('"+ url + "')\"");
    link_titles.push(title);
    add_link_to_group(link_titles.length-1, group);
}

// load a "content" div by copying its contents to the
// element "content_cell".
function load_content(div_id, save) {
    if (save == null) save = true;
    var div = content_divs[div_id];

    if (div == null) return;
    if (save) iframe_hist.push("c:" + div_id);

    var d;
    for (d in content_divs) content_divs[d].style.display = "none";
    the_iframe_div.style.display = "none";
    if (frame_controls) frame_controls.style.display = "none";
    content_divs[div_id].style.display = "";
    //alert("div_id: '" + div_id + "'; display: '" + content_divs[div_id].style.display + "'");
}

// loads a URL by changing "content_cell" to an iframe with the url as
// its src attribute.
function load_url(url, save) {
    if (the_iframe_div == null) return;
    if (save == null) save = true;
    if (save) iframe_hist.push("u:" + url);

    var d;
    for (d in content_divs) content_divs[d].style.display = "none";
    var s = iframe_text;
    s = s.replace('@URL@', url);
    the_iframe_div.innerHTML = s;
    the_iframe_div.style.display = "";
    if (frame_controls) frame_controls.style.display = "";
}

// scans the document for divs with class="content"
function scan_doc() {
    the_iframe_div = document.getElementById("the_iframe");
    frame_controls = document.getElementById("frame_controls");
    if (frame_controls) frame_controls.style.display = "none";
    var divs = document.getElementsByTagName("div");
    if (!divs) return;
    var i;
    for (i = 0; i < divs.length; i++) {
	if (divs[i].className.match(/\bcontent\b/)) {
	    add_div_link(divs[i]);
	    content_divs[divs[i].id] = divs[i];
	}
    }
}

// create the sidebar:
//  -- link groups.
//  -- download links.
function make_sidebar() {
    scan_doc();
    var sb = document.getElementById("SIDEBAR");
    if (!sb) return;
    var i;
    var s = "";

    var j;
    for (j = 0; j < group_list.length; j++) {
	var t = group_list[j].title;
	s += "<h4>" + t + "</h4>\n";
	for (i = 0; i < group_list[j].length; i++) {
	    var str = link_strs[group_list[j][i]];
	    var title = link_titles[group_list[j][i]];
	    s += "<img src=\"bullet.png\" border=\"0\"> "
		+ "<a href=\"#\" " + str + ">" + title + "</a><br />\n";
	}
    }
    s += "<h4>Downloads</h4>\n";
    dl_links.sort();
    var last = "";
    for (j = 0; j < dl_links.length; j++) {
	if (dl_links[j] == last) continue;
	last = dl_links[j];
	s += "<img src=\"bullet.png\" border=\"0\"> " + dl_links[j]
	    + "<br />";
    }
    s += "<br />\n";
    sb.innerHTML = s;
    load_content(first_div);
}

// print a debug message into the div "DEBUG".
function debug_message(s) {
    var div = document.getElementById("DEBUG");
    div.innerHTML += "<br />" + s;
}
