var dd = new Date();
var year = dd.getFullYear();
var month = dd.getMonth();
var calendarloaded = 0;
var shortmonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var fullmonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var category = '';
var categoryOverridesDate = false;
$(document).ready(function(){
  mainhtml = '
';
  $('.calendar').html( mainhtml );
  
  $('.calendar .prev').on( 'click', previousMonth );
  $('.calendar .next').on( 'click', nextMonth );
  
  $('.filter a').on( "click", function() {
    category = $( this ).attr('class');
    selectCategory();
  });
  
  //makeCalendar( year, month, category );
  
  //calendarloaded = 1;
  
});
function custom_onChangeMonth() {
  $.ajax({
    type: "POST",
    url: "/"+rootURL+"seecmscalendar/setcalendardate/",
    data: { month: month, year: year }
  }).done(function( msg ) {
  
  });
}
function previousMonth() {
  if( !$(this).hasClass('disabled') ) {
  
    if( categoryOverridesDate ) {
      category = '';
    }
    
    if( month-1 == -1 ) {
      month = 11;
      year -= 1;
    } else {
      month --;
    }
    makeCalendar();
  }
};
function nextMonth() {
  if( !$('.next').hasClass('disabled') ) {
  
    if( categoryOverridesDate ) {
      category = '';
    }
  
    if( month+1 == 12 ) {
      month = 0;
      year += 1;
    } else {
      month++;
    }
    makeCalendar();
  }
};
function selectCategory() {
  if( category == 'All' || category == 'Any' ) {
    category = 'All';
  }
  
  makeCalendar();
  
};
function makeCalendar() {
  if( year < startYear || ( year == startYear && month < startMonth ) ) {
    dd.setMonth( startMonth );
    dd.setYear( startYear );
    month = dd.getMonth();
    year = dd.getFullYear();
    makeCalendar();
    return( false );
  }
  
  if (typeof( custom_onChangeMonth ) === 'function') {
    custom_onChangeMonth();
  }
  
  if( year == startYear && month == startMonth ) {
    $('.prev').addClass('disabled');
  } else {
    $('.prev').removeClass('disabled');
  }
  
  
  if( year == endYear && month == endMonth ) {
    $('.next').addClass('disabled');
  } else {
    $('.next').removeClass('disabled');
  }
  
  
  
/*  
  ddd = new Date( year, month+1, 1 );
  emonth = ddd.getMonth();
  eyear  = ddd.getFullYear();
  if( !eventMonths[String(emonth+1)+eyear] ) {
    $('.next').addClass('disabled');
  } else {
    $('.next').removeClass('disabled');
  }
*/
  var html = '';
  var d = new Date( year, month, 1 );
  var dim = new Date( year, month+1, 0 );
  var daysInMonth = dim.getDate();
  var categoryText = '';
  $('.feature').hide();
  
  if( category && categoryOverridesDate ) {
    categoryText = category;
    $( '.feature' + ((category!='All')?'.'+category:'') ).show();
  } else if( category ) {
    categoryText = ', '+category;
    $('.feature' + ((category!='All')?'.'+category:'') + '.'+fullmonths[month] + year).show();
  } else {
    $('.feature.'+fullmonths[month] + year).show();
  }
  
  if( $('.feature:visible').length ) {
    
    $('#noeventsmessage').hide();
  } else {
    
    $('#noeventsmessage').show();
  }
  
  $('.calendarmonth').html( shortmonths[month] + ' ' + year );
  
  if( category && categoryOverridesDate ) {
    $('.calendarfullmonth').html( categoryText );
  } else {
    $('.calendarfullmonth').html( fullmonths[month] + ' ' + year + categoryText );
  }
  
  var firstday = d.getDay();
  if( firstday == 0 ) {
    firstday = 7;
  }
  
  for( a = 1; a < firstday; a++ ) {
    html += '';
  }
  
  for( a = 1; a <= daysInMonth; a++ ) {
    if( a < 10 ) {
      aa = '0' + a;
    } else {
      aa = a;
    }
    
    if( eventDates[aa+String(month+1)+year] ) {
      title = eventDates[aa+String(month+1)+year];
      cssclass = 'current';
    } else {
      title = '';
      cssclass = '';
    }
    
    html += ''+aa+'
';
  }
  
  html += '';
  
  $('.dates').html( html );
  
}