$(document).ready( function() {
  // on load, we activate today's date
  var d = new Date();
  var day = leadingZeroCheck(d.getDate());
  var month = leadingZeroCheck(d.getMonth() + 1);
  var year = d.getFullYear();

  var date_id;
  var date_id_stored = $.cookie('date_id_stored');
  
  if (date_id_stored == null) {
    date_id = 'tf-calendar-day-'+year+'-'+month+'-'+day;
  } else {
    date_id = date_id_stored;
  }
  
  activateDay(date_id);
  
  //Make the clicked date active
  $('.tf-calendar-day a').click( function() {
    date_id = $(this).parent().attr('id');
    $('#tf-calendar-dropdown').slideToggle('500');
    activateDay(date_id);
    $('#tf-calendar-dropdown').slideToggle();
    return false;
  });
  
  //Parallelprogramma menu behaviour
  $('.tf-parallel-menu-level-1 li').click( function() {
    $.cookie('parallel_active_parent',this.id);
  });
  
  // We fetch the active parent 
  var activeMenuItem = $.cookie('parallel_active_parent');
  
  // Open up the children of the active parent
  if (activeMenuItem != null) {
    $('#'+activeMenuItem+' ul').show();
    $('#'+activeMenuItem).addClass('selected');
  }
  
  $('#radio-angelo-link').click( function() {
    var url = 'http://fmbrussel.be/player/player.aspx?stream=fmbrussel32&live=true';
    window.open(url, 'fmbrusselplayer', 'toolbar=0,scrollbars=0,location=0,status=0,menubar=1,resizable=1,width=255,height=170,left = 301,top = 232');  
  });
    
} );

function activateDay(date_id) {
  // We make other days inactive  
  deActivateDays();
  
  $.cookie('date_id_stored',date_id);
  
  // We make this day active
  $('#'+date_id+' .calendar-day-image').hide();
  $('#'+date_id+' .calendar-day-image-hover').hide();
  $('#'+date_id+' .calendar-day-image-active').show();
  
  $('#tf-calendar-dropdown').html('');
  
  var arg = date_id.substring(16,27);
  // We fetch the correct content via AJAX
  $.ajax({
    type: "get",
    //url: "/dropdown/"+arg,
    url: "/dropdown/"+arg,
    dataType: "json",
    success: function(data,status) {
	  var nodes = data.items;
	  
	  var selectionClass;
      var selection;
	  for (var i in nodes) {
        selection = nodes[i].selectie;
		if (selection.match("Keuze")) {selectionClass="selectie-de-keuze";}
        if (selection.match("Parallelprogramma")) {selectionClass="selectie-parallelprogramma";}
        if (selection.match("Keuze uit")) {selectionClass="selectie-tf1";}
       
	    $('#tf-calendar-dropdown').append('<div class="tf-calendar-event '+selectionClass+'"><div class="tf-calendar-event-text"><a href="/node/'+nodes[i].nid+'"><div class="tf-calendar-event-wie">'+nodes[i].wie+'</div></a><div class="tf-calendar-event-title">'+nodes[i].title+'</div></div>');
      }
    }
  });
  
}

function deActivateDays() {
  $('.tf-calendar-day .calendar-day-image').show();
  $('.tf-calendar-day .calendar-day-image-hover').hide();
  $('.tf-calendar-day .calendar-day-image-active').hide();
}

function leadingZeroCheck(toCheck) {
  if (toCheck < 10) {
    toCheck = "0"+toCheck
  }
  return(toCheck);
}
