/*
   TheMusicalFruit.com JavaScript
   This Web site and all the songs on it are Copyright (c) 2008 and 2009 
   by TheMusicalFruit.com. All rights reserved.
   This site is the home of Musewack: Elevatoration Music.  
   That is, elevation music in the elevator.
*/

//<script type="text/javascript">
  var songIndex = 0;
  var songList  = new Array();
  var currentTune;
  var mySMSound;
  var myTimer;
  var nowPlayingElement;
  var nowPlayingTitle;
  var bShowLyrics = false;
 	var xmlhttp;

  /* The object of this site */
  function Song (nm, ident)
  {
  	this.name = nm; this.id = ident;
  }

  /* Initialize the song array and create them all in the soundManager */
  soundManager.onload = function() 
  {
    //alert("soundManager.onload: Loading .mp3s");
    initSongs();
    
    // soundManager should be ready to use/call at this point
    var j;
    
    for (songIndex=0; songIndex<songList.length; songIndex++)
    {
      //alert("songList.length = " + songList.length.toString());
      soundManager.createSound(songList[songIndex].id, "tunes/" + songList[songIndex].name + " - 128kbps.mp3"); 
    }
  }
//</script>

//<script type="text/javascript">

  /* Initialize the array of songs */
  function initSongs() 
  {
    var i = 0;

    songList[i++] = new Song("A l\'Abri", 'AlAbri');
    songList[i++] = new Song('A Purple World sans paroles', 'APurpleW');
    songList[i++] = new Song('A Seven Flurry', 'ASevenFl');
    songList[i++] = new Song('All or None at All', 'AllorNon');
    songList[i++] = new Song('Am I Returning', 'AmIRetur');
    songList[i++] = new Song('Arcane Abstractions in the Face of Abstruse Abuse', 'ArcaneAb');
    songList[i++] = new Song('Barbara', 'Barbara');
    songList[i++] = new Song('Before the Sun Goes Down - Solo', 'BeforeTS');
    songList[i++] = new Song('Before the Sun Goes Down', 'BeforeTh');
    songList[i++] = new Song('Before the Sun Goes Down (version 3)', 'BeforeT3');
    songList[i++] = new Song('Before the Sun Goes Down (version 4)', 'BeforeT4');
    songList[i++] = new Song('Before the Sun Goes Down (version 5)', 'BeforeT5');
    songList[i++] = new Song('Blue and Green', 'BlueandG');
    songList[i++] = new Song('Chouette', 'Chouette');
    songList[i++] = new Song('Circus Time', 'CircusTi');
    songList[i++] = new Song('Common Denominator', 'CommonDe');
    songList[i++] = new Song('Crystal Wine', 'CrystalW');
    songList[i++] = new Song('Curb Your Chicken', 'CurbYour');
    songList[i++] = new Song('Every Little Minute You\'re Away', 'EveryLit');
    songList[i++] = new Song('Ferdie Rides a Horse in 3D', 'FerdHors');
    songList[i++] = new Song('Gravity', 'Gravity');
    songList[i++] = new Song('I Need Somebody Who Cares Too Much', 'INeedSom');
    songList[i++] = new Song('I Wonder', 'IWonder');
    songList[i++] = new Song("I\'m Thinkin\'", 'ImThinki');
    songList[i++] = new Song('I-VI-II-V', 'I-VI-II-');
    songList[i++] = new Song('Inbetween', 'Inbetwee');
    songList[i++] = new Song('In Three for Zach and Lily', 'InThreeF');
    songList[i++] = new Song('It Takes More than Love', 'ItTakesM');
    songList[i++] = new Song('Jennifer', 'Jennifer');
    songList[i++] = new Song('La Mantequilla', 'LaManteq');
    songList[i++] = new Song("Little \'Lissa", 'LittleLi');
    songList[i++] = new Song('Mr. Greene', 'MrGreene');
    songList[i++] = new Song('My Window', 'MyWindow');
    songList[i++] = new Song('Neuteronomy', 'Neuteron');
    songList[i++] = new Song('Never Be Lonely - breakage', 'NeverBBk');
    songList[i++] = new Song('Old San Juan', 'OldSanJu');
    songList[i++] = new Song('Over You', 'OverYou');
    songList[i++] = new Song('Over You emix', 'OverYoue');
    songList[i++] = new Song('Rainy Saturday', 'RainySat');
    songList[i++] = new Song('Rendezvous', 'Rendezvo');
    songList[i++] = new Song('Reusable Code', 'Reusable');
    songList[i++] = new Song('Run to the Future', 'Runtothe');
    songList[i++] = new Song('Sailin\' On', 'SailinOn');
    songList[i++] = new Song('Samba Dulce', 'SambaDul');
    songList[i++] = new Song('Separation', 'Separati');
    songList[i++] = new Song('Several Days Ago', 'SeveralD');
    songList[i++] = new Song('Sides', 'Sides');
    songList[i++] = new Song('Slow Walk in the Sunshine', 'SlowWalk');
    songList[i++] = new Song('Some People', 'SomePeop');
    songList[i++] = new Song('Sweet Magnolia Lady', 'SweetMag');
    songList[i++] = new Song('ThE1me', 'ThE1me');
    songList[i++] = new Song('The Magician', 'TheMagic');
    songList[i++] = new Song('The Way It Seems to Me', 'TheWayIt');
    songList[i++] = new Song('Tunnel of Love', 'TunnelOf');
    songList[i++] = new Song('Waltz for Alissa', 'Waltzfor');
    songList[i++] = new Song('When I\'m With You I Never Look at TV', 'WhenImWi');
    songList[i++] = new Song('You and I - version 1', 'YouandI1');
    songList[i++] = new Song('You and I - version 2', 'YouandI2');
    songList[i++] = new Song('z Music', 'zMusic');
    
    nowPlayingTitle = "";
    currentTune     = "";
  }


  // Callback to set the lyrics text into the lyrics textarea /
  function displayLyrics()
  {
  	if (xmlhttp.readyState == 4) 
  	{
      if (xmlhttp.status == 200 || xmlhttp.status == 0) // will be 200 when from a Web server but 0 from a file
      {
        //document.getElementById('theDivToLoad').innerHTML=xmlhttp.responseText;
        document.forms[1].elements[0].value = xmlhttp.responseText;
      }
    }
  }


  // Read the lyrics from the file /
  function loadLyrics(songName)
  {
  	var pURL;

    if (songName == "")
    {
      // Just clear the field whether displayed or not
      document.forms[1].elements[0].value = "";
    }
    else
    {
    	// Go out and read in the lyrics file
	  	if (window.XMLHttpRequest)  // code for Mozilla, Safari, ** And Now IE 7 **, etc
	  	{
	  		//alert("loadLyrics() - window.XMLHttpRequest");
	      xmlhttp = new XMLHttpRequest();
	    } 
	    else 
	    {
	    	if (window.ActiveXObject)  //IE
	    	{
	  		  //alert("loadLyrics() - window.ActiveXObject");
	        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	      }
	    }
	    
	    //if (typeof(xmlhttp) == 'object') 
	    if (xmlhttp) 
	    {
	        pURL = "lyrics/" + songName + ".lyrics";
	        xmlhttp.onreadystatechange = displayLyrics;
	        xmlhttp.open('GET', pURL, true);
	        xmlhttp.send(null);
	    }
    }
  }


  /* Set the name of the song in the status bar */
  function setSongStatus(id)
  {
    var k, l, m;
    
    for (k=0; k < songList.length; k++)
    {
      if (songList[k].id == id)
      {
        defaultStatus           = songList[k].name;
        status                  = songList[k].name;
        nowPlayingElement       = document.getElementById('now-playing');
        nowPlayingElement.value = songList[k].name;
        nowPlayingTitle         = songList[k].name;
        
        if (bShowLyrics == true)
        {
        	loadLyrics(songList[k].name);
        }
      }
    }
  }

  /* Clear the now-playing field */
  function clearSongStatus()
  {
    nowPlayingElement       = document.getElementById('now-playing');
    nowPlayingElement.value = "";
    defaultStatus           = "";
    status                  = "";
    nowPlayingTitle         = "";
    loadLyrics("");
  }

  /* Stop everything */
  function stopAll() 
  {
    clearTimeout(myTimer);
    soundManager.stopAll();
    songIndex = songList.length;
    clearSongStatus();
  }

  /* Stop the song that is currently playing
     Actually used as a Next function when playing all loop
     and stop any current one when a new single play selected
  */
  function stopOne(id)
  {
 	  soundManager.stop(id);
    clearSongStatus();
  }
    
  /* Play one song during the loop to play all songs */
  function playOne() 
  {
    if (songIndex < songList.length)
    { 
      mySMSound = soundManager.getSoundById(currentTune);
      if (mySMSound.playState == 0)
      {
        currentTune = songList[songIndex].id;    
        soundManager.play(songList[songIndex++].id);
        setSongStatus(currentTune);
      }
        myTimer = setTimeout("playOne()", 2000);
    }
    else
    {
      songIndex = 0;
      defaultStatus = "";
    }
  }
  
  /* Play all the tunes in alphabetical order */
  function playAll()
  {
    if (currentTune != "")
    {
    	stopOne(currentTune);
    }

    songIndex = 0; 
    currentTune = songList[songIndex].id;
    playOne();
  }

  /* Play a song that was individually selected */
  function playSingle(id)
  {
    //alert("Called playSingle()" + " id = " + id.toString());  
    if (currentTune != "")
    {
    	stopOne(currentTune);
    }
    soundManager.play(id);
    currentTune = id;
    setSongStatus(id);
  }

  /* Show or hide the lyrics.  Called when there is a change to the checkbox */
  function showTheLyrics(isChecked)
  {
    if (isChecked == true)
    {
    	bShowLyrics = true;
      loadLyrics(nowPlayingTitle);
      document.forms[1].elements[0].style.visibility = 'visible';
    }
    else
    {
    	bShowLyrics = false;
      loadLyrics("");
      document.forms[1].elements[0].style.visibility = 'hidden';
    }
  }

//</script>
