/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Calendar - CalendarSpecialDays
 * All the special exceptions days functions
 * v1 	19 / 07 / 2007
 * Author : Mickal Gentil
 * Contact : mickael.gentil@gmail.com
 * 
 * + 19/07/2007 Mickal Gentil : Added big week end exception
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
/***************************************************************
*
* EXCEPTIONS CONSTANTS DEFINITION
*
***************************************************************/
 var Calendar;
// Default : French FreeDays
Calendar.SPECIAL_DAYS = 
{
    0  : [ 1 ],
    3  : [ 9 ],
    4  : [ 1, 8, 17 ],		
    6  : [ 14 ],
    7  : [ 15 ],
    10 : [ 1, 11 ],
    11 : [ 25 ]
};

Calendar.NB_OF_NIGHTS_MAX = new Number(3);
 
 /***************************************************************
 *
 * EXCEPTIONS METHODS
 *
 ***************************************************************/
  
/*
* Calendar.bigWeekEnd()
* Only weekend and freedays departure
*/
Calendar.bigWeekEnd = function(date, y, m, d)
{ 
  //If date is less than today -> NOK
  if(date < new Date() && date.getDate() != new Date().getDate())
    return true;
    
  /***************************
  * only friday (always OK)
  /***************************/
  if(date.getDay() == 5)
  {
    this.NB_OF_NIGHTS_MAX = 3;
    return false;
  }
    
  /***************************
  * only saturday (always OK)
  /***************************/
  if(date.getDay() == 6)
  {
    var dep = new Date(date);
    dep.setDate(dep.getDate() + 2);
    
    //If Monday is special day -> OK
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()))
    {
      this.NB_OF_NIGHTS_MAX = 3;
      return false;
    }
    
    dep.setDate(dep.getDate() + 1);
    
    //If Tuesday is special day -> OK
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()))
    {
      this.NB_OF_NIGHTS_MAX = 3;
      return false;
    }
       
    this.NB_OF_NIGHTS_MAX = 2;
    
    return false;
  }

  /***************************
  * only sunday
  /***************************/
  if(date.getDay() == 0)
  {
    var dep = new Date(date);
    dep.setDate(dep.getDate() + 2);
    
    //If Tuesday is special day -> OK
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()))
    {
      this.NB_OF_NIGHTS_MAX = 3;
      return false;
    }    
      
    dep.setDate(dep.getDate() - 1);
    
    //If Monday is special day -> OK
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()))
    {
      this.NB_OF_NIGHTS_MAX = 2;
      return false;
    }
        
    return true;
  }
      
  /********************************************************
  * Special day (other than wednesday)
  /*******************************************************/
  if(this.dateIsSpecial(date.getFullYear(),date.getMonth(),date.getDate()) && date.getDay() != 3)
  {
    var dep = new Date(date);
    dep.setDate(dep.getDate() + 1);
      
    //If next day is special day or friday -> OK
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()) || dep.getDay() == 5)
    {
      this.NB_OF_NIGHTS_MAX = 2;
      
      //if next day of the next day is friday
      dep.setDate(dep.getDate() + 1);
      if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()) || dep.getDay() == 5 || dep.getDay() == 6 || dep.getDay() == 0)
      {
        this.NB_OF_NIGHTS_MAX = 3;
      }
           
      return false;
    }
        
    return true;
  }
  
  /***************************
  * Special day before
  /***************************/
  var dep = new Date(date);
  
  
  //If next day of the next day (delicious ^^) is special day -> OK
  /*if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()) && date.getDay() != 2)
  {
    this.NB_OF_NIGHTS_MAX = 3;
    return false;
  }*/
  
  dep.setDate(dep.getDate() + 1);
    
  //If next day is special day -> OK
  if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()) && date.getDay() != 2)
  {
    //if next day of the next day is friday
    dep.setDate(dep.getDate() + 1);
    if(this.dateIsSpecial(dep.getFullYear(),dep.getMonth(),dep.getDate()) || dep.getDay() == 5)
    {
      this.NB_OF_NIGHTS_MAX = 3;
    }
    else
    {
      this.NB_OF_NIGHTS_MAX = 2;
    }
    return false;
  }
    
  return true;
  
};

 /***************************************************************
 *
 * EXCEPTIONS SPECIAL FUNCTIONS
 *
 ***************************************************************/

Calendar.dateIsSpecial = function(year, month, day) 
{
  var m = this.SPECIAL_DAYS[month];
  if (!m) return false;
  for (var i in m) if (m[i] == day) return true;
  return false;
};



