$(document).ready(function(){

    $('#ys').each(function(){
    //setInterval("OnTimer()",500);
    OnTimer();
  });
  
});

function str_replace (search, replace, subject, count) {
    
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

function IsLeapYear (Year) {
  return ((Year % 4) == 0) && (((Year % 100) != 0) || ((Year % 400) == 0))
}

function DaysPerMonth (Year, Month) {
   DaysInMonth = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

   if (Month == -1) Month = 11

   days=DaysInMonth [Month]
   if ((Month == 1) && IsLeapYear(Year)) {days++}
   return days
}

function DiffDateTime (DTa, DTb) {
   maxValues = new Array (0, 12, 0, 24, 60, 60)

   for (i=5; i>=0; i--) {
     if (DTa[i]<DTb[i] && i>0) {
        DTa[i-1]--
        DTa[i] += maxValues[i] - DTb[i]
        if (i==2) DTa[i] += DaysPerMonth (DTa[0], DTa[1])
     } else {
       DTa[i] -= DTb[i]
     }
   }
   return DTa
}

function TimeToString (value, str1, str2, str5) { 
   if (!value) return 0
   mod = value % 10
   if ((value%100)>=10 && (value%100)<=19) return str5
   if (mod == 1) return str1
   if (mod >= 2 && mod <= 4) return str2
   return str5
}  

function DaysLeftText (Year, Month, Day, Hour, Minute, Second) {

   tYear   = TimeToString (Year, 'год', 'года', 'лет')
   tMonth  = TimeToString (Month, 'месяц', 'месяца', 'месяцев')
   tDay    = TimeToString (Day, 'день', 'дня', 'дней')
   tHour   = TimeToString (Hour, 'час', 'часа', 'часов')
   tMinute = TimeToString (Minute, 'минута', 'минуты', 'минут')
   tSecond = TimeToString (Second, 'секунда', 'секунды', 'секунд')

   preValues = new Array (Year, Month, Day, Hour, Minute, Second)
   preTexts = new Array (tYear, tMonth, tDay, tHour, tMinute, tSecond)

   numPartsPresent=0
   for (i=5; i>=0; i--) if (preValues[i]!=0) numPartsPresent++
   text=''
   for (i=5; i>=0; i--) {
      if (preValues[i]!=0) {
         text=preValues[i]+' '+preTexts[i]+' '+text
         if (numPartsPresent>1) {
            text='и '+text
            numPartsPresent=0
         } 
      }
   }
   text = str_replace ('undefined 0', '', text);
   text = str_replace ('и', '', text);
   
   return text
}

function OnTimer () {

   Now = new Date();
   nowYear=Now.getYear ();
   if (nowYear<200) nowYear=nowYear+1900;

   // Time passed since my birth
   DTa = new Array (nowYear, Now.getMonth (), Now.getDate (), Now.getHours (), Now.getMinutes (), Now.getSeconds ());
   DTb = new Array (2005, 04, 24, 12, 0, 0);
   DTc = DiffDateTime (DTa, DTb);
  
   // Time to my nearest birtday
   /*var nextBDY = nowYear;
   if ((Now.getMonth ()>6) || (Now.getMonth()==6 && (Now.getDay()>17 || (Now.getDay()=17 && Now.getHours()>12)))) nextBDY++
   DTa = new Array (nowYear, Now.getMonth (), Now.getDate (), Now.getHours (), Now.getMinutes (), Now.getSeconds ())
   DTb = new Array (nextBDY, 6, 17, 12, 0, 0)
   DTd = DiffDateTime (DTb, DTa)
   */
  
      $('#ys').text(DTc[0]);
      $('#mh').text(DTc[1]);
      $('#dd').text(DTc[2]);
      
      $('#yearstext').text(DaysLeftText(DTc[0]));
      $('#monthtext').text(DaysLeftText('',DTc[1]));
      $('#daystext').text(DaysLeftText('','',DTc[2]));
      

        
      if(DTc[1]==0){
        $('#monthtext').text('месяцев');
        $('#mh').text('0');
      } else if(DTc[1]==2){
        $('#daystext').text('дней');
        $('#dd').text('0');
      }


 }



