// JavaScript Document

jQuery.preloadImages = function() {
    for(var i = 0; i < arguments.length; i++)
    {
        jQuery("<img/>").attr("src", arguments[i]);
    }
};

$(document).ready(function(){

	$.preloadImages("images/play-indicator.png", "images/play-indicator-png8.png", "images/controls/play.png", "images/controls/pause.png", "images/controls/volume.png");
	
	$("iframe.uw-zone").attr("src", "includes/uw-vpr.php");

	var defaultBufferlength = 3;
	var defaultVolume = 50;
	$(".controls a.mute").addClass("volume-5");
	$(".controls .mute a.unmute").removeClass("current");
	$(".controls .mute a.mute").addClass("current");
	
	var shoutcastPlayer = new SWFObject('player.swf','shoutcastplayersfw','0','0','9');
	shoutcastPlayer.addParam('allowfullscreen','false');
	shoutcastPlayer.addParam('allowscriptaccess','always');
	shoutcastPlayer.addParam('flashvars','bufferlength=' + defaultBufferlength + '&volume=' + defaultVolume + '&streamer=rtmp://vpr-flash.streamguys.net:80/shoutcast');
	shoutcastPlayer.write('shoutcast-player');
	var shoutcastPlayerSfw;
	var shoutcastObj;
	var shoutcastActive = true;
	var fastplayPlayer = new SWFObject('player.swf','fastplayplayersfw','0','0','9');
	fastplayPlayer.addParam('allowfullscreen','false');
	fastplayPlayer.addParam('allowscriptaccess','always');
	fastplayPlayer.addParam('flashvars','bufferlength=' + defaultBufferlength + '&volume=' + defaultVolume + '&streamer=rtmp://vpr-flash.streamguys.net:80/fastplay');
	fastplayPlayer.write('fastplay-player');
	var fastplayPlayerSfw;
	var fastplayActive = false;
	var player;
	
	if ($(".no-flash").length == 0) {
		$("body").removeClass("disabled").addClass("enabled");
	};
	
    $(".audio-vpr a").click(function () {
        //TODO: verify that if the src doesn't change, the iframe won't reload
		$("iframe.uw-zone").fadeOut("fast").attr("src", "includes/uw-vpr.php").fadeIn("fast");
		urchinTracker("/audio_player/live/vpr/");
    });
    $(".audio-vpr-classical a").click(function () {
        //TODO: verify that if the src doesn't change, the iframe won't reload
        $("iframe.uw-zone").fadeOut("fast").attr("src", "includes/uw-vpr-classical.php").fadeIn("fast");
		urchinTracker("/audio_player/live/vpr_classical/");
    });
    $(".audio-bbc-world-service a").click(function () {
        //TODO: verify that if the src doesn't change, the iframe won't reload
        $("iframe.uw-zone").fadeOut("fast").attr("src", "includes/uw-bbc-world-service.php").fadeIn("fast");
		urchinTracker("/audio_player/live/bbc_world_service/");
    });
	$(".audio-vt-house a").click(function () {
        //TODO: verify that if the src doesn't change, the iframe won't reload
        $("iframe.uw-zone").fadeOut("fast").attr("src", "includes/uw-vermont-legislature.php").fadeIn("fast");
		urchinTracker("/audio_player/live/vt_house/");
    });
	$(".audio-vt-senate a").click(function () {
        //TODO: verify that if the src doesn't change, the iframe won't reload
        $("iframe.uw-zone").fadeOut("fast").attr("src", "includes/uw-vermont-legislature.php").fadeIn("fast");
		urchinTracker("/audio_player/live/vt_senate/");
    });
	$(".shoutcast a.load").click(function () {
		if (fastplayActive) {
			fastplayPlayerSfw = document.getElementById("fastplayplayersfw");
			fastplayPlayerSfw.sendEvent("STOP");
			fastplayPlayerSfw.sendEvent("LOAD", null);
			fastplayActive = false;
		}
		shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
		shoutcastObj = {type:"sound",file:$(this).attr("href")};
		shoutcastPlayerSfw.sendEvent("LOAD", shoutcastObj);
		shoutcastPlayerSfw.sendEvent("PLAY", true);
		shoutcastActive = true;
		$(".shoutcast table .current").removeClass("current");
		$(this).parent().addClass("current").parent().addClass("current");
		$(".controls a.play").removeClass("disabled").addClass("enabled").removeClass("active").addClass("inactive");
		$(".controls a.play.enabled").click(function () {
			if (shoutcastActive) {
				shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
				shoutcastPlayerSfw.sendEvent("LOAD", shoutcastObj);
				shoutcastPlayerSfw.sendEvent("PLAY", true);
			}
			if (fastplayActive) {
				fastplayPlayerSfw = document.getElementById("fastplayplayersfw");            
				fastplayPlayerSfw.sendEvent("PLAY", true);
			}
			$(".controls a.play").removeClass("active").addClass("inactive");
			$(".controls a.pause").removeClass("inactive").addClass("active");
			$(".controls .status.subordinate").removeClass("active").addClass("inactive");
			$(".controls .status.playing").removeClass("inactive").addClass("active");
			return false;
		});
		$(".controls a.pause").removeClass("inactive").addClass("active");
		$(".controls .status.subordinate").removeClass("active").addClass("inactive");
		$(".controls .status.playing").removeClass("inactive").addClass("active");
		return false;
	});
	$(".fastplay a.load").click(function () {
		if (shoutcastActive) {
			shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
			shoutcastPlayerSfw.sendEvent("STOP");
			shoutcastPlayerSfw.sendEvent("LOAD", null);
			shoutcastActive = false;
		}
		fastplayPlayerSfw = document.getElementById("fastplayplayersfw");
		var obj = {type:"sound",file:$(this).attr("href")};
		fastplayPlayerSfw.sendEvent("LOAD", obj);
		fastplayPlayerSfw.sendEvent("PLAY", true);
		fastplayActive = true;
		$(".controls a.play").removeClass("disabled").addClass("enabled").removeClass("active").addClass("inactive");
		$(".controls a.play.enabled").click(function () {
			if (shoutcastActive) {
				shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
				shoutcastPlayerSfw.sendEvent("LOAD", shoutcastObj);
				shoutcastPlayerSfw.sendEvent("PLAY", true);
			}
			if (fastplayActive) {
				fastplayPlayerSfw = document.getElementById("fastplayplayersfw");            
				fastplayPlayerSfw.sendEvent("PLAY", true);
			}
			$(".controls a.play").removeClass("active").addClass("inactive");
			$(".controls a.pause").removeClass("inactive").addClass("active");
			$(".controls .status.subordinate").removeClass("active").addClass("inactive");
			$(".controls .status.playing").removeClass("inactive").addClass("active");
			return false;
		});
		$(".controls a.pause").removeClass("inactive").addClass("active");
		$(".controls .status.subordinate").removeClass("active").addClass("inactive");
		$(".controls .status.playing").removeClass("inactive").addClass("active");
		return false;
	});
	$(".controls a.pause").click(function () {
		if (shoutcastActive) {
			shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
			shoutcastPlayerSfw.sendEvent("PLAY", false);
		}
		if (fastplayActive) {
			fastplayPlayerSfw = document.getElementById("fastplayplayersfw");
			fastplayPlayerSfw.sendEvent("PLAY", false);
		}
		$(".controls a.play").removeClass("inactive").addClass("active");
		$(".controls a.pause").removeClass("active").addClass("inactive");
		$(".controls .status.subordinate").removeClass("active").addClass("inactive");
		$(".controls .status.paused").removeClass("inactive").addClass("active");
		return false;
	});
	$(".controls .mute a.mute").click(function () {
		shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
		fastplayPlayerSfw = document.getElementById("fastplayplayersfw");            
		shoutcastPlayerSfw.sendEvent("MUTE", true);
		fastplayPlayerSfw.sendEvent("MUTE", true);
		$(this).removeClass("current");
		$(".controls .mute a.unmute").addClass("current");
		$(".volume.pane").hide();
		$(".controls .status.muted").removeClass("inactive").addClass("active");
		return false;
	});
	$(".controls .mute a.unmute").click(function () {
		shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
		fastplayPlayerSfw = document.getElementById("fastplayplayersfw");            
		shoutcastPlayerSfw.sendEvent("MUTE", false);
		fastplayPlayerSfw.sendEvent("MUTE", false);
		$(this).removeClass("current");
		$(".controls .mute a.mute").addClass("current");
		$(".controls .status.muted").removeClass("active").addClass("inactive");
		return false;
	});
	
	$(".volume.ui-slider").slider({
		steps: 100,
		range: false,
		startValue: defaultVolume,
		change: function(e,ui) {
			shoutcastPlayerSfw = document.getElementById("shoutcastplayersfw");
			fastplayPlayerSfw = document.getElementById("fastplayplayersfw");    
			var volume = 100 - ui.value;
			var volumeStep = Math.round(volume / 10);
			shoutcastPlayerSfw.sendEvent("VOLUME", volume);
			fastplayPlayerSfw.sendEvent("VOLUME", volume);
			$(".controls a.mute").removeClass().addClass("mute").addClass("current").addClass("volume-" + volumeStep);
		}
	});
	
	$(".controls a.play").removeClass("enabled").addClass("disabled");
	$(".volume.pane").hide();
	
	$(".controls .mute a.mute").mouseover(function(){
      $(".volume.pane").show();
    }).mouseout(function(){
      $(".volume.pane").hide();
    });
	$(".volume.pane").mouseover(function(){
      $(".volume.pane").show();
    }).mouseout(function(){
      $(".volume.pane").hide();
    });


});

