$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
		{name:"No Excuses", album:"Album: The Meridian of Rhyme", itunes:"http://itunes.apple.com/us/album/the-meridian-of-rhyme/id489568533", mp3:"http://qwiet.com/_music/MOR/mp3/192/No Excuses.mp3", ogg:"http://qwiet.com/_music/MOR/ogg/No Excuses.ogg", lyrics:"http://qwiet.com/_music/MOR/lyrics/TheTruth.php"},			  
		{name:"The Truth", album:"Album: The Meridian of Rhyme", itunes:"http://itunes.apple.com/us/album/the-meridian-of-rhyme/id489568533", mp3:"http://qwiet.com/_music/MOR/mp3/192/The Truth (feat. Playdough and Theory Hazit).mp3", ogg:"http://qwiet.com/_music/MOR/ogg/The Truth (feat. Playdough and Theory Hazit).ogg", lyrics:"http://qwiet.com/_music/MOR/lyrics/TheTruth.php"},
		{name:"Arcs of Life", album:"Album: The Meridian of Rhyme", itunes:"http://itunes.apple.com/us/album/the-meridian-of-rhyme/id489568533", mp3:"http://qwiet.com/_music/MOR/mp3/192/Arcs of Life.mp3", ogg:"http://qwiet.com/_music/MOR/ogg/Arcs of Life.ogg", lyrics:"http://qwiet.com/_music/MOR/lyrics/ArcsOfLife.php"},
		{name:"My Word", album:"Album: The Meridian of Rhyme", itunes:"http://itunes.apple.com/us/album/the-meridian-of-rhyme/id489568533", mp3:"http://qwiet.com/_music/MOR/mp3/192/My Word (feat. Braille and Manchild).mp3", ogg:"http://qwiet.com/_music/MOR/ogg/MyWord.ogg", lyrics:"http://qwiet.com/_music/MOR/lyrics/MyWord.php"},
		{name:"Top of the World", album:"Album: The Meridian of Rhyme", itunes:"http://itunes.apple.com/us/album/the-meridian-of-rhyme/id489568533", mp3:"http://qwiet.com/_music/MOR/mp3/192/Top of the World.mp3", ogg:"http://qwiet.com/_music/MOR/ogg/TOTW.ogg", lyrics:"http://qwiet.com/_music/MOR/lyrics/TOTW.php"},
		{name:"Innovate", album:"Album: Return With Honor", itunes:"http://itunes.apple.com/us/album/return-with-honor/id253873361", mp3:"http://qwiet.com/_music/RWH/mp3/Innovate.mp3", ogg:"http://qwiet.com/_music/RWH/ogg/Innovate.ogg", lyrics:"http://qwiet.com/_music/RWH/lyrics/innovate.php"},
		{name:"So Beautiful", album:"Album: Return With Honor", itunes:"http://itunes.apple.com/us/album/return-with-honor/id253873361", mp3:"http://qwiet.com/_music/RWH/mp3/SoBeautiful.mp3", ogg:"http://qwiet.com/_music/RWH/ogg/SoBeautiful.ogg", lyrics:"http://qwiet.com/_music/RWH/lyrics/sobeautiful.php"},
		{name:"Breathe", album:"Album: Return With Honor", itunes:"http://itunes.apple.com/us/album/return-with-honor/id253873361", mp3:"http://qwiet.com/_music/RWH/mp3/Breathe.mp3", ogg:"http://qwiet.com/_music/RWH/ogg/Breathe.ogg", lyrics:"http://qwiet.com/_music/RWH/lyrics/breathe.php"}	
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
			
		},
		oggSupport: true,
		swfPath:"http://www.qwiet.com/wp-content/themes/urban-elements/scripts/jPlayer"
	})
	
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));

	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#jplayer_previous").click( function() {
		playListPrev();
		$(this).blur();
		return false;
	});

	$("#jplayer_next").click( function() {
		playListNext();
		$(this).blur();
		return false;
	});
	
	
	
	
	
	
	

	function displayPlayList() {
		$("#jplayer_playlist ul").empty();
		for (i=0; i < myPlayList.length; i++) {
			var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
			listItem += "<div class='songItem'>";
			listItem += "<div class='songText'>";
			listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a>";
			//listItem += "<a class='lyrics' href='" + myPlayList[i].lyrics + "'>lyrics</a>";
			listItem += "<br /><span class='albumTitle'>" + myPlayList[i].album + "</span>";
			listItem += "</div>";
			listItem += "<div class='songLinks'>";
			if(myPlayList[i].itunes.length > 1){
				listItem += "<a href='" + myPlayList[i].itunes + "' target='blank'><img src='http://qwiet.com/images/itunes.png' /></a>";
			}
			
			listItem += "</div></div></li>";
			$("#jplayer_playlist ul").append(listItem);
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
				$(this).blur();
				return false;
			});
		}
		
		$("a.lyrics").fancybox({
			'width'				: 500,
			'height'			: '75%',
			'autoScale'			: false,
			'type'				: 'iframe'
		});
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
	}
});
