var bcExp;
var modVP;
var modExp;
var modCon;
var modAds;

// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {
	//alert("EVENT: TEMPLATE_LOAD");
    bcExp = brightcove.getExperience(experienceID);
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	modAds = bcExp.getModule("advertising");
	modCon == bcExp.getModule(APIModules.CONTENT);
	//alert(modAds.getExternalAdsEnabled());
	
	//modVP.addEventListener("videoStart", onVideoStart);

	//modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
	//modAds.addEventListener("adStart", onAdStart);
	//modAds.addEventListener("externalAd", onAdComplete); 
	//modAds.addEventListen(BCAdvertisingEvent.START_CONTENT, onAdManagerStartContent);
	//callFlash("addEventListener", "adComplete", "onAdComplete");
	//callFlash("addEventListener", "contentLoad", "onContentLoad");
	//callFlash("addEventListener", "lineupLoad", "onLineupLoad");
	//callFlash("addEventListener", "mediaStart", "onTitleLoad");
}

/*
function onTemplateLoaded(message) {
	callFlash("enableAdFormats", 14, 2); //this enables frmt=2 and frmt=14 (the 468x60 and 728x90 video pods, respectively, to be "enabled" for the player so that they're sent in the ad call. 
	callFlash("addEventListener", "adStart", "onAdStart");
	callFlash("addEventListener", "adComplete", "onAdComplete");
	callFlash("addEventListener", "contentLoad", "onContentLoad");
	callFlash("addEventListener", "lineupLoad", "onLineupLoad");
	callFlash("addEventListener", "mediaStart", "onTitleLoad");
}
*/
function onVideoStart(evt) {
	alert('video start');
}

function onVideoLoad(evt) {
	alert('video loaded');
}

function onAdManagerStartContent(evt) {
	alert("content started");
}

function onTitleLoad() {
	idj_bc_ui.changeCurrentTrackTitle();
}

function onLineupLoad() {
	//alert('lineup loaded');
	//callFlash("loadTitleById", idj_bc_ui.pendingTrackId, "full");
	
}

function onAdStart(evt) {
	alert("ad started");
	idj_bc_ui.disableLineupSelector();
	idj_bc_ui.disableThumbnails();	
}

function onAdComplete(evt) {
	alert("ad complete");
	idj_bc_ui.enableLineupSelector();
	idj_bc_ui.enableThumbnails();
}


function IdjBrightcoveLineup(title, id) { 
	//, playerId) {
	this.title = title;	
	this.id = id;
	//this.playerId = playerId;
}

function IdjBrightcoveTitle(title, id) {
	this.title = title;
	this.id = id;	
}

function IdjBrightcoveUi() {
	this.lineups = new Array();
	this.isLineupChange = false;
	this.currentLineupIndex = 0;
	this.tracks = new Array();
	
	this.currentTrackTitle;
	this.currentTrackId;
	this.pendingTrackId;
	
	this.domEls = new Array();
	this.domEls["lineupDomWrapper"] = "bc_playlist";
	this.domEls["lineupSelectList"] = "bc_lineup_select";
	this.domEls["currentTrackTitle"] = "bc_current_title";	
	
	this.messages = new Array();
	this.messages["wait"] = "Please Wait...";
} // End BrightcoveUi class def.


IdjBrightcoveUi.prototype.disableLineupSelector = function() {
	var targ = document.getElementById(this.domEls["lineupSelectList"]);
	targ.disabled = true;	
}

IdjBrightcoveUi.prototype.enableLineupSelector = function() {
	var targ = document.getElementById(this.domEls["lineupSelectList"]);
	targ.disabled = false;	
}

IdjBrightcoveUi.prototype.disableThumbnails = function() {
	var targ = document.getElementById(this.domEls["lineupDomWrapper"]);
	$('div#bc_playlist').block();  
	//targ.style.visibility = "hidden";	
}

IdjBrightcoveUi.prototype.enableThumbnails = function() {
	var targ = document.getElementById(this.domEls["lineupDomWrapper"]);
	//targ.style.visibility = "visible";	
	$('div#bc_playlist').unblock();
}


IdjBrightcoveUi.prototype.writeBrightcoveTags = function() {
	
}

IdjBrightcoveUi.prototype.addLineup = function(title, id) {
	var tmp = new IdjBrightcoveLineup(title, id); //, playerId);
	this.lineups.push(tmp);	
}

IdjBrightcoveUi.prototype.getCurrentLineup = function() {
	return this.lineups[this.currentLineupIndex];
}

IdjBrightcoveUi.prototype.addTitle = function(newTitle, newId) {
	var tmp = new IdjBrightcoveTitle(newTitle, newId);
	this.tracks[newId] = tmp;	
}

