if (self!=top)
{
	if(top.location.replace)
		top.location.replace(window.location.href);
	else
		top.location.href = window.location.href;
}

function GetParam(name) {
   var start = location.search.indexOf("?" + name + "=");
   if (start < 0)
      start = location.search.indexOf("&" + name + "=");
   if (start < 0)
      return '';
   start += name.length + 2;
   var end = location.search.indexOf("&", start) - 1;
   if (end < 0)
      end = location.search.length;
   var result = '';
   for (var i = start; i <= end; i++) {
      var c = location.search.charAt(i);
	  result = result + (c == '+' ? ' ' : c);
   }
   return unescape(result);
}

var SMloaded=false;
var SMfailed=false;
var loadedtunes="";
var playedtunes="";
var paused="";
var playing="";
var actualtune=new Array(16);
var tunevolume=new Array(16);
actualtune[0]="Points";
tunevolume[0]= 40;
actualtune[1]="Black_Tiger";
tunevolume[1]= 55;
actualtune[2]="That_Blues";
tunevolume[2]= 70;
actualtune[3]="Something_with_Cheese";
tunevolume[3]= 55;
actualtune[4]="Nascer";
tunevolume[4]= 70;
actualtune[5]="Bazaar";
tunevolume[5]= 55;
actualtune[6]="Baghdad_Extended_Version";
tunevolume[6]= 180;
actualtune[7]="Overtone_Blues";
tunevolume[7]= 65;
actualtune[8]="Tension";
tunevolume[8]= 55;
actualtune[9]="Blueberry_Soup";
tunevolume[9]= 80;
actualtune[10]="Signum";
tunevolume[10]= 50;
actualtune[11]="Sol";
tunevolume[11]= 25;
actualtune[12]="Tango_For_Resande";
tunevolume[12]= 35;
actualtune[13]="Baghdad_2003";
tunevolume[13]= 70;
actualtune[14]="Munspelsvalsen";
tunevolume[14]= 40;
actualtune[15]="She_s_My_Friend";
tunevolume[15]= 90;
var tunedescription=new Array(16);
tunedescription[0]="Points (Simon Jensen Band), &copy; 2007.";
tunedescription[1]="Black Tiger (<a href='http://www.musicalexperiments.com/tangle.php' target='_blank' class='playingnowText' title='Visit Tangle at www.musicalexperiments.com!'>Tangle</a>), &copy; 2010.";
tunedescription[2]="That Blues (Simon Jensen Band), &copy; 2005.";
tunedescription[3]="Something with Cheese (Simon Jensen Band), &copy; 2005.";
tunedescription[4]="(Re)Birth (<a href='http://www.musicalexperiments.com/' target='_blank' class='playingnowText' title='Visit Carrilho Jensen Nyberg at www.musicalexperiments.com!'>Carrilho Jensen Nyberg</a>), &copy; 2009.";
tunedescription[5]="Bazaar (<a href='http://www.musicalexperiments.com/tangle.php' target='_blank' class='playingnowText' title='Visit Tangle at www.musicalexperiments.com!'>Tangle</a>), &copy; 2008.";
tunedescription[6]="Baghdad 2003 (<a href='http://www.musicalexperiments.com/' target='_blank' class='playingnowText' title='Visit Carrilho Jensen Nyberg at www.musicalexperiments.com!'>Carrilho Jensen Nyberg</a>), &copy; 2009.";
tunedescription[7]="Overtone Blues (<a href='http://www.musicalexperiments.com/' target='_blank' class='playingnowText' title='Visit Carrilho Jensen Nyberg at www.musicalexperiments.com!'>Carrilho Jensen Nyberg</a>), &copy; 2009.";
tunedescription[8]="Tension (<a href='http://www.musicalexperiments.com/tangle.php' target='_blank' class='playingnowText' title='Visit Tangle at www.musicalexperiments.com!'>Tangle</a>), &copy; 2009.";
tunedescription[9]="Blueberry Soup (<a href='http://www.musicalexperiments.com/' target='_blank' class='playingnowText' title='Visit Carrilho Jensen Nyberg at www.musicalexperiments.com!'>Carrilho Jensen Nyberg</a>), &copy; 2009.";
tunedescription[10]="Signum (<a href='http://www.musicalexperiments.com/tangle.php' target='_blank' class='playingnowText' title='Visit Tangle at www.musicalexperiments.com!'>Tangle</a>), &copy; 2008.";
tunedescription[11]="Sol (Simon Jensen), &copy; 2001.";
tunedescription[12]="Tango F&ouml;r Resande (N&aring;gon), &copy; 2000.";
tunedescription[13]="Baghdad 2003 (Simon Jensen Band), &copy; 2004.";
tunedescription[14]="Munspelsvalsen (N&aring;gon), &copy; 2000.";
tunedescription[15]="She's My Friend (Patricia Painting), &copy; 1996.";
actualtunenumber=Math.floor(Math.random()*9);

