	$(document).ready(function() {

        // Display the full calendar
		$('#fullcalendar_content').fullCalendar({
			editable: false,
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,basicWeek,basicDay'
			},
            events: function(start, end, callback) {
                // Determine the calendar(s) to load depending on the current selected calendar
                // Look first for checkboxes then graduate to regular hidden field (id calendar_selection) if none are found
                var furl= $('#fullcalendar_json_url').val();
                var checked= new Array();
                $('INPUT[name=calendar_selection]').each(function() {
                    if ($(this).attr('checked')) {
                        checked.push($(this).val());
                    }
                });
                if (checked.length < 1) {
                    var v= $('#calendar_selection').val() + '';
                    if (v.length > 0) checked.push(v);
                }
                //console.log('this is the cal selectin: ', $('INPUT[name=calendar_selection]').val(), 'ok then');
                // do some asynchronous ajax
                $.getJSON($('#fullcalendar_json_url').val() + '&r=' + Math.random(),
                        {
                                start: start.getTime(),
                                end: end.getTime(),
                                'groups[]': checked
                        },
                        function(result) {
                            var calevents= new Array();
                            for (var i in result) {
                                if (result[i].cal_date) {
                                    if (result[i].cal_date) {
                                        var ce= new Object();
                                        ce.id= 'calendar_entry_' + result[i].cms_asset_id;
                                        ce.title= result[i].title;
                                        ce.className= 'full_calendar_' + result[i].group_id;
                                        ce.start= new Date(result[i].cal_start_js);
                                        ce.end= new Date(result[i].cal_end_js);
                                        ce.url= unescape($('#fullcalendar_entry_url').val()).replace(/\{0\}/, result[i].cms_group_revision_id);
                                        ce.url= ce.url.replace(/\{1\}/, result[i].title.replace(/\s+/g, '-'));
                                        var nest_id= parseInt(result[i]['nest_id']);
                                        ce.url= ce.url.replace(/\{2\}/, (isNaN(nest_id) || nest_id < 1) ? 'default' : nest_id);
                                        //ce.url= $('#fullcalendar_entry_url').val() + result[i].cms_revision_id;
                                        calevents.push(ce);
                                    }
                                }
                            }
                            // then, pass the CalEvent array to the callback
                            callback(calevents);
                        });
            }
		});


        // Full calendar close button
        $('#fullcalendar_close').click(function() {
			$('.fc-widget-content').css('border-width', '0');
            $('#fullcalendar_container').hide('scale');
        });

        // Show the fullscreen calendar
        $('#fullcalendar_show').click(function() {
			$('.fc-event-hori').css('border-width', '1px 0');
			$('.fc-content .fc-corner-left a').css('border-left-width', '1px');
			$('.fc-content .fc-corner-right a').css('border-right-width', '1px');
			$('.fc .fc-grid th').css('border-right-width', '1px');
			$('.fc .fc-grid td').css('border-width', '1px 0 0 1px');
			$('.fc-header .fc-state-default').css('border-width', '1px 0');
			$('.fc-header .fc-state-default a').css('border-width', '0 1px');
			$('.fc-header .fc-state-default span').css('border-width', '1px 0 1px 1px');
            $('#fullcalendar_container').css('visibility', 'visible');
            $('#fullcalendar_container').show('scale');
        });

        $('#fullcalendar_container').click(function(e) {
           if ($(e.target).is('#fullcalendar_container'))
               $('#fullcalendar_container').hide('scale');
        });

        if ($('#fullcalendar_is_inline').val()=='yes') {
            $('#fullcalendar_content').css('visibility', 'visible');
            $('#fullcalendar_content').fullCalendar('render');
            // Apply a calendar selection to the inline calendar
            $('#calendar_selection_apply').click(function() {
                $('#fullcalendar_content').fullCalendar('removeEvents');
                $('#fullcalendar_content').fullCalendar('refetchEvents');
            });
            // "Select All" calendar options
            $('#calendar_selection_selectall').click(function() {
                $('INPUT[name=calendar_selection]').each(function() {
                    $(this).attr('checked', $('#calendar_selection_selectall').attr('checked'));
                });
            });
        }

	});