$(document).ready(function(){ // declare freeDays global var freeDays = []; // perform initial json request for free days fetchFreeDays(); $("#calendar").datepicker({ changeMonth: false, changeYear: false, showOtherMonths: false, selectOtherMonths: true, dateFormat: 'DD, d MM, yy', altField: '#date_due', altFormat: 'yy-mm-dd', beforeShowDay: highlightDays, onChangeMonthYear: fetchFreeDays, onSelect: function(date, inst){ var d = new Date(date); var curr_date = d.getDate(); var curr_month = d.getMonth(); curr_month++; var curr_year = d.getFullYear(); var ndx_date = curr_month + "/" + curr_date + "/" + curr_year; var data = {}; for(var i = 0; i < freeDays.length; i++){ if(freeDays[i].date == ndx_date){ data = freeDays[i]; if(data.url.length > 0) { var redirect_url = "http://" + document.domain + "/" + data.url; window.location = redirect_url; } } } }, firstDay: 1 // rows starts on Monday }); $('a.ui-state-default').click(function(evt){ evt.preventDefault(); }); // query for free days in datepicker function fetchFreeDays(year, month) { var start_date = ''; // if a month and year were supplied, build a start_date in yyyy-mm-dd format if (year != undefined && month != undefined) { start_date = year +'-'; start_date += month +'-'; start_date += '01'; } $.ajax({ url: "/special-events.php?start_date="+ start_date, dataType: 'json', async: false, success: function(data){ $.each(data, function(index, value) { freeDays.push({"date": value.freeDate, "isEvent": value.isEvent, "url": value.url}); // add this date to the freeDays array }); } }); } // runs for every day displayed in datepicker, adds class and tooltip if matched to days in freeDays array function highlightDays(date) { for (var i = 0; i < freeDays.length; i++) { var free_day = freeDays[i]; var check_date = new Date(free_day.date).toString(); var cal_date = date.toString(); if ((check_date == cal_date) && free_day.isEvent == true) { return [true, 'event-day', 'Event']; // [0] = true | false if this day is selectable, [1] = class to add, [2] = tooltip to display } } return [true, 'free-day']; } });