// global variable, holds reference to the Flex application
var flexApp;  
var phoenixObject;

function getPhoenixObject() {
   if(phoenixObject==null && phoenixObject==undefined) {
      flexApp = FABridge.flash.root();
      phoenixObject = flexApp.phoenixBridge();
   }
   return phoenixObject;  
} 

function onBodyLoad() {
  phoenix.setListeners(onAudioEvent, onConfigEvent, onMetadata);
  phoenix.setPlatformUrl('http://metadata.phoenixweb.baracoda.com/PhoenixWeb/Access');
}

function authenticate(userId, serviceId) {
  spy('onBodyLoad');
  onBodyLoad();
  spy('authenticate');
  phoenix.authenticate(userId, 'test/1.0', 'fr', onAuthenticated);
}

function getUserId(serviceId) {
	phoenix.getUserId('test/1.0', 'fr', onAuthenticated);
}

function getContextualInformation(id, type, info) {
 

	var info0 = new Object();
       info0.id = 1;
       info0.type = 'info';
       info0.description = 'Ecoutez toutes vos radios préférées et des émissions en podcast !';
       info0.title = '';
       info0.importance = '0';
       info0.linkName = 'Découvrez le player radio!';
 /*
 
        var info0 = new Object();
       info0.id = 1;
       info0.type = 'info';
       info0.description = 'Oasis en ce moment sur Oui FM';
       info0.title = '';
       info0.importance = '0';
       info0.cover = 'http://metadata.phoenixweb.baracoda.com/phoenix/people/pictures/blur.jpg';
       info0.link = 'http://www.google.fr/';
       info0.linkName = 'Ecoutez Oui FM';

	var info1 = new Object();
       info1.id = 2;
       info1.type = 'info';
       info1.description = 'David Guetta en ce moment sur Fun';
       info1.title = '';
       info1.importance = '0';
       info1.link = 'http://www.google.fr/';
       info1.linkName = 'David mixe en live sur Fun !';
 
	var info2 = new Object();
       info2.id = 3;
       info2.type = 'info';
       info2.description = 'Le dernier épisode du Fou du Roi est disponible';
       info2.title = '';
       info2.importance = '0';
       info2.cover = 'http://metadata.phoenixweb.baracoda.com/phoenix/albums/covers/liberta_feat_djazia_peps.jpg';
       info2.link = 'http://www.google.fr/';
       info2.linkName = 'Retrouvez votre podcast préféré';
	
	var infos = [info0, info1, info2];

*/
        var infos = [info0];
	
	// Invocation de la m�thode AS registered dans Main.mxml
	document["PlayerPopupBRadio"].js_contextualInformation(infos);

}

function audio_play() {
	spy('play');
	phoenix.getAudio().play();
}

function audio_pause() {
	spy('play');
	phoenix.getAudio().pause();
}

function audio_stop() {
	spy('stop');
	phoenix.getAudio().stop();
	spy('end stop');
}

function audio_setVolume(vol) {
	spy('start audio_setVolume: '+vol);
	phoenix.getAudio().setVolume(vol);
	spy('end audio_setVolume');
}

function audio_getVolume() {
	return phoenix.getAudio().getVolume();
}

function audio_next() {
	spy('next');
}

function audio_previous() {
	spy('previous');
}
 
function onAudioEvent(audioStatus) {
  spy('new audio status: ' + audioStatus);
  
  getPhoenixObject().js_onAudioEvent(audioStatus);
}

function onConfigEvent(type, name, url) {
  spy('config event: missing plugin (' + name + ', ' + url + ')');
  
  getPhoenixObject().js_onConfigEvent(type, name, url);
}

function onMetadata(metadata, playlist) {
  spy('new metadata: ' + safeString(metadata.trackName));
  
  getPhoenixObject().js_onMetadata(metadata, playlist);
}

function onAuthenticated(res, value, errorCode, reason) {
  if(!res) {
    spy('error during authenticate: ' + errorCode + ', ' + reason);
    return;
  }
  
  if(phoenix.getToken())
  spy('token: ' + phoenix.getToken());
  
  spy('isLogged: ' + phoenix.isLogged());
  spy('name: ' + phoenix.getName());

  spy('authenticate successful !');

  var info0 = new Object();
  info0.id = 8766;
  info0.name = 'A La Une';
  info0.type = 'category';
  info0.isContainer = 'true';
  
  getPhoenixObject().js_authenticate(info0);
}

function onProductAction(id, type, isContainer) {
  spy('got click on ' + id);
  
  if(isContainer=="true") {
  	phoenix.open(id, type, onCatalog);
  } else {
  	phoenix.clearMetadataList();
  	phoenix.inspect(id, onProductInspect);
  }
}

function onProductInspect(res, product, errorCode, reason) {
  if(!res) {
    spy('error during inspect: ' + errorCode + ', ' + reason);
    return;
  }

  product.play();
  product.subscribe();
  
  getPhoenixObject().js_inspect();
}

function safeString(str) {
  return str ? str : '(none)';
}

function spy(msg) {
  //getPhoenixObject().js_setDebugMeg(msg);
}

function appendDiv(div, str) {
  $(div).innerHTML = $(div).innerHTML + str;
}

function getToken() {
	return phoenix.getToken();
}

function getCatalog() {
	phoenix.openRoot('catalog', onCatalog);
}

function onCatalog(res, objects, errorCode, reason) {
  if(!res) {
    spy('error during open: ' + errorCode + ', ' + reason);
    return;
  }

  spy('open successful !');
  
  var letters = extractObjects(objects);
  getPhoenixObject().js_onCatalog(letters);
}

function onContainer(res, objects, errorCode, reason) {
  if(!res) {
    spy('error during open: ' + errorCode + ', ' + reason);
    return;
  }

  spy('open successful !');
  
  var letters = extractObjects(objects);
  getPhoenixObject().js_onContainer(letters);
}

function search(query, filter) {
  phoenix.search(query, filter, onSearch);
}

function onSearch(res, objects, errorCode, reason) {
  if(!res) {
    spy('error during open: ' + errorCode + ', ' + reason);
    return;
  }

  spy('search successful !');
  
  var letters = extractObjects(objects.get('by name'));
  getPhoenixObject().js_search(letters);
}

function extractObjects(objects) {
  var letters = $A();
  objects.each(function(o) {
    letters.push(extractObject(o));
  });
  return letters;
}

function extractObject(obj) {
  return obj.letter;
}

