/***************************************************
Created By MT 2005
June:		Creation of initial code. Bugs with player.
19 July : 	Code Rewrite to erase player issues DT
****************************************************/
/*
This is list of the songs
*/
var musicArray = new Array (
								"ItsNowOrNever",
								"YellowRiver",
								"FatherSon",
								"UnchainedMelody",
								"DontTalkToHim",
								"LoveIsInTheAir",
                            	"JohnnyRememberMe",
                            	"WindBeneathMyWings",
								"BachelorBoy",
								"Diana",
								"IThinkOfYou",
								"LittleOlWineDrinkerMe",
								"DaydreamBeliever",
								"LetsTwistAgain",
								"BeMyBaby",
								"WhereDidOurLoveGo",
								"CantHelpFallingInLoveWithYou",
								"LivingDoll",
								"TellMeWhen",
								"GreenGreenGrassOfHome",
								"DancingShoes",
								"Delilah",
								"RhythmOfTheRain",
								"LeaningOnALampPost",
								"Locomotion",
								"StarryEyed",
								"SummerHoliday",
								"TheYoungOnes",
								"Smile",
								"Believe",
"Cabaret","IBelieve","RamblingRose","SecretLove","TheNextTime","StrangersInTheNight","BakerStreet","TooYoung","ILoveYouBecause","SomePeople"
							);
/****************************************************************/
/*
Timeout id.
This id is assigned each time th esetTimeout method runs - this allows the setTimeout to be cancelled when the next song is chosen or the player is cancelled
*/
var theTimeout = null;
/****************************************************************/
/*
Cache of all the images
*/
if (document.images) 
{
  imageCacheEmpty = new Array();
  imageCacheFull = new Array();
  img1 = new Image();
  img1.src = "picture.jpg";
  for(var z = 1;z<=9;z++)
  { 
    var x = "CD"+z
    x = new Image();
    x.src = "images/CDPLAYERempty_2x"+z+".gif";
    imageCacheEmpty[z] = x;
    var y = "CD"+z
    y = new Image();
    y.src = "images/CDPLAYERFULL_2x"+z+".gif";
    imageCacheFull[z] = y;
  }
}
/****************************************************************/
/*
Count represents the song number
*/
var count = -1;
/****************************************************************/
/*
Player Control
*/
function playerControl(event)
{
	if(event == "start")
	{
		count = 1;
		buildEmpties(0);//clear the picture
		clearTimeout(theTimeout);
		play(count);
		return;
	}
	if(event == "next")
	{
		count += 1;
		buildEmpties(0);//clear the picture
		clearTimeout(theTimeout);
		play(count);				
		return;
	}
	if(event == "previous")
	{
		count -= 1;
		buildEmpties(0);//clear the picture
		clearTimeout(theTimeout);
		play(count);
		return;
	}
	if(event == "stop")
	{
	  buildEmpties(0);//clear the picture
	  //all that's left is a close event!	
	  clearTimeout(theTimeout);
	  document.getElementById("soundspan").innerHTML="";
	}
}
/****************************************************************/
/*
Player Builder
*/
function buildSound(soundName) 
{
  //this needs to be altered so autostart is false - fix for later!!
  document.getElementById("soundspan").innerHTML= "<embed id='soundPlayer' autostart=true src='"+soundName+"' hidden=true loop=false starttime=00:00 endtime=25:00>";
} 
/****************************************************************/
/*
Player Control
*/
function playSound() 
{
  //this code needs to be enhanced to play mozilla as well	- commented out till buildSound() autostart is false
  //document.getElementById("soundPlayer").start();
} 
/****************************************************************/
/*
Graphic Controller
*/
function visualController()
{ 
	var rand = Math.floor(Math.random()*9)+1;
    buildFulls(rand);
    buildEmpties(rand);
}
/****************************************************************/
/*
Build Full lights
*/
function buildFulls(rand)
{
  for(var i = 1;i<=rand;i++)
  {
    document.getElementById("CD"+i).src = imageCacheFull[i].src;
  }
}
/****************************************************************/
/*
Build Empties : Now this is a candidate to drop because the zindex 
can be used purely to draw the full pictures
*/
function buildEmpties(rand)
{
  for(var y = rand+1;y<=9;y++)
  { 
    document.getElementById("CD"+y).src = imageCacheEmpty[y].src;
  }  
}
/****************************************************************/
/*
Player Controller.
1/ Check that the selected song (count) is not outside the acceptable array range. If so reset to 1 (first song.)
*/
function play(val)
{
  if(count < 0 || val > musicArray.length) {val = 1;}
  var theSong = musicArray[val];
  buildSound("music\/"+theSong+".mp3");//load the song
  startSpan(theSong);//display the song title
  playSound();
  setTimeout("countDown(24000,200,visualController())",2000);
}

/****************************************************************/
/*
Time Controller.
Javascript is crippled by its lack of a clock so here's one I made
Thanks for all the fun Richard
*/
function countDown (endTime, timeIncrement, theFunc)
{
	if(endTime <= 0)
	{
		endTime = 0;
		return;
	}
	endTime -= timeIncrement;
	theFunc;
	theTimeout = setTimeout("countDown("+endTime+","+timeIncrement+",visualController())",timeIncrement);
}
/****************************************************************
This section holds the code that positions the dynamic text that is displayed
when the song is playing
****************************************************************/
/*
variables that describe the position of the cdplayer info window
*/
var foundTopPos = false; 
var foundLeftPos = false;
var topPos;
var leftPos;
/****************************************************************/
/*
Lazy initialise the positions
*/
function findLeftPos(elementX)
{
  if(foundLeftPos)
  {
    return leftPos;
  }
  var x=0;
  var offsetPointer = elementX 
  while(offsetPointer)
  {
    x += offsetPointer.offsetLeft;
    offsetPointer = offsetPointer.offsetParent;
  }
  foundLeftPos = true;
  leftPos = x;
  return leftPos;
}

function findTopPos(elementX)
{ 
  if(foundTopPos)
  { 
    return topPos; 
  }
  var x=0;
  var offsetPointer = elementX
  while(offsetPointer)
  {
    x += offsetPointer.offsetTop;
    offsetPointer = offsetPointer.offsetParent;
  }
  foundTopPos = true;
  topPos = x+5;
  return topPos;
}
/****************************************************************/
/*
Complete onscreen display of playing song
*/
function startSpan(val)
{
  var mar = document.getElementById("cdWindowSpan");
  var cdwin = document.getElementById("cdWindow"); 
  mar.style.left= findLeftPos(cdwin);
	var MIE = /Microsoft/;
	if(MIE.test(navigator.appName))
	{
		//alert("hi")
		mar.style.top = findTopPos(cdwin) - 15 ;
	}
	else
	{
    mar.style.top = findTopPos(cdwin);
	}
  mar.width = cdwin.offsetWidth - 20;
  mar.height = cdwin.offsetHeight - 10;
  mar.style.position = "absolute";
  mar.style.zindex = "1"; 
  mar.innerHTML = "<font face=tahoma size=1; color=green>"+val+"</font>";
}
/****************************************************************/


