var playItem = 0;
var plIndex = [];

var jlp_shfl = getCookie("jlp_shfl");



Array.prototype.find=function(v){
	for (i=0;i<this.length;i++){
		if (this[i]==v) return i;
	}
	return 0;
}



for (i=0;i<myPlayList.length;i++) {
		plIndex[i] = i;
}

//shuffle
function randOrd(){
	return (Math.round(Math.random())-0.5);
}
if (jlp_shfl == "shuffle") {
	plIndex.sort(randOrd);
}



function setCookie (name, value) {
	document.cookie = name + "=" + escape(value) + "; expires=Thu, 01-Jan-2055 00:00:01 GMT; path=/";
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) 	{
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function changeShflStatus(el) {
	var nowPlay = plIndex[playItem];

	if (el.checked) {
		setCookie("jlp_shfl","shuffle");
		plIndex.sort(randOrd);
	} else {
		setCookie("jlp_shfl","notshuffle");
		plIndex.sort();
	}
	playItem = plIndex.find(nowPlay);

}


jQuery(document).ready(function(){

	if (shfl==1){
		if (jlp_shfl == "shuffle") {
			document.getElementById('jlp_shfl').checked = true;
		} else if (jlp_shfl == "notshuffle") {
			document.getElementById('jlp_shfl').checked = false;
		}

	}

	jQuery('#jp-nowplay marquee').marquee('pointer').mouseover(function () {
	  jQuery(this).trigger('stop');
	}).mouseout(function () {
	  jQuery(this).trigger('start');
	}).mousemove(function (event) {
	  if (jQuery(this).data('drag') == true) {
		this.scrollLeft = jQuery(this).data('scrollX') + (jQuery(this).data('x') - event.clientX);
	  }
	}).mousedown(function (event) {
	  jQuery(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
	  jQuery(this).data('drag', false);
	});


	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = jQuery("#jplayer_play_time");
	var jpTotalTime = jQuery("#jplayer_total_time");

	jQuery("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(enable_autoplay); // Parameter is a boolean for autoplay.
		},
		swfPath: baseUri + "modules/mod_jlplayer2/js",
		nativeSupport: nativeSupport
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text(jQuery.jPlayer.convertTime(playedTime));
		jpTotalTime.text(jQuery.jPlayer.convertTime(totalTime));
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

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

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

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			jQuery("#jplayer_playlist").append("<div id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</div>");
			jQuery("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = jQuery(this).data("index");
				if (plIndex[playItem] != index) {
					_index = plIndex.find(index);
					playListChange( _index, index );
				} else {
					jQuery("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange(0, plIndex[0] );
		} else {
			playListConfig(0, plIndex[0] );
		}
	}

	function playListConfig(_index, index ) {
		jQuery("#jplayer_playlist_item_"+plIndex[playItem]).removeClass("jplayer_playlist_current");
		jQuery("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
		playItem = _index;
		jQuery("#jquery_jplayer").jPlayer("setFile", myPlayList[plIndex[playItem]].mp3);
		jQuery('#jp-nowplay div div').html(myPlayList[plIndex[playItem]].artist+' '+myPlayList[plIndex[playItem]].title);
	}

	function playListChange(_index, index ) {
		playListConfig(_index, index );
		jQuery("#jquery_jplayer").jPlayer("play");
	}


	function playListNext() {
		if (playItem < 0 ) {
			
		} else {
			var _index = (playItem+1 < myPlayList.length) ? playItem+1 : -1;
			var index = plIndex[_index];
			//alert('play index=' + _index);
			playListChange(_index, index );
		}
	}

	function playListPrev() {
		var _index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		var index = plIndex[_index];
		playListChange(_index, index );

	}
	jQuery('#jp-playlist-ctrl-wrap').hover(function(){
		jQuery(this).css("background-color","#dddddd");
	},function(){
		jQuery(this).css("background-color","#cccccc");
	}).click(function(){
		if (jQuery('#jplayer_playlist').is(":hidden")) {
			jQuery('#jplayer_playlist').slideToggle(function(){
				jQuery("#jplayer_playlist_ctrl").css("background-position","0px 0px");
			});

		} else {
			jQuery('#jplayer_playlist').slideToggle(function(){
				jQuery("#jplayer_playlist_ctrl").css("background-position","0px -10px");
			});
		}
	});

});

