$(document).ready(function() {
  $('input.aantal').change(bereken);
  
  $('input.submit').click(bereken);
  
  if ($('div.accommodaties').length > 0) {
    toon_accommodaties();
  }

  $('div.cursussen tr:not(.omschrijving) a').click(function() {
    location = $(this).attr('href');
    return false;
  });

  $('div.cursussen tr:not(.omschrijving)').click(function() {
    location = $(this).attr('href');
    return false;
  });
  
  // $('div.cursussen tr:not(.omschrijving)').click(function() {
  //   $(this).toggleClass('selected').next('tr').toggleClass('selected');
  // });

  if ($('#ticker ul').length > 0) {
    li_html = $('#ticker ul').html();
    setTimeout(function() { setInterval(tickertape, 5000) }, 1000);
  }  
  toc($('div#tableofcontent'));
});

var li_html;

function tickertape() {
  var top = $('#ticker ul').position().top - 45;
  var end = $('#ticker ul').height() + top - 45 < 0;
  if (end) {
    // console.log(li_html);
    $('#ticker ul').append(li_html);
  }
  // $('#ticker ul').animate( {top: top+'px'}, 600, function() { if (end) $('#ticker ul').css("top", "-18px");});
  $('#ticker ul').animate( {top: top+'px'}, 600 );
}

function toc(toc) {
  if (toc.length == 0)
    return;
  var page = location.href;
  var div  = toc.parents('div#content');
  var ul = $('<ul class="toc">');
  $('h4', div).each(function(i) {
    var text  = $(this).text();
    var aname = "toc" + i;
    
    $('<li><a href="' + page + '#' + aname + '">' + text + '</a></li>').appendTo(ul);
    $(this).before('<a name="' + aname + '"></a>'); 
  });
  toc.append(ul);
}

function toon_accommodaties() {
  var gebied = $('body').attr('id');
  var loc = location.href;
  var p = loc.lastIndexOf('/');
  var selpage = loc.substr(p+1);   
  for (var i=0; i < accommodaties[gebied].length; i++) {
    var a = accommodaties[gebied][i];
    var page = a[1];
    var titel = a[0];
    var nummer = i+1;
    var selected = '';
    if (selpage == page) {
      selected = 'selected';
    }
    $('<a href="/' + gebied + '/' + page + '" id="' + page + '" class="num' + nummer + selected + '">' + titel + '</a>').appendTo('div.accommodaties');
  }
}


function bereken() {
  var totaal = 0;
  $('input.aantal').each(function() {
      var aantal = parseInt($(this).val());
      if (aantal) {
        totaal += aantal;
      }
  });
  $('h3:contains(Totaal)').text('Totaal: '+ totaal);  
  
  if (totaal > 50) {
    alert('U kunt maximaal 50 folders bestellen');
    return false;      
  } else {
    return true;
  }
}
 


