var DUMAS = jQuery.extend( DUMAS,{});

var getPlayerVersion = deconcept.SWFObjectUtil.getPlayerVersion;

(function(){

	var samplePlayerEnabled = false, swfURL, TARGET_VERSION = 9, playingHrefId, isPlaying = false, playAll = false, playerInstance;

	function enableInlineFlash(swf) {
		samplePlayerEnabled = true;
		swfURL = swf;
	};
	
	function toLog(msg){
//		console.log(msg)
	};

	function toError(msg){
		alert(msg);
	};

	function initPlayer(){
		if($.browser.opera){
			window.setTimeout( function(){
				registerFlash();
			}, 500)

		} else {
			registerFlash();
		};
	};

	function registerFlash(){
		if ( getPlayerVersion().major >= TARGET_VERSION && location.hostname.substring(0, 3) != "wmp" && samplePlayerEnabled){
			var playerObj = new SWFObject( swfURL, "player", "1", "1", "9", "#363634", false);
			$('body').prepend("<div id='soundSamplePlayer' style='position:absolute;top:-9000px'></div>");
			$("a[href^='/samples/m3u/album/']").attr('title', 'Listen to this album');
			$("a[href^='/samples/m3u/song/']").attr('title', 'Listen to this song');
			$("a[href^='/samples/m3u/book/']").attr('title', 'Listen to this book');
			playerObj.write("soundSamplePlayer");
			initEventHandlers();
			drawPlayerControl();
			playerInstance = document.getElementById("player");
		};
	};

	function initEventHandlers(root){
		$("a:not(.samplePlayClick)[href$='.m3u']", root)
			.unbind('click.samplePlay')
			.addClass('samplePlayClick')
			.bind('click.samplePlay', function(){
				var albHref = $(this).parents('div.r').find('a.listenAll').attr('href');
				triggerPlayer( $(this).attr('href'), albHref);
				return false
			});
		$("a:not(.samplePlayDbl)[href^='/samples/m3u/album/']", root)
			.addClass('samplePlayDbl')
			.bind('dblclick.samplePlay', function(){
				try{
					playerInstance.toFlashNextSample();
				} catch(e){}
					return false
			});

		$("a:not(.sampleHover)[href$='.m3u']", root)
			.addClass('samplePlayHover')
			.hover(function (){
				if($(this).children("img").attr("src") != '/images/samplePlayer/playThrob.gif' && ($(this).children("img").attr("src") != '/images/samplePlayer/pause.gif')) {
						$(this).children('img').attr('src', '/images/samplePlayer/playOver.gif');
					};
				}, function (){
				if($(this).children("img").attr("src") != '/images/samplePlayer/playThrob.gif' && ($(this).children("img").attr("src") != '/images/samplePlayer/pause.gif')) {
					$(this).children('img').attr('src', '/images/samplePlayer/play.gif');
				}
			});

	};

	function triggerPlayer(trackHref, albHref){
			try{
				playerInstance.toFlashTriggerPlayer(trackHref, albHref, playAll );
			} catch(e){
		}
	};

	function nextSample(){
		try{
			playerInstance.toFlashNextSample();
		} catch(e){}

	};

	function prevSample(){
		try{
			playerInstance.toFlashPrevSample();
		} catch(e){}
	};

	function flashStatus(playStatus, hrefId, pAll, albumHref ){
		isPlaying = playStatus;
		playingHrefId = hrefId;
		playAll = pAll;
		 if( $('.playerControl').length){ // if an album
			if (isPlaying == 'true'){
				turnOnPlayingIcon(hrefId);
				turnOnPlayAllPlaying(albumHref);
			} else if (isPlaying == 'false'){

				turnOnPauseIcon(hrefId);
				turnOnPlayAllPause(albumHref, pAll);
			} else {
				resetAllIcons();
				resetPlayAll();
			};
		} else {
			 hrefId = albumHref.substring(0, albumHref.length-4);
			 if (isPlaying == 'true' && (hrefId != "")){
				turnOnPlayingIcon(hrefId);
			} else if (isPlaying == 'false'){
				turnOnPauseIcon(hrefId);
			} else {
				resetAllIcons();
			};
			//homepage
			if( $('#currentAlbumMeta').length){
				if (isPlaying == 'true'){
					turnOnPlayAllPlaying(albumHref);
				} else if (isPlaying == 'false'){
					turnOnPlayAllPause(albumHref, pAll);
				} else {
					resetPlayAll();
				}
			};
		 };
	};

	function turnOnPlayIcon(theHrefId){
		$("a[href*="+theHrefId+".m3u]").children('img').attr('src', '/images/samplePlayer/play.gif');
	};

	function turnOnPlayingIcon(theHrefId){
		resetAllIcons();
		//color playing text
		if( $("body[id='previewPage']").length){
			$("a[href*="+theHrefId+".m3u]").parent().addClass('selectedSong');
		} else {
			$("a[href*="+theHrefId+".m3u]").parents('td.listen').next().children('p.songName').addClass('selectedSong');
		}
		//turn on the correct one
		$("a[href*="+theHrefId+".m3u]").children('img').attr('src', '/images/samplePlayer/playThrob.gif');
		// and give it the appro tooltip
		setTooltip('Pause this', theHrefId);

		$("img[src='/images/samplePlayer/playThrob.gif']").hover(
			function(){
				if( $(this).attr('src') == '/images/samplePlayer/playThrob.gif'){
					$(this).attr('src', '/images/samplePlayer/pause.gif');
				}
			},
			function(){
				if( $(this).attr('src') == '/images/samplePlayer/pause.gif'){
					$(this).attr('src', '/images/samplePlayer/playThrob.gif');
				}
		});
	};

	function turnOnPauseIcon(theHrefId){
		resetAllIcons();
		//turn on the correct play/pause icon -not implemented now

		if( $("body[id='previewPage']").length){
			$("a[href*="+theHrefId+".m3u]").parent().addClass('selectedSong');
		} else {
			$("a[href*="+theHrefId+".m3u]").parents('td.listen').next().children('p.songName').addClass('selectedSong');
		}
		$("a[href*="+theHrefId+".m3u]").children('img').attr('src', '/images/samplePlayer/play.gif');
		//tootip
		setTooltip('Keep playing this', theHrefId);
	};

	function resetAllIcons(){
		//turn off all pause icons and reset tooltip and reset color
		$('p.songName, li').removeClass('selectedSong');
		$("img[src$='playThrob.gif']").attr('src', '/images/samplePlayer/play.gif');
		$("img[src$='pause.gif']").attr('src', '/images/samplePlayer/play.gif');
		setTooltip('reset');
	};

	function turnOnPlayAllPlaying(albHref){
		//turns on the pause btn
		var discTarget = $("a.listenAll[href="+albHref+"]");
		discTarget.addClass('pauseAll');
		discTarget.attr('title','Pause the current song');
	};

	function turnOnPlayAllPause(albHref, playAll){
		//turns on the play btn, checks to see if alb is playing to display correct tooltip
		var discTarget = $("a.listenAll[href="+albHref+"]");
		discTarget.removeClass('pauseAll');
		if(playAll == true){
			discTarget.attr('title','Keep playing the current song');
		} else {
			discTarget.attr('title','Listen to all songs on this album');
		}
	};

	function resetPlayAll(){
		$('a.listenAll').removeClass('pauseAll');
		$('a.listenAll').attr('name', 'Listen to all songs on this album');
	};

	function setTooltip(action, hrefId){
		if( hrefId && action != 'reset'){
			if( hrefId.indexOf('album') !=-1){
				$("a[href*="+hrefId+".m3u]").attr('title', action+' album');
				$("a[href*="+hrefId+".m3u]").children('img').attr('alt', action+' album');
			} else if( hrefId.indexOf('book') !=-1){
				$("a[href*="+hrefId+".m3u]").attr('title', action+' book');
				$("a[href*="+hrefId+".m3u]").children('img').attr('alt', action+' book');
			} else {
				$("a[href*="+hrefId+".m3u]").attr('title', action+' song');
				$("a[href*="+hrefId+".m3u]").children('img').attr('alt', action+' song');
			}
		} else if (action == 'reset') {
			//reset tooltips
			var songTarget = $("a[href^='/samples/m3u/song/']");
			var albTarget = $("a[href^='/samples/m3u/album/']");
			var bookTarget = $("a[href^='/samples/m3u/book/']");
			songTarget.attr('title', 'Listen to this song');
			songTarget.children('img').attr('alt','Listen to this song');
			albTarget.attr('title', 'Listen to this album');
			albTarget.children('img').attr('alt','Listen to this album');
			bookTarget.attr('title', 'Listen to this book');
			bookTarget.children('img').attr('alt','Listen to this book');
		};
	};

	function drawPlayerControl(){
		$('div.playerControl div.controls').css('width','88px');
		var prevSpan = "<span class='prev'><a href='#' class='prevLink' title='Listen to previous song'></a></span>"
		var nextSpan = "<span class='next'><a href='#' class='nextLink' title='Listen to next song'></a></span>"
		$("a.listenAll").each(function(){
			$(this).parents('span').before(prevSpan);
			$(this).parents('span').after(nextSpan);
		});

		$('span.next a.nextLink').click( function(){
			nextSample();
			return false;
		});
		$('span.prev a.prevLink').click( function(){
			prevSample();
			return false;
		});
	};

	function reset(){
		if(playerInstance && playerInstance.toFlashReset){
			playerInstance.toFlashReset();
		};
	};
	//public methods
	DUMAS.EMUSIC_PLAYER = {
		init: function(root){initEventHandlers(root)},
		enable: function(swfURL){enableInlineFlash(swfURL)},
		nextSample: function(){nextSample() },
		resetAllIcons: function(){ resetAllIcons() },
		resetPlayAll: function(){ resetPlayAll() },
		toLog: function(msg) {toLog(msg)},
		toError: function(msg){ toError(msg)},
		stop: function(){ reset()},
		flashStatus: function( isPlaying, currentId, playAll, albumHref){ flashStatus(isPlaying, currentId, playAll, albumHref)}
	}


 	$(function() {
			initPlayer();
	});


})();
