/**
 *
 * Picabo AjaxFramework
 *
 * @category   PcbJs
 * @package    PcbJs_Calendar
 * @copyright  Copyright (c) 2008 Picabo s.r.o.(http://www.picabo.cz)
 * @author     Lukas Musalek(musalek@picabo.cz)
 * @version    004 0001 30.04.2009 16:45
 * 
 *  
 */

/* ************************************
 *
 *  Description: Array - popup objects holder 
 *
 * ************************************ */

var PCB_Calendar_holder = null;

/* ************************************
 *
 *  Description: simulate object creation and display
 *
 *  see function (constructor) TinyPopup
 *  see TinyPopup.prototype.showPopup()
 *
 * ************************************ */
function setCalendar(el, popup) {
   
   PCB_Calendar_holder = new PCB_Calendar(el, popup);
}

function PCB_Calendar(el, popup) {
  this._popup = popup;
  this._el = el;
  this._cDate=new Date();
  this._cYear=this._cDate.getFullYear();
  this._cMonth=this._cDate.getMonth();
  this._cDay=this._cDate.getDate();
  this._day = 1;
  
  var datum = document.getElementById(this._el).value.split(".");
  if (datum[1]>=1 && datum[1]<=12 && datum[2]>=document.getElementById("year_from").options[0].text && datum[2]<=document.getElementById("year_from").options[document.getElementById("year_from").options.length-1].text ) {
    if (datum[0]>=1 && datum[0]<=this.getDaysPerMonth(datum[1]-1)) this._day=datum[0];
    else this._day=this.getDaysPerMonth(datum[1]-1);
    this.setCalendar(new Date(datum[2],datum[1]-1,1));
  }  
  else {
    datum = new Date();
    this._day = datum.getDate();
    pomMonth = datum.getMonth();
    pomYear = datum.getFullYear();
    this.setCalendar(new Date(pomYear,pomMonth,1));
  } 
}

PCB_Calendar.prototype.send = function(day) {
  this.mM = ((this._cMonth+1)<10)? "0"+(this._cMonth+1) : (this._cMonth+1);
  if (day!="") {
    document.getElementById(this._el).value=day+'.'+(this.mM)+'.'+this._cYear;
    
    eval(this._popup);
    if (document.getElementById(this._el).onchange) {
      document.getElementById(this._el).onchange();
    }
  }
}

//vypisu kalendar                
PCB_Calendar.prototype.setCalendar = function(dt) {
 
  this._cYear=dt.getFullYear();
  this._cMonth=dt.getMonth();
  this._cDay=dt.getDate();
  this._firstDay = dt.getDay();
  for(i = 1; i < 43 ; i++) {
    document.getElementById("cal_td"+i).innerHTML=" ";
    document.getElementById("cal_td"+i).className = '';
  }  
  if (this._firstDay==0)
    this._firstDay +=7;
  this._dayspermonth = this.getDaysPerMonth(this._cMonth);
  this._tyden = this._firstDay - 1;
  for(i = 1; i < this._dayspermonth+1; i++){
    i1=(i<10)? "0"+i : i;
    pom = i+this._firstDay-1;
    this._tyden++; 
    document.getElementById("cal_td"+pom).innerHTML=i1;
    if (this._tyden<6) document.getElementById("cal_td"+pom).className = 'normal';
    else {
      document.getElementById("cal_td"+pom).className = 'weekend';
      if (this._tyden==7) this._tyden = 0;
    } 
    if (this._day == i) document.getElementById("cal_td"+pom).className = 'dnes';  
  }
  for(i = pom+1; i < 43; i++) {
    //document.getElementById("td"+i).style.display = 'none;';
  }
  document.getElementById("month_from").selectedIndex=this._cMonth; //nastavim mesic v selektu
  document.getElementById("year_from").selectedIndex=this._cYear-parseInt(document.getElementById("year_from").options[0].text);
}

//prevedu rok ze selektu na spravny tvar (template)              
PCB_Calendar.prototype.setNYear = function(y) {
  this._cYear=parseInt(document.getElementById("year_from").options[0].text)+y;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}

//prevedu mesic ze selektu na spravny tvar (mesic beze zmeny)  (template)         
PCB_Calendar.prototype.setNMonth = function(m) {
  this._cMonth=m;
  this.setCalendar(new Date(this._cYear,this._cMonth,1));
}

//kolik dni je ve kterem mesici          
PCB_Calendar.prototype.getDaysPerMonth = function(m){
  daysArray=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  days=daysArray[m];
  if (m==1){
    if((this._cYear% 4) == 0) {
      if(((this._cYear% 100) == 0) && (this._cYear% 400) != 0)
        days = 28;
      else
        days = 29;
    }
  }
  return days;
}

