$(document).ready(function(){

	var playItem = 0;
	
	// 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.
			//demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
		},
		nativeSupport: false, //only use flash
		swfPath: "swf",
		errorAlerts: true,
		warningAlerts: true,
		oggSupport: false
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));

		//demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
	})
	.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 += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlaylist[i].tune_name +"</a></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;
			});
		}
	}
*/
	function displayPlayList() {

		for (i=0; i < myPlayList.length; i++) {
		    //SET DEFAULT IMAGE
		    var profileimage = "";
			if(myPlayList[i].img == "")
				profileimage = "pictures/profile/no_picture.jpg";	
			else	
				profileimage = "users/100x100/"+myPlayList[i].img;	
		
			var pl = '<tr id="jplayer_playlist_item_'+i+'" style="cursor:pointer;">';
			pl += '<td><img class="jplayer_playlist_image" src="'+profileimage+'" alt="'+myPlayList[i].creator+'" /></td>';
            pl += '<td style="width:220px;"><span class="text_lightgrey5">' + myPlayList[i].tune_name + ' </span><br />';
            pl += '<span class="text_grey6"> ' + myPlayList[i].creator + ' </span><br />';
            
            if(myPlayList[i].number_of_likes == 0) {
                pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" class="text_lightgrey6" style="display:none;">You like this!</span>';
                
            }else if(myPlayList[i].number_of_likes == 1) {
                if(likes !== null && likes["tune"] !== null && likes["tune"] != undefined) {
                    if(likes["tune"][myPlayList[i].tune_id] != undefined) {
                        pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" class="text_lightgrey6">You like this!</span>';
                    }else {
                        pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" style="display:none;" class="text_lightgrey6">You and </span>';
                        pl += '<span onClick="showLikes(\'tune\',\''+ myPlayList[i].tune_id + '\')" class="text_lightgrey6" style="cursor:pointer;">';
                        pl += '1 other likes this!</span>';
                    }
                }else {
                    pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" style="display:none;" class="text_lightgrey6">You and </span>';
                    pl += '<span onClick="showLikes(\'tune\',\''+ myPlayList[i].tune_id + '\')" class="text_lightgrey6" style="cursor:pointer;">';
                    pl += '1 other likes this!</span>';
                }
            }else {
                var others = myPlayList[i].number_of_likes-1;

                if(likes !== null && likes["tune"] !== null && likes["tune"] != undefined) {
                    if(likes["tune"][myPlayList[i].tune_id] != undefined) {
                        pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" class="text_lightgrey6">You and </span>';
                        pl += '<span onClick="showLikes(\'tune\',\''+ myPlayList[i].tune_id + '\')" class="text_lightgrey6" style="cursor:pointer;">';
                        if(others > 1) {
                            pl += others + ' others likes this!</span>';
                        }else {
                            pl += '1 other likes this!</span>';
                        }
                    }else{
                        pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" class="text_lightgrey6" style="display:none;">You and </span>';
                        pl += '<span onClick="showLikes(\'tune\',\''+ myPlayList[i].tune_id + '\')" class="text_lightgrey6" style="cursor:pointer;">';
                        if(others > 1) {
                            pl += others + ' others likes this!</span>';
                        }else {
                            pl += '1 other likes this!</span>';
                        }                    
                    }
                }else{
                    pl += '<span id="tune_' + myPlayList[i].tune_id + '_like_you" class="text_lightgrey6" style="display:none;">You and </span>';
                    pl += '<span onClick="showLikes(\'tune\',\''+ myPlayList[i].tune_id + '\')" class="text_lightgrey6" style="cursor:pointer;">';
                    if(others > 1) {
                        pl += others + ' others likes this!</span>';
                    }else {
                        pl += '1 other likes this!</span>';
                    }                    
                }
            }
            
            pl += '<td style="width:14px;"><img src="pictures/player/add_minibutton_2.gif" alt="Add tune to your playlist" class="add_to_playlist" id="add_to_playlist_'+i+'" song_id="'+myPlayList[i].tune_id+'" title="Add tune to your playlist">';

            if(likes !== null && likes["tune"] !== null && likes["tune"] != undefined) {
                if(myPlayList[i].creator_id != user_id) {
                    if(likes["tune"][myPlayList[i].tune_id] != undefined) {
                        var hide_like = "display:none;";
                        var hide_unlike = "";
                    }else {
                        var hide_like = "";
                        var hide_unlike = "display:none;";
                    }
                    pl += '<img src="pictures/icons/12x12_like.gif" style="margin-top:6px; width:10px;' + hide_like + '" id="tune_' + myPlayList[i].tune_id + '_like" onClick="saveOpinion(\'like\',\'tune\',\''+myPlayList[i].tune_id+'\',\''+user_id+'\')" alt="Like this tune" title="Like this tune">';
                    pl += '<img src="pictures/icons/12x12_unlike.gif" style="margin-top:6px; width:10px;' + hide_unlike + '" id="tune_' + myPlayList[i].tune_id + '_unlike" onClick="saveOpinion(\'unlike\',\'tune\',\''+myPlayList[i].tune_id+'\',\''+user_id+'\')" alt="Unlike this tune" title="Unlike this tune">';
                }
            }else {
                if(myPlayList[i].creator_id != user_id) {
                    var hide_like = "";
                    var hide_unlike = "display:none;";
                    pl += '<img src="pictures/icons/12x12_like.gif" style="margin-top:6px; width:10px;' + hide_like + '" id="tune_' + myPlayList[i].tune_id + '_like" onClick="saveOpinion(\'like\',\'tune\',\''+myPlayList[i].tune_id+'\',\''+user_id+'\')" alt="Like this tune" title="Like this tune">';
                    pl += '<img src="pictures/icons/12x12_unlike.gif" style="margin-top:6px; width:10px;' + hide_unlike + '" id="tune_' + myPlayList[i].tune_id + '_unlike" onClick="saveOpinion(\'unlike\',\'tune\',\''+myPlayList[i].tune_id+'\',\''+user_id+'\')" alt="Unlike this tune" title="Unlike this tune">';
                }
            }

			pl += '</td></tr>';        			                                        
			
			$("#jplayer_playlist table").append(pl);
			
			$("#add_to_playlist_"+i).css("cursor", "pointer").click(function(){
				addToPlaylist($(this).attr('song_id'));
			});
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}



	function playListInit(autoplay) {

		var profileimage = "";
		if(myPlayList[playItem].img == "")
			profileimage = "pictures/profile/no_picture.jpg"; 
		else	
			profileimage = "users/600x600/"+myPlayList[playItem].img;	
		$("#jplayer_large_img").attr("src", profileimage);
		$("#jp-song_title").html(myPlayList[playItem].tune_name);
		$("#jp-artist_name").html(myPlayList[playItem].creator);
		
		$("#current_song_add_to_playlist").unbind('click'); // Unbind old click events
		$("#current_song_add_to_playlist").click(function(){ // add new click event
			addToPlaylist(myPlayList[playItem].tune_id);
		});
				
		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;
		if(myPlayList.length > 0){
		    var profileimage = "";
			if(myPlayList[index].img == "")
				profileimage = "pictures/profile/no_picture.jpg"; 
			else	
				profileimage = "users/600x600/"+myPlayList[index].img;	
			$("#jplayer_large_img").attr("src", profileimage);
			playItem = index;		
		}else
			$("#jplayer_large_img").attr("src", "pictures/profile/no_picture.jpg");
		
		$("#jp-song_title").html(myPlayList[playItem].tune_name);
		$("#jp-artist_name").html(myPlayList[playItem].creator);
		
		//url = 'http://upatune.com/tunes/playlist/4c91d3f564bdb1dbe78526b205029d31.mp3';
		//$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
		$("#jquery_jplayer").jPlayer("setFile", 'audio/'+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 );
	}
});