var LIVE_SERVER1 = "http://livemedia1.gcnlive.com:";
var LIVE_SERVER2 = "http://livemedia2.gcnlive.com:";
var LIVE_SERVER3 = "http://livemedia3.gcnlive.com:";
var ON_DEMAND_SERVER1 = "http://ondemandmedia1.gcnlive.com:";
var ON_DEMAND_SERVER2 = "http://ondemandmedia2.gcnlive.com:";
var liveChannel1URLList = new Array(LIVE_SERVER1,LIVE_SERVER2,LIVE_SERVER3);
var liveChannel2URLList = new Array(LIVE_SERVER1,LIVE_SERVER2,LIVE_SERVER3);
var liveChannel3URLList = new Array(LIVE_SERVER1,LIVE_SERVER2,LIVE_SERVER3);
var liveChannel4URLList = new Array(LIVE_SERVER1,LIVE_SERVER2,LIVE_SERVER3);
var onDemandURLList = new Array(ON_DEMAND_SERVER1,ON_DEMAND_SERVER2);

var liveChannelHighPortList = new Array("23810","23820","23830","23840");
var liveChannelLowPortList = new Array("3010","3012","3014","3016");
var liveChannelPortList = liveChannelHighPortList;
var currentBandwidth = "high";
var currentVolume = 50;
var checkInvalidInterval = null;
var reloadTimeoutAmt = 0;
var currentChannel = 0;
var player = null;

var STATE_IDLE = "IDLE";
var STATE_BUFF = "BUFFERING";
var STATE_PLAY = "PLAYING";
var STATE_PAUS = "PAUSED";
var STATE_COMP = "COMPLETED";
var DIALUP_BW_IMG = "images/lowBand.png";
var HIGH_BW_IMG = "images/highBand.png";
var DIALUP_BW_TXT = "You are listening to the low bandwidth channels.";
var HIGH_BW_TXT = "You are listening to the high bandwidth channels.";
var STATE_PLAY_OFF_IMG = "images/play0.png";
var STATE_PAUS_OFF_IMG = "images/pause0.png";
var STATE_PLUS_OFF_IMG = "images/plus0.png";
var STATE_MINUS_OFF_IMG = "images/minus0.png";
var STATE_PLAY_ON_IMG = "images/play1.png";
var STATE_PAUS_ON_IMG = "images/pause1.png";
var STATE_PLUS_ON_IMG = "images/plus1.png";
var STATE_MINUS_ON_IMG = "images/minus1.png";
var STATE_VOL_0_IMG = "images/volume0.png";
var STATE_VOL_10_IMG = "images/volume1.png";
var STATE_VOL_20_IMG = "images/volume2.png";
var STATE_VOL_30_IMG = "images/volume3.png";
var STATE_VOL_40_IMG = "images/volume4.png";
var STATE_VOL_50_IMG = "images/volume5.png";
var STATE_VOL_60_IMG = "images/volume6.png";
var STATE_VOL_70_IMG = "images/volume7.png";
var STATE_VOL_80_IMG = "images/volume8.png";
var STATE_VOL_90_IMG = "images/volume9.png";
var STATE_VOL_100_IMG = "images/volume10.png";
var VOL_0_IMG = "images/vol0.png";
var VOL_20_IMG = "images/vol20.png";
var VOL_40_IMG = "images/vol40.png";
var VOL_60_IMG = "images/vol60.png";
var VOL_80_IMG = "images/vol80.png";
var VOL_100_IMG = "images/vol100.png";
var NUM_OF_CHANNELS = 4;
var FIFTEEN_SECONDS_IN_MILLIS = 15000;
var SIXTY_MINUTES_IN_MILLIS = 3600000;

//////////////////////////////////////////////////
// returns a reference to the object with the passed 'objID' if it exists, otherwise returns false.
//////////////////////////////////////////////////
function Obj(objID) {
	if (document.getElementById && document.getElementById(objID)) {return document.getElementById(objID);}
	else if (document.all && document.all(objID)) {return document.all(objID);}
	else if (document.layers && document.layers[objID]) {return document.layers[objID];}
	else {return false;}
}

//////////////////////////////////////////////////
// sets the player variable with the created flash player.
// This function is called by the flash player once it has been created.
//////////////////////////////////////////////////
function playerReady(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	player = Obj(obj['id']);
};

