/////////////////////////////////////////////////
/* calculate days till christmass, May 1st etc */
/////////////////////////////////////////////////

function christmass() {
  var now = new Date();  
  var date = now.getDate();
  var month = now.getMonth() + 1;
  var year = now.getYear();
  if (year < 1000){
    year+=1900       
  }
  
  var nextXmas = new Date("December 24, 2011")
  nextXmas.setYear(year);
  if(now.getTime()>nextXmas.getTime()){
    year+=1;
    nextXmas.setYear(year);
  }
  
  msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
  daysLeft = (nextXmas.getTime() - now.getTime()) / msPerDay;
  daysLeft = Math.round(daysLeft);
  return(daysLeft);
}

function mayFirst() {
  var now = new Date();  
  var date = now.getDate();
  var month = now.getMonth() + 1;
  var year = now.getYear();
  if (year < 1000){
    year+=1900       
  }
  var nextMay = new Date("May 1, 2011")
  nextMay.setYear(year);
  if(now.getTime()>nextMay.getTime()){
    year+=1;
    nextMay.setYear(year);
  }
  
  msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
  daysLeft = (nextMay.getTime() - now.getTime()) / msPerDay;
  daysLeft = Math.round(daysLeft);
  return(daysLeft);
}

function birthday(){
  var now = new Date();  
  var date = now.getDate();
  var month = now.getMonth() + 1;
  var year = now.getYear();
  if (year < 1000){
    year+=1900       
  }
  var nextBday = new Date("October 14, 2011")
  nextBday.setYear(year);
  if(now.getTime()>nextBday.getTime()){
    year+=1;
    nextBday.setYear(year);
  }
  
  msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
  daysLeft = (nextBday.getTime() - now.getTime()) / msPerDay;
  daysLeft = Math.round(daysLeft);
  return(daysLeft);
}

function countDays(){ // get days to Christmas etc.
  var now = new Date();
  var year = now.getYear();
  if (year < 1000){
    year+=1900       
  }
  trace('this year: '+year);
  var allSentences = new Array();
  var tempLanguage;
  var tempHoliday;
  var tempArray = new Array();
  var tempDays;
  tempArray[0] = 'PÄIVIÄ JOULUUN ';
  tempArray[1] = 'DAYS TILL CHRISTMAS ';
  tempArray[2] = 'ДНЕЙ ДО РОЖДЕСТВА ';
  tempArray[3] = 'クリスマスまであと日 ';
  tempArray[4] = 'DAGAR KVAR TILL JULEN ';
  allSentences[0] = tempArray;
  
  var tempArray2 = new Array();
  tempArray2[0] = 'PÄIVIÄ VAPPUUN ';
  tempArray2[1] = 'DAYS TILL 1ST OF MAY ';
  tempArray2[2] = 'ДНЕЙ ДО 1 МАЯ ';
  tempArray2[3] = 'メーデーまであと日 ';
  tempArray2[4] = 'DAGAR KVAR TILL VAPPEN ';
  allSentences[1] = tempArray2;
  
  var tempArray3 = new Array();
  tempArray3[0] = 'PÄIVIÄ KLUUVIN SYNTTÄREIHIN ';
  tempArray3[1] = 'DAYS TILL KLUUVI\'S BIRTHDAY ';
  tempArray3[2] = 'ДНЕЙ ДО ДНЯ РОЖДЕНИЯ КЛУУВИ ';
  tempArray3[3] = 'クルービの誕生日まであと日 ';
  tempArray3[4] = 'DAGAR KVAR TILL KLUUVIS FÖDELSEDAG ';
  allSentences[2] = tempArray3;
  
  var tempArray4 = new Array();
  tempArray4[0] = 'PÄIVIÄ KLUUVIN AVAJAISIIN ';
  tempArray4[1] = 'DAYS TILL KLUUVI\'S OPENING ';
  tempArray4[2] = 'ДНЕЙ ДО ОТКРЫТИЯ КЛУУВИ ';
  tempArray4[3] = 'クルービのオープニングまであと日 ';
  tempArray4[4] = 'DAGAR KVAR TILL KLUUVIS ÖPPNING ';
  allSentences[3] = tempArray4;
  
  if(language=='fin'){tempLanguage=0;}
  if(language=='eng'){tempLanguage=1;}
  if(language=='rus'){tempLanguage=2;}
  if(language=='jpn'){tempLanguage=3;}
  if(language=='sve'){tempLanguage=4;}
  trace('mayFirst:'+mayFirst());
  trace('xmass:'+christmass());
  trace('B.day:'+birthday());

  if(mayFirst()<christmass() && mayFirst()<birthday()){
    tempHoliday=1; // 1st of May
    tempDays=mayFirst();
  } else if(christmass()<birthday()) {
    tempHoliday=0; // cristmas
    tempDays=christmass();
  } else if(year>2011){
    tempHoliday=2; // birthday
    tempDays=birthday();
  } else {
    tempHoliday=3; // opening
    tempDays=birthday();
  }
  
  
  $('#xmassCount').append(allSentences[tempHoliday][tempLanguage]+tempDays);
  
}
