var width_h1 = 23;
var width_li = 79;
var width_custom = 446;
var width_video = 446;
var width_episodes = 446;

var maxMargin = 0;
var maxItems = 0;
var loadProgress = 0;
var activeItem = 1;
var activeCustom = 1;
var videoItems = null;

var opened = true;
var nextEpisodeHTML = '';
var periodicalid = 0;

function startAutoslide()
{
  keepscrolling = true;
  $clear(periodicalid);
  if (activeCustom == nr_of_custom_items)
  {
    activeCustom = 2;
    periodicalid = slideCustomLeft.periodical(5000);
  }
  else
  {
    if (opened)
      periodicalid = slideCustomRight.periodical(5000);
  }
}

function hideLastEpisodeDiv()
{
  $$('div.lastepisode').fade('out');
  $$('div.lastepisodetxt').fade('out');
}

function showLastEpisodeDiv()
{
  if (!lifeview)
  {
    $$('div.lastepisode').fade('in');
    $$('div.lastepisodetxt').fade('in');
  }
}

function showNextEpisode()
{
  if ($('btn_custom_right'))
  {
    showtimeline();
    activeCustom = 2;
    keepscrolling = false;
    slideCustomLeft();
    $('a_next_episode').onclick();
  }
  else
  {
    showlivestream();
  }
}

function showtimeline(){
  if(opened == true){ return true; }
  $("redactie_wrapper").fade('in');
  $("video_wrapper").fade('in');
  $('redactie_wrapper').morph({'height':252});
  $('video_wrapper').morph({'height':252});
  $$(".redactie").tween('height', [0,252]);
  $$(".video").tween('height', [0,252]);
  $$("#breaker").tween('height', [0,14]);
  opened = true;
}

function hidetimeline(){
  if(opened == false){ return false; }
  clearActiveItem();
  activeitem = 0;
  slideVideoLeft();

  $("redactie_wrapper").fade('out');
  $("video_wrapper").fade('out');
  $('redactie_wrapper').morph({'height':0});
  $('video_wrapper').morph({'height':0});
  $$(".redactie").tween('height', [252,0]);
  $$(".video").tween('height', [252,0]);
  $$("#breaker").tween('height', [14,0]);

  opened = false;
}

function positionThumbSlider()
{
  var newMargin = width_episodes;
 
  for (var i = 0; i < video_info.length; i++)
  {
    if (video_info[i]['type'] == 'h1')
      newMargin -= width_h1;
    else
      newMargin -= width_li;
  }
 
  if (newMargin > 0)
    newMargin = 0;
 
  $('episodes_slider').style.marginLeft = newMargin + 'px';
  $('episodes_slider').style.width = ((newMargin * -1) + (width_episodes * 2)) + 'px';
 
  maxMargin = newMargin;
}

function fillThumbImage(i)
{
  if ( (video_info[i]['type'] == 'li') && ($('img'+i).src != video_info[i]['thumb']) )
  {
    $('img'+i).src = video_info[i]['thumb'];
    $('img'+i).getParent().style.background = 'none';
  }
}

function fillFirstThumbImages()
{
  var counter = 1;
  for (var i = (video_info.length - 1); i >= 0 && counter <= 10; i--)
  {
    loadProgress = i;
    fillThumbImage(i);
    counter++;
  }
  maxItems = loadProgress;
}

var slideThumbLeft = function()
{
  $('btn_thumb_right').removeClass('inactive');
  var currentMarginLeft = $('episodes_slider').getStyle('margin-left');
  var currentMarginLeft = Number(currentMarginLeft.substr(0, (currentMarginLeft.length - 2)));
  var newMargin = (currentMarginLeft + width_li);
  if (newMargin > 0)
    newMargin = 0;
  $('episodes_slider').morph({'margin-left':newMargin});
  loadProgress--;
  if (loadProgress < 0)
    loadProgress = 0;
  fillThumbImage(loadProgress);
  if (newMargin == 0)
    $('btn_thumb_left').addClass('inactive');
}

var slideThumbRight = function ()
{
  $('btn_thumb_left').removeClass('inactive');
  var currentMarginLeft = $('episodes_slider').getStyle('margin-left');
  var currentMarginLeft = Number(currentMarginLeft.substr(0, (currentMarginLeft.length - 2)));
  var newMargin = (currentMarginLeft - width_li);
  if (newMargin < maxMargin)
    newMargin = maxMargin;
  $('episodes_slider').morph({'margin-left':newMargin});
  loadProgress++;
  if (loadProgress > maxItems)
    loadProgress = maxItems;
  if (newMargin == maxMargin)
    $('btn_thumb_right').addClass('inactive');
}

function positionVideoSlider()
{
  $('video_slider').style.width = (nr_of_video_items * width_video) + 'px';
}

function fillVideoImage(i, pos)
{
  if (!pos)
    pos = 0;
  if ( $('video'+pos).getElement('img').src != video_info[i]['preview'] )
    $('video'+pos).getElement('img').src = video_info[i]['preview'];
  $('video'+pos).getElement('div.overlaytxt').innerHTML = video_info[i]['teaser'];
  $('video'+pos).getElement('div.startbtn').style.visibility = 'visible';
}