//////////////////////////////////////////////////
// this is called when a play button is clicked and plays the appropriate channel
// based on the passed 'channel' parameter.
//////////////////////////////////////////////////
function playChannelClick(channel) {
	var chState = getPlayerState();
	if(channel != currentChannel) {
		//stop player on current channel and 
		//play the player on channel [channel]
		createLiveChannelPlayer(channel, "true");
		changeStateImage(channel, "play", true);
	} else {
		if(chState == STATE_PAUS || chState == STATE_IDLE) {
			//play the player on current channel
			player.sendEvent("PLAY", "true");
			changeStateImage(channel, "play", true);
		}
	}
	
	checkInvalidInterval = setInterval("checkForInactivePlayer()", FIFTEEN_SECONDS_IN_MILLIS);
}

//////////////////////////////////////////////////
// this is called when a pause button is clicked and pauses the appropriate channel
// based on the passed 'channel' parameter.
//////////////////////////////////////////////////
function pauseChannelClick(channel) {
	var chState = getPlayerState();
	if(channel == currentChannel && chState == STATE_PLAY) {
		//pause the player on current channel
		player.sendEvent("PLAY", "false");
		changeStateImage(channel, "pause", true);
	}
}

//////////////////////////////////////////////////
// this increases the player volume by 10, up to 100.
//////////////////////////////////////////////////
function plusClick() {
	if(currentVolume < 100) {
		currentVolume += 10;
		player.sendEvent("VOLUME", currentVolume);
		setVolumeIndicator();
	}
}

//////////////////////////////////////////////////
// this changes the plus button to the "on" state.
//////////////////////////////////////////////////
function plusOver() {
	Obj("plusimg").src = STATE_PLUS_ON_IMG;
}

//////////////////////////////////////////////////
// this changes the plus button to the "off" state.
//////////////////////////////////////////////////
function plusOut() {
	Obj("plusimg").src = STATE_PLUS_OFF_IMG;
}

//////////////////////////////////////////////////
// this decreases the player volume by 10, down to 0.
//////////////////////////////////////////////////
function minusClick() {
	if(currentVolume > 0) {
		currentVolume -= 10;
		player.sendEvent("VOLUME", currentVolume);
		setVolumeIndicator();
	}
}

//////////////////////////////////////////////////
// this changes the plus button to the "on" state.
//////////////////////////////////////////////////
function minusOver() {
	Obj("minusimg").src = STATE_MINUS_ON_IMG;
}

//////////////////////////////////////////////////
// this changes the plus button to the "off" state.
//////////////////////////////////////////////////
function minusOut() {
	Obj("minusimg").src = STATE_MINUS_OFF_IMG;
}

//////////////////////////////////////////////////
// this changes the volume indicator to the appropriate level.
//////////////////////////////////////////////////
function setVolumeIndicator() {
	var imgId = currentVolume / 10;
	switch(imgId) {
		case 0: Obj("volumeimg").src = STATE_VOL_0_IMG; break;    
		case 1: Obj("volumeimg").src = STATE_VOL_10_IMG; break;    
		case 2: Obj("volumeimg").src = STATE_VOL_20_IMG; break;    
		case 3: Obj("volumeimg").src = STATE_VOL_30_IMG; break;    
		case 4: Obj("volumeimg").src = STATE_VOL_40_IMG; break;    
		case 5: Obj("volumeimg").src = STATE_VOL_50_IMG; break;    
		case 6: Obj("volumeimg").src = STATE_VOL_60_IMG; break;    
		case 7: Obj("volumeimg").src = STATE_VOL_70_IMG; break;    
		case 8: Obj("volumeimg").src = STATE_VOL_80_IMG; break;    
		case 9: Obj("volumeimg").src = STATE_VOL_90_IMG; break;    
		case 10: Obj("volumeimg").src = STATE_VOL_100_IMG; break;    
		default: Obj("volumeimg").src = STATE_VOL_100_IMG; break;    
	}
}

//////////////////////////////////////////////////
// this checks to see if the player is able to play the current URL.
// if it can't play the current URL, ann attempt will be made to 
// play the next item in the playlist.
//////////////////////////////////////////////////
function checkForInactivePlayer() {
	if(getPlayerState() == STATE_IDLE) {
		//play the next item in the playlist. i.e.switch servers
		player.sendEvent("NEXT");
	} else {
		clearInterval(checkInvalidInterval);
	}
}

//////////////////////////////////////////////////
// this creates the player for the Live Player page.
//////////////////////////////////////////////////
function createLiveChannelPlayer(channel, as) {
	var fv = "file="+ getPrimaryLiveChannelURL(channel)+liveChannelPortList[channel-1]+"/;stream.nsv&type=sound&repeat=always&volume="+currentVolume+"&autostart=false";
	var s1 = new SWFObject("player.swf","ply","300","0","9");
	s1.addParam("allowfullscreen","false");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("flashvars",fv);
	s1.write("player");
	if(as == "true") {
		currentChannel = channel;
		setTimeout("player.sendEvent('PLAY', 'true')", 1500);
		showNowLoading(channel);
	}
	setTimeout("fillLiveChannelPlaylist(currentChannel)", 1000);
}

