/*
Script: DatePickerTop.js

Autor:
	IntraCOM, <http://intracom.pl>

Info:
  Adaptacja skryptu DatePicker, użytego do wyboru daty w polach input. 
*/

var DatePickerTop = new Class({
	/** set options */
	options: {
		calUrl    : 'index.php?page=terminarz/_id&data=',
		dayUrl    : 'index.php?page=terminarz/tydzien&data=',
    data      : false,
    container : 't_kalendarz_tresc',
		bName     : 'menu1_mc',
    bPrev     : 't_miesiac_prev',
		bNext     : 't_miesiac_next',
    className : 'DatePicker', 
		monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'], 
		daysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], 
		dayNames  : ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'] 
	}, 
	
	/** setup the new DatePicker */
	initialize: function(el, options){
		
		this.setOptions(options);
		
		this.bName = $(this.options.bName);
		this.bPrev  = $(this.options.bPrev);
		this.bNext  = $(this.options.bNext);
		
	  if ( this.options.data == false ) {
      d = new Date(); this.year = d.getFullYear(); this.month = d.getMonth(); this.day = d.getDate();
    }
    else {
      d = this.options.data.split('-'); this.year = d[0].toInt(); (this.month = d[1].toInt() - 1); this.day = d[2].toInt();
    }
		
		this.cur_year  = this.year;
		this.cur_month = this.month;
		
		this.setup();
		this.build();
	},
	
	/** setup the calendar */
	setup: function(){
		
    //następny miesiąc
    this.bNext.addEvent('click', function() {
  		if ( this.month == 11 ) {
        this.month = 0;
        this.year = this.year + 1;
      }
      else {
        this.month = this.month + 1;
      }
      this.build();
    }.bind(this));
    
    //poprzedni miesiąc
		this.bPrev.addEvent('click', function() {
  		if ( this.month == 0 ) {
        this.month = 11;
        this.year = this.year - 1;
      }
      else {
        this.month = this.month - 1;
      }
      this.build();
    }.bind(this));
    
    //bieżący miesiąc
		this.bName.addEvent('click', function() {
  		if ( this.year == this.cur_year && this.month == this.cur_month ) {
        return;
      }
      this.year  = this.cur_year;
  		this.month = this.cur_month;
      this.build();
    }.bind(this));
	}, 
	
	/** build the calendar */
	build: function(){
		date = new Date();
		date.setFullYear(this.year, this.month, 1);
		this.year % 4 == 0 ? this.options.daysInMonth[1] = 29 : this.options.daysInMonth[1] = 28;
		var firstDay = (date.getDay()==0) ? -5 : 2 - date.getDay();
		
		/** start creating calendar */
		$(this.options.bName).set('html', this.options.monthNames[this.month]+' <span>'+this.year+'</span>');
		calTableTbody = $(this.options.container);
		calTableTbody.empty();
		
		/** create the day cells */
		date2 = new Date();
		while (firstDay <= this.options.daysInMonth[this.month]){
			calDayRow = new Element('tr').injectInside(calTableTbody);
			for (i = 0; i < 7; i++){
				klasa = ( i == 6 ) ? 'dzien niedziela' : 'dzien';
        if ((firstDay <= this.options.daysInMonth[this.month]) && (firstDay > 0))
        {
					this.day = firstDay;
					da = this.biezacaData();
          calDayCell = new Element('td', {'class':klasa, 'styles':{'text-align':'center'}, 'title':da, 'id':'t_'+da}).set('text', firstDay).injectInside(calDayRow);
					if (date2.getFullYear() == this.year && date2.getMonth() == this.month && date2.getDate() == firstDay) calDayCell.addClass('dzis');
				} 
        else {
					calDayCell = new Element('td', {'class':'none'}).set('text', ' ').injectInside(calDayRow);
				}
				firstDay++;
			}
		}
		
		/* pobierz zadania z danego miesiąca */
    var url  = www_dir + 'ajax/wydarzenia_w_miesiacu/' + this.biezacaData() + '.html';		
    new Ajax(url, {
    		method: 'get',
				useWaiter: true,
				waiterOptions: {},
				waiterTarget: this.options.container,
    		onComplete: function(a) {
					a = a.split('<p>');	// bo 1,2,...<p><div>wyd1</div><span><div>wyd2</div><span>...
          var dni   = a[0].split(',');
					var newsy = a[1].split('<span>');
          dni.each(function(d){
            this.day = d;
            var dz = this.biezacaData();
						var td = $('t_'+dz);
            if ( td )
						{
							if ( td.hasClass('niedziela') )
								td.addClass('niedziela_active');
							else
								td.addClass('active');
							td.setStyle('cursor', 'pointer');
							td.onclick = function(dz) {
								location.href = www_dir + 'news/kalendarium/'+ this.get('id').substr(2) +'.html';
							};
							// dodaj tekst do tips-a
							var txt = '';
							for(i=0; i<newsy.length; i++) {
								if ( newsy[i].indexOf(dz) != -1 ) {
									txt += newsy[i];
								}
							}
							td.store('tip:text', txt);							
							kalendarz_tips.attach(td);
							td.removeEvent('mouseleave', td.retrieve('tip:leave') || $empty);
						}
          }.bind(this));
        }.bind(this)    		
    	}).request();
	},	
		
	/* ustaw aktywny dzień */
	biezacaData: function(){
		var da  = this.year + '-';
        da += (this.month+1<10) ? '0'+(this.month+1) : (this.month+1);
        da += '-';
        da += (this.day<10) ? '0'+this.day : this.day;
	  return da;
  },

	/* ustaw aktywny dzień */
	setActive: function(td){
		if ( this.active ) {
      this.active.removeClass('active');
    }
    this.active = td;
    this.active.addClass('active');
	}
});

DatePickerTop.implement(new Events, new Options);

