// KaL: initialisation and custom logic for embedded datepicker / calendar

jQuery(document).ready(function() 
    {      
        $('#' + calendarClientID).datepicker(
        {
             prevText: '<<',
             nextText: '>>',
             dateFormat: 'dd.mm.yy', 
             firstDay: 1, 
             onSelect: function(dateText, inst) { 
                var indexOfQ = window.location.href.indexOf('?');
                var redirectUrl = '';
                if (indexOfQ >- 1)
                {
                    window.location.href = window.location.href.substr(0,indexOfQ)+ "?from=" + dateText + "&to=" + dateText;
                }
                else
                {
                    window.location.href = window.location.href + "?from=" + dateText + "&to=" + dateText;
                }    
                
                if (depId > -1) {
                   window.location.href = window.location.href + "&depId=" + depId;
                }    
             },
             beforeShowDay: 
                 function(date) { 
                    var eventOnThisDay = false;
                    if (eventDays != null)
                    {
                        for (i = 0; i < eventDays.length; i++) {                        
                            if (date.getDate() == eventDays[i][0] && date.getFullYear() == eventDays[i][2] && date.getMonth()+1 == eventDays[i][1])
                            {
                                eventOnThisDay = true;
                            }
                        }
                    } 
                    
                    if (eventOnThisDay)
                    {
                        return [true, 'calendarActiveDay'];
                    }
                    else
                    {
                        return [false, 'calendarInactiveDay'];
                    }                        
                 }
         }).datepicker('setDate', dateToSet);          
 });
