//Use the find function to select a catagory and play a video
var flexApp;
var flexAppVideo;
var obj;

function getQueryVariable(variable) { 
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++) { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable) { 
      return pair[1]; 
    } 
  } 
//  alert('Query Variable ' + variable + ' not found'); 
}

function changePlaylistItem(up) {
	/*if (up) { document.getElementById("misc").innerHTML = "Up"; }
	else { document.getElementById("misc").innerHTML = "Down"; }*/
	
	obj = flexApp.js_changePlaylistItem(up);
	
	/*document.getElementById("misc").innerHTML += " TITLE :: " + obj.title +"<br />";
	document.getElementById("misc").innerHTML += " TITLE :: " + obj.bannerAction +"<br />";
	document.getElementById("misc").innerHTML += " TITLE :: " + obj.bannerImage +"<br />";*/
	
	play(obj.url, obj.title, obj.bannerAction, obj.bannerImage);
}

function play(video,title,action,img) {
	//document.getElementById("videoId").innerHTML = "FLEX is now playing :: ";
	//document.getElementById("videoId").innerHTML += " :: " + flexAppVideo.js_setVideo(video,title,action,img);
	var s = flexAppVideo.js_setVideo(video,title,action,img);
}

function nowPlaying(title) {
	//document.getElementById("videoId").innerHTML = title;
}

function videoLoaded() {
	/*document.getElementById("playlistId").innerHTML = "Returned from FLEX ";
	document.getElementById("playlistId").innerHTML += " URL :: " + flexApp.js_getVideoUrl() +"<br />";
	document.getElementById("playlistId").innerHTML += " TITLE :: " + flexApp.js_getVideoTitle() +"<br />";
	document.getElementById("playlistId").innerHTML += " TITLE :: " + flexApp.js_getVideoBannerAction() +"<br />";
	document.getElementById("playlistId").innerHTML += " TITLE :: " + flexApp.js_getVideoBannerImage() +"<br />";*/
	
	play(flexApp.js_getVideoUrl(),flexApp.js_getVideoTitle(),flexApp.js_getVideoBannerAction(),flexApp.js_getVideoBannerImage());
}

function flashVideoLoaded() {
}

function flashLoaded() {
}

var initCallback1 = function()
{
	//var playFromSearch = getQueryVariable("playFromSearch");
	
	try {
		flexApp = FABridge.flex.root();
		//document.getElementById("playlistId").innerHTML = "playlist bridge ready";

		//if (playFromSearch) {
		//	play("http://office.aws.lifewaretek.net/mp3/download/" + playFromSearch, '', '', '');
		//}
	}
	catch(err) {
		//document.getElementById("playlistId").innerHTML = err;
		alert("Playlist: " + err);
	}
}



var initCallback2 = function()
{	
	var playFromSearch = getQueryVariable("playFromSearch");
	
	try {
		flexAppVideo = FABridge.flexVideo.root();
		
		clearInterval(videoIntervalId);
		
		if (playFromSearch) {
			play("http://office.aws.lifewaretek.net/mp3/download/" + playFromSearch, '', '', '');
		}
		
		//document.getElementById("videoId").innerHTML = "video bridge ready";
	}
	catch(err) {
		//alert("Video: " + err);
		
		videoIntervalId = setInterval ("initCallback2()", 500);

		//document.getElementById("videoId").innerHTML = err;
	}
}

FABridge.addInitializationCallback("flex",initCallback1);
FABridge.addInitializationCallback("flexVideo",initCallback2);

