/**
 * @fileoverview
 * This file implements the HDA Player interface to be used by clients wanting
 * to implement a custom player.
 *
 * @version 2.1
 * @author Copyright 2006 H-care s.r.l.
 */
var HDAPlayer = {
  /*
   * You can use this property to tell the gateway where the Flash files are
   * located. It can be a relative directory or a full URL.
   */
  flashRoot: '',

  /*
   * You can pass arbitrary values to the player by setting this property.
   */
  flashVars: '',

  /*
   * You can set the player size by setting these properties.
   */
  width: 109,
  height: 113,

  /**
   * Initialize and start the player
   *
   * When calling this function, the innerHTML of the element with the
   * provided id will be replaced with the player SWF. You don't need to put
   * embed or object tags for the player, they will be created by this
   * function.
   *
   * @param {String} playerContainerId
   *    the id of the HTML element (usually a <div> in which the player will be
   *    injected
   * @param {String} uid
   *	the id of the connection with the Flash client. Optional, if not
   * 	specified it is equal to playerContainerId
   */
  start: function(playerContainerId, uid) {
  	if (!uid) uid = playerContainerId;
    this.initFlashProxy(uid);
    this.playerContainerId = playerContainerId;
    this.uid = uid;
    this.writePlayer(playerContainerId, uid);
  },

  writePlayer: function(playerContainerId, uid) {
    if (!uid) uid = playerContainerId;
    var element = document.getElementById(playerContainerId);
    var tag = new FlashTag(this.flashRoot + 'HDAPlayer.swf', this.width, this.height);
    tag.setVersion("7,0,0,0");
    tag.setId("HDAPlayer");
    tag.setBgcolor("ffffff");
	tag.setFlashvars('fr='+this.flashRoot+'&lc=' + uid+'&l='+this.width+'&h='+this.height);
	element.innerHTML = tag.toString();
  },

  // Parse the <meta> tags and extract context and keywords informations
  sendMeta: function() {
    var meta = document.getElementsByTagName('meta');
    var res = new Array();
    for (var i = 0; i < meta.length; i++) {
      if (meta[i].name != null && meta[i].name.indexOf('hda-') == 0) {
        var attribute = meta[i].name.substring(4, meta[i].name.length);
        res[attribute] = meta[i].content;
      }
    }
    if (res && res['key']) {
      this.sendEvent(res['key'], res['value'], 'page');
    }
  },

  /**
   * Initialize the Flash to Javascript proxy
   *
   * This method can be used to initialize the proxy from pages where you don't
   * want to activate the player, but communicate with an existing player
   * somewhere else (on another frame, perhaps).
   *
   * @param {String} uid
   *	the id of the connection with the Flash client
   */
  initFlashProxy: function(uid) {
  	if (this.flashVars.indexOf('fr=') < 0) {
  	  this.flashVars += 'fr=' + this.flashRoot;
  	}
    this.flashProxy = new FlashProxy(uid, this.flashRoot + 'JavaScriptFlashGateway.swf');
  },

  listeners: new Object(),

  /**
   * Register the event listeners
   *
   * The method registers the listeners used to handle the events coming from
   * the player.
   *
   * A listener method is considered valid and added to the player if its name
   * starts with 'on'. A listener will be called in response to events whose
   * name is equal to the lower cased method name, with the 'on' part stripped.
   * For example a method named 'onLoadUrl' will be called for 'loadurl'
   * events.
   *
   * @param {Object} listeners
   *    an object containing the functions to be used as event listeners.
   */
  registerListeners: function(listeners) {
    this.listeners = new Object(); // always clean up the listeners

    for (e in listeners) {
      if (e.substring(0, 2) != 'on' || typeof listeners[e] != 'function') {
        // ignore non valid listeners
        continue;
      }
      var eventName = e.substring(2).toLowerCase();
      this.listeners[eventName] = listeners[e];
    }
  },

  /*
   * Internal method, called by the Flash player to activate the event
   * listeners. Clients should not use this.
   *
   * @param eventName
   *    the name of the event
   * @ignore
   */
  fireEvent: function(eventName) {

    var params = [];
    for (var i = 0; i < arguments.length; i++) {
      params[i] = arguments[i];
    }

    if (typeof HDAPlayer.listeners[eventName] == 'function') {
      HDAPlayer.listeners[eventName].apply(this, params.slice(1));
    } else if (typeof HDAPlayer.listeners.fallback == 'function') {
      HDAPlayer.listeners.fallback.apply(this, params);
    }
  },

  /**
   * Send an event to the player
   *
   * This method is the primary mean of communication from the outside world to
   * the player: for example it can be used from the pages to send events in
   * response to user actions.
   *
   * Params is a string formatted as an URL query string: name=value pairs
   * separated by & characters. Values must be encoded as
   * application/x-www-form-urlencoded MIME format.
   *
   * @param {String} key
   *    the event key
   * @param {String} params
   *    the event params, additional information the page might want to send
   * @param {String} kind
   *    can be used to specify the event type. Optional and currently unused.
   */
  sendEvent: function(key, params, kind) {
    this.flashProxy.call('sendEvent', { key: key, evnt: params, kind: kind });
  },

  /**
   * Ask the player to load a node or a rule
   *
   * This method is normally used internally by the framework, usually to
   * explicitly load specific content in response to menu or load actions.
   *
   * As it overrides part of the application logic, clients should carefully
   * consider using this method only if really needed.
   *
   * @param {Number} node
   *    the id of the node to load
   * @param {Number} rule
   *    the id of the rule to load
   * @param {String} params
   *    attributes to be added to the clue
   */
  sendLoad: function(node, rule, params) {
    var event = '';
    if (node) {
      event = 'node=' + node;
    } else if (rule) {
      event = 'rule=' + rule;
    } else {
      return;
    }
    if (params) {
      event += '&' + params;
    }
    this.flashProxy.call('sendEvent', { evnt: event, kind: 'load' });
  },

  play: function() {
    this.flashProxy.call('playVideo');
  },

  stop: function() {
    this.flashProxy.call('stopVideo');
  },

  skip: function() {
    this.flashProxy.call('skipVideo');
  },

  repeat: function() {
    this.flashProxy.call('repeatVideo');
  },

  help: function() {
    this.flashProxy.call('help');
  },

  toggleVideo: function() {
    this.flashProxy.call('toggleVideo');
  }

}

// Gestisce tutti i messaggi FSCommand in un filmato Flash
function HDAPlayer_DoFSCommand(command, args) {
//	alert(command+" "+args);
	var argsArray = eval(args);
	HDAPlayer.fireEvent(argsArray[1],argsArray[2]);
}
