/********
    This file is part of the freedom website.

    Freedom website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    The freedom website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the freedom website.  If not, see <http://www.gnu.org/licenses/>.
********/

  // preload hover images 
  var img1= new Image(132,177); 
  img1.src="http://www.freedom-artist.com/images/emerge-thumb-hover.png"; 

  var img2= new Image(132,177); 
  img2.src="http://www.freedom-artist.com/images/angels-thumb-hover.png";

  var img3= new Image(132,177); 
  img3.src="http://www.freedom-artist.com/images/age-thumb-hover.png";

  var img4= new Image(25,25); 
  img4.src="http://www.freedom-artist.com/images/close-box-hover.png";

  var img5= new Image(132,177); 
  img5.src="http://www.freedom-artist.com/images/diamond-mind-thumb-hover.png";


function handleEvent (e)
{
  if (!e) e = window.event.srcElement;

  if (e.getAttribute("class") == "song") 
  {
    var album = e.parentNode.parentNode.parentNode.id.replace(/album-/, "");
    handleSong(album, e);
    return;
  }

  switch (e.id)
  {
    case "emerge-album":
    case "angels-album":
    case "age-album":
    case "diamond-album":
      handleAlbum(e.id);
      break;

    case "close-box-video-age":
      handleCloseVideo(e.id);
      break;

    case "close-box-emerge":
    case "close-box-angels":
    case "close-box-age":
    case "close-box-diamond":
      handleClose(e.id);
      break;

    case "emerge-cover":
    case "angels-cover":
    // case "age-cover":
    // case "diamond-cover":
      handleAlbumCover(e);
      break;

    case "download-emerge":
      album = e.id.replace(/download-/g, "");
      handleDownload(album);
      break;

    case "display-video-age":
      album = e.id.replace(/display-video-/g, "");
      handleVideo(album);
      break;

  }
}

function handleAlbum (aID)
{
  var album = aID.replace(/-album/g, "");
  
  flushDisplay(album);

  album += "-display";

  document.getElementById(album).style.display = "inline";
}

function flushDisplay (aAlbum)
{
  if (aAlbum != "emerge")
  {
    document.getElementById("emerge-display").style.display = "none";
    hideSongPanel(aAlbum);
  }

  document.getElementById("angels-display").style.display = "none";
  document.getElementById("age-display").style.display = "none";
  document.getElementById("diamond-display").style.display = "none";

  document.getElementById("welcome").style.display = "none";
  document.getElementById("digest").style.display = "none";
}

function handleAlbumCover (aEl)
{
  var src = aEl.src;

  if (/front/g.test(src)) aEl.src = src.replace(/front/, "inner");
  if (/inner/g.test(src)) aEl.src = src.replace(/inner/, "back");
  if (/back/g.test(src)) aEl.src = src.replace(/back/, "front");
}

function handleSong (aAlbum, aEl)
{
  var song  = aEl.firstChild.nodeValue;
  var date  = aEl.getAttribute("date");

  // load song image
  var id = "song-image-" + aAlbum;
  document.getElementById(id).src = "images/" + song + ".png";

  // display song title
  id = "song-title-" + aAlbum;
  document.getElementById(id).innerHTML = song;

  // display song date
  id = "song-date-" + aAlbum;
  document.getElementById(id).innerHTML = date;

  // display song download link
  id = "song-download-" + aAlbum;
  document.getElementById(id).innerHTML = '<a href="music/' + getAlbumName(aAlbum) + "/" + song + '.mp3">download</a>';

  // display song player
  displayPlayer(aAlbum, song);

  displaySongPanel(aAlbum);
}

function displaySongPanel (aAlbum)
{
  // display song panel
  id = "song-display-" + aAlbum;
  document.getElementById(id).style.display = "inline";
}

function hideSongPanel (aAlbum)
{
  // display song panel
  id = "song-display-" + aAlbum;
  document.getElementById(id).style.display = "none";

  id = "song-image-" + aAlbum;
  document.getElementById(id).src = "images/blank.png";
}

function hideVideoPanel (aAlbum)
{
  id = "watch-video-" + aAlbum;
  document.getElementById(id).style.display = "none";
}

function handleDownload (aAlbum)
{
  var id = "download-display-" + aAlbum;

  document.getElementById(id).style.display = "inline";
}

function handleVideo (aAlbum)
{
  var id = "watch-video-" + aAlbum;

  document.getElementById(id).style.display = "inline";
}

function handleCloseVideo (aID)
{
  document.getElementById(aID).parentNode.style.display = "none";
}

function handleClose (aID)
{
  var album = aID.replace(/close-box-/g, "");
  var id    = album + "-display";

  // hide target album display
  document.getElementById(id).style.display = "none";

  // change album cover image back to front
  id    = album + "-cover";
  var image = document.getElementById(id);
  image.src = "images/" + id + "-front.png";

  // hide the song display
  hideSongPanel(album);
  hideVideoPanel(album);

  // hide the download instructions
  id = "download-display-" + album;
  document.getElementById(id).style.display = "none";

  document.getElementById("welcome").style.display = "inline";
  document.getElementById("digest").style.display = "inline";

}

function displayPlayer (aAlbum, aTrack)
{
  var flashvars =
  {
    playerID        : "1",
    soundFile       : "music/" + getAlbumName(aAlbum) + "/" + aTrack + ".mp3",
    artists         : "freedom",
    titles          : aTrack,
    initialvolume   : 100,
    text            : "8786C4",
    loader          : "999999",
    track           : "cccccc",
    tracker         : "666666"
  };

  var params =
  {
    quality: "high",
    menu: "false",
    wmode: "transparent"
  };

  // target id of player
  var targetID = aAlbum + "-player";
  var attributes = { id : targetID, styleclass : "player" };

  swfobject.embedSWF("flash/player.swf", targetID, "290", "24", "9.0.0", null, flashvars, params, attributes);
}

function getAlbumName (aAlbum)
{
  var folder = "emerge";

  if (aAlbum == "angels") folder = "angels and monkeys";
  else if (aAlbum == "age")  folder = "age of ether";
  else if (aAlbum == "diamond")  folder = "diamond mind";

  return folder;
}

