// JavaScript Document
/*
*objekt umoznnujici prevod dat ziskanych z upravenoho dynarch kalendaru do podoby nutne pro
*ajax requsty posilane pro potreby denkiu
*a obracen prevadejici udalosti obsluhy deniku (posum o jeden tydne mesic den vpred vzad) nebo zmenu zobrazonaveho modu
*tak aby se adekvatne projevili i v dneiku
*diky tomuot objektu je propojena prace s denikem rizena funkcemi v ramic skriptu hednler.js a kalendare dynarch
*skipt jsem vytvoril abych minimalizoval zasahy nutne do struktury dynarch calendar
*/

//konstruktor
//primaj date -datum
// a type typd modu kalendare
CalConector=function(type,date){
  if(type=="day" || type=="week" || type=="month"){
    this.type=type;
    }
  else this.type='day';
  if(arguments.length==2){
      this.date=new Date();
      this.date.setFullYear(date.getFullYear());
      this.date.setMonth(date.getMonth());
      this.date.setDate(date.getDate());
    }
  else {
    this.date=new Date();
    }
    this.SpecialDays={};
    this.PreviousSpecialDays={};
    this.calendar=null;
    this.BeforeMonth=null;
    this.BeforeDay=null;
    this.BeforeWeekNum=null;
    this.BeforeWeekMonth=null;
    this.BeforeWeekYear=null;
    this.BeforeDayMonth=null;
    this.BeforeDayYear=null;
    this.initialCalendarRequest=true;
    //this.initSpecialDays()
  }
  
//metoda umoznujici nastaveni modu kalendare /deniku
CalConector.prototype.setType=function(type){
  if(type=="day" || type=="week" || type=="month"){
    this.type=type;
    if(this.calendar)this.calendar._init(1,this.date);
    }
  }
  
  //metoda diky ktere ziskam odkaz na objekt dynarch calendare
CalConector.prototype.setCalendarHandler=function(cal){
  this.calendar=cal;
  }
//nastaveni data
CalConector.prototype.setDate=function(date){
    //alert(date);
    //alert(this.date)
    this.date=new Date();
    this.date.setDate(1);
    this.date.setFullYear(date.getFullYear());
    this.date.setMonth(date.getMonth());
  //  alert(this.date.getMonthDays());
  //  alert(date.getDate());
    if(this.date.getMonthDays()<date.getDate()){
       this.date.setDate(this.date.getMonthDays());
      }
    else this.date.setDate(date.getDate());
    
  }
//metoda ziska z cisla poradi tynde (0 based) v mesic datum odpovidajici dnu v tomto tydnu
//a nastavi odpovidajicim zpusobem dynarch calendar
CalConector.prototype.getWeekDay=function(it){
   //alert(this.date+'it '+it);
   //ziskani informaci o esici
   var firstDay=this.getMonthFirstDay(this.date.getMonth(),this.date.getFullYear());
   var dayNum=this.date.getMonthDays();
   var weekCount=Math.ceil((firstDay+dayNum)/7);
   var actualWeek=Math.floor((firstDay+this.date.getDate()-1)/7);
   var firstweekDay=(actualWeek)*7+1-firstDay;
   var lastweekDay=(actualWeek)*7+7-firstDay;
   //cislo tydne mezi ) a max
   if(actualWeek>0 && actualWeek<weekCount-1){
      this.type="day";
      this.date.setDate(firstweekDay+it);
      
      if(this.calendar)this.calendar._init(1,this.date);
    }
  //cislo tydne )
  else if(actualWeek==0){ 
      if(firstDay==0){
          this.type="day";
          this.date.setDate(firstweekDay+it);
          if(this.calendar)this.calendar._init(1,this.date);
        }
      else{
          this.type="day";
          var prev=this.getPrevMonth(this.date);
          var pNum=prev.getMonthDays();
          var offset=pNum+1-firstDay;
          var pDays=[];
          for(var i=offset;i<=pNum;i++){
              pDays.push(i)
            }
          for(var i=1;i<=7-firstDay;i++){
              pDays.push(i)
            }
          if(it<firstDay){
            this.date.setFullYear(prev.getFullYear());
            this.date.setMonth(prev.getMonth());
          }
          this.date.setDate(pDays[it])
          //alert(pDays)
          if(this.calendar)this.calendar._init(1,this.date);
        }   
    }
  //cislo tydne max
  else if(actualWeek==weekCount-1){ 
      this.type="day";
      var next=this.getNextMonth(this.date);
      var nDays=[];
      for(var i=firstweekDay;i<=dayNum;i++){
          nDays.push(i);
        }
     // alert(dayNum+'dn - '+'kknd fr'+firstDay+' weekCoutn'+weekCount);
      var pom=weekCount*7-dayNum-firstDay;
      for(var i=1;i<=pom;i++){
          nDays.push(i);
        }
      //alert(nDays);
     // alert(offset)
      var offset=7-pom-1
      if(it>offset){
          this.date.setFullYear(next.getFullYear());
          this.date.setMonth(next.getMonth());
        }
      this.date.setDate(nDays[it]);
      if(this.calendar)this.calendar._init(1,this.date);
    }
  }
