$(function() {


    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd',
        onSelect: function(d) { 
          var types = {limited:'Limited availability', extremelylimited: 'Extremely limited availability', best: 'Best availability', '': 'No performance', noperformance: 'No Performance'};
          var texts = {
            limited: 'You can still try and book tickets for this performance of Oliver!',
            extremelylimited: 'You can still try and book tickets for this performance of Oliver!, but there are now very few tickets available.',
            best: 'There is still good availability for this performance of Oliver!',
            '': '',
            noperformance: ''
          };


          var dates = $('#ticketcalendar').data('dates');


          $('#ticketcalendarinfo div.date').text($.datepicker.formatDate('DD d MM yy', $.datepicker.parseDate('yy-mm-dd', d)));
          if (d in dates) {
            $('#ticketcalendarinfo h4').text(types[dates[d]['status']] );
            if (dates[d]['text']) {
              $('#ticketcalendarinfo p').text(dates[d]['text']);
            } else {
              $('#ticketcalendarinfo p').text(texts[dates[d]['status']] );
            }
          } else {
            dd = $.datepicker.formatDate('DD d MM yy', $.datepicker.parseDate('yy-mm-dd', d));
            if (dd.match(/^su/i)) {
              $('#ticketcalendarinfo h4').text('No performance');
              $('#ticketcalendarinfo p').text('');
            } else {
              $('#ticketcalendarinfo h4').text('Best availability');
              $('#ticketcalendarinfo p').text('There is still good availability for this performance of Oliver!');
            }
          }
        },
        onChangeMonthYear: function(y,m,ins) {



          $.ajax({
              type: 'GET',
              url: '../cms/views/json/getavailability.php',
              dataType: 'json',
              async: false,
              data: {month: m, year: y},
              success: function(d, t) {
                var dates = {};
                for (i = 0; i < d.length; i++) {
                  dates[d[i]['date']] = {status: d[i]['status'], text: d[i]['text']};
                }
                $('#ticketcalendar').data('dates', dates);

                // First fetch after page load? Populate the info box with today's data
                if (!$('#ticketcalendar').data('notfirstrun')) {
                  $('#ticketcalendar').data('notfirstrun', true);

                  // This works, and I can't believe it. Eugh.
                  var f = $('#ticketcalendar').datepicker('option', 'onSelect');
                  f($.datepicker.formatDate('yy-mm-dd', new Date()));

                }
                
              },
              error: function(d, t) {
                $('#ticketcalendar').data('dates', {});
                if (!$('#ticketcalendar').data('notfirstrun')) {
                  $('#ticketcalendar').data('notfirstrun', true);


                }
              }
            });

        },
        beforeShowDay: function(d) {
          dstr = $.datepicker.formatDate('yy-mm-dd', d);
          dates = $('#ticketcalendar').data('dates');
          dd = $.datepicker.formatDate('D', d);
          if (dstr in dates) {
            return [true,'cal-' + dates[dstr]['status'],'']
          } 
          if (dd.match(/^su/i)) {
            return [true, 'cal-noperformance', ''];
          }
          return [true, 'cal-best', ''];
        }
      });


    $('#ticketcalendar').datepicker();

  });

