// @(#) functions.js - Neil's JavaScript functions, Version 1.00.02
// (C) Copyright 2007-2008 by Neil Channen. All rights reserved.
//
// Edition history:
// ================
//     Version     Date         Person           Reason
//     -------  ----------  ---------------  ---------------
//     1.00.00  2007-09-04  Neil R. Channen  Initial version
//     1.00.01  2007-09-07  Neil R. Channen  Support text in slideshow
//     1.00.02  2008-10-09  Neil R. Channen  Add map points, set their letters


//
// Google Maps interface
//
function configMap(lat, lng, id) {
    var zoom = 6;
    if (configMap.arguments.length > 3) {
	zoom = configMap.arguments[3];
    }
    var type = G_NORMAL_MAP;
    if (configMap.arguments.length > 4) {
	type = configMap.arguments[4];
    }
    var icon;
    if (configMap.arguments.length > 5) {
	var letter = configMap.arguments[5];
	if (letter != "") {
	    icon = new GIcon(G_DEFAULT_ICON);
	    icon.image = "http://www.google.com/mapfiles/marker"
		    + letter + ".png";
	}
    } else {
	icon = new GIcon(G_DEFAULT_ICON);
    }
    if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById(id));
	map.addControl(new GSmallMapControl());
	var place = new GLatLng(lat, lng);
	map.setCenter(place, zoom, type);
	if (icon != null) {
	    var markerOptions = { icon:icon };
	    map.addOverlay(new GMarker(place, markerOptions));
	}
    }
    return(map);
}
function addMapPoint(map, lat, lng) {
    var icon = new GIcon(G_DEFAULT_ICON);
    if (addMapPoint.arguments.length > 3) {
	var letter = addMapPoint.arguments[3];
	icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    }
    if (GBrowserIsCompatible()) {
	var place = new GLatLng(lat, lng);
	var markerOptions = { icon:icon };
	map.addOverlay(new GMarker(place, markerOptions));
    }
}

//
// Inline tabs
//
var tabList = new Array;
var contentList = new Array;
function initTabs(tabset) {
    for (i = 0; i < tabset.childNodes.length; i++) {
	if (tabset.childNodes[i].nodeName == "LEGEND") {
	    var legend = tabset.childNodes[i];
	    var tabNum = 0;
	    for (j = 0; j < legend.childNodes.length; j++) {
		if (legend.childNodes[j].nodeName == "#text") {
		    legend.removeChild(legend.childNodes[j]);
		    // look at the new one that's moved into its position
		    --j;
		} else {
		    tabList.push(legend.childNodes[j]);
		    eval("legend.childNodes[j].addEventListener(\"click\", function(){activateTab("+tabNum+")}, false)");
		    ++tabNum;
		}
	    }
	    // Dashes right of all entries except the last
	    for (j = 0; j < tabList.length; j++) {
		if (j < tabList.length-1) {
		    tabList[j].style.borderRight = "1px dashed rgb(120,172,255)";
		}
	    }
	} else if (tabset.childNodes[i].nodeName != "#text") {
	    contentList.push(tabset.childNodes[i]);
	}
    }
    activateTab(0);
}
function activateTab(num) {
    for (i = 0; i < tabList.length; i++) {
	if (i == num) {
	    tabList[i].className = "active";
	    contentList[i].style.display = "inline-block";
	} else {
	    tabList[i].className = "inactive";
	    contentList[i].style.display = "none";
	}
    }
}

//
// Simple slideshow
//
var imgObj;
var linkObj;
var textObj;
var pos;
var num;
var content;
function slideshow(img, link, text) {
    imgObj = img;
    linkObj = link;
    textObj = text;
    pos = 0;
    // slideshow.arguments isn't a real array; can't do slice()
    content = new Array();
    for (i = 3; i < slideshow.arguments.length; ++i) {
	content.push (slideshow.arguments[i]);
    }
    num = content.length / 3;
    flip ();
}
function flip () {
    imgObj.src = content[pos * 3];
    linkObj.href = content[pos * 3 + 1];
    textObj.innerHTML = content[pos * 3 + 2];
    ++pos;
    if (pos >= num) {
	pos = 0;
    }
    setTimeout (flip, 2000);
}
