//---- Additional javascript functions for SWC Walk documents. © Sean O'Neill ----

/*--------------------------------------------------------------------------------
  son_regionmaps writes several links to www.streetmap.co.uk
    maparray is an array of 'easting|northing|areaname' (needs several for route)
*/
function son_regionmaps(maparray)
{
  var i;
  var map;
  var bar = '|';
  var sep = '';

  for (i = 0; i < maparray.length; i++) {
    map = maparray[i].split(bar);
    document.write(sep + '<A HREF="http://www.streetmap.co.uk/newmap.srf' +
		'?x=' + map[0] + '&amp;y=' + map[1] + '&amp;z=3' +
		'&amp;tl=' + map[2].replace(/ /g, '+') + '">' + 
		map[2].replace(/ /g, '&nbsp;') + '</A>');
    sep = ' | ';
  }
}

/*--------------------------------------------------------------------------------
  son_traintimes writes several links to www.traintimes.org.uk
    station is 'id|name' of destination, eg. 'BTN|Brighton'; name is optional
    optional params are:
      from: is 'id|name' of starting station; defaults to 'London'
      date: is date(s) of travel, separated by '|' if more than one,
            eg. 'Dec 31|Jan 1'; defaults to 'Tomorrow|Saturday|Sunday'
      time: is 'time out|time back', eg. '10:30a|16:30' ('a' = arrival time);
            both times are optional but if neither, defaults to '11:00a|16:00'
*/
function son_traintimes(station, params)
{
  var i;
  var str;
  var org = '<A HREF="http://www.traintimes.org.uk/';
  var bar = '|';
  var sep = ' | ';
  var slash = '/';
  var start_from = son_getcookie('myStation', 'London');

  var dest = station.split(bar);
  var dest_id = dest[0] + slash;
  var dest_name = dest[1] || dest[0];

  var from = (params.from || start_from).split(bar);
  var from_id = from[0] + slash;
  var from_name = from[1] || from[0];

  var date = (params.date || 'Tomorrow|next%20Saturday|next%20Sunday').split(bar);
  var days = (params.date || 'Tomorrow|Saturday|Sunday').split(bar);

  var time = (params.time || '11:00a|16:00').split(bar);
  var time_out = time[0] + slash;
  var time_back = (time[1] || '') + slash;

  if (time_back == slash) {
    str = 'From ' + from_name + ' to ' + dest_name + ' on: ';
    org += from_id + dest_id;
    for (i = 0; i < days.length; i++) {
      document.write(str + org + time_out + date[i] + '">' + days[i] + '</A>');
      str = sep;
    }
  }
  else if (time_out == slash) {
    str = 'From ' + dest_name + ' to ' + from_name + ' on: ';
    org += dest_id + from_id;
    for (i = 0; i < days.length; i++) {
      document.write(str + org + time_back + date[i] + '">' + days[i] + '</A>');
      str = sep;
    }
  }
  else {
    str = 'Between ' + from_name + ' and ' + dest_name + ' on: ';
    org += from_id + dest_id;
    for (i = 0; i < days.length; i++) {
      document.write(str + org + time_out + date[i] + slash + time_back + date[i] + '">' + days[i] + '</A>');
      str = sep;
    }
  }
}

/*--------------------------------------------------------------------------------
  son_showsection strikes out headings not on a route and hides their directions.
    options indicates which Walk Option has been clicked; this one is highlighted.
    sections is a string whose length is the number of sections; '.' = off route.
    The Walk Notes (Details0) can't be toggled manually, so determines the state;
    if section is on route, directions are shown or not depending on this state.
*/
function son_showsection(options, sections)
{
  var blockID;
  var i;

  for (i = 0; i < options.length; i++) {
    blockID = document.getElementById('Option' + i);
    switch (options.charAt(i)) {
      case '.': blockID.className = 'optionoff'; break;
      case '?': blockID.className = 'optionall'; break;
      default : blockID.className = 'optionon';
    }
  }
  var onroute;
  blockID = document.getElementById('Outline0');
  var show_outline = blockID.className;
  blockID = document.getElementById('Details0');
  var show_details = blockID.className;

  for (i = 1; i < sections.length; i++) {
    onroute = sections.charAt(i) != '.';
    blockID = document.getElementById('Heading' + i);
    blockID.className = onroute ? 'include' : 'exclude';
    blockID = document.getElementById('Outline' + i);
    blockID.className = onroute ? show_outline : 'hide';
    blockID = document.getElementById('Details' + i);
    blockID.className = onroute ? show_details : 'hide';
  }
}

/*--------------------------------------------------------------------------------
  son_showdetails toggles the display of the Outline and Details sections.
    section is the section number to change, or 0 (toggle all sections):
    both sections are hidden if off route, else if display is 'show' or 'hide',
    Details section is set to this and Outline section to the opposite;
    otherwise, Outline and Details sections are toggled between 'show' and 'hide'.
    if section is 0, display is a string whose length is no. of sections to change.
*/
function son_showdetails(section, display)
{
  var headingID = document.getElementById('Heading' + section);
  var outlineID = document.getElementById('Outline' + section);
  var detailsID = document.getElementById('Details' + section);

  if (headingID.className == 'exclude') {
    outlineID.className = 'hide';
    detailsID.className = 'hide';
  }
  else if (display == 'show') {
    outlineID.className = 'hide';
    detailsID.className = 'show';
  }
  else if (display == 'hide') {
    outlineID.className = 'show';
    detailsID.className = 'hide';
  }
  else {
    var toggle_showhide = detailsID.className;
    detailsID.className = outlineID.className;
    outlineID.className = toggle_showhide;
  }

//  if clicked main heading, loop (recursively) through all other sections
  if (section == 0) for (var i = 1; i < display.length ; i++) {
    son_showdetails(i, detailsID.className);
  }
}

/*--------------------------------------------------------------------------------
  son_getcookie retrieves the cookie value for a named cookie
*/
function son_getcookie(cookie_name, default_value)
{
  var i;
  var sep = "; ";
  var cookie_array = document.cookie.split(sep);
  var cookie_item;
  sep = "=";

  for (i = 0; i < cookie_array.length; i++) {
    cookie_item = cookie_array[i].split(sep);
    if (cookie_name == cookie_item[0]) return cookie_item[1];
  }
  return default_value;
}

/*--------------------------------------------------------------------------------
  son_setcookie stores a value for a named cookie
*/
function son_setcookie(cookie_name, cookie_value)
{
  document.cookie = cookie_name + '=' + cookievalue + ';expires=31 December 2020';
}
