button_setImageDir("images/");
button_prepareButton("layouthome", "layout_onHome", "button_bw_start.png", "button_bw_start_f2.png", "button_bw_start_f3.png", "button_bw_start_f4.png", true, "An den Anfang springen");
button_prepareButton("layoutrewind", "layout_onRewind", "button_bw.png", "button_bw_f2.png", "button_bw_f3.png", "button_bw_f4.png", false, "Folie zurück");
button_prepareButton("layoutplay", "layout_onPlay", "button_play.png", "button_play_f2.png", "button_play_f3.png", "button_play_f4.png", false, "Wiedergabe starten / Pause");
button_addButtonState("layoutplay", "1", "button_pause.png", "button_pause_f2.png", "button_pause_f3.png", "button_pause_f4.png", false);
button_prepareButton("layoutforward", "layout_onForward", "button_ffw.png", "button_ffw_f2.png", "button_ffw_f3.png", "button_ffw_f4.png", false, "Folie vorwärts");
button_prepareButton("layoutend", "layout_onEnd", "button_ffw_end.png", "button_ffw_end_f2.png", "button_ffw_end_f3.png", "button_ffw_end_f4.png", false, "An das Ende springen");
button_prepareButton("layoutmute", "layout_onMute", "button_volume_f3.png", "button_volume_f2.png", "button_volume.png", "button_volume_f4.png", true, "Ton an/aus");
button_addButtonState("layoutmute", "1", "button_volume.png", "button_volume_f2.png", "button_volume_f3.png", "button_volume_f4.png", false);

var g_playerHeight = 88;
var g_minHeaderWidth = 320;
var g_minHeaderHeight = 130;
var g_minThumbsHeight = 70;

var g_overviewOffset = 150;

var timeSlider = null;
var volumeSlider = null;
//var playerButton = null;

// Variables for the layout
var headerWidth = -1;
var headerHeight = -1;
var thumbsWidth = -1;
var thumbsHeight = -1;
var videoWidth = -1;
var videoHeight = -1;
var slidesWidth = -1;
var slidesHeight = -1;
var playerWidth = -1;
var playerHeight = 88;

var g_hasWarnedNS4 = false;
var g_isSliding = false;
var g_wasPlaying = false;

var g_timeLabel = null;
var g_statusLabel = null;
var g_statusLabelDisplay = null;
var g_lastDisplayedTimeMs = -1000;
var g_layoutIgnoreTimeChange = false;

var isIE = false;

if (document.all)
  isIE = true;
      
function s(id)
{
  if (isNS4)
    return eval('document.' + id);
  else
    return document.getElementById(id).style;
}

function o(id)
{
  if (isNS4)
    return s(id);
  return document.getElementById(id);
}

function layout_onPlay()
{
  control_playPause();
}

function layout_onMute()
{
  control_mute();
}

function layout_onHome()
{
  control_seekTime(0);
}

function layout_onRewind()
{
  var currentSlide = layout_getCurrentSlide();
  if (currentSlide > 0)
    control_seekTime(g_thumbEntries[currentSlide - 1].timeStamp);
  else
    layout_onHome();
}

function layout_onEnd()
{
  control_seekTime(g_mediaDurations[0]);
}

function layout_onForward()
{
  var currentSlide = layout_getCurrentSlide();
  if (currentSlide < g_thumbCount - 1)
    control_seekTime(g_thumbEntries[currentSlide + 1].timeStamp);
  else
    layout_onEnd();
}

function layout_getStatusLabel()
{
  if (g_statusLabel == null)
    g_statusLabel = getHtmlObject('', 'BufferLabel', '', false, true);
  return g_statusLabel;
}

function layout_displayIsBuffering(bDisplay, bufferingProgress)
{
  var statusDiv = layout_getStatusLabel();
  if (bDisplay)
  {
    if (bufferingProgress >= 0)
      statusDiv.innerHTML = 'Pufferung: ' + bufferingProgress + '%';
    else
      statusDiv.innerHTML = 'Status: Pufferung';
  }
  else
    statusDiv.innerHTML = '';

  g_statusLabelDisplay = bDisplay;
}