function fillFirstVideoImages()
{
  var counter = 1;
  var videoTeller = 1;
  for (var i = (video_info.length - 1); i >= 0 && counter <= 2; i--)
  {
    if (video_info[i]['showbig'])
    {
      fillVideoImage(i, videoTeller);
      videoTeller++;
      counter++;
    }
  }
}

function markActiveITem()
{
  if ($('img'+videoItems[activeItem]) && $('img'+videoItems[activeItem]).getParent())
    $('img'+videoItems[activeItem]).getParent().addClass('active');
}

function clearActiveItem()
{
  if ($('img'+videoItems[activeItem]) && $('img'+videoItems[activeItem]).getParent())
    $('img'+videoItems[activeItem]).getParent().removeClass('active');
}

var slideVideoLeft = function()
{
  startAutoslide();
  showLastEpisodeDiv()
  if (activeItem == 0)
    activeItem = 2;
  $('video').innerHTML = '';
  $('video').style.visibility = 'hidden';
  $('video0').style.display = 'none';
  $('video_slider').style.display = '';
  if (activeItem > 1)
  {
    $('btn_video_right').removeClass('inactive');
    clearActiveItem();
    activeItem--;
    var newMargin = ((activeItem - 1) * width_video * -1);
    $('video_slider').morph({'margin-left':newMargin});
    fillVideoImage(videoItems[activeItem], activeItem);
    markActiveITem();
    if (activeItem == 1)
      $('btn_video_left').addClass('inactive');
  }
}

var slideVideoRight = function ()
{
  startAutoslide();
  showLastEpisodeDiv()
  if (activeItem == nr_of_video_items)
    activeItem = nr_of_video_items - 1;
  $('video').innerHTML = '';
  $('video').style.visibility = 'hidden';
  $('video0').style.display = 'none';
  $('video_slider').style.display = '';
  if (activeItem < nr_of_video_items)
  {
    $('btn_video_left').removeClass('inactive');
    clearActiveItem();
    activeItem++;
    var newMargin = ((activeItem - 1) * width_video * -1);
    $('video_slider').morph({'margin-left':newMargin});
    fillVideoImage(videoItems[activeItem], activeItem);
    markActiveITem();
    if (activeItem == nr_of_video_items)
      $('btn_video_right').addClass('inactive');
  }
}

var resizeVideo = function()
{
/*
  var video = $$('object[data=data:application/x-silverlight-2,]');
  if (video && video[0])
  {
    video[0].width = '446';
    video[0].height = '252';
  }
*/
}

function playVideo()
{
  $clear(periodicalid);
  hideLastEpisodeDiv();
  $('video').innerHTML = video_info[videoItems[activeItem]]['stream'];
  $('video').setStyle('visibility','visible');
  $('video_slider').setStyle('display','none');
//  resizeVideo.delay('2000');
}

function playLastEpisode()
{
  $clear(periodicalid);
  hideLastEpisodeDiv()
  $('video').innerHTML = '';
  $('video').style.visibility = 'hidden';
  $('video0').style.display = 'none';
  $('video_slider').style.display = '';
  $('btn_video_left').removeClass('inactive');
  $('btn_video_right').removeClass('inactive');
  clearActiveItem();
  for (var i = (video_info.length - 1); i >= 0; i--)
  {
    if (video_info[i]['lastepisode'])
    {
      transferThumbToVideo(i);
      break;
    }
  }
/*
  var newMargin = ((activeItem) * width_video * -1);
  $('video_slider').morph({'margin-left':newMargin});
  fillVideoImage(videoItems[activeItem], activeItem);
  markActiveITem();
*/
  playVideo();
}

function playCustomVideo(a)
{
  $clear(periodicalid);
  clearActiveItem();
  hideLastEpisodeDiv();
  $('video').set( 'html', '<iframe id="iframe" allowtransparency="true" scrolling="no" frameborder="0" style="overflow: hidden; width: 446px; height: 252px;" src="'+a+'"></iframe>');
  $('video').setStyle('visibility','visible');
  $('video_slider').setStyle('display','none');
}

function playVideoOld(a)
{
  clearActiveItem();
  hideLastEpisodeDiv();
  $('video').setStyle('visibility','visible');
  $('video_slider').setStyle('display','none');
  $('video').set( 'html', '<iframe id="iframe" allowtransparency="true" scrolling="no" frameborder="0" style="overflow: hidden; width: 446px; height: 252px;" src="http://www.rtl.nl/%28/programma/life4you/inline_player_extern_new/%29/'+a+'"></iframe>' );
}