//////////////////////////////////////////////////
// returns the first entry in 'liveChannel<x>URLList' based on the channel.
//////////////////////////////////////////////////
function getPrimaryLiveChannelURL(channel) {
	var url = "";
	if(channel == 1) {
		url = liveChannel1URLList[0];
	} else if(channel == 2) {
		url = liveChannel2URLList[0];
	} else if(channel == 3) {
		url = liveChannel3URLList[0];
	} else if(channel == 4) {
		url = liveChannel4URLList[0];
	}
	
	return url;
}

//////////////////////////////////////////////////
// this creates and loads a playlist for the Live player based on the live channel URL list.
//////////////////////////////////////////////////
function fillLiveChannelPlaylist(channel) {
	var newLivePlaylist = new Array();
	if(channel == 1) {
		for(i=0;i<liveChannel1URLList.length;i++) {
			var url = liveChannel1URLList[i] + liveChannelPortList[channel-1] + "/;stream.nsv";
			newLivePlaylist[i] = { file:url, type:'sound' };
		}
	} else if(channel == 2) {
		for(i=0;i<liveChannel2URLList.length;i++) {
			var url = liveChannel2URLList[i] + liveChannelPortList[channel-1] + "/;stream.nsv";
			newLivePlaylist[i] = { file:url, type:'sound' };
		}
	} else if(channel == 3) {
		for(i=0;i<liveChannel3URLList.length;i++) {
			var url = liveChannel3URLList[i] + liveChannelPortList[channel-1] + "/;stream.nsv";
			newLivePlaylist[i] = { file:url, type:'sound' };
		}
	} else if(channel == 4) {
		for(i=0;i<liveChannel4URLList.length;i++) {
			var url = liveChannel4URLList[i] + liveChannelPortList[channel-1] + "/;stream.nsv";
			newLivePlaylist[i] = { file:url, type:'sound' };
		}
	}

	player.sendEvent("LOAD", newLivePlaylist);
}

//////////////////////////////////////////////////
// this changes the images for the play and pause buttons to display as on or off
// based on the passed channel, which button, and if it is on or off.
//////////////////////////////////////////////////
function changeStateImage(channel, button, isOn) {
	var img;
	if(button == "play") {
		img = Obj("ch"+channel+"playimg");
		if(isOn == true) {
			img.src = STATE_PLAY_ON_IMG;
			if(channel == "od") {
				changeStateImage("od", "pause", false);
			} else {
				for(i=1;i<=NUM_OF_CHANNELS;i++) {
					if(i != channel) {
						changeStateImage(i, "play", false);
					}
					changeStateImage(i, "pause", false);
				}
			}
		} else {
			img.src = STATE_PLAY_OFF_IMG;
		}
	} else if(button == "pause") {
		img = Obj("ch"+channel+"pauseimg");
		if(isOn == true) {
			img.src = STATE_PAUS_ON_IMG;
			changeStateImage(channel, "play", false);
		} else {
			img.src = STATE_PAUS_OFF_IMG;
		}
	}
}

//////////////////////////////////////////////////
// this determines and sets which bandwidth stream to change to.
//////////////////////////////////////////////////
function switchBandwidth() {
	if(currentBandwidth == "low") {
		switchToHigh();
	} else if(currentBandwidth == "high") {
		switchToLow();
	}
}

//////////////////////////////////////////////////
// this changes the live channel ports list that the Live player uses so that the
// low bandwidth streams are used for dial up users.  all play and pause 
// buttons are also turned off.
//////////////////////////////////////////////////
function switchToLow() {
	liveChannelPortList = liveChannelLowPortList;
	player.sendEvent("PLAY", "false");
	for(i=1;i<=NUM_OF_CHANNELS;i++) {
		changeStateImage(i, "play", false);
		changeStateImage(i, "pause", false);
	} 
	currentChannel = 0;
	
	Obj("bwimg").src = HIGH_BW_IMG;
	Obj("curbw").innerHTML = DIALUP_BW_TXT;
	currentBandwidth = "low";
}

