/*
 * Various extensions to jplayer (http://www.happyworm.com/jquery/jplayer):
 *
 *  -- populates playlist using ajax
 *  -- adds preview/full track option
 *  -- adds buy/download links
 *  -- adds track descriptions
 *
 * Author: Ian Mathias-Baker
 * Version: 1.0.0
 * Date: 19th December 2010
 */


$(document).ready(function(){$.jPlayer.timeFormat.padMin=true;$("#jpId").jPlayer("volume",10);customCssIds:true
var playItem=0;var playfolder="";var mode="";var myPlayList=[];$.ajax({type:"GET",url:"data.xml",dataType:"xml",success:function(xml){$(xml).find('config').each(function(){playfolder=$(this).find('playfolder').text();});}});$.ajax({url:'data.xml',type:'GET',dataType:'xml',success:function(returnedXMLResponse){var i=0;$('track',returnedXMLResponse).each(function(){var trackData={"heading":$(this).attr("heading"),"name":$(this).attr("name"),"description":$(this).attr("description"),"buttontarget":$(this).attr("buttontarget"),"buttontext":$(this).attr("buttontext"),"mp3":$(this).attr("mp3"),"mp3preview":$(this).attr("mp3preview")};myPlayList.push(trackData);});i++;}});var jpPlayTime=$("#jplayer_play_time");var jpTotalTime=$("#jplayer_total_time");var jpTrackName=$("#jplayer_trackname");var jpTrackDescription=$("#jplayer_trackdescription");var jpDownloadLink=$("#jplayer_downloadlink");var jpStatus=$("#demo_status");$("#jquery_jplayer").jPlayer({ready:function(){$.ajax({url:'data.xml',type:'GET',dataType:'xml',success:function(returnedXMLResponse){displayPlayList();playListInit(true);}})}}).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;});$("#preview").click(function(){playListNext()
playListPrev();$(this).blur();return true;$('input[name=mode]:eq(0)').attr('checked','checked');});$("#stop").click(function(){playListNext()
playListPrev();$(this).blur();$('#jplayer_stop').trigger('click');return false;});$("#fulltrack").click(function(){playListNext()
playListPrev();$(this).blur();return true;$('input[name=mode]:eq(1)').attr('checked','checked');});$("#jplayer_next").click(function(){playListNext();$(this).blur();return false;});$("#vhalf").click(function(){$("#jquery_jplayer").jPlayer("volume",60);$(this).blur();return false;});function displayPlayList(){$("#jplayer_playlist ul").empty();for(i=0;i<myPlayList.length;i++){if(myPlayList[i].heading!=""){var listItem=(i==myPlayList.length-1)?"<li class='jplayer_playlist_item_last'>":"<li class='sectionheading'><h4 class='bottom'>"+myPlayList[i].heading+"</h4></li>";listItem+="<li class='playlist_item'><a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+myPlayList[i].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;});}
if(myPlayList[i].heading==""){var listItem=(i==myPlayList.length-1)?"<li class='playlist_item jplayer_playlist_item_last'>":"<li class='playlist_item'>";listItem+="<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+myPlayList[i].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 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");mode=$('#playmode input:radio:checked').val();jpTrackName.html(myPlayList[index].name);if(mode==0){var filename=myPlayList[index].mp3preview;var check=filename.substr(-12);if(check==".preview.mp3"){jpTrackName.html(myPlayList[index].name+"&nbsp;(Preview)");}}
if(myPlayList[index].description==""){jpTrackDescription.html("");jpTrackDescription.hide("fast");}
if(myPlayList[index].buttontarget==""){jpDownloadLink.html(" ");jpDownloadLink.hide("fast");}
if(myPlayList[index].description!=""){jpTrackDescription.html(myPlayList[index].description);jpTrackDescription.show("fast");}
if(myPlayList[index].buttontarget!=""){jpDownloadLink.html("<a href='"+myPlayList[index].buttontarget+"' class='buttonlink' target='_blank'>"+myPlayList[index].buttontext+"</a>");jpDownloadLink.show("fast");}
playItem=index;mode=$('#playmode input:radio:checked').val();if(mode==1){$("#jquery_jplayer").jPlayer("setFile",playfolder+"/"+myPlayList[playItem].mp3);}
else
{$("#jquery_jplayer").jPlayer("setFile",playfolder+"/"+myPlayList[playItem].mp3preview);}}
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);}});
