
function check_js(){//this removes the warning that you don't have javascript enabled
	//NB: THIS FUNCTION NEEDS TO BE CALLED *BEFORE* check_jukebox(), OTHERWISE THIS WILL REMOVE THE TRACK NAME TEXT
		document.getElementById('jukescroll').innerHTML="";
}



function check_jukebox(){//this function is called each time the page (in the mianframe) is loaded/changed	
	//find out if the juke box is playing or not
	if(parent.jukeframe.playing){//if the jukebox is currently playing
		//set the button to be a pause button
		document.getElementById('play_pause_button').innerHTML = '<img src="images/pause.png" alt="pause" />';
		
		//and get the current song info
		scrolltext=parent.jukeframe.current_info.title;
		completed=true;//that will stop it from scrolling straight away, cos that would be quite annoying
		scroll_it();//we have to 'scroll_it()' rather than start_scroll because start_scroll() sets completed to false
	}
	else {
		document.getElementById('jukescroll').innerHTML="";//for the sake of Safari which seems to ignore check_js() when you change pages
	}
	//if it's not playing then the button will already be a play button, so no need to do anything with that
	//scrolltext will be empty, but it will be set when someone presses play
	
	//and now check that the player has actually loaded (it would make more sense to do this first, but as it has to be done
	//after a delay (see below), I really want to display the buttons and the text first, and THEN do this
	t=setTimeout("check_jukebox_has_loaded();", 1500);
}

function check_jukebox_has_loaded(){//if the user doesn't have Flash 8+ then the player wont load
	//NB:this function needs to be called after a short delay, in order to make sure that the jukebox frame has fully loaded first
	if(parent.jukeframe.not_loaded){//if the player has NOT loaded
		document.getElementById('jukescroll').innerHTML='Please <a href="http://www.macromedia.com/go/getflashplayer/" target="_blank">Upgrade Flash Player</a>';
	}
}


//var timerID = null;
//var timerRunning = false;
var t,scroll_speed,completed,position=0;
var scrolltext='';
var length=scrolltext.length;
var scroll_space=30;//the number of characters that will fit into the available space
var end=position+scroll_space;
var stop=false;
var repeat_pause=10000;
var scroll_speed=1000/6;
var microtime;
var stopped=true;
var pause_loop_has_looped=false;



function start_scroll(){
	if(parent.jukeframe.not_loaded)//if the jukebox did not load (due to flash<version 8)
		return;//do nothing so that the 'Please Upgrade Flash Player' text stays there
	
	//stop any current itterations
	stop=true;
	//and now we need to wait until the current itteration has finished
	if(!stopped){//if it has NOT stopped yet
		t=setTimeout("start_scroll()",11);//wait a moment and try again
		return;
	}
	//initialize the settings (apart from the scroll_text)
	stop=false;
	completed=false;
	pause_loop_has_looped=false;
	position=0;
	end=position+scroll_space;
	length=scrolltext.length;	
	//and set it going
	stopped=false;
	scroll_it();
}
	
function scroll_it() {
	if(stop){//if the function is being called again with new text, and therefore this itteration needs to stop
		stop=false;
		stopped=true;
		return;
	}
	
	if(!scrolltext){//if there's no song name
		document.getElementById('jukescroll').innerHTML='';//clear the text
		stopped=true;
		return;
	}
	if(length<=scroll_space){//if the text will fit in the space without scrolling
		//document.getElementById('jukescroll').innerHTML=scrolltext;
		//stopped=true;
		//return;
	}
	if(completed){//if the thing has gone around once and we are back to the beginning
		document.getElementById('jukescroll').innerHTML=scrolltext.substring(0,scroll_space);//just display the text
		position=0;
		end=position+scroll_space;
		//and now we want to pause for repeat_pause seconds before scrolling again
		//but during that time the function could be called again with new text, so we need to keep checking to see if stop is set to true
		if(!pause_loop_has_looped){//the first time only
			date_object=new Date;
			microtime=date_object.getTime();//so microtime contains the time (in ms) at which we started the pause
			pause_loop_has_looped=true;
		}
		
		if(stop){//if the function HAS been told to stop
			stopped=true;
			completed=false;//reset that
			return;
		}
		//....OR....
		now= new Date();
		if(now.getTime()<(microtime+repeat_pause)){//if we haven't yet paused for repear_pause miliseconds
			t=setTimeout("scroll_it()", 10);//wait 10 miliseconds and re-check
			return;
		}
		//if we HAVE now waited for repeat_pause miliseconds...
		//then reset stuff
		completed=false;
		pause_loop_has_looped=false;
		//and the scrolling will continute...
	}
	else {
	
		if(end<length) display=scrolltext.substring(position,end);
		
		else {
			if(position<length) scrolltext+=' ';//add a space to the end so that the text keeps moving along
			//this needs to be non breaking, but we do that just before it's displayed, cos it only scrolls along one character at a time
			display=scrolltext.substring(position,end);
			//this is only needed if you want it to make the text into a continuous loop:
			//display=scrolltext.substring(position) + scrolltext;//and another copy of the message onto the end
			//display=display.substring(0,scroll_space);//and then take the first scroll_space characters of the whole thing
		}
		
		display=display.replace(/ /,  '&nbsp;');//make the spaces non-breaking - this is needed to make it keep scrolling across
		document.getElementById('jukescroll').innerHTML=display;
		
		position++;
		end++;
		
		if(position>=length){
			completed=true;
		}
	}

	t=setTimeout("scroll_it()",scroll_speed);
}







function change_button(value){//this function gets called from the jukebox frame to change the image in the play/pause button
	document.getElementById('play_pause_button').innerHTML=value;
}




