$(function() {
  $('.show_program').live('click', showProgramma);
  $('#close').live('click', hideProgramma);
  $('.programma-dagen li:not(.last) a').live('click', showDag);
  $('a[href^=http://dmsonline.wellnesselande.nl]').click(openSubscribeForm);
  $('a.subscribe_close').live('click', closeSubscribeForm);
});

function closeSubscribeForm(evt) {
  evt.preventDefault();
  $('#programma-overlay').fadeOut(50);
  $('#subscribe_lb').remove();
}

function openSubscribeForm(evt) {
//  if(console) {
    evt.preventDefault();
    
    $('#programma-overlay').fadeIn(50, function(e) { $(this).css('opacity', .75); }).height($('body').innerHeight()).width($('body').innerWidth());
    
    $('body').append('<div class="programma" id="subscribe_lb"><iframe src="'+$(this).attr('href')+'"></iframe><a class="subscribe_close">x</a></div>');
    
//  }  
}

function showProgramma(evt) {
  evt.preventDefault();
  $('#programma-overlay').fadeIn(50, function(e) { $(this).css('opacity', .75); }).height($('body').innerHeight()).width($('body').innerWidth());
  
  var showBox = function() {
    if(jQuery.support.opacity !== true) {
      $('#programma').fadeIn(150).css({'top': $('html').scrollTop() + 10, 'position':'absolute'});
      $(window).bind('scroll', repositionProgramma);
    } else {
      $('#programma').fadeIn(150);
    }
  };
  if($('#programma .programma-body').html() == '') {
    $.ajax({
      url: '/ajax/programma/',
      dataType: 'html',
      type: 'get',
      beforeSend: function(xhr) {
        $('#programma-loader').show(0);      
      },
      success: function(data, status) {
        $('#programma .programma-body').html(data)
        $('#programma-loader').hide(0);
        showBox();
      }
    });
  } else {
    showBox();
  }
}

function hideProgramma(evt) {
  evt.preventDefault();
  $('#programma').fadeOut(150);
  $('#programma-overlay').fadeOut(50);
  $('#programma-loader').hide(0);
  $(window).unbind('scroll', repositionProgramma);
  
}

function repositionProgramma(evt) {
  $('.programma').css('top', $('html').scrollTop() + 10);
}

function showDag(evt) {
  evt.preventDefault();
  $('.programma-dagen li.active').removeClass('active');
  $('.programma-body tbody:not(.hidden)').addClass('hidden');
  $(this).parent().addClass('active');
  $('.programma-body tbody#'+$(this).attr('rel')).removeClass('hidden');
}