//ziska datum aktulani dne podle jeho poradi v kalendari  (it brane z tabulky i dny ktere jsou v jinem mesici)
CalConector.prototype.getMonthDay=function(it){
    var firstDay=this.getMonthFirstDay(this.date.getMonth(),this.date.getFullYear());
    var dayNum=this.date.getMonthDays();
    var weekCount=Math.ceil((firstDay+dayNum)/7);
    if(it>=firstDay && it<=firstDay+dayNum){
        this.date.setDate(it-firstDay+1);
        this.type="day";
        if(this.calendar)this.calendar._init(1,this.date);
      }
  }

//vraci datum odpovidajici predeslemu dnu
CalConector.prototype.prevDay=function(){
    var d=this.date.getDate();
    if(d!=1)this.date.setDate(this.date.getDate()-1);
    else{
        var m=this.date.getMonth();
        if(m!=0){
            this.date.setMonth(this.date.getMonth()-1);
            this.date.setDate(this.date.getMonthDays());
          }
        else{
          this.date.setFullYear(this.date.getFullYear()-1);
          this.date.setMonth(11);
          this.date.setDate(this.date.getMonthDays());
          }
      }
      if(this.calendar)this.calendar._init(1,this.date);
  }  
//vraci datum odpovidajici predeslemu mesici
CalConector.prototype.prevMonth=function(){
    if(this.date.getMonth()==0){
          this.date.setDate(1);
          this.date.setMonth(11);
          this.date.setFullYear(this.date.getFullYear()-1)
            }
    else{ 
          this.date.setDate(1)
          this.date.setMonth(this.date.getMonth()-1);
          this.date.setFullYear(this.date.getFullYear());
        }
    if(this.calendar)this.calendar._init(1,this.date);
  }  
//vraci datum odpovidajici predeslemu tydnu
CalConector.prototype.prevWeek=function(){
  this.SpecialDays={};
  var firstDay=this.getMonthFirstDay(this.date.getMonth(),this.date.getFullYear());
  var weekCount;
  var dayNum=this.date.getMonthDays();
  var weekCount=Math.ceil((firstDay+dayNum)/7);
  var actualWeek=Math.floor((firstDay+this.date.getDate()-1)/7);
  if(actualWeek>0){ 
      var pom=7*actualWeek-firstDay-6;
      this.date.setDate(pom)
      if(this.calendar)this.calendar._init(1,this.date);
    }
  else{
    var pom=this.getPrevMonth(this.date);
    //alert(pom)
    this.date.setDate(1);
    this.date.setFullYear(pom.getFullYear());
    this.date.setMonth(pom.getMonth());
    this.date.setDate(pom.getMonthDays()-(6-firstDay));
    if(this.calendar)this.calendar._init(1,this.date);
    }
  }  
CalConector.prototype.nextDay=function(){
  var d=this.date.getDate();
 
    if(d!=this.date.getMonthDays())this.date.setDate(this.date.getDate()+1);
    else{
        var m=this.date.getMonth();
        if(m!=11){
            this.date.setDate(1)
            this.date.setMonth(this.date.getMonth()+1);
          }
        else{
          this.date.setDate(1);
          this.date.setFullYear(this.date.getFullYear()+1);
          this.date.setMonth(0);
          }
      }
      //alert(this.date)
      if(this.calendar)this.calendar._init(1,this.date);
  }  
CalConector.prototype.nextMonth=function(){
  if(this.date.getMonth()==11){
          this.date.setDate(1);
          this.date.setMonth(0);
          this.date.setFullYear(this.date.getFullYear()+1);
            }
    else{ 
          this.date.setDate(1);
          this.date.setMonth(this.date.getMonth()+1);
          this.date.setFullYear(this.date.getFullYear())
        }
    if(this.calendar)this.calendar._init(1,this.date);
  }  
CalConector.prototype.nextWeek=function(){
  var firstDay=this.getMonthFirstDay(this.date.getMonth(),this.date.getFullYear());
  var weekCount;
  var dayNum=this.date.getMonthDays();
  var weekCount=Math.ceil((firstDay+dayNum)/7);
  var actualWeek=Math.floor((firstDay+this.date.getDate()-1)/7);
  if(actualWeek<weekCount-1){ 
      var pom=(++actualWeek)*7+1-firstDay;
      this.date.setDate(pom)
      if(this.calendar)this.calendar._init(1,this.date);
    }
  else{
    var pom=this.getNextMonth(this.date);
    this.date.setDate(1);
    this.date.setFullYear(pom.getFullYear());
    this.date.setMonth(pom.getMonth());
    if(this.calendar)this.calendar._init(1,this.date);
    }
  }  


