var SMfailed=false;
var SMloaded=false;
var loadedtunes="";
var playing="";
var paused="";
var failwindow;

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

soundManager.onload=function() {
	SMloaded=true;
}

soundManager.onerror=function() {
	SMfailed=true;
}

function playtune(tune) {
	if(SMloaded) {
		soundManager.stopAll();
		if(loadedtunes.search(tune)!=-1) {
			soundManager.play(tune);
			playing=tune;
			paused="";
		}	
		else {
			soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onfinish:function() {playing="";onmout(tune)},volume:100});
			soundManager.play(tune);
			loadedtunes+=tune;
			playing=tune;
			paused="";
		}
	}
	else {
		soundManager.onload=function() {
			soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onfinish:function() {playing="";onmout(tune)},volume:100});
			soundManager.play(tune);
			SMloaded=true;
			loadedtunes+=tune;
			playing=tune;
			paused="";
		}
	}
}

function pausetune(tune) {
	if(SMloaded) {
		if(loadedtunes.search(tune)!=-1)
			if(playing==tune) {
				soundManager.pause(tune);
				paused=tune;
				playing="";
			}
			else {
				playing="";
				paused="";
			}
	}
	else {
		soundManager.onload=function() {
			SMloaded=true;
			playing="";
			paused="";
		}
	}
}

function resumetune(tune) {
	if(SMloaded)
		if(loadedtunes.search(tune)!=-1)
			if(paused==tune) {
				soundManager.resume(tune);
				playing=tune;
				paused="";
			}
			else {
				soundManager.play(tune);
				playing=tune;
				paused="";
			}	
		else {
			soundManager.stopAll();
			soundManager.createSound(tune,"snd/"+tune+".mp3");
			soundManager.play(tune);
			loadedtunes+=tune;
			playing=tune;
			paused="";
		}
	else {
		soundManager.onload=function() {
			soundManager.createSound(tune,"snd/"+tune+".mp3");
			soundManager.play(tune);
			SMloaded=true;
			loadedtunes+=tune;	
			playing=tune;
			paused="";
		}
	}
}

function stoptunes() {
	if(SMloaded) {
		soundManager.stopAll();
		playing="";
		paused="";
	}
	else {
		soundManager.onload=function() {
			SMloaded=true;
			playing="";
			paused="";
		}
	}
}

function onmov(tune) {
	var ovbutton;
	if(document.all)
		ovbutton = document.all(tune);
	else if(document.getElementById)
		ovbutton = document.getElementById(tune);
	else if(document.layers)
		ovbutton = document.layers[tune];
	if(ovbutton)
		if(playing==tune)
			ovbutton.src='img/button_pause_mo.gif';
		else
			ovbutton.src='img/button_play_mo.gif';
}

function onmout(tune) {
	var oubutton;
	if(document.all)
		oubutton = document.all(tune);
	else if(document.getElementById)
		oubutton = document.getElementById(tune);
	else if(document.layers)
		oubutton = document.layers[tune];
	if(oubutton)
		if(playing==tune)
			oubutton.src='img/button_pause.gif'
		else
			oubutton.src='img/button_play.gif';
}

function downlclick() {
	var cbutton;
	var ptune;
	if(!SMfailed) {
		if(document.all) {
			if(playing!="")
				ptune=document.all(playing);}
		else if(document.getElementById) {
			if(playing!="")
				ptune=document.getElementById(playing);}
		else if(document.layers) {
			if(playing!="")
				ptune = document.layers[playing];}
		if(ptune)
			if(playing!="")
			{
				ptune.src='img/button_play.gif';
				stoptunes();
			}
	}
}

function onc(tune) {
	var cbutton;
	var ptune;
	if(!SMfailed) {
		if(document.all) {
			cbutton=document.all(tune);
			if(playing!="") ptune=document.all(playing);}
		else if(document.getElementById) {
			cbutton=document.getElementById(tune);
			if(playing!="") ptune=document.getElementById(playing);}
		else if(document.layers) {
			cbutton=document.layers[tune];
			if(playing!="") ptune = document.layers[playing];}
		if(cbutton) {
			if(playing==tune) {
				pausetune(tune);
				cbutton.src='img/button_play_mo.gif';
			}
			else if(playing=="" && paused==tune) {
				resumetune(tune);
				cbutton.src='img/button_pause_mo.gif';
			}
			else
			{
				if(ptune) {
					ptune.src='img/button_play.gif';
					soundManager.stop(playing);
				}
				playtune(tune);
				cbutton.src='img/button_pause_mo.gif';
			}
		}
		else
			if(playing==tune)
				pausetune(tune);
			else if(playing=="" && paused==tune)
				resumetune(tune);
			else
			{
				if(playing!="")
					soundManager.stop(playing);
				playtune(tune);
			}
	}
	else {
		if(failwindow)
			failwindow.close();
		failwindow=window.open("snd/"+tune+".mp3");
	}
}

function textclick(tune) {
	var tuneobj;
	var playingobj;
	if(!SMfailed) {
		if(document.all) {
			tuneobj=document.all(tune);
			if(playing!="") playingobj=document.all(playing);}
		else if(document.getElementById) {
			tuneobj=document.getElementById(tune);
			if(playing!="") playingobj=document.getElementById(playing);}
		else if(document.layers) {
			tuneobj=document.layers[tune];
			if(playing!="") playingobj=document.layers[playing];}
		if(playing==tune) {
			pausetune(tune);
			if(tuneobj) {tuneobj.title="Play";tuneobj.style.color="#ffffff";}
		}
		else if(paused==tune) {
			resumetune(tune);
			if(tuneobj) {tuneobj.title="Pause";tuneobj.style.color="#009900";}
		}
		else {
			if(SMloaded) {
				soundManager.stopAll();
				if(loadedtunes.search(tune)!=-1) {
					soundManager.play(tune);
					if(tuneobj) {tuneobj.title="Pause";tuneobj.style.color="#009900";}
					if(playing!="") 
						if(playingobj) {playingobj.title="Play";playingobj.style.color="#ffffff";}
					playing=tune;
					paused="";
				}	
				else {
					soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onfinish:function() {playing="";if(tuneobj) {tuneobj.title="Play";if(tuneobj.style.color!="")tuneobj.style.color="#ffffff";}},volume:100});
					soundManager.play(tune);
					loadedtunes+=tune;
					if(tuneobj) {tuneobj.title="Pause";tuneobj.style.color="#009900";}
					if(playing!="")
						if(playingobj) {playingobj.title="Play";playingobj.style.color="#ffffff";}
					playing=tune;
					paused="";
				}
			}
			else {
				soundManager.onload=function() {
					soundManager.createSound({id:tune,url:'snd/'+tune+'.mp3',autoLoad:true,stream:true,autoPlay:true,onfinish:function() {playing="";if(tuneobj) {tuneobj.title="Play";if(tuneobj.style.color=="")tuneobj.style.color="#ffffff";}},volume:100});
					soundManager.play(tune);
					SMloaded=true;
					loadedtunes+=tune;
					if(tuneobj) {tuneobj.title="Pause";tuneobj.style.color="#009900";}
					if(playing!="")
						if(playingobj) {playingobj.title="Play";playingobj.style.color="#ffffff";}
					playing=tune;
					paused="";
				}
			}	
		}
	}
	else {
		if(failwindow)
			failwindow.close();
		failwindow=window.open("snd/"+tune+".mp3");
	}
}