soundManager.url = '/soundmanager/soundmanager3.swf'; // set URL to load (overriding default)
soundManager.debugMode = false;
soundManager.consoleOnly = false;
soundManager.onload = function() { LIFEWAY.worship.previewplayer.setReadyToGo(true); };

LIFEWAY.worship.previewplayer = new function(){
	
		var readyToGo = false;
		var mediaAssets;
		var storedVersionId;
		var playing = false;
		var fullSong = false;
		var storedDownloadUrl = "";
		
		return {			
			/* Invoked from client code to play the preview and make ajax call to 
			 * retrieve the preview url.  If the version id coming in is different from
			 * the previous one - shut off any earlier sound and toggle it to 
			 */
			playPreview : function(songVersionId){
				if(playing) {
					LIFEWAY.worship.previewplayer.stopSong(storedVersionId);
				}
				
				storedVersionId = songVersionId;
				fullSong = false;			
				
				var url = "/digitalAsset/previewUrl/"+songVersionId;
				sendAjaxRequest(url, 'POST', true, null, playPreviewSuccess, playPreviewFailure);
			},
			
			playFullSong : function(songVersionId, downloadUrl) {
				if(playing) {
					LIFEWAY.worship.previewplayer.stopSong(storedVersionId);
				}
				
				storedVersionId = songVersionId;
				fullSong = true;
				storedDownloadUrl = downloadUrl; 
				
				if(LIFEWAY.worship.previewplayer.loadMedia(downloadUrl)) {
					toggleImage("audioBtn"+storedVersionId, "/images/", "preview-loader.gif", "spin");
					LIFEWAY.worship.previewplayer.playPreviewSong();
				}
			},
						
			setReadyToGo : function(ready){
				readyToGo = ready;
			},
			
			toggleImage : function(elementId, imagePath, imageValue, altValue) {
				var imageLocation = imagePath + imageValue;
				$(elementId).src = imageLocation;
				$(elementId).alt = altValue;
			},
			
			/*
		 	 * Make sure the song file is loaded before calling the play method
		     */
			playPreviewSong : function() {
				if (!isLoaded()){
					setTimeout("LIFEWAY.worship.previewplayer.playPreviewSong()",100);					
					return;
				}
				
				soundManager.play(storedVersionId);
				playing = true;
				toggleCurrentButton("play", storedVersionId);
			},
		
			/*
			 * Stop the music file from playing
			 */
			stopSong : function(versionId){
				soundManager.stopAll();
				playing = false;
				toggleCurrentButton("stop", versionId);
			},
		
			whileLoading : function() {
				toggleImage(storedVersionId, "/images/", "preview-loader.gif", "spin");
			},
			
			hideLoading : function() {
				LIFEWAY.worship.previewplayer.toggleImage(' + storedVersionId + ', "/images/plan/", "stop.gif", "stop");
			},
			
			toggleBackToPlay : function() {
				toggleCurrentButton("stop", storedVersionId);
			},
			
			//	
			// recurse until soundmanager is available - recurse until its loaded - then
			// create the sound with the id as the storedVersionId
			//
			loadMedia : function(mediaUrl) {
				if (!isSoundManagerAvailable()){
					setTimeout("LIFEWAY.worship.previewplayer.loadMedia('" + mediaUrl + "')",1000);
					return false;
				}
				if(mediaUrl) {
					soundManager.createSound(
						{
						  	id: storedVersionId,
						  	url: mediaUrl,
						  	autoLoad: true,
						  	autoPlay: false,
							//multiShot: false,
							//whileloading: LIFEWAY.worship.previewplayer.whileLoading,
							onload: LIFEWAY.worship.previewplayer.hideLoading,
						  	onjustbeforefinishtime: 175,
							onfinish: LIFEWAY.worship.previewplayer.toggleBackToPlay
						}
					)
					return true;
				} else {
					return false;
				}
			}
		};
		
		/*
		 * Method invoked after making ajax to retrieve the preview url
		 */
		function playPreviewSuccess(oReq) {
			var json = eval( '(' + oReq.responseText + ')' );
			if(LIFEWAY.worship.previewplayer.loadMedia(json.MediaUrlDTOImpl.contentUrl)) {
				toggleImage("audioBtn"+storedVersionId, "/images/", "preview-loader.gif", "spin");
				LIFEWAY.worship.previewplayer.playPreviewSong();
			} else {
				//alert("bad")
			}
		};

		/*
		 * Method invoked when failure of ajax call
		 */
		function playPreviewFailure(oReq) {
		      LIFEWAY.common.displayErrorMessage(oReq.responseText);
		};

		/*
		 * Toggles the current image element to a play or stop image and resets the onclick
		 * event on it as well.
		 */
		function toggleCurrentButton(mode, currentVersionId){
			var assetButtonId = "audioBtn"+currentVersionId;		
			if(mode == "play") {
				toggleClassName(assetButtonId, "audioBtnPlay", "audioBtnStop");
				toggleImage(assetButtonId, "/images/", "preview-loader.gif", "stop");
				$(assetButtonId).onclick = function onclick(event) { LIFEWAY.worship.previewplayer.stopSong(currentVersionId) };
				$(assetButtonId).onmouseover = function onmouseover(event) { LIFEWAY.worship.previewplayer.toggleImage(assetButtonId, "/images/plan/", "stop.gif", "stop"); };
				$(assetButtonId).onmouseout = function onmouseout(event) { LIFEWAY.worship.previewplayer.toggleImage(assetButtonId, "/images/", "preview-loader.gif", "stop"); };			
			} else {
				toggleClassName(assetButtonId, "audioBtnStop", "audioBtnPlay");
				toggleImage(assetButtonId, "/images/", "play.gif", "play");
				
				if (fullSong) {
					$(assetButtonId).onclick = function onclick(event) { LIFEWAY.worship.previewplayer.playFullSong(currentVersionId, storedDownloadUrl) };				
				} else {
					$(assetButtonId).onclick = function onclick(event) { LIFEWAY.worship.previewplayer.playPreview(currentVersionId) };				
				}
								
				$(assetButtonId).onmouseover = null;
				$(assetButtonId).onmouseout = null;			
			}
		};
		
		function toggleClassName(elementId, existingClassName, newClassName) {
			$(elementId).removeClassName(existingClassName);				
			$(elementId).addClassName(newClassName);
		}; 	

		function toggleImage(elementId, imagePath, imageValue, altValue) {
			var imageLocation = imagePath + imageValue;
			$(elementId).src = imageLocation;
			$(elementId).alt = altValue;
		
		};
			
		//
		// this method will check if the sound has actually been loaded into the soundmanager
		// and if not to invoke loadMedia().  Then if the readyState of the song is not 3
		// then return false else return true
		//
		function isLoaded(){
			var done = false;
			var song = soundManager.getSoundById(storedVersionId);
			if (song){
				if(song.readyState == 3) {
					done = true;
				}
			} else {
				var url = "/digitalAsset/previewUrl/"+songVersionId;
				sendAjaxRequest(url, 'POST', true, "mediaType=1", playPreviewSuccess, playPreviewFailure);
			}
			return done;
		};

		//this is whether soundmanager has been loaded
		function isSoundManagerAvailable(){
			return readyToGo;
		}; 
};