var bc_slider = null;
var bc_volSlider = null;
var bc_gCurrentVideoId = null;
var bc_gArrayOfVideos = null;
var bc_gIsPlaying = false;
var bc_gIsSliding = false;
var bc_gCurrentPosition = null;
var bc_gCurrentVolume = 1;
var bc_gVideoLength = null;
var bc_gCurrentSpot = null;
var bc_gMuted = false;
var bc_gCurrentVol = 100;

var config = new Array();
config["videoRef"] = null; //the default video loaded into the player by ref id specified in console
config["playerTag"] = null; //player tag used for identifying this page in brightcove reporting
config["autoStart"] = false; //tells the player to start playing video on load
config["preloadBackColor"] = "#eaecf0"; //background color while loading the player
config["externalAds"]= true;
config["height"] = 1;
config["width"] = 1;

var BC_SCRUBBER_WIDTH = 225;
var BC_VOLUME_SCRUBBER_WIDTH = 112;

function bc_onLoadExtended() { 
	bc_slider = new Control.Slider(
	'bc_scrubber', 
	'bc_scrubberContainer', 
	{
		onChange: function(value) {
			bc_handleScrubChange(value);
		},
		onSlide: function(value) {
			bc_handleSlideChange(value);
		}
	});
	
	bc_volSlider = new Control.Slider(
	'bc_volScrubber', 
	'bc_volScrubberContainer', 
	{
		onChange: function(value) {
			bc_handleVolumeChange(value);
		},
		onSlide: function (value) {
			bc_handleVolumeSlide(value);
		},
		sliderValue:1
	});
	
	bc_fixShadowHeight();
}

function onTemplateLoaded(message) {
	callFlash("addEventListener", "contentLoad", "bc_onContentLoad");
	callFlash("addEventListener", "mediaStart", "bc_onMediaStart");
	callFlash("addEventListener", "mediaComplete", "bc_mediaComplete");
	callFlash("addEventListener", "progress", "bc_onProgress");
}

function bc_onContentLoad() {
	$('bc_loading').style.display = "none";
	callFlash("getCurrentTitle");
	callFlash("getVolume");
	callFlash("getPlayerLineups");
}

function getPlayerLineups_Result(pArrayLineups) {
	try {
		for(var i=0; i<pArrayLineups.length; i++) {
			if(pArrayLineups[i].id == bc_gLineupId) {
				bc_gArrayOfVideos = pArrayLineups[i].videoIds;
			}
		}
	} catch (e) {}
}

function getCurrentTitle_Result(pTitleDTO) {
	if(bc_gCurrentVideoId != null) {
		try {

			$("bc_playButton_" + bc_gCurrentVideoId).style.display = "none";
		} catch (e) {}
	}
	
	try {
		$("bc_playButton_" + pTitleDTO.id).style.display = "block";
	} catch (e) {}
	
	bc_gCurrentVideoId = pTitleDTO.id;
	bc_gVideoLength = pTitleDTO.length/1000;
	var songname = "";
	var artist = "";
	try {
		artist = (pTitleDTO.linkText != null) ? " by " + pTitleDTO.linkText : "";
		songname = pTitleDTO.displayName.split("_")[1] + artist;
	} catch (e) {
		
	}
	$('bc_titleName').innerHTML = songname;
	$('bc_totalTime').innerHTML = milToMin(Math.floor(pTitleDTO.length/1000));
}

function bc_onMediaStart(pEvt) {
	if(bc_gStartTime != null) {
		callFlash("seek", bc_gStartTime);
		bc_gStartTime = null;
	}
	callFlash("getCurrentTitle");
	bc_gIsPlaying = true;
	$('bc_playButton').style.display = "none";
	$('bc_pauseButton').style.display = "block";
}

function bc_mediaComplete(pEvt) {
	$('bc_playButton').style.display = "block";
	$('bc_pauseButton').style.display = "none";
	bc_gIsPlaying = false;
	bc_playNext();
}

function getVolume_Result(pVolume) {
	if(bc_gCurrentVolume != pVolume/100) {
		bc_volSlider.setValue(pVolume/100);
	}
}

function bc_handleSlideChange(pVal) {
	bc_gIsSliding = true;
	var width = Math.ceil(pVal * BC_SCRUBBER_WIDTH);
	$('bc_playedContainer').style.width = width + "px";
}

function bc_handleScrubChange(pVal) {
	if(bc_gCurrentPosition != pVal) {
		var width = Math.ceil(pVal * BC_SCRUBBER_WIDTH);
		$('bc_playedContainer').style.width = width + "px";
		bc_gCurrentSpot = Math.ceil(pVal * bc_gVideoLength)
		callFlash("seek", bc_gCurrentSpot);
		bc_gIsSliding = false;
	}
}

