/* JavaScript Functions for Speak-A-Message */

var lastFile = "";
var lastLoaded = ""; 

function playSound(aFile) {
  if (-1 == navigator.userAgent.indexOf("MSIE")) {
  	  document.location = aFile; 	
//    playFireFox(aFile); //for some reaon does not work on Vista
  } else {
	  player = document.getElementById("mediaplayer");
	  if (aFile == lastFile) {
	   player.controls.stop();
	    lastFile = "";
	  } else {
	    lastFile = aFile;
	    player.URL = decode_utf8(aFile);
	    player.controls.play(); 
	  }
  }
}

function getFlashAudioPlayer() {
		  if (window.document['audioplayer']) {
			return window.document['audioplayer'];
		  }
		  if (navigator.appName.indexOf("Microsoft Internet")==-1)
		  {
			if (document.embeds && document.embeds[audioplayer])
			  return document.embeds['audioplayer']; 
		  } else  {
			return document.getElementById('audioplayer');
		  }
}

function mp3extension(aFile) {
 if (aFile) {
 	if (aFile.toLowerCase().lastIndexOf('.mp3') != aFile.length-4) {
 	   return aFile.substr(0,aFile.length-4)+'.mp3';
 	}
 } 
 return aFile; 
}

function playSound2(aFile) {
  try {
  	player = getFlashAudioPlayer();
  	if (aFile == lastFile) {
	  	player.stopAudio();
	  	lastFile = '';
  	} else {
	  	if (lastLoaded == aFile) {
	  	 player.playAudio();
	  	} else { 
	  		player.loadFile(mp3extension(aFile));
	  		player.setLoop('false');
	  	}
	  	lastFile = aFile; 
	  	lastLoaded = aFile; 
  	} 
  } catch (e) {
    alert('Unable to play audio file. Please check whether the Flash Player is installed and make sure that your firewall does not prevent playback of audio files. '); 
  }
}

function playFireFox(aFile) {
 var ele = document.getElementById("fireplay"); 
 if (ele) {
    var s = "<object type=\"application/x-mplayer2\" height=\"100\" width=\"200\"><param name=\"fileName\" value=\"";
    ele.innerHTML = s + "http://www.speak-a-message.com/" + aFile +  "\"><param name=\"autostart\" value=\"1\"><param name=\"playcount\" value=\"1\"></object>"
 }
}

function deleteSound(aFile) {
  document.location="http://www.speak-a-message.com/selectcommand.php?cmd=deletesound&file="+aFile;
}

function selectSound(aFile) {
  document.location="http://www.speak-a-message.com/selectsound.php?file="+aFile;
}

function selectSoundExternal(aFile) {
  document.location="http://www.speak-a-message.com/selectsound.php?url=http://www.speak-a-message.com/"+aFile;
}

function deleteSkin(skinId) {
  document.location="http://www.speak-a-message.com/selectcommand.php?cmd=deleteskin&skinid="+skinId;
}

function selectSkin(skinId) {
  document.location="http://www.speak-a-message.com/selectskin.php?skinid="+skinId;
}

function decode_utf8(utftext) {
  var plaintext = ""; var i=0; var c=c1=c2=0; 
  while(i<utftext.length) {
    c = utftext.charCodeAt(i);
    if (c<128) {
        plaintext += String.fromCharCode(c); 
        i++;}
    else if((c>191) && (c<224)) {
        c2 = utftext.charCodeAt(i+1);
        plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
        i+=2;}
     else {
       c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
       plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63)); 
      i+=3;}
     }
   return plaintext;
}

function jsGoHref(nodeOrUrl) {
	if (nodeOrUrl != null) {
	   if (typeof(nodeOrUrl) == "object") {	
		   childs = nodeOrUrl.getElementsByTagName('a'); 
		   for (var i=0; i<childs.length;i++) {
			   child = childs[i];
			   if (child.href) {
				   document.location = child.href; 
				   break; 
			   }
		   }
	   } else if (typeof(nodeOrUrl) == "string") {
		   document.location = nodeOrUrl;
	   }
	}
	return false;	 
}


/** Obfuscator for electronic mail */
function co42(param1,param2) {document.location.href = 'mai'+'lto:'+param1+'@'+param2;}
function co43(){return '@';}