/////////////////////////////////////////////////////////////////////////
//                                                                     //
// These are JavaScript functions that are used by more than one page  //
// in the "Tarot by Iain Scott" web site.                              //
//                                                                     //
// Copyright © ISMm 2004                                               //
//                                                                     //
// Last modified: 24 March 2004                                        //
//                                                                     //
/////////////////////////////////////////////////////////////////////////


// Global variables
var songListLong = new Array();                                      // in which the catalogue of songs is listed by long name
var songListShort = new Array();                                     // in which the catalogue of songs is listed by short name
var noMP3 = "onemored whatever remember"; // for which there are no MP3 previews

buildSongLists();

//
// This function builds an array containing the current lists of songs (long and short names)
// in the same order that they appear in the menu controlled from within "menu.htm". The songs
// are listed in the order in they are navigated when stepping through the "next" and "prev"
// links when viewing the song pages as selected from the menu.
//
// NB. It is deliberate that the 0th index is left empty in both case.
//
function buildSongLists () {
  //first the list of long names
  songListLong[1] = "Did I Tell You";
  songListLong[2] = "These Are The Flowers";
  songListLong[3] = "It Was Real";
  songListLong[4] = "Kind";
  songListLong[5] = "That Was The Way";
  songListLong[6] = "Wish";
  songListLong[7] = "She Dances";
  songListLong[8] = "Green Dress";
  songListLong[9] = "Sweet Child";
  songListLong[10] = "Could\'ve";
  songListLong[11] = "For Joy";
  songListLong[12] = "Light In The Mirror";
  songListLong[13] = "Time To Go";
  songListLong[14] = "Oh The Way";
  songListLong[15] = "Tangled Up In Golden Hair";
  songListLong[16] = "Trouble\'s Come Down";
  songListLong[17] = "Angela";
  songListLong[18] = "Soldier";
  songListLong[19] = "The World That She Knew";
  songListLong[20] = "To Love Ya";
  songListLong[21] = "Song For Storm";
  songListLong[22] = "Stupid";

  // second the list of short names
  songListShort[1] = "diditell";
  songListShort[2] = "theseare";
  songListShort[3] = "itwasrea";
  songListShort[4] = "kind";
  songListShort[5] = "thatwast";
  songListShort[6] = "wish";
  songListShort[7] = "shedance";
  songListShort[8] = "greendre";
  songListShort[9] = "sweetchi";
  songListShort[10] = "couldve";
  songListShort[11] = "forjoy";
  songListShort[12] = "lightint";
  songListShort[13] = "timetogo";
  songListShort[14] = "ohtheway";
  songListShort[15] = "tangledu";
  songListShort[16] = "troubles";
  songListShort[17] = "angela";
  songListShort[18] = "soldier";
  songListShort[19] = "theworld";
  songListShort[20] = "toloveya";
  songListShort[21] = "songfors";
  songListShort[22] = "stupid";
}

//
// This function write the navigation panel at the bottom of the current page.
// If the linked to page is not in the same directory as the page that called
// this handler, care must be taken to ensure the correct URL relative to
// this page.
//
function writeNavPanel (thisSection,thisPage) {
  var prevPage;
  var nextPage;
  var totalPages = getTotalPages(thisSection);
  
  // calculate the previous page number
  if (thisPage == 1) {
    var prevPage = totalPages;
  }
  else {
    prevPage = thisPage - 1;
  }
  
  // calculate the next page number
  if (thisPage == totalPages) {
    nextPage = 1;
  }
  else {
    nextPage = thisPage + 1;
  }
  
  // write the navigation panel
  document.writeln("<td height=\"30\" width=\"50\" align=\"left\" class=\"smallURL\"><a href=\"" + thisSection + prevPage + ".htm\" class=\"smallURL\">&lt;&lt; prev<\/a><\/td>");
  document.writeln("<td height=\"30\" width=\"270\" align=\"center\" class=\"small\">page " + thisPage + " of " + totalPages + "<\/td>");
  document.writeln("<td height=\"30\" width=\"50\" align=\"right\" class=\"smallURL\"><a href=\"" + thisSection + nextPage + ".htm\" class=\"smallURL\">next &gt;&gt;<\/a><\/td>");
}