soundManager.onload=function() {
	if(document.getElementById)
		var textbox=document.getElementById('playingnowID');
	else if(document.all)
		var textbox=document.all('playingnowID');
	else if(document.layers)
		var textbox=document.layers['playingnowID'];
	if(textbox)
		textbox.innerHTML='Loading ...';
	SMloaded=true;
	soundManager.createSound({id:actualtune[actualtunenumber],url:'snd/'+actualtune[actualtunenumber]+'.mp3',autoLoad:true,stream:true,autoPlay:true,onload:function(){if(textbox) textbox.innerHTML=(paused==""?"":"(PAUSED) ")+tunedescription[actualtunenumber];},onfinish:function(){playing="";playnewtune();},volume:100});
	soundManager.play(actualtune[actualtunenumber]);
	soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
	loadedtunes+=actualtune[actualtunenumber];
	playedtunes+=actualtune[actualtunenumber];
	playing=actualtune[actualtunenumber];
	paused="";
}

soundManager.onerror=function(){
	SMfailed=true;
	playing="";
	var hidebox;
	var textbox;
	var mbutton;
	var nbutton;
	if (GetParam("reload")!="1" && GetParam("browser")=="sv")
		if (window.location.replace)
			window.location.replace('http://www.simonjensen.com/start.htm?browser=sv&reload=1');
		else if (window.location.href)
			window.location.href="http://www.simonjensen.com/start.htm?browser=sv&reload=1";
		else if (window.location)
			window.location="http://www.simonjensen.com/start.htm?browser=sv&reload=1";
		else if(window.location.assign)
			window.location.assign("http://www.simonjensen.com/start.htm?browser=sv&reload=1");
	else if (GetParam("reload")!="1")
		if (window.location.replace)
			window.location.replace('http://www.simonjensen.com/start.htm?reload=1');
		else if (window.location.href)
			window.location.href="http://www.simonjensen.com/start.htm?reload=1";
		else if (window.location)
			window.location="http://www.simonjensen.com/start.htm?reload=1";
		else if(window.location.assign)
			window.location.assign("http://www.simonjensen.com/start.htm?reload=1");
	if(document.getElementById)
		hidebox=document.getElementById('noflashHide');
	else if(document.all)
		hidebox=document.all('noflashHide');
	else if(document.layers)
		hidebox=document.layers['noflashHide'];
	if (hidebox)
		hidebox.style.display="block";
	else
	{
		if(document.getElementById)
		{
			textbox=document.getElementById('playingnowID');
			mbutton=document.getElementById('musicbutton');
			nbutton=document.getElementById('nextbutton');
		}
		else if(document.all)
		{
			textbox=document.all('playingnowID');
			mbutton=document.all('musicbutton');
			nbutton=document.all('nextbutton');
		}
		else if(document.layers)
		{
			textbox=document.layers['playingnowID'];
			mbutton=document.layers['musicbutton'];
			nbutton=document.layers['nextbutton'];
		}
		if(textbox)
			textbox.innerHTML="The player failed to load. Reloading this page might help!<br />But your browser also needs Shockwave Flash installed.<br />You can get it from the following pages:<br /><a href='http://get.adobe.com/flashplayer/' target='_blank'>http://get.adobe.com/flashplayer/</a><br /><a href='http://get.adobe.com/shockwave/' target='_blank'>http://get.adobe.com/shockwave/</a>";
			textbox.style.paddingLeft="10px";
			textbox.style.borderLeft="solid 1px #333333";
		if(mbutton)
			mbutton.src='img/button_playmusic_INACT.gif'; 
		if(nbutton)
			nbutton.src='img/button_nexttune_INACT.gif';
	}
}

