/*
@author     : Igor "SkAZi" Potapov <igor@potapoff.org>
@copyright  : Plan-B Ltd
@require    : Mootools 1.2
*/


var Calendar = new Class({

    'initialize': function( options ){
        
        this.options = options || {};
        
        this.year   = this.options.year  || new Date().getFullYear();
        this.month  = this.options.month && this.options.month !== 0 ? this.options.month-1 : new Date().getMonth();
        this.day = this.options.day || new Date().getDate();
        
        var that = this;
        window.addEvent('domready', function(){
            that.setPopups();
            if($('calendar-next-month'))
                $('calendar-next-month').addEvent('click', that.nextMonth.bind(that) );
            if($('calendar-previous-month'))
                $('calendar-previous-month').addEvent('click', that.prevMonth.bind(that) );      
        });
    },
    
    'setPopups': function(){
        $$('#calendar-body li.full a').each(function( a ){
            a.setStyle('opacity', 0).getParent('li').addEvents({
                'mouseover': function(){
                    this.getElements('a')[0].fade('in');
                },
                'mouseout': function(){
                    this.getElements('a')[0].fade('out');
                }
            });
        });
       
    },
    
    'visual': function( url ){
        if(this.options.onvisual) this.options.onvisual();
        
        var that = this;
            
        $('calendar').fade('out');
         
        (function(){
            
            new Request({
                'url': url,
                'method': 'get',
                'onFailure': function(){
                    $('calendar-body').fade('in');
                },
                'onSuccess': function( response ){
                    $('calendar').set('html', response).fade('in');
                    $('calendar-body').setStyle('backgroundImage', '');
                    
                    that.setPopups();
                    if($('calendar-next-month'))
                        $('calendar-next-month').addEvent('click', that.nextMonth.bind(that) );
                    if($('calendar-previous-month'))
                        $('calendar-previous-month').addEvent('click', that.prevMonth.bind(that) );                    
                   
                    if(that.options.onaftervisual) that.options.onaftervisual();
                }
                
            }).send();
            
        }).delay(400);
    },
    

    'nextYear': function(){ 
        this.year++; 
        this.visual();
        return false;
    },

    'prevYear': function(){ 
        this.year--; 
        this.visual();
        return false;
    },

    'nextMonth': function(){ 
        this.visual( $('calendar-next-month').get( 'href' ) );
        return false;
    },

    'prevMonth': function(){ 
        this.visual( $('calendar-previous-month').get( 'href' ) );
        return false;
    }

    
});
