//*******************************************************
//  Gestionnaire de calendrier par Florian Arroseres
//  Contact : florian.arroseres@gmail.com
//*******************************************************
function calendar(Element, currentDate, dateToDisplay, Mode, Events_url){
  dateToDisplay = dateToDisplay.split('-');
  
  var that = this;

  // Définition de la date courante
  this.cYear  = currentDate[0];
  this.cMonth = currentDate[1]-1;
  this.cDay   = currentDate[2];
  
  // Définition de la date à afficher
  this.dYear  = dateToDisplay[0];
  this.dMonth = dateToDisplay[1]-1;
  
  // Définition de la langue du calendrier
  this.language = 'Fr';
  
  this.Display  = false;
  
  //*******************************************************
  //  Définition des méthodes
  //*******************************************************
  
  //=======================================================
  //  Nombre de jours dans le mois
  //=======================================================
  this.daysInMonth = function(Year, Month){
    for(var i=31; i>28 ; i--){
      tmpDate = new Date(Year, Month, i);         
      if(tmpDate.getDate() == i) return i;
    }
    
    return i;
  };
  
  //*****************************************************
  //  Nombre de jours dans le mois précédent
  //*****************************************************
  this.daysInPreviousMonth = function(){
    if(this.dMonth == 0){
      Month = 11;
      Year  = this.dYear - 1;
    } else {
      Month = this.dMonth - 1;
      Year  = this.dYear;
    }
    
    return this.daysInMonth(Year, Month);
  };
  
  //=======================================================
  //  Nombre de semaines dans le mois
  //=======================================================
  this.weeksInMonth = function(){
    fDay = this.firstDay(this.dYear, this.dMonth);
    Days = this.daysInMonth(this.dYear, this.dMonth);
    
    return Math.ceil((Days+fDay) / 7);
  };
  
  //=======================================================
  //  Premier jours du mois
  //=======================================================
  this.firstDay = function(){
    tmpDate = new Date(this.dYear, this.dMonth);
    return tmpDate.getDay();
  };
  
  //=======================================================
  // Sélection d'un élément par son id
  //=======================================================
  this.$ = function(element){
    if(document.getElementById) { E = document.getElementById(element) }
    else if(document.all.tags) { E = document.all.tags(element) }
    else if(document.layers) { E = document.layers[element] }
    
    return E;
  };
  
  
  //*******************************************************
  //  Définition des méthodes publiques
  //*******************************************************
  
  //=======================================================
  //  Affichage du calendrier
  //=======================================================
  this.display = function(){
    this.Display = true;
    this.$(Element).innerHTML = ''; 
    
    var firstDay = this.firstDay();
    var Weeks    = this.weeksInMonth();
    var Days     = this.daysInMonth(this.dYear, this.dMonth);
    var PDays    = this.daysInPreviousMonth();
    var count    = new Number(-firstDay);
    var events   = '';

    url    = Events_url;
    url    = "/widgets_agenda/get_events/"+this.dYear+"/"+this.setZero(parseInt(this.dMonth)+1);

    $.ajax({url: url, async: false, success: function(data){
      if(data != 'null'){
        events = eval('('+ data +')');
      } else {
        events = {};
      }
    }})

    //----------------------------------------------------------------------------------
    // Création des différents bloc principaux
    //----------------------------------------------------------------------------------
    oContainer    = document.createElement('div');
    oContainer.id = "calendar";
    
    oTitle     = document.createElement('div');
    oTitle.id  = "title_month";
    
    oDays      = document.createElement('div');
    oDates     = document.createElement('div');
	oDates.className = "calendrier_content";
    
    //----------------------------------------------------------------------------------
    // Création du titre du calendrier
    //----------------------------------------------------------------------------------
    label = document.createTextNode('<<');
    oSpan = document.createElement('span');
    oLink = document.createElement('a');
    oLink.className = "previous";
    oLink.href = 'javascript:void(0);';
    oLink.onclick = function(){ that.previousMonth(); };
    oTitle.appendChild(oLink);
    
    textTitle = eval('this.aMonth'+this.language+'[this.dMonth]')+' '+this.dYear;
    label = document.createTextNode(textTitle);
    oTitle.appendChild(label);
    
    label = document.createTextNode('>>');
    oSpan = document.createElement('span');
    oLink = document.createElement('a');
    oLink.className = "next";
    oLink.href = 'javascript:void(0);';
    oLink.onclick = function(){ that.nextMonth(); };
    oTitle.appendChild(oLink);
    

    //----------------------------------------------------------------------------------
    // Création des cellules des jours
    //----------------------------------------------------------------------------------
    for(var k=0 ; k<7 ; k++){
      label = eval('this.aDay'+this.language+'['+k+']');
      label = document.createTextNode(label.substring(0, 2));
      
      day = document.createElement('div');
      day.className = 'cal_day_head';
      day.appendChild(label);
      
      oDays.appendChild(day);
    }
    
    //----------------------------------------------------------------------------------
    // Création des cellules des dates
    //----------------------------------------------------------------------------------
    for(var i=0 ; i<6 ; i++){
      for(var j=0 ; j<7 ; j++){
        count++;
        
        if(i%2){
          if(j%2) CName = "odd";
          else CName = "even";
        } else {
          if(j%2) CName = "even";
          else CName = "odd";
        }
        
        month = this.setZero(this.dMonth+1);
        day   = this.setZero(count);
        
        tmp = eval('events.e'+this.dYear+month+day);
        if(events != false && tmp)
          CName = "events";
                
        cDateTmp = this.cYear+'-'+this.cMonth+'-'+this.cDay;
        dDateTmp = this.dYear+'-'+this.dMonth+'-'+day;
        
        oDate = document.createElement('div');
        oLink = document.createElement('a');
        
        if(cDateTmp == dDateTmp){
          oDate.className = "cal_cur_day";
          label = document.createTextNode(count);
          id    = day+'-'+month+'-'+this.dYear;
          
        }else if(count < 1){
          oDate.className = "cal_previous_month";
          label = document.createTextNode(PDays + count);
          
          if(this.dMonth == 0){ Year = this.dYear-1; Month = 12; }
          else{ Year = this.dYear; Month = this.dMonth; }
          
          id = this.setZero(PDays + count)+'-'+this.setZero(Month)+'-'+Year;
          
        }else if(count > Days){
          oDate.className = "cal_next_month";
          label = document.createTextNode(count - Days);
          
          if(this.dMonth == 11){ Year = parseInt(this.dYear)+1; Month = 1; }
          else{ Year = this.dYear; Month = this.dMonth + 2; }
          
          id = this.setZero(count - Days)+'-'+this.setZero(Month)+'-'+Year;
          
        }else{
          oDate.className = "cal_day_" + CName + " " + tmp;
          label = document.createTextNode(count);
          id    = day+'-'+month+'-'+this.dYear;
        }
        
        oLink.id = id;
        
        if(CName == 'events' || Mode == "selection")
		{
          oLink.href = '/calendrier/detail/'+this.dYear+"-"+month+"-"+day;
        }
        
        oLink.appendChild(label);
        oDate.appendChild(oLink);
        oDates.appendChild(oDate);
      }
    }

    //----------------------------------------------------------------------------------
    
    oContainer.appendChild(oTitle);
    oContainer.appendChild(oDays);
    oContainer.appendChild(oDates);
    
    this.$(Element).appendChild(oContainer);
    //alert(this.$(Element).innerHTML);
  }
  
  this.setZero = function(Number){
    if(Number < 10)
      Number = '0'+Number;
    
    return Number;
  }
  
  //=======================================================
  //  Affichage du mois précédent
  //=======================================================
  this.previousMonth = function(){
    if(this.dMonth > 0)
      this.dMonth--;
    else {
      this.dMonth = 11;
      this.dYear--;
    }
    this.display();
  }
  
  //=======================================================
  //  Affichage du mois suivant
  //=======================================================
  this.nextMonth = function(){
    if(this.dMonth < 11)
      this.dMonth++;
    else {
      this.dMonth = 0;
      this.dYear++;
    }
    this.display();
  }
  
  //=======================================================
  //  Affichage d'une date spécifique
  //=======================================================
  this.changeDate = function(date){
    alert('ok');
  }
  
  this.isDisplay = function(){
    if(this.Display == true)
      return true;
    else
      return false;
  }
}

// Tableau des jours en français
calendar.prototype.aDayFr   = ['Dimanche',
                               'Lundi', 
                               'Mardi', 
                               'Mercredi', 
                               'Jeudi', 
                               'Vendredi',
                               'Samedi'];

// Tableau des mois en français
calendar.prototype.aMonthFr = ['Janvier', 
                               'Février', 
                               'Mars', 
                               'Avril', 
                               'Mai', 
                               'Juin', 
                               'Juillet', 
                               'Août', 
                               'Septembre', 
                               'Octobre', 
                               'Novembre', 
                               'Décembre'];
