/**
 * Object to support/improve JW Player API
 * (as documented at http://www.jeroenwijering.com/?item=Javascript_interaction)
 *
 *  Usage:
 *  var instance = new JW_FLV.player('playerID');
 *  instance.next();
 *  instance.jumpto(90);
 *  instance.play();
 *  instance.stop()
 *
 **/
var JW_FLV = (function()
{
  // private
  var JW_FLV = {}, players = {};
  var STOPPED = 0, BUFFERING = 1, PLAYING = 2, FINISHED = 3;  // states

  // public methods
  JW_FLV.getStatus = function(pid, key)
  {
    return players[pid][key];
  };

  JW_FLV.setStatus = function(pid, key, val)
  {
    if (! players[pid]){ players[pid] = {}; }
    return players[pid][key] = val;
  };

  JW_FLV.player = function(pid)
  {
    var origAPI = document.getElementById(pid) || document.getElementsByName(pid)[0];
    if (!origAPI){ throw 'Cannot find element with an id or name of ' + pid; }

    var newAPI =
    { // original API (to allow usage seen in forum examples)
      sendEvent:  function(key, evt) { return origAPI.sendEvent(key, evt); },
      loadFile:   function(obj)      { return origAPI.loadFile(obj);       },
      getLength:  function()         { return origAPI.getLength();         },
      addItem:    function(obj, key) { return origAPI.addItem(obj, key);   },
      removeItem: function(key)      { return origAPI.removeItem(key);     },
      itemData:   function(key)      { return origAPI.itemData(key);       },

      // new API methods
      // status accessors
      setStatus: function(key, val) { return JW_FLV.setStatus(pid, key, val); },
      getStatus: function(key)      { return JW_FLV.getStatus(pid, key);      },

      // status shortcuts
      elapsed:   function() { return JW_FLV.getStatus(pid, 'elapsed');   },
      remaining: function() { return JW_FLV.getStatus(pid, 'remaining'); },
      total:     function() { return JW_FLV.getStatus(pid, 'total');     },
      percent:   function() { return JW_FLV.getStatus(pid, 'percent');   },
      volume:    function() { return JW_FLV.getStatus(pid, 'volume');    },
      item:      function() { return JW_FLV.getStatus(pid, 'item');      },
      state:     function() { return JW_FLV.getStatus(pid, 'state');     },
      load:      function() { return JW_FLV.getStatus(pid, 'load');      },
      width:     function() { return JW_FLV.getStatus(pid, 'width');     },
      height:    function() { return JW_FLV.getStatus(pid, 'height');    },

      // action shortcuts
      play: function(ndx)
      {
	if (ndx){
	  origAPI.sendEvent('playitem', ndx);
	} else {
	  var state = newAPI.state();
	  if (!state || state === STOPPED || state === FINISHED)
	    origAPI.sendEvent('playpause');
	}
      },
      pause: function()
      {
	var state = newAPI.state();
	if (state === BUFFERING || state === PLAYING)
	  origAPI.sendEvent('playpause');
      },
      playpause: function()     { origAPI.sendEvent('playpause');     },
      stop:      function()     { origAPI.sendEvent('stop');          },
      prev:      function()     { origAPI.sendEvent('prev');          },
      next:      function()     { origAPI.sendEvent('next');          },
      scrub:     function(secs) { origAPI.sendEvent('scrub', secs);   },
      getlink:   function(ndx)  { origAPI.sendEvent('getlink', ndx);  },
      playitem:  function(ndx)  { origAPI.sendEvent('playitem', ndx); },

      // action aliases
      toggle: function()     { return origAPI.playpause(); },
      jumpto: function(secs) { return newAPI.scrub(secs);  },
      start:  function()     { return origAPI.play();      }

    }; // PUBLIC API

    return newAPI;
  };

  return JW_FLV;

})();