function bc_handleVolumeChange(pVal) {
	if(bc_gCurrentVolume != pVal) {
		var width = Math.ceil((pVal * BC_VOLUME_SCRUBBER_WIDTH));
		$('bc_volumeLevelContainer').style.width = width + "px";
		bc_gCurrentVolume = pVal;
		callFlash("setVolume", (pVal * 100));
		if(pVal == 0) {
			$('bc_muteButton').style.display = "none";
			$('bc_unMuteButton').style.display = "block";
			bc_gMuted = true;
		} else if(bc_gMuted) {
			$('bc_muteButton').style.display = "block";
			$('bc_unMuteButton').style.display = "none";
			bc_gMuted = false;
		}
	}
}

function bc_handleVolumeSlide(pVal) {
	if(bc_gCurrentVolume != pVal) {
		var width = Math.ceil((pVal * BC_VOLUME_SCRUBBER_WIDTH));
		$('bc_volumeLevelContainer').style.width = width + "px";
		bc_gCurrentVolume = pVal;
		callFlash("setVolume", (pVal * 100));
		if(pVal == 0) {
			$('bc_muteButton').style.display = "none";
			$('bc_unMuteButton').style.display = "block";
			bc_gMuted = true;
		} else if(bc_gMuted) {
			$('bc_muteButton').style.display = "block";
			$('bc_unMuteButton').style.display = "none";
			bc_gMuted = false;
		}
	}
}

function bc_play() {
	callFlash("startVideo");
}

function bc_pause() {
	callFlash("pauseVideo", true);
	$('bc_playButton').style.display = "block";
	$('bc_pauseButton').style.display = "none";
	bc_gIsPlaying = false;
}

function bc_playNext() {
	for(var i =0; i < bc_gArrayOfVideos.length; i++) {
		if(bc_gArrayOfVideos[i] == bc_gCurrentVideoId + "") {
			bc_playAudio(bc_gArrayOfVideos[i + 1]);
			return;
		}
	}
}

function bc_playPrevious() {
	for(var i =0; i < bc_gArrayOfVideos.length; i++) {
		if(bc_gArrayOfVideos[i] == bc_gCurrentVideoId + "") {
			bc_playAudio(bc_gArrayOfVideos[i - 1]);
			return;
		}
	}
}

function bc_mute() {
	$('bc_muteButton').style.display = "none";
	$('bc_unMuteButton').style.display = "block";
	callFlash("setVolume", 0);
	bc_gMuted = true;
	bc_volSlider.setValue(0);
}

function bc_unMute() {
	$('bc_unMuteButton').style.display = "none";
	$('bc_muteButton').style.display = "block";
	callFlash("setVolume", 100);
	bc_gMuted = false;
	bc_volSlider.setValue(1);
}

function bc_onProgress(pObj) {
	pVal = pObj.parameters.position;
	if(!bc_gIsSliding && pVal && bc_gCurrentPosition != pVal) {
		if(bc_gVideoLength) {
			var pos = pVal / bc_gVideoLength;
			bc_gCurrentPosition = pos;
			bc_slider.setValue(pos);
			var width = Math.ceil((pVal * BC_SCRUBBER_WIDTH) / bc_gVideoLength);
			$('bc_playedContainer').style.width = width + "px";
			$('bc_currentTime').innerHTML = milToMin(Math.floor(pVal));
		}
	}
}

function milToMin(sec) {
	var min = Math.floor(sec/60);
	sec = sec % 60;
	var t = two(sec);// + ":" + t
		
	var hr = Math.floor(min/60);
	min = min % 60;
	t = two(min) + ":" + t;
		
	var day = Math.floor(hr/60);
	var hr = hr % 60;
	if (hr > 0){
		t = two(hr) + ":" + t;
	}
			//t = day + ":" + t
	if(t.charAt(0) == "0"){
		t = t.substring(1, t.length);
	}	
	return t;
}

//helper function for milToMin() -- milliseconds to minutes
function two(x) {return ((x>9)?"":"0")+x}
//helper function for milToMin() -- milliseconds to minutes
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}


/************************************************************************************
 * Events for the audio Controls
 ************************************************************************************/
function bc_controlMouseOver(pId) {
	$(pId).style.display = "block";
}

function bc_controlMouseOut(pId) {
	
	if(!(pId.indexOf(bc_gCurrentVideoId) > -1)) {
		$(pId).style.display = "none";
	}
}

function bc_playMouseOver() {
	$('bc_playButton').className = "bc_playButtonOver";
}

function bc_playMouseOut() {
	$('bc_playButton').className = "bc_playButton";
}


function bc_fixShadowHeight() {
	try {
		$('audio_leftShade').style.height = ($('bc_rightColumnWrapper').offsetHeight - 78) + "px";
		$('audio_rightShade').style.height = ($('bc_rightColumnWrapper').offsetHeight - 75) + "px";
	} catch (e) {
		
	}
}

/**********************************************************************************
 * Function not related to the controls
 **********************************************************************************/
function bc_playAudio(pId) {
	callFlash("loadTitleById", pId, "full");
}

function bc_openWindow() {
	bc_pause();
	window.open("./player.do?bcpid=" + bc_gPlayerId + "&bclid=" + bc_gLineupId + "&bctid=" + bc_gCurrentVideoId +"&time=" + bc_gCurrentSpot, "player", "width=501, height=120");
}