/**
 * 
 */

$(document).ready(function() { 

	var specialDays = new Array();
	
	/*
	var specialDays = {
    '2011': { 
        '12': { 
            '1': {
                content: "Event numer 1", 
                className: "specialDate",
                popupID: "popup1"
            },
            '2': {
                content: "Event numer 2", 
                className: "specialDate",
                popupID: "popup2"
            },
            '3': {content: "Event numer 3", 
                  className: "specialDate "
                 },
            '25': {content: "Event numer 4", 
                  className: "specialDate"
                 },
            '26': {
                content: "Event numer 17", 
                className: "specialDate"
            } 
        }
    }
	}; 
	*/

	$.ajax({
		  // url: "/pl/calendar/index2/",
		  // data: "action=showdates",
   		  // type: "POST",
		  // data: ({ specialDaysPHP: specialDays }),
		  url: "/pl/calendar/",
		  data: "action=showdates",
		  dataType: "json",
		  success: function(data){
			  	   if ( data != null ) {
							specialDays = data;
			       }
		           $("#datepicker").datepicker({
		           dateFormat: 'yy/mm/dd',
		           inline: true,
		           showCurrentAtPos: 0,
		           onChangeMonthYear: function(year, month, inst) {
		        	   			/* Todo: if do pobierania ajaxa tylko jezeli przekroczono zakres zmiany
		        	   			var now = new Date(this.value); 
		        	   			*/
		        	   
		       					$.ajax({
		       					type: "POST",
		       					url: "/pl/calendar/",
		       					data: ({ year: year, month: month }),
		       					dataType: "json",
		       					async: false,
		       					success: function(calendarEvents) {
		       						specialDays = [];
		       						if ( calendarEvents != null ) {
		       							specialDays = calendarEvents;
		       						}
		       					}
		       					});
		       					$(inst).datepicker("refresh");
		           },
		           beforeShowDay: function(date) {
		               var d = date.getDate(),
		                   m = date.getMonth()+1,
		                   y = date.getFullYear();
		                   
		               if (specialDays[y] && specialDays[y][m] && specialDays[y][m][d]) {
		                   var s = specialDays[y][m][d];
		                   return [true, s.className, s.content]; // selectable      
		               }
		               return [false,'']; // non-selectable  
		           },
		           onSelect: function(dateText) {
		                	  		window.location.href = "/pl/wydarzenia/" + dateText; 
		        	   				// alert("Parse dateText into your variables. dateText = " + dateText);
		        	   				// $.get('/pl/wydarzenia/' + dateText + '/?ajaxrequest=yes', function(data) {
		        	   				// $('.rightColumnContent').html(data);
		        	   				// });
		                		} 
		           });
		           }
	});
	
	
	
});
	
	
	
	