//////////////////////////////////////////////////
// this changes the live channel ports list that the Live player uses so that the
// high bandwidth streams are used.  all play and pause buttons are also turned off.
//////////////////////////////////////////////////
function switchToHigh() {
	liveChannelPortList = liveChannelHighPortList;
	player.sendEvent("PLAY", "false");
	for(i=1;i<=NUM_OF_CHANNELS;i++) {
		changeStateImage(i, "play", false);
		changeStateImage(i, "pause", false);
	} 
	currentChannel = 0;
	
	Obj("bwimg").src = DIALUP_BW_IMG;
	Obj("curbw").innerHTML = HIGH_BW_TXT;
	currentBandwidth = "high";
}

//////////////////////////////////////////////////
// this returns the current state of the player.
//////////////////////////////////////////////////
function getPlayerState() {
	return player.getConfig().state;
}

//////////////////////////////////////////////////
// this sets the amount of time to wait to load the live channel information
// and then calls the "reload" method after waiting that amount of time so
// that the live channel information is refreshed on the page every hour, 
// at the top of the hour.
//////////////////////////////////////////////////
function loadCurrentChannelInfo() {
	if(reloadTimeoutAmt < SIXTY_MINUTES_IN_MILLIS) {
		reloadTimeoutAmt = (60-currentMinute) * 60000;
	}

	setTimeout("reloadCurrentChannelInfo()", reloadTimeoutAmt);
}

//////////////////////////////////////////////////
// this forces the iFrame containing the live channel information to refresh
// its data so that the channels' information is changed.
//////////////////////////////////////////////////
function reloadCurrentChannelInfo() {
	Obj("liveChannelIFrame").src = "";
	Obj("liveChannelIFrame").src = "../programs/current.php";
	reloadTimeoutAmt = SIXTY_MINUTES_IN_MILLIS;
	loadCurrentChannelInfo();
}

//////////////////////////////////////////////////
// returns the first entry in 'onDemandURLList'.
//////////////////////////////////////////////////
function getPrimaryOnDemandURL() {
	return onDemandURLList[0];
}

//////////////////////////////////////////////////
// this creates the player for the On Demand page.
//////////////////////////////////////////////////
function createOnDemandPlayer() {
	var fv = "file="+getPrimaryOnDemandURL()+top.frames[0].progPort1+"/;stream.nsv&type=sound&repeat=always&volume="+currentVolume+"&autostart=false";
	var s1 = new SWFObject("player.swf","ply","300","0","9");
	s1.addParam("allowfullscreen","false");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("flashvars",fv);
	s1.write("player");
	currentChannel = "od";
	setTimeout("fillOnDemandPlaylist()", 1000);
	setTimeout("player.sendEvent('PLAY', 'true')", 1500);
	checkInvalidInterval = setInterval("checkForInactivePlayer()", FIFTEEN_SECONDS_IN_MILLIS);
	showNowLoading(currentChannel);
}

//////////////////////////////////////////////////
// this returns the URL for the programs_data PHP file.
// this file contains the information for the currently playing On Demand program.
//////////////////////////////////////////////////
function getOnDemandIFrameSrc() {
	return "../programs/programs_data.php?program=" + progCode;
}

//////////////////////////////////////////////////
// this creates and loads a playlist for the On Demand player based on the on demand URL list.
//////////////////////////////////////////////////
function fillOnDemandPlaylist() {
	var newOnDemandPlaylist = new Array();
	for(i=0;i< onDemandURLList.length;i++) {
		var url = onDemandURLList[i] + top.frames[0].progPort1 + "/;stream.nsv";
		newOnDemandPlaylist[i] = { file:url, type:'sound' };
	}

	player.sendEvent('LOAD', newOnDemandPlaylist);
}

//////////////////////////////////////////////////
// this shows the "now loading" message
//////////////////////////////////////////////////
function showNowLoading(channel) {
	var whichDiv = "loading_" + channel;
	Obj(whichDiv).style.display = "block";
	setTimeout("checkIfPlayerIsLoading(currentChannel)", 2000);
}

//////////////////////////////////////////////////
// this hides the "now loading" message
//////////////////////////////////////////////////
function hideNowLoading(channel) {
	var whichDiv = "loading_" + channel;
	Obj(whichDiv).style.display = "none";
}

//////////////////////////////////////////////////
// this checks to see if the player is playing or not.  if it is playing, the loading message 
// will be hidden and the loading interval will be cleared.
//////////////////////////////////////////////////
function checkIfPlayerIsLoading(channel) {
	if(getPlayerState() != STATE_IDLE) {
		hideNowLoading(channel);
	} else {
		setTimeout("checkIfPlayerIsLoading(currentChannel)", 2000);
	}
}
