var calendar = {
    days: null,
    init: function() {
        $('.ui-datepicker-week-end span').live('ready',function(){
            console.debug(this);
        });
        $('#pokaz_kalendarz').live('click', function(){
            $('div.body.calendarList').remove();
            $('div.body.calendar').show();
            return false;
        });
        $('.rozwin').live('click',function(){
            $(this)
                .removeClass('rozwin')
                .addClass('zwin')
                .html('&laquo; zwiń')
                .prev()
                    .css('height',null);
            $(this).blur();return false;
        });
        $('.zwin').live('click',function(){
            $(this)
                .removeClass('zwin')
                .addClass('rozwin')
                .html('rozwiń &raquo;')
                .prev()
                    .css('height','100px');
            $(this).blur();return false;
        });
        calendar.datepicker();
        var today = new Date().getFullYear()+'-'+((new Date().getMonth()+1)<10?'0':'')+(new Date().getMonth()+1)+'-'+(new Date().getDate()<10?'0':'')+new Date().getDate();
        if( calendar.days[today] != undefined ) {
            calendar.onSelect(today);
        }
    },
    longEntryShort: function() {
        $('div.calendarList .entry .body').each(function(){
            $(this).css('height', null);
            if( $(this).height() > 100 ) {
                $(this)
                    .css({
                        height: '100px',
                        overflow: 'hidden',
                        position: 'relative'
                        })
                    .after('<a class="rozwin" href="#rozwiń" style="font-size: 10px;position:absolute;right:0;bottom:0px;z-index:5">rozwiń &raquo;</a>')
            }
        });
    },
    onSelect: function(dateText) {
        $('div.body.calendar').block({
            overlayCSS:{ backgroundColor: '#fff'},
            message: '<img src="/_img/ajax-loader.gif" class="ajaxLoader" />',
            css: {
                border: '0',
                backgroundColor: 'transparent'
            }
        });
        $.ajax({
            url: '/ajax/kalendarz',
            dataType: 'json',
            data: {data: dateText},
            success: function(data){
                $('div.body.calendar').unblock();
                if( $(data).size() > 0 ) {
                    $('div.body.calendar').hide();
                    var new_container = $('<div class="body calendarList"><span>'+dateText+'</span><a href="#pokaz_kalendarz" id="pokaz_kalendarz">pokaż kalendarz</a></div>');
                    $(data).each(function(k,v){
                        new_container.append('<div class="entry"><p class="time">'+v.time+'</p><p class="body">'+v.body+'</p></div>');
                    });
                    $('div.body.calendar').after(new_container);
                    $('div.body.calendar').hide();
                    new_container.show();
                    calendar.longEntryShort();
                }
            }
        });
    },
    beforeShowDay: function(date){
        var dateString = date.getFullYear()+'-'+((date.getMonth()+1)<10?'0':'')+(date.getMonth()+1)+'-'+(date.getDate()<10?'0':'')+date.getDate();
        var klasa = '';
        if( date.getDay() == 0 ) {
            klasa = 'niedziela';
        }
        var pokazac = false;
        if( calendar.days[dateString] != undefined ) {
            pokazac = true;
        }
        return [pokazac,klasa]; 
    },
    datepicker: function(){
        $('div.calendar').datepicker({
            closeText: 'Zamknij',
            prevText: '&laquo; Poprzedni',
            nextText: 'Następny &raquo;',
            currentText: 'Dziś',
            monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
            monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze','Lip','Sie','Wrz','Pa','Lis','Gru'],
            dayNames: ['Niedziela','Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
            dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
            dayNamesMin: ['Nd','Pn','Wt','Śr','Cz','Pt','So'],
            weekHeader: 'Tydz',
            dateFormat: 'yy-mm-dd',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: '',
            beforeShowDay: calendar.beforeShowDay,
            onSelect: calendar.onSelect
        });
    }
};