IdjBrightcoveUi.prototype.initLineupSelector = function() {
	var targ = document.getElementById(this.domEls["lineupSelectList"]);

	var input_params = new Array();
	var tmp = window.location.search;
	tmp = tmp.substring(1);
	tmp = tmp.split("&");
	for (i in tmp) {
		tmp2 = tmp[i].split("=");
		input_params[tmp2[0]] = tmp2[1];
	}
	var len = this.lineups.length;
	
	for (var i=0; i<len; i++) {
		var tmp = document.createElement("option");
		tmp.value = this.lineups[i].id; // + "_" + this.lineups[i].playerId;
		tmp.innerHTML = this.lineups[i].title;
		targ.appendChild(tmp);	
		
		if (this.lineups[i].id  == input_params["bclid"]) {
			targ.selectedIndex = i;	
		}
	}	
		
}

IdjBrightcoveUi.prototype.doLineupChange = function() {
	var el = document.getElementById(this.domEls["lineupSelectList"]);
	var arr = (el[el.selectedIndex].value).split("_");
	var lineupId = arr[0];
	//var playerId = arr[1];
	this.currentLineupIndex = el.selectedIndex;
	//this.getAjaxLineup(playerId, lineupId);
	this.getAjaxLineup(lineupId);
	//window.location.href = "av.php?bcpid=" + playerId + "&bclid=" + lineupId;
	//window.location.href = window.location.protocol + window.location.pathname + "?bcpid=" + playerId + "&bclid=" + lineupId;
} // End doLineupChange

IdjBrightcoveUi.prototype.getAjaxLineup = function(lineupId) {
	document.getElementById(this.domEls["lineupDomWrapper"]).innerHTML = '<div class="bc_message">' + this.messages['wait'] + '</div>';
	// do the ajax bit:
	var reqUrl = "/www2/lib2/brightcove/ajax_requests/get_lineup_bc3.php"  + '?bclid=' + lineupId;

	var onSuccessFunc = function(req) { handleLineupsResponse(req); }
	var onErrorFunc = function(req) { alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText); }
	var onTimeOutFunc = function(req) { alert(req.statusText); }
	var req = AjaxRequest.get({'url': reqUrl, 'onSuccess': onSuccessFunc, 'onError': onErrorFunc, 'timeout':6000, 'onTimeout': onTimeOutFunc });
}

function handleLineupsResponse(reqObj) {

	var retObj = eval("(" + reqObj.responseText + ")");
	var counter = 1;	
	var targ = document.getElementById("bc_playlist");
	targ.innerHTML = "";
	idj_bc_ui.tracks = new Array();
	idj_bc_ui.isLineupChange = true;

	for (var i=0; i<retObj.data.length; i++) {
		var tmp = document.createElement("div");
		tmp.className = "track_item";
		tmp.id = "track_" + counter;
		var tmp2 = document.createElement("div");
		tmp2.className = "thm";
		var anch = document.createElement("a");
		anch.href = "#";
		anch.onclick = new Function('idj_bc_ui.playTrack(' + retObj.data[i].id + '); return false;');
		anch.innerHTML = '<img src="' + decodeURI(decodeURIComponent(retObj.data[i].thumbnails['120x90'])) + '" />';
		tmp2.appendChild(anch);
		var tmp3 = document.createElement("div");
		tmp3.className = "play";
		var anch = document.createElement("a");
		anch.href = "#";
		anch.onclick = new Function('idj_bc_ui.playTrack(' + retObj.data[i].id + '); return false;');
		anch.innerHTML = decodeURI(decodeURIComponent(retObj.data[i].title));
		tmp3.appendChild(anch);
		idj_bc_ui.addTitle(retObj.data[i].title, retObj.data[i].id);
		tmp.appendChild(tmp2);
		tmp.appendChild(tmp3);
		targ.appendChild(tmp);	
	}
}




IdjBrightcoveUi.prototype.renderLineup = function() {
	// This would be implemented under an Ajax model
} // End renderLineup
/*
IdjBrightcoveUi.prototype.playTrack = function(newId) {
	callFlash("loadTitleById", newId, "full");
	this.changeCurrentTrackTitle(newId);
} // End playTrack()
*/

IdjBrightcoveUi.prototype.playTrack = function(newId) {
	this.pendingTrackId = newId;
	document.getElementById(this.domEls["currentTrackTitle"]).innerHTML = "Loading...";

    //bcExp = brightcove.getExperience(experienceID);
    //modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	modVP.loadVideo(newId);

	this.changeCurrentTrackTitle();
	/*
	if (this.isLineupChange == false) {	
		//alert('existing lineup');
		//callFlash("loadTitleById", newId, "full");
		//this.changeCurrentTrackTitle();
	} else {
		//alert(this.lineups[this.currentLineupIndex].id);
		//alert(this.lineups[this.pendingTrackId].id);
		
		callFlash("fetchLineupById", this.lineups[this.currentLineupIndex].id); //this.lineups[this.pendingTrackId].id);
		this.isLineupChange = false;	
	}
	*/
} // End playTrack()


IdjBrightcoveUi.prototype.changeCurrentTrackTitle = function() {

	this.currentTrackTitle = this.tracks[this.pendingTrackId].title; //this.tracks[newId].title;
	this.currentTrackId = this.pendingTrackId;
	this.pendingTrackId = null;
	document.getElementById(this.domEls["currentTrackTitle"]).innerHTML = decodeURI(decodeURIComponent(this.currentTrackTitle)); //unescape(trackTitle);
}

// -------------------------------
