var ie6 = false;
var items = new Array();
var current = 0;
var previous;
var click = false;
var running = true;
var myTimer;

function profiler()
{
  // Profiler-Einträge speichern
  items = $('#meldungen .meldung');
  
  // Alle ausblenden, ersten einblenden
  show_start(current);
  
  // Navigation verlinken
  $(items).find('.meldung_navi a').click(function()
  {
    window.clearTimeout(myTimer);
    if ($(this).hasClass('run') ) {
      if ($(this).hasClass('pause') ) {
        $(this).removeClass('pause');
        $(this).addClass('play');
        $(this).attr('title','Abspielen');
        running = false;
      }
      else
      {
        $(this).removeClass('play');
        $(this).addClass('pause');
        $(this).attr('title','Anhalten');
        running = true;
        click = true;
        var toShow = current + 1;
        if (toShow > items.length - 1)
        {
          toShow = 0;
        }
        show_item(toShow);
      }
    }
    else
    {
      $(items[current]).find('.meldung_navi a.play').addClass('pause');
      $(items[current]).find('.meldung_navi a.play').attr('title','Anhalten');
      $(items[current]).find('.meldung_navi a.play').removeClass('play');
      running = true;
      click = true;
      show_item(parseInt($(this).text()) - 1);
    }
  });
  
  // Overlay triggern
  $("a.link_video[rel]").overlay({
    mask: { color: '#000000', opacity: 0.7 },
    onBeforeLoad: function() {
      if (ie6 == true)
      {
        $('#select_themen').hide();
      }
      window.clearTimeout(myTimer);
      running = false;
    },
    onClose: function() {
      if (ie6 == true)
      {
        $('#select_themen').show();
      }
      $f("player").stopBuffering();
      running = true;
      click = true;
      var toShow = current + 1;
      if (toShow > items.length - 1)
      {
        toShow = 0;
      }
      show_item(toShow);
    }
  });
  
  /*
  $("a#a_close").click(function() {
    if (ie6 == true)
    {
      $('#select_themen').show();
    }
    $f("player").stopBuffering();
		running = true;
    click = true;
    var toShow = current + 1;
    if (toShow > items.length - 1)
    {
      toShow = 0;
    }
    show_item(toShow);
	});
	*/
  
  // Videos verlinken
  $("a.link_video").click(function(event) {
    var videourl = $(this).attr("video");
    $f("player", "http://www.brot-fuer-die-welt.de/swf/flowplayer.commercial-3.2.0.swf", {
      key: '#@b3ea608f7d00ba0ce74',
      plugins: {controls: conf.skins.gross},
      play: conf.plays.gross,
      clip: conf.defaults.single,
      playlist: [
        {
          url: videourl,
          autoPlay: true
        }
      ]
    });
	});
  
}

function show_item(pID)
{
  if (click == true)
  {
    previous = current;
  }

  current = pID;
  
  // Hintergrundbild in Container einsetzen
  if (click == false)
  {
    previous = current - 1;
    if (previous < 0)
    {
      previous = items.length - 1;
    }
  }
  
  click = false;
  
  // Meldungen richtig stapeln (z-index)
  $(items).css('z-index','0');
  $(items[previous]).css('z-index','1');
  $(items[pID]).css('z-index','2');
  // Gewünschtes einblenden
  $(items[pID]).css('opacity','0');
  $(items[pID]).animate({
    opacity: 1.0
  }, 1000, function() {
    runTimer();
  });
}

function show_start(pID)
{
  current = pID;
  
  click = false;
  
  // Meldungen richtig stapeln (z-index)
  $(items).css('z-index','0');
  $(items[pID]).css('z-index','2');
  
  $(items).css('opacity','0');
  $(items[pID]).animate({
    opacity: 1.0
  }, 1000, function() {
    runTimer();
  });
}

function runTimer()
{
  var next;
  if (current < items.length - 1)
  {
    next = current + 1;
  }
  else
  {
    next = 0;
  }
  if (running == true)
  {
    myTimer = window.setTimeout('show_item(' + next + ')', 3000);
  }
  
}

$(window).load(function() {

  // IE6 ?
  if (navigator.appVersion.indexOf('MSIE 5') != -1 || navigator.appVersion.indexOf('MSIE 6') != -1)
  {
    ie6 = true;
  }
  
  // Box einblenden
  $('#meldungen').show();
  
  // Texte platzieren (an unteren Rand rücken)
  $('.meldung_inhalt').each(function(index) {
    var margin = - $(this).outerHeight();
    $(this).css('marginTop',margin);
  });

  profiler();
  
});

