/*

(C)2008 B-Lex IT

*/

function blexplaylist()
{
  this.activeelement = null;
  this.lists = [];
  this.medialist = {};
  this.mediaorder = [];

  this.currentmediaid  = null;
}

// start or continue playing from the current position in the list
blexplaylist.prototype.play = function blexplaylist_play()
{
  if (this.currentposition == null && this.mediaorder.length > 0)
    this.playitem(1);
}

blexplaylist.prototype.prev = function blexplaylist_prev()
{
  if (this.mediaorder.length == 0)
    return null;

  var itempos = this.getitemposition(this.currentmediaid);

  if (itempos==1)
    this.playitem(this.mediaorder.length);
  else
    this.playitem(itempos-1);
}

blexplaylist.prototype.next = function blexplaylist_next()
{
  if (this.mediaorder.length == 0)
    return null;

  var itempos = this.getitemposition(this.currentmediaid);

  if (itempos==this.mediaorder.length)
    this.playitem(1);
  else
    this.playitem(itempos+1);
}

blexplaylist.prototype.playitem = function blexplaylist_playitem(position)
{
  if (position <= 0 || position > this.mediaorder.length)
  {
    //console.error('playlist.playitem() got an invalid playlist position: '+position);
    return;
  }

  var mediaid = this.mediaorder[position-1];

  //console.log('playitem(%i) (which has mediaitem id "%s")', position, mediaid);

  this.currentmediaid = mediaid;
  this.playerinstance.loadnewmedia( this.medialist[mediaid] );
}


blexplaylist.prototype.setlist = function blexplaylist_setlist(list)
{
  // build two lists:
  // - mediaorder[ number ] = "mediaitemid"
  // - medialist[ "mediaitemid" ] = {}

  this.medialist = {};
  this.mediaorder = [];

  if (typeof list == 'undefined')
    return;

  var itemsamount = list.length;

  for(var itemid=0; itemid<itemsamount; itemid++)
  {
    var mediaid = list[itemid].rowkey;

    // when the mediaid has the same rowkey as the currently running mediaitem
    // then force an update of the cuepoints in the player
    if (mediaid == this.currentmediaid)
      this.playerinstance.updatecuepointslist( list[itemid].cuepoints );

    this.mediaorder[itemid] = mediaid;
    this.medialist[mediaid] = list[itemid];
  }
}

blexplaylist.prototype.getitemposition = function blexplaylist_getitemposition(finditemid)
{
  var itemcount = this.mediaorder.length;
  for(var itemid=0; itemid < itemcount; itemid++)
  {
    if (this.mediaorder[itemid] == finditemid)
      return itemid+1;
  }
  return 0;
}

// when using hookto on an element on the page,
// clicking within an element with the attribute mediaitem will activate
// the media from the medialist with the given name.
blexplaylist.prototype.hookto = function blexplaylist_hookto(elementid)
{
  var _playlistinstance = this;

  var elem = document.getElementById(elementid);

  this.lists[this.lists.length] = elem;
  elem.onclick = function(evt) { _playlistinstance.playselectedmedia(evt); };
}

blexplaylist.prototype.destroy = function blexplaylist_destroy()
{
  this.lists = []; // destroy references to elements with mediaitems
  this.activeelement = null;
}

// search through all medialists and highlight the item with the given mediaitem id
blexplaylist.prototype.highlight = function blexplaylist_highlight(mediaitemid)
{
  var listcount = this.lists.length;

  for(var listid=0; listid < listcount; listid++)
  {
    var node = this.lists[listcount].firstChild;
    while (node)
    {
      if (node.getAttribute && node.getAttribute('mediaitem') == mediaitemid)
      {
        this.movehighlight(node);
        return; // whe're done
      }

      node = node.nextSibling;
    }
  }
}

blexplaylist.prototype.playselectedmedia = function blexplaylist_playselectedmedia(evt) {

  if (!evt)
    evt = window.event;

  var target = evt.target ? evt.target : evt.srcElement;

  var node = target;
  var mediaitem = null;

  while(node && !(mediaitem=node.getAttribute('mediaitem')))
    node = node.parentNode;

  if (mediaitem)
  {
    if (this.playerinstance)
      this.playerinstance.loadnewmedia(this.medialist[mediaitem]);

    this.movehighlight(node);
  }
}

// visual selection of the mediaitem --------------------
blexplaylist.prototype.movehighlight = function blexplaylist_movehighlight(node)
{
  if (this.activeelement != null)
    removeClass(this.activeelement, 'activemedia');

  this.activeelement = node;

  addClass(this.activeelement, 'activemedia');
}

if (typeof toddCurrentLoads != 'undefined') toddCurrentLoads["blexmediaplayer/playlist.js"]=true;