function playnewtune() {
	var listened=0;
	var lastplayednumber=actualtunenumber;
	for (var t=0; t<actualtune.length; t++)
		if(playedtunes.search(actualtune[t])!=-1)
			listened++;
	if(listened<4)
	{
		do {
			actualtunenumber=Math.floor(Math.random()*6);
		} while (playedtunes.search(actualtune[actualtunenumber])!=-1);
	}
	else if(listened<6)
	{
		do {
			actualtunenumber=Math.floor(Math.random()*10);
		} while (playedtunes.search(actualtune[actualtunenumber])!=-1);
	}
	else if(listened<actualtune.length)
	{
		do {
			actualtunenumber=Math.floor(Math.random()*actualtune.length);
		} while (playedtunes.search(actualtune[actualtunenumber])!=-1);
	}
	else
	{
		playedtunes="";
		do {
			actualtunenumber=Math.floor(Math.random()*5);
		} while (actualtunenumber==lastplayednumber);
	}
	playtune(actualtune[actualtunenumber]);
	var mbutton;
	if(document.getElementById)
		mbutton=document.getElementById('musicbutton');
	else if(document.all)
		mbutton=document.all('musicbutton');
	else if(document.layers)
		mbutton=document.layers['musicbutton'];
	if(mbutton)
		mbutton.src='img/button_pausemusic.gif'; 		
}
	
function playtune(tune) {
	var textbox;
	if(SMloaded)
	{
		soundManager.stopAll();
		if(loadedtunes.search(tune)!=-1) {
			soundManager.play(tune);
			soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
			if(document.getElementById)
				textbox=document.getElementById('playingnowID');
			else if(document.all)
				textbox=document.all('playingnowID');
			else if(document.layers)
				textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML=tunedescription[actualtunenumber];
			playedtunes+=tune;
			playing=tune;
			paused="";
		}	
		else {
			if(document.getElementById)
				var textbox=document.getElementById('playingnowID');
			else if(document.all)
				var textbox=document.all('playingnowID');
			else if(document.layers)
				var textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML='Loading ...';
			soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onload:function(){if(textbox) textbox.innerHTML=(paused==""?"":"(PAUSED) ")+tunedescription[actualtunenumber];},onfinish:function() {playing="";playnewtune();},volume:100});
			soundManager.play(tune);
			soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
			loadedtunes+=tune;
			playedtunes+=tune;
			playing=tune;
			paused="";
		}
	}
	else {
		soundManager.onload=function() {
			if(document.getElementById)
				var textbox=document.getElementById('playingnowID');
			else if(document.all)
				var textbox=document.all('playingnowID');
			else if(document.layers)
				var textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML='Loading ...';
			SMloaded=true;
			soundManager.createSound({id:actualtune[actualtunenumber],url:'snd/'+actualtune[actualtunenumber]+'.mp3',autoLoad:true,stream:true,autoPlay:true,onload:function(){if(textbox) textbox.innerHTML='(PAUSED) '+tunedescription[actualtunenumber];},onfinish:function() {playing="";playnewtune();},volume:100});
			soundManager.play(actualtune[actualtunenumber]);
			soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
			loadedtunes+=actualtune[actualtunenumber];
			playedtunes+=actualtune[actualtunenumber];
			playing=actualtune[actualtunenumber];
			paused="";
		}
	}
}