function transferThumbToVideo(i)
{
  showLastEpisodeDiv();
  showtimeline();
  clearActiveItem();
  $('btn_video_left').removeClass('inactive');
  $('btn_video_right').removeClass('inactive');
  $('video').innerHTML = '';
  $('video').style.visibility = 'hidden';
  $('video_slider').style.display = '';
  var found = false;
  for (j = 0; j < videoItems.length; j++)
  {
    if (videoItems[j] == i)
    {
      found = true;
      if (activeItem == j)
        break;
      activeItem = j;
      if (j == 0)
      {
        $('video_slider').style.marginLeft = '0px';
        $('video0').style.display = '';
      }
      else
      {
        var newMargin = ((activeItem - 1) * width_video * -1);
        $('video0').style.display = 'none';
        $('video_slider').morph({'margin-left':newMargin});
      }
      break;
    }
  }
  if (!found)
  {
    $('video_slider').style.marginLeft = '0px';
    $('video0').style.display = '';
    activeItem = 0;
  }
  videoItems[activeItem] = i;
  fillVideoImage(i, activeItem);
  markActiveITem();
  if (activeItem == 1)
    $('btn_video_left').addClass('inactive');
  if (activeItem == nr_of_video_items)
    $('btn_video_right').addClass('inactive');
  startAutoslide();
}

var slideCustomLeft = function()
{
  $clear(periodicalid);
  if (activeCustom > 1)
  {
    $('btn_custom_right').removeClass('inactive');
    activeCustom--;
    var newMargin = ((activeCustom - 1) * width_custom * -1);
    $('custom_slider').morph({'margin-left':newMargin});
    if (activeCustom == 1)
      $('btn_custom_left').addClass('inactive');
  }
  if (keepscrolling)
    startAutoslide();
}

var slideCustomRight = function ()
{
  $clear(periodicalid);
  if (activeCustom < nr_of_custom_items )
  {
    $('btn_custom_left').removeClass('inactive');
    activeCustom++;
    var newMargin = ((activeCustom - 1) * width_custom * -1);
    $('custom_slider').morph({'margin-left':newMargin});
    if (activeCustom == nr_of_custom_items)
      $('btn_custom_right').addClass('inactive');
  }
  if (!neverScroll)
    startAutoslide();
}

function initVideoItems()
{
  videoItems = Array(nr_of_video_items);
  var videoTeller = 1;
  for (var i = (video_info.length - 1); i >= 0; i--)
  {
    if (video_info[i]['showbig'])
    {
      videoItems[videoTeller] = i;
      videoTeller++;
    }
  }
}

var showTitle = function(e)
{
  var imgtag = '';
  if ( (e.target.tagName == 'A') || (e.target.tagName == 'LI') )
    imgtag = e.target.getElement('img[id^=img]').id;
  else
    imgtag = e.target.id
  var i = Number(imgtag.substr(3, 5));
  $('nextepisode').innerHTML = '<table height="48"><tr><td><p style="padding:0 15px 0 15px; font-size:15px; font-weight:bold;">'+video_info[i]['title']+'</p></td></tr></table>';
}

var restoreNextEpisode = function(e)
{
  $('nextepisode').innerHTML = nextEpisodeHTML;
}

var showlivestream = function()
{
//  playCustomVideo('http://www.rtl.nl/(/programma/life4you/inline_player_extern_new/)/components/programma/life4you/miMedia/livestream/live.xml');
  $clear(periodicalid);
  hideLastEpisodeDiv();
  $('video').innerHTML = livestream;
  $('video').setStyle('visibility','visible');
  $('video_slider').setStyle('display','none');
//  resizeVideo.delay('2000');
}

window.addEvent('domready', function()
{
  initVideoItems();
  positionThumbSlider();
  positionVideoSlider();
  fillFirstThumbImages();
  fillFirstVideoImages();
  $('btn_thumb_right').addEvent('click', slideThumbRight);
  $('btn_thumb_right').addClass('inactive');
  $('btn_thumb_left').addEvent('click', slideThumbLeft);
  $('btn_video_right').addEvent('click', slideVideoRight);
  $('btn_video_left').addEvent('click', slideVideoLeft);
  $('btn_video_left').addClass('inactive');
  if ($('btn_custom_right'))
  {
    if($('btn_custom_right') != null){
    	$('btn_custom_right').addEvent('click', slideCustomRight);
    }
    if($('btn_custom_left') != null){
  	  $('btn_custom_left').addEvent('click', slideCustomLeft);
  	  $('btn_custom_left').addClass('inactive');
    }
    if (keepscrolling)
      startAutoslide();
  }
  else
  {
//    $('nextepisode').style.cursor = 'default';
//    $().style.cursor = 'default';
  }
  if ($('img'+(video_info.length-1)) && $('img'+(video_info.length-1)).getParent())
    $('img'+(video_info.length-1)).getParent().addClass('active');
  $$('ul.episode_thumbs').getElements('li').each(function(el){
    el.addEvent('mouseenter', showTitle );
  });
  $('episodes').addEvent('mouseleave', restoreNextEpisode );
  nextEpisodeHTML = $('nextepisode').innerHTML;
  if (lifeview)
  {
    hideLastEpisodeDiv();
    showlivestream.delay(1000);
    $('nextepisode').innerHTML = '<img alt="" class="logo" src="getfile.php?id=2602"><p>Live de uizending bekijken<br>Klik dan hier</p>';
    nextEpisodeHTML = $('nextepisode').innerHTML;
  }
  doSpecialInit()
});

