/**
 * Machinimatrix Javascript code
 *
 * Machinimatrix is (c) 2010 The-Machinimatrix Reuse not allowed
 *
 */

    function createEntryPoints(id)
    {
        var element = document.getElementById(id);
        var tag = "div";
        var prep    = "<" + tag + " class='cuepoint-unselected' onClick='seekTo(this)'id='";
        var app     = "'>";

        var i;
        var content;
        if (cuePoints.length > 1)
        {
          content = "<div class=\"video-entry-points-label\"> Video entry points:</div>";

          for (i=1;i<cuePoints.length;i++)
          {
              content += prep + cuePoints[i] + app + labels[i] + "</" + tag + ">";
          }
          element.innerHTML = content;
        }
    }

    var lastCuepoint    = -1;
    var pendingCuepoint = -1;
    function queuePointHandler(cuepoint)
    {
        /*
            A pendingCuepoint is created when the video has not yet started from the very
            beginning. (see the resume and load functions below). The cuepoint 100 is
            just a marker: If this cuepoint is reached and there is a pendingCuepoint
            available, we immediately jump to that cuepoint. Otherwise we handle the
            cuepoint here (see else case)
        */
        if(cuepoint == 100)
        {
            if(pendingCuepoint > 0)
            {
                seekVideo(pendingCuepoint);
                pendingCuepoint = -1;
            }
        }
        else
        {
            // Another cuepoint has already been selected before.
            // Hence we cleanup the display:
            if(lastCuepoint > -1)
            {
               element = document.getElementById(lastCuepoint);
               if(element == null)
               {
                   alert("warning: cuepoint ["+lastCuepoint+"] not found");
               }
               element.className = "cuepoint-unselected";
               //element.style.color="black";
               //element.style.backgroundImage="none";
            }

            // And now setup the displa for the new cuepoint:
            lastCuepoint=cuepoint;
            element = document.getElementById(lastCuepoint);
            element.className = "cuepoint-selected";
            //element.style.color="red";
            //element.style.backgroundImage= "url('../img/jass_bullet_12_12.png')";
            //element.style.backgroundRepeat= "no-repeat";
        }
    }


    /*
        Convenience function. Get the seekpoint from the name of the given object (me)
        This is the trick with which we connect cuepoints with associated display elements.
    */
    function seekTo(me)
    {
       id = me.id;
       cuepoint = id / 1000;
       queuePointHandler(id)
       seekVideo(cuepoint);
    }

    /*
        Seek to the given second within the video. Note that the actual seek position will
        be dependent on the position of the keyframes. If the video has not enough keyframes,
        the allowed seekpositions can be significantly different from the anticipated positions.
        keyframes every 10 seconds can be expected.
    */
    function seekVideo(seconds)
    {
      var p = $f();
      if (p.getState() == 1) {
        _resumeAndSeek(p, seconds);
      } else if (p.isLoaded())
      {
        p.seek(seconds);
        if (!p.isPlaying())
        {
          p.play();
        }
      } else
      {
        _loadAndSeek(p, seconds);
      }
    }

    var initialLoad = true; // used to avoid multiple initialisations

    function _resumeAndSeek(p, seconds)
    {
      p.onStart(function () {
        if (!initialLoad) return; // Make sure we don't do this for future onStarts.
        initialLoad = false;
        p.seek(0); // first seek to the videos start (we have seen audio dropouts, if we don't do that!)
        if (!p.isPlaying())
        {
          p.play();
        }
        pendingCuepoint = seconds; // Store the true seekpoint and handle it right after start of the video. (see above)
      });
      p.play();
    }

    function _loadAndSeek(p, seconds)
    {
      p.onStart(function () {
        if (!initialLoad) return; // Make sure we don't do this for future onStarts.
        initialLoad = false;
        p.seek(0);
        if (!p.isPlaying())
        {
           p.play();
        }
        pendingCuepoint = seconds;
      });
      p.load();
    }


