$(document).ready(function()
{
	var otherID = '';
	var fv = '';
	var audioPath ='/_audio/';
	var audioSrc = '';
	var audioTitle ='';
	var thumbPath = '/_images/thumbs/';
	var thumbSrc ='';
	var imagePath = '/_images/';
	var imageSrc = '';
	var timer;

	/**
	* @description  Checks if target element is an object or embed element	
	*
	* @param 	targetID - the id to check
	*/			
	function isObject(targetID)
	{
		var isFound = false;
		var el = document.getElementById(targetID);

		if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED"))
		{
			isFound = true;
		}
		return isFound;
	}
	 
	/**
	* @description  Creates an empty div to replace embedded SWF object
	*
	* @param 	targetID - the ID of the div to replace
	*/		
	function replaceSwfWithEmptyDiv(targetID)
	{
		var el = document.getElementById(targetID);

		if($(el))
		{
			var div = document.createElement("div");
			el.parentNode.insertBefore(div, el);	
			swfobject.removeSWF(targetID);
			div.setAttribute("id", targetID);
			
		}
	}
	
	/**
	* @description Loads a swf
	*
	* @param url - the swf to load
	*
	* @param targetID - the placeholder	
	*/	 
	function loadSWF(targetID)
	{ 
		var $audioElement   = $('#player');
		var $playerWrapper = $('#playerWrapper');
		
		if(targetID == "x")
		{
			otherID = "y";
			audioTitle ='Sand Boogie';
			audioSrc = audioPath + 'x_sand_boogie';
			thumbSrc = thumbPath + 'x.jpg';
			imageSrc = imagePath + 'x_sand_boogie.jpg';
			fv = 'src=' + audioSrc  +'.mp3&img=' + imageSrc + '&thumb=' + thumbSrc + '&title=' + audioTitle;
		}
		else
		{
			otherID = "x";
			audioSrc = audioPath + 'y_rymdskepp';
			audioTitle ='Perfect Temperature for Leaving Home, Pt 2';
			thumbSrc = thumbPath + 'y.jpg';
			imageSrc = imagePath + 'y_rymdskepp.jpg';
			fv = 'src=' + audioSrc  +'.mp3&img=' + imageSrc + '&thumb=' + thumbSrc + '&title=' + audioTitle;
		}
	 
		if(isObject(otherID))
		{
			replaceSwfWithEmptyDiv(otherID);
		}
	 
	   if (swfobject.hasFlashPlayerVersion("10")) 
	   {
			var swf = { data: "/MP3Player.swf", width:"410", height:"485" };
			var vars = { flashvars:fv , id:"MP3Player", name: "MP3Player", allowscriptaccess: "always", bgcolor: "#FFFFFF", wmode: "transparent" };
			var obj = swfobject.createSWF(swf, vars, targetID);
	   }
	   else
	   {
		   	if(Modernizr.audio)
			{
				if($audioElement.length)
				{
					//alert('player found');
					$('#' + otherID).unbind('mouseover');
					$audioElement.get(0).pause();
					$audioElement.remove();
					$playerWrapper.remove();
					$('#' + otherID).hide();
				}	
					
				$playerWrapper = createPlayerWrapper(audioTitle, thumbSrc);
				$('#' + targetID).prepend($playerWrapper);										
				$audioElement =  createNewAudio();
				$($playerWrapper).prepend($audioElement);
				$('#' + targetID).fadeIn('slow');
				//alert(targetID);
				
				/**
				* @description Timer for fade-in / fade-out for audio object wrapper
				*	
				*/	
				$('#' + targetID).bind('mousemove', function() 
				{
					if (timer) 
					{
						clearTimeout(timer);
						timer = 0;
					}
					
					$('#playerWrapper').fadeIn('fast');
					timer = setTimeout(function() 
					{
						$('#playerWrapper').fadeOut('slow')
					}
					, 2500)
				})			
			}
			else
			{			   
				$('#' + targetID).prepend('<div id="errorContainer"><p><a href="http://www.adobe.com/go/getflashplayer" target="_blank"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></div>');										
				$('#' + targetID).fadeIn('slow');			   
		   }
	   }
	 
	}

	/**
	* @description Create new audio object (html5)
	*	
	*/			
	function createNewAudio()
	{
		var createNewAudio  = $('<audio></audio>')
								//.hide()
								.attr('id','player')
								.attr('controls', 'controls')
								.attr('autoplay', 'autoplay')
								.append('<source src="' + audioSrc + '.mp3" type="audio/mp4" />')
								.append('<source src="' + audioSrc + '.ogg" type="audio/ogg" />');
								return createNewAudio;		
	}
	
	/*function createNewAudio (song) 
	{
		var audiotag            = $('<audio></audio>');
		if (audiotag.get(0).canPlayType && (audiotag.get(0).canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '') || audiotag.get(0).canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''))) 
		{
			audiotag.remove();
			var createNewAudio  = $('<audio autoplay></audio>')
										.hide()
										.attr('id','player')
										.addClass(song)
										.append('<source src="/x/get/' + type + '/' + song + '.m4a" type="audio/mp4" />')
										.append('<source src="/x/get/' + type + '/' + song + '.oga" type="audio/ogg" />');
		} 
		else 
		{
			audiotag.remove();
			var createNewAudio  = $('<object></object>')
										.attr('id','player')
										.attr('width',0)
										.attr('height',0)
										.hide()
										.addClass(song)
										.attr('classid','clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B')
										.attr('codebase','http://www.apple.com/qtactivex/qtplugin.cab')
										.append('<param name="src" value="/x/get/' + type + '/' + song + '.m4a">')
										.append('<param name="autoplay" value="true">')
										.append('<param name="controller" value="false">');
		}
		return createNewAudio;
	}	*/

	/**
	* @description Create audio object wrapper with title
	*	
	*/	
	function createPlayerWrapper(title, thumb)
	{
		var showHideDiv = 	$('<div id="playerWrapper"><div id="main-middle">' + '<img class ="imgFrame" src="' + thumb + '" alt="" />' + '<h3>' + title + '</h3>' + '</div></div>');
		return showHideDiv;
	}

	/**
	* @description Function for #y-link on CLICK
	*	
	*/		
	$('#y-link').click(function(event)
	{
		event.preventDefault();
		loadSWF('y');
	});	
	
	/**
	* @description Function for #x-link on CLICK
	*	
	*/		
	$('#x-link').click(function(event) 
	{
		event.preventDefault();
		loadSWF('x');
	});	
		
					
});

