/*!
   Swagg Player: Music Player for the web
   --------------------------------------------
   http://johnny-ray.com/swagg-player

   Copyright (c) 2010, Johnny Austin. All rights reserved.
   Code provided under the MIT License:
   http://www.opensource.org/licenses/mit-license.php

   V0.5
*/
(function($)
	{
				var songs = new Array();
				var song = {
								id: " ",
								title: " ",
								url: " ",
								artist: " ",
								thumb: " "
							};
				var curr_song = 0;
				soundManager.url = 'swf';
				var soundId = soundManager.sID;
				var isplaying = new Boolean(true);
				var play_button = 'images/player/play-black.png'; // normal play button
				var play_over_button = 'images/player/play-over.png'; // play button when mouse hovers
				var pause_button = 'images/player/pause-black.png'; // normal pause button
				var pause_over_button = 'images/player/pause-over.png'; // pause button when mouse hovers
				var skip_over_button = 'images/player/skip-over.png'; // ...I think you get the idea with the button naming convention
				var skip_button = 'images/player/skip-black.png';
				var stop_button = 'images/player/stop-black.png';
				var stop_over_button = 'images/player/stop-over.png';
				var back_button = 'images/player/back-black.png';
				var back_over_button = 'images/player/back-over.png';
				var xml = 'xml/songs.xml'; // path to xml file which stores the songs. structure must remain in tact!
				var loading = 'images/loading.gif'; // path to loading gif for when the songs array is still being populated
				
		$.fn.SwaggPlayer = function(options) {
				var swagg_div = this;
				swaggOn(swagg_div);
		}
			//$(document).ready(function(){
			function swaggOn(swagg_div) {
				document.getElementById('art').setAttribute('src', loading);
				isplaying = true;
				// get songs from XML document						   
				$.ajax({
					type: "GET",
					url: xml,
					dataType: "text/xml",
					success: parseXml,
					error: function(xml){
					}
				});
			
				// ======================= mouse event hooks for play button ===========================
				$('#play-link').click(function() {
					 play();
				});
				$('#play-link').mouseover(function() {
					 document.getElementById('play').setAttribute('src', pause_over_button);
				});
				$('#play-link').mouseout(function() {
					 document.getElementById('play').setAttribute('src', pause_button);
				});
				
				// =================== mouse event hooks for skip button =========================
				$('#skip-link').click(function() {
					 skip();
				});
				$('#skip-link').mouseover(function() {
					 document.getElementById('skip').setAttribute('src', skip_over_button);
				});
				$('#skip-link').mouseout(function() {
					 document.getElementById('skip').setAttribute('src', skip_button);
				});
				
				// =================== mouse event hooks for stop button =========================
				$('#stop-link').click(function() {
					 stopMusic(curr_song.toString());
				});
				$('#stop-link').mouseover(function() {
					 document.getElementById('stop').setAttribute('src', stop_over_button);
				});
				$('#stop-link').mouseout(function() {
					 document.getElementById('stop').setAttribute('src', stop_button);
				});
				
				// =================== mouse event hooks for back button =========================
				$('#back-link').click(function() {
					 skipBack();
				});
				$('#back-link').mouseover(function() {
					 document.getElementById('back').setAttribute('src', back_over_button);
				});
				$('#back-link').mouseout(function() {
					 document.getElementById('back').setAttribute('src', back_button);
				});
			
				// init soundManager
				soundManager.onload =  function() {
					isplaying = true;
					while(songs == null){console.log('waiting...');}
					for (var i = 0; i < songs.length; i++) {
						soundManager.createSound({
							id: i.toString(),
							url: songs[i].url,
						});
						document.getElementById('art').setAttribute('src',songs[curr_song].thumb);
						showSongInfo();
						skip();
					} // end for
				} // end soundManager onload function
				
				// init swagg-player div
				$(swagg_div).draggable();
				$(swagg_div).mouseover(function() {
					$(this).css('cursor', 'move');								
				});
			
			//});
			}
			
			// plays the current track
			function play() {	
				togglePlayPauseButton();
				soundManager.togglePause(curr_song.toString());
				showSongInfo();
			}
			
			// if music is playing, this function pauses the music and if it's not playing, this
			// function plays the current track
			function togglePlayPauseButton(){
					if (isplaying == true){
					$('#play-link').mouseover(function() {
						document.getElementById('play').setAttribute('src', play_over_button);
					});
					$('#play-link').mouseout(function() {
						document.getElementById('play').setAttribute('src', play_button);
					});
					isplaying = false;
				}
				else {
					$('#play-link').mouseover(function() {
						document.getElementById('play').setAttribute('src', pause_over_button);
					});
					$('#play-link').mouseout(function() {
						document.getElementById('play').setAttribute('src', pause_button);
					});
					isplaying = true;
				}
			}
			
			// displays artist [song title]
			function showSongInfo(){
				document.getElementById('info').innerHTML = "<p>" + songs[curr_song].artist + "  [" + songs[curr_song].title + "] </p>";
				//document.getElementById('download').setAttribute('href',songs[curr_song].url);
			}
			
			// goes to the previous song. if the currently playing song is the first
			// song, it goes to the last song in the list
			function skipBack() {
				var t = curr_song;
				if (t == 0){
					t = songs.length - 1;	
				}
				else{
					t = t - 1;	
				}
				soundManager.stop(curr_song.toString());
				curr_song = t;
				switchArt();
				soundManager.togglePause(curr_song.toString());
				isplaying = true;
				showSongInfo();
			}
			  
			// skips to the next song. if currently playing song is the last song in the list
			// it goes back to the first song
			function skip() {
				var t = curr_song;
				soundManager.stop(curr_song.toString());
				if (t < songs.length){
					if (t == songs.length - 1)
						curr_song = 0;
					else
						curr_song = t+1;
				}
				soundManager.play(curr_song.toString());
				switchArt();
				isplaying = true;
				showSongInfo();
			}
			
			// switches to the currently playing song's album art using fancy jquery slide effect
			function switchArt(){
				$('#art').hide('slide', function() {
					document.getElementById('art').setAttribute('src',songs[curr_song].thumb);
				});
			}
			  
			// stops the currently playing track and changes toggles the pause button back to the play button
			function stopMusic() {
				soundManager.stop(curr_song.toString());
				isplaying = false;
				document.getElementById('play').setAttribute('src', play_button);
				$('#play-link').mouseover(function() {
						document.getElementById('play').setAttribute('src', play_over_button);
				});
				$('#play-link').mouseout(function() {
						document.getElementById('play').setAttribute('src', play_button);
				});
			}
			// for debugging purposes
			//function displaySongs() {
			//	for (j = 0; j < songs.length; j++) {
			//		$("#out").append(songs[j].title + " <br/>")	
			//	}
			//}
			
			// Parse the xml which holds the song information and conver it
			// into a JSON object
			function parseXml(xml)
			{
				var i = 0;
				if (getInternetExplorerVersion() != -1) {
					$(xml).filter("song").each(function()
					{
						var temp;
						song.id = $(this).attr("id");
						song.title = $(this).attr("track");
						song.url = $(this).attr("url");
						song.artist = $(this).attr("artist");
						song.thumb = $(this).attr("thumb");
						songs[i] = jQuery.extend(true, {}, song);
						i = i + 1;
					});
				} // end if
				else {
					$(xml).find("song").each(function()
					{
						var temp;
						song.id = $(this).attr("id");
						song.title = $(this).attr("track");
						song.url = $(this).attr("url");
						song.artist = $(this).attr("artist");
						song.thumb = $(this).attr("thumb");
						songs[i] = jQuery.extend(true, {}, song);
						i = i + 1;
					});
				} // end else			
			}
			
			// ===========================================================================
			// Code to detect if the user is using internet explorer.
			// Barrowed from http://msdn.microsoft.com/en-us/library/ms537509(VS.85).aspx
			// ===========================================================================
			function getInternetExplorerVersion()
			// Returns the version of Internet Explorer or a -1
			// (indicating the use of another browser).
			{
			  var rv = -1; // Return value assumes failure.
			  if (navigator.appName == 'Microsoft Internet Explorer')
			  {
				var ua = navigator.userAgent;
				var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
				if (re.exec(ua) != null)
				  rv = parseFloat( RegExp.$1 );
			  }
			  return rv;
			}
			
			// In case I ever need to check IE version
			function checkVersion()
			{
			  var msg = "You're not using Internet Explorer.";
			  var ver = getInternetExplorerVersion();
			
			  if ( ver > -1 )
			  {
				if ( ver >= 8.0 ) 
				  msg = "You're using a recent copy of Internet Explorer."
				else
				  msg = "You should upgrade your copy of Internet Explorer.";
			  }
			  //alert( msg );
			}
		//}
})(jQuery);