//fuknce umozni zvyrazneni "specifikovanych dnu"  v racmi dynarch claendare 
//je volan pri kazde zmene data v dynarch calendary
CalConector.prototype.initSpecialDays=function(){
  //alert(this.date+" specialInit")
  this.SpecialDays={};
  //podle modu zobrazeni zvyraznim jeden den. tyden nebo mesic
    if(this.type=='day'){
        this.SpecialDays=null;
         var pom=this.date.getMonth()
         var days=[]
         days.push(this.date.getDate())
         this.SpecialDays={}
        this.SpecialDays[pom]=days
      }
    else if(this.type=="month"){
        var pocetdnu=this.date.getMonthDays();
        var days=[];
        for(var i=1;i<=pocetdnu;i++){
            days.push(i);
          }
        var pom=this.date.getMonth()
        this.SpecialDays={}
        this.SpecialDays[pom]=days
      }
    else if(this.type=="week"){
    //alert(this.date.getMonthDays())
        this.SpecialDays={};
        var firstDay=this.getMonthFirstDay(this.date.getMonth(),this.date.getFullYear());
        var weekCount;
        var dayNum=this.date.getMonthDays();
        var weekCount=Math.ceil((firstDay+dayNum)/7);
        var actualWeek=Math.floor((firstDay+this.date.getDate()-1)/7);
        this.actualWeek=actualWeek;
        var days=[];
        var firstweekDay=(actualWeek)*7+1-firstDay;
        var lastweekDay=(actualWeek)*7+7-firstDay;
        //alert("FF"+firstDay+"fd :"+firstweekDay+' : ld :'+lastweekDay+" aw "+actualWeek+" date :"+this.date.getDate())
        if(firstweekDay>0 && lastweekDay<=this.date.getMonthDays()){
          for(var i=firstweekDay;i<=lastweekDay;i++){
              days.push(i);
            }
            var pom=this.date.getMonth()
            this.SpecialDays={}
            this.SpecialDays[pom]=days
          }
        else if(firstweekDay <=0){ 
          if(firstDay==0){
             for(var i=1;i<=7;i++){
              days.push(i);
              }
              this.SpecialDays[this.date.getMonth()]=days
            }
          else{ 
            var pom=this.getPrevMonth(this.date)
           
          
            var prevMDayNum=pom.getMonthDays();
            var offset=prevMDayNum+1-firstDay;
            //alert(offset)
            for(var i=offset;i<=prevMDayNum;i++){
              days.push(i)
            }
            var pomkey=pom.getMonth();
            this.SpecialDays[pomkey]=days
            var days2=[];
            for(var i=1;i<=7-firstDay;i++){
              days2.push(i)
            }
            this.SpecialDays[this.date.getMonth()]=days2 
          }
        }
        else if(lastweekDay>this.date.getMonthDays()){
            var days=[]
            //alert(firstweekDay+' pod'+this.date.getMonthDays())
            for(var i=firstweekDay;i<=this.date.getMonthDays();i++){
              //alert(i)
              days.push(i);
            }
            this.SpecialDays[this.date.getMonth()]=days;
            days2=[];
            var dayscount=(weekCount*7)-this.date.getMonthDays()-firstDay
            //alert(dayscount)
            for(var i=1;i<=dayscount;i++){
                days2.push(i)
              }
            var pom=this.getNextMonth(this.date)
            this.SpecialDays[pom.getMonth()]=days2 
      }
    }
  }
CalConector.prototype.getPrevMonth=function(date){
  var pom=new Date();
  pom.setDate(1);
  if(this.date.getMonth()==0){
      pom.setMonth(11);
      pom.setFullYear(this.date.getFullYear()-1)
  }
  else{ 
        pom.setMonth(this.date.getMonth()-1);
        pom.setFullYear(this.date.getFullYear())
      }
  return pom;
  }  
CalConector.prototype.getNextMonth=function(date){
  var pom=new Date();
  pom.setDate(1)
  if(this.date.getMonth()==11){
      pom.setMonth(0);
      pom.setFullYear(this.date.getFullYear()+1);
  }
  else{ 
      pom.setMonth(this.date.getMonth()+1);
      pom.setFullYear(this.date.getFullYear())
        }
  return pom;
  }  
//vrati poradi prvniho dne mesice v ramci tydne
CalConector.prototype.getMonthFirstDay=function(month,year){
    var date=new Date();
    date.setDate(1);
    date.setFullYear(year);
    date.setMonth(month);
   // date.setDate(1);
    var pom=date.getDay();
    pom=(pom==0)?pom=6:--pom;
    return pom
  }

