var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function playaudio(what){
	if (OS == 'Mac'){
		if (what.indexOf(":") != -1 || what.substring(0,1) == '/'){
			// no changes
		} else {
			var URL = unescape(location.href);
			var mstart = URL.lastIndexOf("/") + 1;
			var mpath = URL.substring(0,mstart);
			what = mpath + what;
		}
		if (document.AudioPlayer && document.AudioPlayer.Stop) { // can control quicktime
			document.AudioPlayer.Stop();
			document.AudioPlayer.SetURL(what);
			document.AudioPlayer.Play();
			initButton();
		} else { // cannot control quicktime
			window.open(what,'media','width=200,height=50,status=no,resizable=no');
		}
	} else {
		if (what.substring(0,1) == '/') {
			what = window.location.protocol + '//' + window.location.host + what;
		}
		document.getElementById('AudioPlayerSpan').innerHTML='<object id="AudioPlayer" width="1" height="1" '
	      +'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '
	      +'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" '
	      +'standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">'
	      +'<param name="url" value="'+what+'">'
	      +'<param name="uiMode" value="full">'
	      +'<param name="autoStart" value="true">'
		  +'<param name="loop" value="true">'
	      +'<embed name="AudioPlayer" type="application/x-mplayer2" '
	      +'pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" '
	      +'showcontrols="true" uimode="full" width="1" height="1" '
	      +'src="'+what+'" autostart="true" loop="true">'
		+'<\/object>';
		if (document.AudioPlayer.controls) {
			initButton();
		}
	}
}

function stopaudio() {
	if (OS == 'Mac'){
		document.AudioPlayer.Stop();
	} else {
		document.getElementById('AudioPlayerSpan').innerHTML= '';
	}
	document.getElementById('topButton').style.visibility = 'hidden';
}

function floatButton () {
 if (OS == 'Mac'){ // Quicktime
	if (document.AudioPlayer.GetTime() == document.AudioPlayer.GetEndTime() && document.AudioPlayer.GetTime() != 0) stopaudio();
 } else if (document.AudioPlayer && document.AudioPlayer.controls) { // can control Media Player
	if (document.AudioPlayer.status == 'Stopped') stopaudio();
 }
 if (document.all) {
  document.all.topButton.style.pixelTop = document.body.scrollTop;
  document.all.topButton.style.pixelLeft = document.body.clientWidth - document.all.topButton.offsetWidth;
 } else if (document.layers) {
  document.topButton.top = window.pageYOffset;
  document.topButton.left = window.innerWidth - document.topButton.clip.width - 15;
 } else if (document.getElementById) {
  document.getElementById('topButton').style.left = (window.innerWidth - 85) + 'px';
  document.getElementById('topButton').style.top = window.pageYOffset + 'px';
 }
}

function initButton () {
 setInterval ('floatButton();', 100);
 document.body.onresize = floatButton;
 if (document.all) {
  document.all.topButton.style.pixelLeft = document.body.clientWidth - document.all.topButton.offsetWidth;
  document.all.topButton.style.visibility = 'visible';
 } else if (document.layers) {
  document.topButton.left = window.innerWidth - document.topButton.clip.width - 15;
  document.topButton.visibility = 'show';
 } else if (document.getElementById) {
  document.getElementById('topButton').style.left = (window.innerWidth - 85) + 'px';
  document.getElementById('topButton').style.visibility = 'visible';
 }
}

function addAudioElements() {
	// Prepare Audio Elements
	if (OS == 'Mac'){
		document.write('<span id="AudioPlayerSpan" style="position:absolute; top:0px; right:0px;"><OBJECT classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="1" height="1" id="AudioPlayer" ><PARAM name="src" value=""><EMBED width="1" height="1" src="" name="AudioPlayer" enablejavascript="true"></EMBED></OBJECT></span>');
	} else {
		document.write('<span id="AudioPlayerSpan" style="position:absolute; top:0px; right:0px;"></span>');
	}
	document.write('<SPAN ID="topButton" STYLE="position: absolute; visibility: hidden; top: 0px; white-space: nowrap; cursor: pointer; color: #ffffff; background-color: #FF0000;" onmouseup="javascript:stopaudio();"">&nbsp;Stop Audio&nbsp;</SPAN>');
}