function layout_displayIsDownloading(bDisplay)
{
  var statusDiv = layout_getStatusLabel();
  if (bDisplay)
    statusDiv.innerHTML = 'Lade...';
  else
    statusDiv.innerHTML = '';
}

function layout_getSlide(timeMs)
{
  var slideNr = -1;
  for (var i=0; i<g_thumbCount-1; ++i)
  {
    if ((timeMs >= g_thumbEntries[i].timeStamp &&
        timeMs < g_thumbEntries[i+1].timeStamp) ||
	  (i == 0 && timeMs < g_thumbEntries[i+1].timeStamp))
      slideNr = i;
  }
  if (slideNr == -1)
    slideNr = g_thumbCount-1;
  return slideNr;
}

function layout_getCurrentSlide()
{
  var timeMs = control_getTimeMs();
  return layout_getSlide(timeMs);
}

function layoutInit()
{
  setSlidesNS4Prefix('document.layers["slidesDiv"].');

  if (!isNS4)
  {
    // delete the overview listbox; it's only used in netscape 4
    if (document.getElementById('OverviewList'))
      document.getElementById('OverviewList').innerHTML = '';

    timeSlider = new LSlider('timeSlider');
    timeSlider.attachOnChange(layout_onTimeChange);
    timeSlider.attachOnSlideStart(layout_onSlideStart);
    timeSlider.attachOnSlideEnd(layout_onSlideEnd);
    timeSlider.width         = 320;
    timeSlider.height        = 25;
    timeSlider.minVal        = 0;
    timeSlider.maxVal        = g_mediaDurations[0];
    timeSlider.valueDefault  = 0;
    timeSlider.valueInterval = 1000;
    timeSlider.imgDir = '';
    timeSlider.setBackgroundImage('images/playersliderbg.jpg', 'repeat');
    timeSlider.setSliderIcon('images/sliderknob.png', 14, 25);
    timeSlider.useInputField = 0;
    timeSlider.draw('timeSliderDiv');
    
    volumeSlider = new LSlider('volumeSlider');
    volumeSlider.attachOnChange(layout_onVolumeChange);
    volumeSlider.width = 92;
    volumeSlider.height = 38;
    volumeSlider.minVal = 0;
    volumeSlider.maxVal = 100;
    try
    {
      // This may fail in some cases; wrap in a try... catch-block
      // just to be sure.
      volumeSlider.valueDefault = control_getVolume();
    }
    catch (er)
    {
      volumeSlider.valueDefault = 50;
    }
    volumeSlider.valueInterval = 1;
    volumeSlider.imgDir = 'images/';
    volumeSlider.setBackgroundImage('player_volume.png', 'no-repeat');
    volumeSlider.setSliderIcon('volumeknob.png', 14, 15);
    volumeSlider.useInputField = 0;
    volumeSlider.draw('volumeSliderDiv');
    
    if (!g_isStandaloneMode)
      s('thumbsFrame').visibility = 'visible';

    resizeLayout();
  }

  layout_enableControl("home", true);
  layout_enableControl("rewind", true);
  layout_enableControl("forward", true);
  layout_enableControl("end", true);
  layout_updateTime(0);
}

function layout_onSlideStart(obj)
{
  g_isSliding = true;
  control_beginSlide();
}

function layout_onSlideEnd(obj)
{
  g_isSliding = false;
  control_endSlide();
}

function layout_onTimeChange(obj)
{
  if (g_isSliding || (!g_isSliding && !g_layoutIgnoreTimeChange))
    control_seekTime(obj.getValue());
}

function layout_onVolumeChange(obj)
{
  control_setVolume(obj.getValue());
}

