/* Shared constants for print_topmatter. Pretty much the only
   one I need is for prose. */

var ProsePanels = [
  ["newsletter", "498,55,571,79", "70", "Newsletter", "Newsletter"],
  ["tirades", "498,79,552,104", "270", "Tirades", "Tirades"],
  ["blog", "498,104,539,123", "440", "Blog", "Blog"]];

var MusicPanels = [
  ["featured_mp3s", "498,19,557,54", "50", "FeaturedMP3s", "Featured MP3s"],
  ["cds", "498,54,544,73", "310", "CDs", "CDs"],
  ["songs", "498,73,544,95", "430", "Songs", "Songs"]
];

/* So this is printing a couple of nested divs. The position
   of the titleimage is governed by all.css. */

function print_topmatter(map_prefix, subareas, remote_prefix, add_openmike)
{
  var core_links = [
    ["/index.html", "0,0,360,184"],
    ["/listen.html", "390,12,488,44"],
    ["/hear_me.html", "390,44,488,75"],
    ["/read_me.html", "390,75,488,107"],
    ["/book_me.html", "390,107,488,140"],
    ["/etc.html", "390,140,488,168"]];
  document.write('<div id="topmatter">\n');
  document.write('<img src="/images/' + map_prefix + 'ImageMap.jpg" border="0"\n' +
'style="position: absolute; top:0; left: 0" alt="[Sam Bayer]" usemap="#navmap">\n');
  document.write('<map name="navmap">\n');
  document.write('<!-- left x, upper y, right x, lower y, 0,0 upper left corner -->\n');
  for (var i = 0; i < core_links.length; i++) {
    document.write('<area href="' + core_links[i][0] + '" shape="rect" coords="' + core_links[i][1] + '">\n');
  }
  if (add_openmike) {
    document.write('<area href="/openmikes.html" shape="circle" coords="525,141,33">\n');
  }
  if (subareas) {
    for (var i = 0; i < subareas.length; i++) {
      if (remote_prefix == null) {
	document.write('<area href="?panel=' + subareas[i][0] + '" onclick = "return show_panel(\''+ subareas[i][0] + '\');" shape="rect" coords="' + subareas[i][1] + '">\n');
      } else {
	document.write('<area href="' + remote_prefix + '?panel=' + subareas[i][0] + '" shape="rect" coords="' + subareas[i][1] + '">\n');
      }
    }
  }
  document.write('</map>\n</div> <!-- topmatter -->\n');
}

function print_navbar(url_prefix, subarea_info) {
  document.write('<div id = "navbar" class="navigationBar">\n');
  var isActive = ' class = "active"';
  for (var i = 0; i < subarea_info.length; i++) {
    var info = subarea_info[i];
    document.write('<a href = "' + url_prefix + '?panel=' + info[0] + '" style = "left: ' + info[2] + 'px" id = "' + info[0] + 'Img"' + isActive + ' onclick = "return show_panel(\'' + info[0] + '\');">\n');
    isActive = "";
    document.write('<img border = 0 src = "/images/Header' + info[3] + '.gif" alt = "[' + info[4] + ']">\n</a>\n');
  }
  document.write('</div> <!-- navbar -->\n');
}

// Borrowed getChildElementsByClassName from Rob Allen.

/* 
Dynamic Tabs 1.0.1
Copyright (c) 2005 Rob Allen (rob at akrabat dot com)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

*/

function getChildElementsByClassName(parentElement, className)
{
	var i, childElements, pattern, result;
	result = new Array();
	pattern = new RegExp("\\b"+className+"\\b");


	childElements = parentElement.getElementsByTagName('*');
	for(i = 0; i < childElements.length; i++)
	{
		if(childElements[i].className.search(pattern) != -1)
		{
			result[result.length] = childElements[i];
		}
	}
	return result;
}


// Function to switch among panes, if available.
// Panels are all of class "panel", and images are
// descendants of id = "navbar". The img will have
// label+"Img", the panel will have label+"Panel".
// The idea will be that we add the "active" token
// to the class entries, and remove it from all
// the others.

var navbar = null;
var img_list = null;

// Be aware the show_panel may be called with 
// extra arguments.

function show_panel(panel_name)
{
  // Step 1. Find the navbar. Do all these things
  // only once.
  if (navbar == null) {
    navbar = document.getElementById("navbar");
  }
  // Step 2. Find the imgs. We're using the anchor
  // tag instead of the img tag because of how hover
  // needs to work for IE. Grrr.
  if (img_list == null) {
    img_list = navbar.getElementsByTagName("a");
  }

  // Now, we have everything. Mark them all as 
  // unhandled, and then mark the handled one. If
  // I can't find one that matches the panel_name, then
  // mark the first.

  show_panel_internal(panel_name, img_list);
  // For use in onclick methods.
  return false;
}

function show_panel_internal(panel_name, img_list)
{

  var i;

  var img_target = panel_name + "Img";
  
  for (i = 0; i < img_list.length; i++) {
    var img = img_list[i];
    // Remove Img, add Panel.
    var panelId = img.id.substring(0, img.id.length - 3) + "Panel";
    var panel = document.getElementById(panelId);
    img.className = null;
    panel.className = "panel";
    if (img.id == img_target) {
      img.className = "active";
      panel.className = "panel active";
    }
  }
}

// For when someone references the page with the
// # marker. This needs to go at the end of each page,
// or in onload. UPDATE: now uses the query string. 

// query parsing from lots of places, but 
// found at http://www.idealog.us/2006/06/javascript_to_p.html

function getQueryVariables()
{
  var windowLoc = window.location;
  if (windowLoc.search.length > 0) {
    var obj = {}
    var query = windowLoc.search.substring(1);
    var vars = query.split("&"); 
    for (var i=0;i<vars.length;i++) { 
      var pair = vars[i].split("=");
      obj[pair[0]] = pair[1];
    }
    return obj;
  } else {
    return {};
  }
}

function check_panel_name()
{
  var vars = getQueryVariables();
  if (vars.panel) {
    show_panel(vars.panel, vars);
  }
}
