$(function() {
  var
    $shows = $("#shows").addClass("expandable"),
    $jPlayer = $("<div>", { id: "jplayer" }).appendTo("body"),
    $playing
    
	$jPlayer.jPlayer({
		ready: function () {
      // $("#jp_container .track-default").click();
		},
		timeupdate: function(event) {
		  var
		    status = event.jPlayer.status,
		    time = Math.round(status.currentTime),
		    timeBits = [Math.floor(time / 60), time % 60]
		    
		  if (status.duration >= 3600)
		    timeBits.unshift(Math.floor(time / 3600))
		    
		  timeBits = $.map(timeBits, function(i) {
  		  return (i < 10 ? "0" + i : i)
		  })

			$playing.find("a.play small span").text(timeBits.join(":"));
		},
		play: function(event) {
      // my_playState.text(opt_text_playing);
		},
		pause: function(event) {
		  $playing.removeClass("playing")
		},
		ended: function(event) {
		  $playing.removeClass("playing")
		},
		swfPath: "/javascripts/",
    cssSelectorAncestor: "#jplayer",
		supplied: "mp3",
		wmode: "window"
	})
  
  $("<li>", { "class": "more", "html": "<a href='#'>" + $shows.data("more-label") + "</a>" })
    .insertAfter($shows.children().eq(3))
  
  $("ul.expandable li.more a").click(function() {
    $(this).closest("ul.expandable").addClass("expanded")
    return false
  })
  
  $("details > summary").click(function() {
    var $details = $(this).parent()
    $details.toggleClass("open")
  })
  
  $("a.play").click(function() {
    var
      $mix = $(this).closest("li"),
      $time = $mix.find("a.play small"),
      $currentTime
      
    if ($playing && $playing.attr("id") !== $mix.attr("id")) $playing.removeClass("playing")
    $playing = $mix
    
    if ($mix.toggleClass("playing").hasClass("playing")) {
      // play
      $currentTime = $time.find("span")
      if (!$currentTime.length) {
        // $("<span>", { text: $time.text() }).appendTo($time.text(" / "))
        $currentTime = $("<span>", { "class": "current-time", text: "0:00", style: "width:" + ($time.outerWidth() + 5) + "px" }).prependTo($time)
      }
      
      $jPlayer.jPlayer("setMedia", {
  			mp3: $(this).attr("href")
  		})
  		
  		$jPlayer.jPlayer("play")     
    } else {
  		$jPlayer.jPlayer("pause")
    }
    
    return false
  })
})
