var mySound = new soundManager();

// todox do something with these
function sound_LoadComplete(){
   document.fire("sound:loadcomplete");
}
function sound_Complete(){
   document.fire("sound:complete");
}
function io_Error(){
   document.fire("io:error");
}
function sound_Open(){
   document.fire("sound:open");
}
function sound_Progress(percentLoaded){
   document.fire("sound:progress", { 'percentLoaded':percentLoaded });
}

function soundManager(){
	var oThis = this;
	var flexApp;
	var flexSound;

	FABridge.addInitializationCallback("rhadio", function() {
	   flexApp = FABridge.rhadio.root();
	   flexSound = flexApp.getMySound();
	   document.fire("flash:loaded");
	}); 
   
   // Public
   this.playAudio = function(url){
      if (!flexSound) return;
      document.fire("sound:preopen");
      flexSound.playAudio(url);
   }
   this.playPause = function(){
      if (!flexSound) return;
		
		if (flexSound.hasAudio()){
			flexSound.isPlaying() ? flexSound.pause() : flexSound.unpause();
			document.fire("sound:playpause", { isPlaying:flexSound.isPlaying() });
		}
   }
   this.stop = function(){
      flexSound.stop();
      document.fire("sound:stopped");
   }
   this.setVolume = function(v){
      if (!flexSound) return;
      
      flexSound.setVolume(v);  
   }
   this.getPosition = function(){
      return flexSound.getPosition();
   }
   this.setPosition = function(percent){
      flexSound.setPosition(percent);
      
      if (!oThis.isPlaying())
         oThis.playPause();
   }
   this.estimatedLength = function(){
      return flexSound.estimatedLength();
   }
   this.computeSpectrum = function(FFTMode, stretchFactor){
      return flexSound.computeSpectrum(FFTMode, stretchFactor);
   }
   this.isPlaying = function(){
      return flexSound.isPlaying();
   }
}