//
// This function write the navigation panel at the bottom of the current songs page.
// If the linked to page is not in the same directory as the page that called
// this handler, care must be taken to ensure the correct URL relative to
// this page.
//
function writeSongsNavPanel (thisSong) {
  var thisSection = "songs";
  var prevPage;
  var nextPage;
  var totalPages = getTotalPages(thisSection);
  var thisPage = getSongPage(thisSong);
  
  // calculate the previous page number
  if (thisPage == 0) {
    alert("The song '" + thisSong + "' is not recognised.");
  }
  else {
    if (thisPage == 1) {
      var prevPage = totalPages;
    }
    else {
      prevPage = thisPage - 1;
    }
  
    // calculate the next page number
    if (thisPage == totalPages) {
      nextPage = 1;
    }
    else {
      nextPage = thisPage + 1;
    }
  
    // write the navigation panel
    document.writeln("<td height=\"30\" width=\"50\" align=\"left\" class=\"smallURL\"><a href=\"" + songListShort[prevPage] + ".htm\" class=\"smallURL\">&lt;&lt; prev<\/a><\/td>");
    document.writeln("<td height=\"30\" width=\"270\" align=\"center\" class=\"small\">page " + thisPage + " of " + totalPages + "<\/td>");
    document.writeln("<td height=\"30\" width=\"50\" align=\"right\" class=\"smallURL\"><a href=\"" + songListShort[nextPage] + ".htm\" class=\"smallURL\">next &gt;&gt;<\/a><\/td>");
  }
}

//
// This function returns the number of a song in the song lists from its short name.
//
function getSongPage (shortName) {
  var gotSong = 0;
  
  for (i = 1; i < songListShort.length; i++) {
    if (shortName == songListShort[i]) {
      gotSong = i;
      break;
    }
  }
  
  return gotSong;
}

//
// This returns the total number of pages in the specified section.
//
function getTotalPages (whichSection) {
  switch (whichSection) {
    case 'about':
      pages = 4;
      break;
    case 'buy':
      pages = 1;
      break;
    case 'contact':
      pages = 1;
      break;
    case 'dates':
      pages = 1;
      break;
    case 'forum':
      pages = 1;
      break;
    case 'guitars':
      pages = 3;
      break;
    case 'home':
      pages = 1;
      break;
    case 'influences':
      pages = 9;
      break;
    case 'links':
      pages = 3;
      break;
    case 'songs':
      pages = songListShort.length - 1;
      break;
    default:
	    alert("Unrecognised section '" + whichSection + "'.");
      pages = 0;
      break;
  }
  
  return pages;
}

//
// Select one of the foreground images at random for display.
//
function pickForeground () {
  var imageCount = 10;
  var imageNum = Math.floor(imageCount * Math.random()) + 1;
  var imagePath = "../../graphics/foregrounds/";
  var imageName = imagePath + "cimage" + imageNum + ".jpg";
  
  document.writeln("<td width=\"200\" height=\"200\" align=\"center\"><img src=\"" + imageName + "\" width=\"200\" height=\"200\"></td>");
}

//
// This downloads the specified MP3 preview file.
//
function getMP3Preview (whichPreview) {
  if (noMP3.indexOf(whichPreview) > -1) {
    alert("The preview for this song is coming soon.");
  }
  else {
    location.href = "../../mp3s/" + whichPreview + ".mp3";
  }
}

//
// This function checks the user's screen size against the minimum size specified by the
// parameters. If it succeeds, it returns true. Otherwise it returns false.
//
//    screenMinW - minimum allowable width of the screen
//    screenMinH - minimum allowable height of the screen
//
// See also "checkAvailScreenSize"
//
function checkScreenSize (screenMinW,screenMinH) {
  if ((screen.width < screenMinW) || (screen.height < screenMinH)) {
    userAlert(3);
  }
}

