$(document).ready(function(){
	//Expand/Collapse Agemcy/Sublist Pages
	var showText="Expand List";
	var hideText="Close List";	
	$(".switch").prev().append('<h5 class="switchLink">'+showText+'</h5>');
	$('.switchLink').click(function() {
		if ($(this).text()==showText) {
		$(this).text(hideText);
		}
		else {
		$(this).text(showText);
		}
	});
	$(".toggle_containerAg").hide();
		$('div.triggerAg').css("display","block");
		$("div.triggerAg").toggle(function(){
			$(this).addClass("active"); 
			}, function () {
			$(this).removeClass("active");
		});
		$("div.triggerAg").click(function(){
			$(this).next(".toggle_containerAg").slideToggle("slow,");
	});		
});
var results=null;
function initCalendar(date,calendarKey) {
  $.getJSON("https://www.colorado.gov/apps/calendar/dwr/jsonp/DWRBridgeManager/getEventDaysInMonth?param0="+calendarKey+"&param1="+date+"&callback=?",
  function(jsonResults){
     results = jsonResults;
	$('#datepicker').datepicker({
           beforeShowDay: function(thedate) {
              if( $.inArray(thedate.getDate(),results.daysWithEvents) == -1 ) return [true,""]; 
	         return [true, "presentEvents"];         
           },
           onSelect: function(dateText, inst) {
              var dateTokens=dateText.split('/');(window.location.href='https://www.colorado.gov/apps/calendar/html/event/SIPA/'+dateTokens[2]+'/'+dateTokens[0]+'/'+dateTokens[1]+'?viewperiod=1');
           },
           onChangeMonthYear: function(year, month, inst) { 
              var newDate = year+"-"+month;
              getEventDays(newDate,calendarKey);
           }
        });
  });
}
function getEventDays(date, calendarKey) {
   $.getJSON("https://www.colorado.gov/apps/calendar/dwr/jsonp/DWRBridgeManager/getEventDaysInMonth?param0="+calendarKey+"&param1="+date+"&callback=?",
   function(jsonResults){
      if (jsonResults.date == date) {
         results = jsonResults;
         $('#datepicker').datepicker("refresh");
      }
   });
};
$(function() {
   var date = new Date().getFullYear() + "-" + (new Date().getMonth()+1);
   var calendarKey = "SIPA";
   initCalendar(date, calendarKey);
});


