/*
item      = 'lefttime'; // id of the entry on the page where the counter is to be inserted
month     = '0';        // 1 through 12 or '*' within the next month, '0' for the current month
day       = '1';        // day of month or + day offset
hour      =  0;         // 0 through 23 for the hour of the day
minutes   =  0;         // 0 through 59 for the minutes of the hour
dayOfWeek =  0;         // day of week sun=1 sat=7 or 0 for whatever day it falls on
timeZone  =  2;         // offset in hours from UTC to your timezone

REQUIRE PROTOTYPE FRAMEWORK, AND COOKIE.JS
*/


function CountDown(item, month, day, hour, minutes, dayOfWeek, timeZone) {
  this.item      = item;
  this.month     = month;
  this.day       = day;
  this.hour      = (!hour) ? 0 : hour;
  this.minutes   = (!minutes)  ? 0 : minutes;
  this.dayOfWeek = (!dayOfWeek) ? 0 : dayOfWeek;
  this.timeZone  = (!timeZone)  ? 2 : timeZone;
  
  this.lang = new Array('hu', 'en');  
  this.lang['hu'] = new Array('nap', 'óra', 'perc', 'másodperc');  
  this.lang['en'] = new Array('days', 'hours', 'mins', 'secs');
  
  this.currentLang = 'hu';
}

CountDown.prototype = {
  
  init: function() {
    var pageLoaded = 0;
	
    Event.observe(window, 'load', function() { pageLoaded = 1; });
			
    if ($(this.item)) {
    	
      this.currentLang = (Cookie.read('lang')) ? Cookie.read('lang') : 'hu';
      
      if (stId) { clearTimeout(stId); }
      
      this.displayCountdown(this.setCountdown());
      
    } else if (!pageLoaded) {      
      
      var oThis = this;      
      
      var stId  = setTimeout(function() { oThis.init(); }, 100);
      
    }
  },
  
  setCountdown: function() {
    var month = this.month;
    
    if (this.month == '*') { month = 0; }
    
    var c = this.setC(month);
    
    if ((this.month == '*') && (c < 0))  { c = this.setC('*'); }
    
    return c;
  },
  
  setC: function(month) {
    var toDate = new Date();
    
    if (this.day.substr(0, 1) == '+') {
      var day1 = parseInt(this.day.substr(1));
      toDate.setDate(toDate.getDate() + day1);
    } else {
      toDate.setDate(this.day);
    }
    
    if (month == '*') {
      toDate.setMonth(toDate.getMonth() + 1);
    } else if (month > 0) {
      if (month <= toDate.getMonth()) { toDate.setFullYear(toDate.getFullYear() + 1); }
      toDate.setMonth(month - 1);
    }
    
    if (this.dow > 0) { toDate.setDate(toDate.getDate() + (this.dow - 1 - toDate.getDay()) % 7); }
    
    toDate.setHours(this.hour);
    toDate.setMinutes(this.minutes - (this.timeZone * 60));
    toDate.setSeconds(0);
    
    var fromDate = new Date();
    
    fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
    
    var diffDate = new Date(0);
    
    diffDate.setMilliseconds(toDate - fromDate);
    
    return Math.floor(diffDate.valueOf() / 1000);
  },
  
  displayCountdown: function(countdn) {
    
    if (countdn < 0) {
      $(this.item).innerHTML = '0&nbsp;nap&nbsp;&nbsp;0&nbsp;óra&nbsp;&nbsp;0&nbsp;perc&nbsp;&nbsp;0&nbsp;másodperc';
    } else {
      var secs = countdn % 60;
      
      if (secs < 10) { secs = '0' + secs; }
      
      var countdn1 = (countdn - secs) / 60;
      var mins     = countdn1 % 60;
      
      if (mins < 10) { mins = '0' + mins; }
      
      countdn1 = (countdn1 - mins) / 60;
      
      var hours = countdn1 % 24;
      var days  = (countdn1 - hours) / 24;
      
      $(this.item).innerHTML = days + '&nbsp;' + this.lang[this.currentLang][0] + '&nbsp;&nbsp;' + hours + '&nbsp;' + this.lang[this.currentLang][1] + '&nbsp;&nbsp;' + mins + '&nbsp;' + this.lang[this.currentLang][2] + '&nbsp;&nbsp;' + secs + '&nbsp;' + this.lang[this.currentLang][3];
           
      var oThis = this;
            
      setTimeout(function() { oThis.displayCountdown(countdn - 1); }, 999);
    }
  }
  
}