//
// This function presents the user with a "confirm" dialog containing an
// appropriate message. We do this so we can keep all standard error messages
// in one place and ensure we handle user choices in a consistent manner.
// If the user responds "OK", this function returns true, otherwise it
// returns false.
//
function userAlert (alertWhat) {
  switch (alertWhat) {
    case 1:
      msgStr = "Sorry, the window cannot be opened. You might be low on memory.";
      break;
    case 2:
      msgStr = "Sorry, the web site URL cannot be determined.";
      break;
    case 3:
      msgStr = "This site is designed for use on monitors that are set to at least 1024 x 768 pixels.\n\n";
      msgStr += "Please adjust the size of your monitor using your Display Control Panel.";
      break;
    default:
      msgStr = "Sorry, there is an unspecified error.";
      break;
  }
  
  alert(msgStr);
}

//
// This function creates or updates a cookie with the given name to contain the given value.
// Only the first two parameters are required.
//
//    cookieName  - name of the cookie whose value is to be set
//    cookieValue - value to be assigned to the cookie
//
// Note
//    1. If the cookie already exists, it is silently overwritten with the new value
//    2. Early versions of JavaScript do not implement the "toGMTString" method correctly
//       and strange results can be expected. Which is why I've commented it out of the
//       'preferred' version of this function! To avoid the problem, pass the expiry date
//       as a well-formed Date object using a statement such as...
//
//                var expires = new Date(2044,11,16,06,00,00);
//
function setCookie (cookieName,cookieValue) {
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : "";
  var path = (argc > 3) ? argv[3] : "";
  var domain = (argc > 4) ? argv[4] : "";
  var secure = (argc > 5) ? argv[5] : false;

  document.cookie = cookieName + "=" + escape(cookieValue);
  document.cookie += ((expires == "") ? "" : ("; expires=" + expires)); //.toGMTString()));
  document.cookie += ((path == "") ? "" : ("; path=" + path));
  document.cookie += ((domain == "") ? "" : ("domain=" + domain));
  document.cookie += ((secure == true) ? ";secure" : "");
}

//
// This function takes as its first argument a well-formed date object to which the
// specified number of days must be added. The thus modified date is returned as the
// result. Note the use of the skew to adjust for the difference between Macintosh
// dates and other systems' dates.
//
function setCookieDate (cookieDate,days) {
  var baseDate = new Date(0);
  var skewDate = baseDate.getTime();
  
  if (skewDate > 0) {
    cookieDate.setTime(cookieDate.getTime() - skewDate);
  }

  cookieDate.setTime(cookieDate.getTime() + ((24 * 60 * 60 * 1000) * days));
  
  return cookieDate
}

//
// This function looks for a cookie with the name given in the parameter. If the cookie
// is found it calls "getCookieValue" to get the value of the cookie and returns that
// value as its result. If the cookie does not exist, it returns the NULL string "".
//
//    cookieName - name of the cookie whose value is required
//
// Requires: getCookieValue
//
function getCookie (cookieName) {
  var arg = cookieName + "=";
  var argLen = arg.length;

  var cookieValue = "";
  var cookieLen = document.cookie.length;
  var cookieOffset;
  var i = 0;

  while (i < cookieLen) {
    cookieOffset = i + argLen;

    if (document.cookie.substring(i,cookieOffset) == arg) {
      cookieValue = getCookieValue(cookieOffset);
      break;
    }

    i = document.cookie.indexOf(" ",i) + 1;

    if (i == 0) break;
  }

  return cookieValue;
}

//
// This function returns the decoded value of the cookie residing in the cookie jar
// at the specified offset.
//
//    cookieOffset - offset at which to look for the cookie value
//
// Typically this is called from within "getCookie".
//
function getCookieValue (cookieOffset) {
  var endStr = document.cookie.indexOf(";",cookieOffset);

  if (endStr == -1) {
    endStr = document.cookie.length;
  }

  return unescape(document.cookie.substring(cookieOffset,endStr));
}
