$(function(){
	$.jPlayer.timeFormat.showHour = true;
	var _livePlayer = $(".liveplayer .jplayer").jPlayer({
	  solution: "flash, html",
	  swfPath: "/wp-content/themes/majority2/js",
	  supplied: "mp3",
		cssSelectorAncestor: ".liveplayer"
	});
	$(".liveplayer .play").click(function(){
		$(_livePlayer).jPlayer("setMedia", {mp3: "http://live.majority.fm:8000/live"});
		$(_livePlayer).jPlayer("play");
	});
	$(".liveplayer .pause").click(function(){
		_livePlayer.jPlayer("clearMedia");
	});
	$(".player:visible").each(function(){
		var thisPlayerDiv = $(this);
		var theCssAncestor = thisPlayerDiv.attr("player-ancestor");
		if(theCssAncestor==null){
			theCssAncestor=".post";
		}
		thisPlayerDiv.find(".popup").html("Play in popup");
		thisPlayerDiv.find(".download").html("Download");
		thisPlayerDiv.find(".jp-time-divider").html("/");
		
		var id = $(this).attr("post-id");
		var uri = $(this).attr("uri");
		var _jplayer = $(this).find(".jplayer:first").jPlayer({
		  ready: function () {
		   _jplayer.jPlayer("setMedia", {mp3: uri});
		  },
			ended: function (){
	      _jplayer.jPlayer("playHead", 0);
				thisPlayerDiv.find(".jp-audio").hide();
	    },
	    play: function(){
		
					thisPlayerDiv.find(".jp-audio").show();
	    },
			solution: "flash, html",
		  swfPath: "/wp-content/themes/majority2/js",
		  supplied: "mp3",
			cssSelectorAncestor: theCssAncestor
		 });
		thisPlayerDiv.find(".play").click(function(){
			_jplayer.jPlayer("play");
		});
		thisPlayerDiv.find(".pause").click(function(){
			_jplayer.jPlayer("pause");
	
		});
		thisPlayerDiv.find(".jp-audio .time").click(function(e){
	      thisPlayerDiv.find(".jp-seek-bar").trigger(e);
	    });
	  thisPlayerDiv.find(".jp-audio .time span").click(function(e){
	      thisPlayerDiv.find(".jp-seek-bar").trigger(e);
	  });
	});
	$(".player .popup").click(function(){
		var _uri = $(this).parents(".player:first").attr("uri");
		var _title = "Majority Report Player";
		if($(this).parents(".post:first").find(".entry-title")){
			_title = $(this).parents(".post:first").find(".entry-title").html();
		}
		if($(this).parents(".player:first").attr("player-title")){
			_title = $(this).parents(".player:first").attr("player-title");
		}
		window.open( "/wp-content/themes/majority2/popup.php?uri="+_uri+"&title="+escape(_title), _title, "status = 1, height = 126, width = 560, resizable = 0");
	});
	$("#listen-live.live .popup").click(function(){
                var _title = "Majority Report LIVE";
                window.open( "/wp-content/themes/majority2/live.php", _title, "status = 1, height = 170, width = 320, resizable = 0");
        });
});

