/* 
 * Quick JS Calendar
 * Copyright (c) 2010 CMT Consulting
 * http://www.cmtconsulting.pl
 *
 */
var Calendar = {
    
    d: new Date(),
    m: ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"],
    
    init: function() {
        Calendar.cm = Calendar.d.getMonth();
        Calendar.cy = Calendar.d.getFullYear();
        Calendar.update();
        Calendar.cd = Calendar.cy+'-0'+(Calendar.cm+1)+'-'+Calendar.d.getDate();
        Calendar.getEvents(Calendar.cd);
        $('#calendar #days #'+Calendar.cd).addClass('selected');
    },
    update: function() {
        $('#calendar #date').html(Calendar.currentDate());
        $('#calendar #days').html(Calendar.monthDays());
        $('#calendar #days #'+Calendar.cd).addClass('selected');
        $('#calendar #days .day').click(function(){
            Calendar.getEvents(this.id);
            $('#calendar #days .day.selected').removeClass('selected');
            $(this).addClass('selected');
            Calendar.cd = this.id;
            });
    },
    currentDate: function () {
      return Calendar.m[Calendar.cm]+" "+Calendar.cy;
    },
    nextMonth: function () {
         if(Calendar.cm != 11){
             Calendar.cm = Calendar.cm+1;
         }else{
             Calendar.cm = 0;
             Calendar.cy = Calendar.cy+1;
         }
         Calendar.update(); 
    },
    prevMonth: function () {
        if(Calendar.cm != 0){
            Calendar.cm = Calendar.cm-1;
        }else{
            Calendar.cm = 11;
            Calendar.cy = Calendar.cy-1;
        }
        Calendar.update();
    },
    monthDays: function () {
        cmd = 32 - new Date(Calendar.cy, Calendar.cm, 32).getDate();
        fwd = new Date(Calendar.cy, Calendar.cm, 1).getDay();
        dd = Calendar.cy+'-0'+(Calendar.cm+1)+'-';
        days = '';
        for(i=1;i<=cmd;i=i+1){
            days = days+'<div id="'+dd+i+'" class="day">'+i+'</div>'; 
        }
  fwd = fwd==0?6:fwd-1;
            return '<div style="float:left;width:'+fwd*40+'px;">&nbsp;</div>'+days;
        
    },
    getEvents: function (date) {
        $.ajax({
           type: "POST",
           url: "http://www.adamowicz.pl/wp-content/themes/wpcandy/calendar.php",
           data: "date="+date,
           dataType: "json",
           success: function(r){
             e = '';
             if(r == ''){
                 e = '<div id="event-empty">Brak danych dla wybranego dnia</div>';
             }else{
                 $(r).each(function(){
                      e = e+'<div class="event"><b>'+this.time+'</b><p>'+this.event+'</p></div>';
                  })
             }
             $('#calendar-events').html(e);
           }
         });
    }
}
