



function setupSeekBar() {

setTimeout(initFlashControls, 100);

var captionsArray = new Array();
var captionCurrent = -1;

function loadCaptions()
{
 //alert("loadCaptions");
 var i, x, frame, cl;


 for(i = 0; x = document.getElementsByTagName('div')[i]; ++i) {
  cl    = x.getAttribute('class')
  frame = x.getAttribute('frame');

  if(cl == "caption" && frame) {
   captionsArray[frame] = x;
  }
 }

}

function debugCaptions() {
 alert("debugCaptions");
 var debug = "debug: ";

 for (var i in captionsArray) {
  debug = debug + i + "=" + captionsArray[i] + " ";
 }
 alert(debug);
}

function showCaption(flash,x)
{
 var width = flash.getAttribute("width");
 

 var delFields = document.getElementById('caption');
 var newFields = captionsArray[x].cloneNode(true);

 newFields.id = 'caption';
 newFields.style.display = 'block';
 newFields.style.width = (parseInt(width) - 20)  + "px";
  

 try {
   delFields.parentNode.replaceChild(newFields,delFields);
 }
 catch(x){
  alert("showCaption");
 }
 
}

function showCaptionFrame(flash)
{
  var x = flash.CurrentFrame();
  var z;
  for (var y in captionsArray) {
   if(y > x) break;
   z = y;
  }
  if(z) {
   if(z != captionCurrent) {
    captionCurrent = z;
    showCaption(flash,z);
   }
  }
}



function initFlashControls()
{
  var count = 0;

  function tt(elem)
  {
    if (typeof elem.TotalFrames != "undefined") /* do not coerce elem.StopPlay to bool, because that breaks IE */
    {
      addFlashControls(elem);
      ++count;
    }
  }

  var i, x;

  for (i = 0; x = document.getElementsByTagName("object")[i]; ++i)
    tt(x);

  for (i = 0; x = document.getElementsByTagName("embed")[i]; ++i)
    tt(x);

 //alert("initFlashControls");
 loadCaptions();
}


function addFlashControls(flash)
{
  var controlsDiv = document.createElement("div");
  controlsDiv.setAttribute("style","margin-top: 10px; margin-bottom: 10px;");

  /* Put the controls under the Flash. 
   *
   * If the Flash is an <embed> in an <object>, we do not want to touch the <object>, because that would make
   * Mozilla re-test whether the <object> is broken and reset the <embed>.  So in that case, we put the controls
   * under the <object>.
   */
  var where = flash;
  while (where.parentNode.tagName.toLowerCase() == "object")
    where = where.parentNode;
  where.parentNode.insertBefore(controlsDiv, where.nextSibling);

  /* Construct controls using DOM2 instead of innerHTML.
   * In Mozilla, innerHTML= is like innerText= at standalone flash URLs.
   */
  var table = document.createElement("table");
  controlsDiv.appendChild(table);
  
  var row = table.insertRow(-1);
  
  var pauseButton = document.createElement("button");
  pauseButton.appendChild(document.createTextNode("Pause"));
  var buttonCell = row.insertCell(-1);
  buttonCell.appendChild(pauseButton);

  
  var slider = row.insertCell(-1);
  slider.width = "100%";
  
  var visibleSlider = document.createElement("div");
  visibleSlider.style.position = "relative";
  visibleSlider.style.height = "10px";
  visibleSlider.style.width = "100%";
  visibleSlider.style.MozBorderRadius = "4px";
  visibleSlider.style.background = "#aaa";
  slider.appendChild(visibleSlider);
  
  var thumb = document.createElement("div");
  thumb.style.position = "absolute";
  thumb.style.height = "20px";
  thumb.style.width = "10px";
  thumb.style.top = "-5px";
  thumb.style.MozBorderRadius = "4px";
  thumb.style.background = "#666";
  visibleSlider.appendChild(thumb);
  

  var timeCell = row.insertCell(-1);
  var timeText = document.createTextNode("----");
  timeCell.appendChild(timeText);


  var sliderWidth;
  var paused = false;
  var dragging = false;

  table.width = Math.max(parseInt(flash.width) || 0, 400);
  
  addEvent(pauseButton, "click", pauseUnpause);
  addEvent(slider, "mousedown", drag);
  addEvent(slider, "drag", function() { return false; }); /* For IE */
  window.setInterval(update, 30);

  function pauseUnpause()
  {
    paused = !paused;

    pauseButton.style.borderStyle = paused ? "inset" : "";

    if (paused)
      flash.StopPlay();
    else
      flash.Play();
  }

  function update()
  {
    sliderWidth = parseInt(getWidth(slider) - getWidth(thumb));

    if (!paused && !dragging) {
      thumb.style.left = parseInt(flash.CurrentFrame() / totalFrames() * sliderWidth) + "px";
      updateTimeText();
      showCaptionFrame(flash);
   }
  }

  function updateTimeText()
  {
   //timeText.nodeValue = parseInt( flash.CurrentFrame() / totalFrames() * 100) + "%";
   var st = flash.CurrentFrame() / 12;
   var m =  parseInt(st / 60);
   var s =  parseInt(st - m * 60);
   if(String(m).length < 2) m = "0" + String(m);
   if(String(s).length < 2) s = "0" + String(s);
   timeText.nodeValue = m + ":" + s;
  }

  function dragMousemove(e)
  {
    var pageX = e.clientX + document.body.scrollLeft; /* cross-browser, unlike e.pageX, which IE does not support */
    var pos = bounds(0, pageX - getX(slider) - 5, sliderWidth);
    var frame = bounds(1, Math.ceil(totalFrames() * pos / sliderWidth), totalFrames() - 2);

    thumb.style.left = pos + "px";

    flash.GotoFrame(frame);

    updateTimeText();
  }

  function release(e)
  {
    removeEvent(document, "mousemove", dragMousemove);
    removeEvent(document, "mouseup", release);
    if (!paused)
      flash.Play();
    dragging = false;
  }

  function drag(e)
  {
    addEvent(document, "mousemove", dragMousemove);
    addEvent(document, "mouseup", release);
    dragging = true;
    dragMousemove(e);
  }



  /* Boring functions, some of which only exist to hide differences between IE and Mozilla. */

  function bounds(min, val, max)
  {
    return Math.min(Math.max(min, val), max);
  }

  function totalFrames()
  {
    /* This is weird.  TotalFrames differs between IE and Mozilla.  CurrentFrame does not. */

    if (typeof flash.TotalFrames == "number")
      return flash.TotalFrames; /* IE */
    else if (typeof flash.TotalFrames == "function")
      return flash.TotalFrames(); /* Mozilla */
    else
      return 1; /* Partially loaded Flash in IE? */
  }

  function getWidth(elem)
  {
    if (document.defaultView && document.defaultView.getComputedStyle)
      return parseFloat(document.defaultView.getComputedStyle(elem,null).getPropertyValue("width")); /* Mozilla */
    else
      return parseFloat(elem.offsetWidth); /* IE (currentStyle.width can be "auto" or "100%") */
  }

  function getX(elem)
  {
    if (!elem) return 0;
    return (elem.offsetLeft) + getX(elem.offsetParent);
  }

  function addEvent(elem, eventName, fun)
  {
    if (elem.addEventListener) /* Mozilla */
      elem.addEventListener(eventName, fun, false);
    else /* IE */
      elem.attachEvent("on" + eventName, fun);
  }

  function removeEvent(elem, eventName, fun)
  {
    if (elem.addEventListener)
      elem.removeEventListener(eventName, fun, false);
    else
      elem.detachEvent("on" + eventName, fun);
  }

}

}