function calcSizes(width, height)
{
  // We want to match width x height as good as possible

  // First, the heights
  playerHeight = 88;
  slidesHeight = height - 88;
  if (slidesHeight < g_slidesHeight + 20)
    slidesHeight = g_slidesHeight + 20; // Smaller than minimum height
  
  headerHeight = g_minHeaderHeight;
  if (g_hasVideo)
    videoHeight = g_videoHeight + 14 + 28; // border is 14, status bar 28
  else
    videoHeight  = g_stillImageHeight + 7 + 14 + 28; // 1 + 14 + 28
  
  thumbsHeight = slidesHeight - headerHeight - videoHeight;
  if (thumbsHeight < g_minThumbsHeight)
  {
    // Thumbs stretch the slides in height
    thumbsHeight = g_minThumbsHeight;
    slidesHeight = headerHeight + videoHeight + thumbsHeight;
  }

  // Now, the widths; this is more tricky
  // We're going from left to right
  headerWidth = g_minHeaderWidth;
  if (g_hasVideo && headerWidth < g_videoWidth + 18)
    headerWidth = g_videoWidth + 18;
  if (g_hasStillImage && headerWidth < g_stillImageWidth + 18)
    headerWidth = g_stillImageWidth + 18;
  thumbsWidth = headerWidth;
  videoWidth = headerWidth;

  slidesWidth = g_slidesWidth + 30;
  if (slidesWidth < width - headerWidth)
    slidesWidth = width - headerWidth;

  playerWidth = slidesWidth + headerWidth;

  if (g_isStandaloneMode)
  {
    playerWidth = g_videoWidth + 33;
    if (playerWidth < 549)
      playerWidth = 549;
  }

  //alert("header: " + headerWidth + "x" + headerHeight + ", thumbs: " + thumbsWidth + "x" + thumbsHeight + ", video: " + videoWidth + "x" + videoHeight + ", slides: " + slidesWidth + "x" + slidesHeight + ", player: " + playerWidth + "x" + playerHeight);
}

function resizeLayout()
{
  if (isNS4)
  {
    if (!g_hasWarnedNS4)
      alert("Das Verändern der Größe des Netscape Browserfensters kann zu \nunvorhergesehenen Ergebnissen führen, während diese Seite angezeigt wird. \nBeim Auftreten von Fehlern laden Sie bitte die Seite neu.");
    g_hasWarnedNS4 = true;
    return;
  }

  var width; 
  if (!isIE)
    width = window.innerWidth - 30;
  else
    width = document.body.offsetWidth - 45;
  var height;
  if (!isIE)
    height = window.innerHeight - 30;
  else
    height = document.body.offsetHeight - 35;

  calcSizes(width, height);

  s('playerwidth').width = playerWidth - 529;
  var tmpValue = timeSlider.getValue();
  timeSlider.width = playerWidth - 304;
  timeSlider.render('timeSliderDiv');
  timeSlider.setValue(tmpValue);

  if (g_isStandaloneMode)
    return;
  
  s('headerwidth').width = headerWidth - 101;
  s('headerheight').height = headerHeight - 20;
  s('thumbswidth').width = thumbsWidth - 98;
  s('thumbsheight').height = thumbsHeight - 14;
  s('slideswidth').width = slidesWidth - 30;
  s('slidesheight').height = slidesHeight - 20 + 2; // padding of header cells

  s('videowidth').width = videoWidth - 98;
  s('videoheight').height = videoHeight - 14;

  s('thumbsFrame').top = headerHeight + 20;
  s('thumbsFrame').width = thumbsWidth - 14;
  s('thumbsFrame').height = thumbsHeight - 16;
}

// PlugIn Detection
function detectPlugins()
{
  if (canDetectPlugins())
  {
    if (!detectWindowsMedia())
      alert("Das Windows Media PlugIn konnte nicht erkannt werden! \nMögliche Ursachen: \n- Das Windows Media PlugIn ist nicht installiert. \n- In Ihrem Browser sind ActiveX-Steuerelemente deaktiviert.\n- Keine Neuinstallation des WMP nach Installation von Netscape/Mozilla/Firefox.");
  }
  else
    alert("Die automatische PlugIn-Erkennung schlug fehl! \nMöglicherweise sind nicht alle notwendigen PlugIns installiert.");
}

