/*
@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.options.afterpath = this.options.afterpath || '';
        
        this.city   = this.options.cityis? this.options.cityis+"/": "";
        this.year   = this.options.yearis  || new Date().getFullYear();
        this.month  = this.options.monthis && this.options.monthis !== 0 ? this.options.monthis-1 : new Date().getMonth();
        this.day = this.options.day || new Date().getDate();
        
        var that = this;
        window.addEvent('domready', function(){
            $('calendar-next-month').addEvent('click', that.nextMonth.bind(that) );
            $('calendar-previous-month').addEvent('click', that.prevMonth.bind(that) );
            if(that.year <= new Date().getFullYear() && that.month <= new Date().getMonth())
                $('calendar-previous-month').setStyle('visibility', 'hidden')
            if((that.year-new Date().getFullYear())*12 + that.month-new Date().getMonth() > 1)
                $('calendar-next-month').setStyle('visibility', 'hidden');                
            that.visual( true );
        });
    },
    
    'build': function(){
        var empty = null;
        
        var arr = [];
        var dt = new Date(this.year, this.month, 1);
        
        for(var i=0; i<(dt.getDay() || 7)-1; i++)
            arr.push( empty );
        
        var i = 1;
        while(dt.getMonth()==this.month){
            arr.push( [ i ] );
            dt.setDate( ++i );
        }

        return arr;
    },

    'str': function( i ){
        var i10 = (i%10);
        var io10 = parseInt(i/10);
        
        return (i>9? '<span class="c'+io10+'"></span>': '') + '<span class="c'+i10+'"></span>';
    },
    
    'visual': function( fast ){
        if(this.options.onvisual) this.options.onvisual();
        
        var that = this;
        if(fast)
            $('calendar-month').setStyles({
                'backgroundPosition': "0 "+(-this.month*20+3)+"px"
            });
        else
            $('calendar-month').morph({
                'backgroundPosition': "0 "+(-this.month*20+3)+"px"
            });
            
        var tyear = this.year || new Date().getFullYear();;
        var tmonth = this.options.monthis || new Date().getMonth();
        var tday = this.options.dayis ||  new Date().getDate();
        var ttyear = new Date().getFullYear();
        var ttmonth = new Date().getMonth();
        var ttday = new Date().getDate();
        
        var monthis = tmonth-1 == this.month;

        
        var arr = this.build();
        
        var text = [];
        var dd = 1;
        for(var j=0; j<arr.length; j++){
            if(arr[j]){
                if( tyear == this.year && tmonth-1 == this.month && tday==dd )
                    text.push( '<li class="today">'+this.str( arr[j][0] )+'</li>' );
                else
                    if(this.options.path)
                        text.push( '<li class="clickable"><a href="'+this.options.path+'/'+tyear+'/'+(this.month<9?'0':'')+(this.month+1)+'/'+(dd<10?'0':'')+dd+'/'+this.city+this.options.afterpath+'">'+this.str( arr[j][0] )+'</a></li>' );
                    else
                        text.push( '<li>'+this.str( arr[j][0] )+'</li>' );
                dd++;
            } else 
                text.push( '<li class="empty"></li>' );
        }
        
        if(!fast)
            $('calendar-body').fade('out');
        
        (function(){
            
            $('calendar-body').set('html', text.join('')).fade('in');
            
            $$('#calendar-body li.full a').each(function( a ){
                a.setStyle('opacity', 0);
                a.getParent('li').addEvents({
                    'mouseover': function(){
                        this.getElements('a')[0].fade('in');
                    },
                    'mouseout': function(){
                        this.getElements('a')[0].fade('out');
                    }
                });
            });
            
            if(that.options.onaftervisual) that.options.onaftervisual();
        }).delay( fast? 1: 400);
    },
    

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

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

    'nextMonth': function(){
        $('calendar-previous-month').setStyle('visibility', 'visible');
        this.month++; 
        if(this.month > 11){
            this.month = 0;
            this.nextYear();
        } else 
            this.visual();
            
        
        console.log((this.year-new Date().getFullYear())*12 + this.month-new Date().getMonth());
        if((this.year-new Date().getFullYear())*12 + this.month-new Date().getMonth() > 1)
            $('calendar-next-month').setStyle('visibility', 'hidden');
            
            
        return false;
    },

    'prevMonth': function(){
        $('calendar-next-month').setStyle('visibility', 'visible');
        this.month--; 
        if(this.month < 0){
            this.month = 11;
            this.prevYear();
        } else
            this.visual();

        if(this.year <= new Date().getFullYear() && this.month <= new Date().getMonth())
            $('calendar-previous-month').setStyle('visibility', 'hidden');

        return false;
    }

    
});