function pausetune(tune) {
	var textbox;
	if(SMloaded)
	{
		if(loadedtunes.search(tune)!=-1)
			if(playing==tune)
			{
				soundManager.pause(tune);
				if(document.getElementById)
					textbox=document.getElementById('playingnowID');
				else if(document.all)
					textbox=document.all('playingnowID');
				else if(document.layers)
					textbox=document.layers['playingnowID'];
				if(textbox)
					textbox.innerHTML="(PAUSED) "+tunedescription[actualtunenumber];
				paused=tune;
				playing="";
			}
			else
			{
				playtune(actualtune[actualtunenumber]);
				soundManager.pause(actualtune[actualtunenumber]);
				if(document.getElementById)
					textbox=document.getElementById('playingnowID');
				else if(document.all)
					textbox=document.all('playingnowID');
				else if(document.layers)
					textbox=document.layers['playingnowID'];
				if(textbox)
					textbox.innerHTML="(PAUSED) "+tunedescription[actualtunenumber];
				playing="";
				paused=actualtune[actualtunenumber];
			}
	}
	else {
		soundManager.onload=function() {
			if(document.getElementById)
				var textbox=document.getElementById('playingnowID');
			else if(document.all)
				var textbox=document.all('playingnowID');
			else if(document.layers)
				var textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML='Loading ...';
			SMloaded=true;
			soundManager.createSound({id:actualtune[actualtunenumber],url:'snd/'+actualtune[actualtunenumber]+'.mp3',autoLoad:true,stream:true,autoPlay:true,onload:function(){if(textbox) textbox.innerHTML='(PAUSED) '+tunedescription[actualtunenumber];},onfinish:function() {playing="";playnewtune();},volume:100});
			soundManager.play(actualtune[actualtunenumber]);
			soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
			soundManager.pause(actualtune[actualtunenumber]);
			loadedtunes+=actualtune[actualtunenumber];
			playedtunes+=actualtune[actualtunenumber];
			playing="";
			paused=actualtune[actualtunenumber];
			if(document.getElementById)
				textbox=document.getElementById('playingnowID');
			else if(document.all)
				textbox=document.all('playingnowID');
			else if(document.layers)
				textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML="(PAUSED) "+tunedescription[actualtunenumber];
		}
	}
}

function resumetune(tune) {
	var textbox;
	if(SMloaded)
		if(loadedtunes.search(tune)!=-1)
			if(paused==tune)
			{
				soundManager.resume(tune);
				if(document.getElementById)
					textbox=document.getElementById('playingnowID');
				else if(document.all)
					textbox=document.all('playingnowID');
				else if(document.layers)
					textbox=document.layers['playingnowID'];
				if(textbox)
					textbox.innerHTML=tunedescription[actualtunenumber];
				playing=tune;
				paused="";
			}
			else
			{
				soundManager.play(tune);
				soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
				if(document.getElementById)
					textbox=document.getElementById('playingnowID');
				else if(document.all)
					textbox=document.all('playingnowID');
				else if(document.layers)
					textbox=document.layers['playingnowID'];
				if(textbox)
					textbox.innerHTML=tunedescription[actualtunenumber];
				playing=tune;
				paused="";
			}	
		else
		{
			soundManager.stopAll();
			if(document.getElementById)
				var textbox=document.getElementById('playingnowID');
			else if(document.all)
				var textbox=document.all('playingnowID');
			else if(document.layers)
				var textbox=document.layers['playingnowID'];
			if(textbox)
				textbox.innerHTML='Loading ...';
			soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onload:function(){if(textbox) textbox.innerHTML='(PAUSED) '+tunedescription[actualtunenumber];},onfinish:function() {playing="";playnewtune();},volume:100});
			soundManager.play(tune);
			soundManager.setVolume(actualtune[actualtunenumber],tunevolume[actualtunenumber]);
			loadedtunes+=tune;
			playedtunes+=tune;
			playing=tune;
			paused="";
		}
}
