/**
* @require sysCommon.js
*/

xCalendar = {
  initDone : false,
  cal_cur  : false,
  cal_pane : false,
  week     : {ru : new Array('пн','вт','ср','чт','пт','сб','вс'), 
              de : new Array('mo','di','mi','do','fr','sa','so'),
              en : new Array('mo', 'tu', 'we', 'th', 'fr', 'sa', 'su')},
  mtitles  : {ru : new Array('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'),
              de : new Array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'),
              en : new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')},
  mstitles : {ru : new Array('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек'),
              de : new Array('Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sen', 'Okt', 'Nov', 'Dez'),
              en : new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sen', 'Oct', 'Nov', 'Dec')},

  init : function() {
    if (this.initDone) {
      return;
    }
    this.cal_pane = document.body.appendChild(document.createElement('div'));
    this.cal_pane.style.position = 'absolute';
    this.cal_pane.style.display  = 'none';
    this.initDone = true;
  },
  getMonthTitle : function(idx) {
    return this.mtitles[window.lang][idx];
  },
  getDayTitle : function(idx) {
    return this.week[window.lang][idx];
  },
  getShortMonthTitle : function(idx) {
    return this.mstitles[window.lang][idx];
  },
  setCurrent : function(node) {
    this.cal_cur = node;
  },
  getCurrent : function() {
    return this.cal_cur;
  },
  
  hide : function() {
    this.cal_pane.style.display = 'none';
  },

  show : function(node, tStamp) {
    this.init();
    this.setCurrent(node);
    var d;
    var ts = String(tStamp || String(node.value));

    try {
      if (ts.indexOf('.') > 0) {
        var arr = ts.split('.');
        d = new Date(arr[2], arr[1] - 1, arr[0]);
      } else {
        d = new Date(parseInt(ts));
      }
      if (isNaN(d.getYear())) {
        d = new Date;
      }
    } catch(e) {
      d = new Date();
    }

    var daylength = 60 * 60 * 24 * 1000;
    var year      = d.getFullYear();
    var month     = d.getMonth();
    var day       = d.getDate();
    var months    = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if((year % 4 == 0) && (year % 400 != 0)) {
      months[1] = 29;
    }

    var ms = new Date(year, month, 1);
    var frst_day = ms.getDay();
    frst_day = frst_day > 0 ? frst_day : 7;

    var calendar = '';
    var rows     = 6;
    var cols     = 7;

    var k = 0;
    var set = false;

  var prevMonth = month == 0 ? this.getShortMonthTitle(11) + ' ' + (year - 1) : this.getShortMonthTitle(month - 1) + ' ' + year;  
  var nextMonth = month == 11 ? this.getShortMonthTitle(0) + ' ' + (year + 1) : this.getShortMonthTitle(month + 1) + ' ' + year;

    calendar += '<table class="xformsCalendarTable" cellpadding="0" cellspacing="0" border="0">';
    calendar += '<tr><td colspan="2" onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                         'onmouseout="this.className = \'xformsCalendarButton\';" ' + 
                         'class="xformsCalendarButton" ' + 
                         'onclick="xCalendar.show(xCalendar.getCurrent(), ' + 
                         (d.getTime() - (daylength * (day > months[month == 0 ? 11 : month - 1] ? (day - 1) : months[month == 0 ? 11 : month - 1]))) + 
                         ');">' + prevMonth + '</td><td class="xformsCalendarAccented" colspan="2">' + xCalendar.getShortMonthTitle(month) + ' ' + year + '</td>' + 
                     '<td colspan="2" onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                         'onmouseout="this.className = \'xformsCalendarButton\';" ' + 
                         'class="xformsCalendarButton" ' + 
                         'onclick="xCalendar.show(xCalendar.getCurrent(), ' + 
                         (d.getTime() + (daylength * (day > months[month == 11 ? 0 : month + 1] ? months[month == 11 ? 0 : month + 1] : months[month]))) + 
                         ');">' + nextMonth + '</td><td onclick="xCalendar.hide();" ' + 
                         'onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                         'onmouseout="this.className = \'xformsCalendarButton\';" ' + 
                         'class="xformsCalendarButton">×</td></td></tr>';
    for (var j = 0; j < cols; j++) {
      calendar += '<td class="xformsCalendarDayOfWeek">' + xCalendar.getDayTitle(j) + '</td>';
    }
    calendar += '</tr>';
    for (var i = 1; i <= rows; i++) {
      calendar += '<tr>';
      for (var j = 1; j <= cols; j++) {
        var dayStyle       = j <= 5 ? 'xformsCalendarDay' : 'xformsCalendarWeekend';
        var dayStyleActive = j <= 5 ? 'xformsCalendarDayActive' : 'xformsCalendarWeekendActive';
        if(j == frst_day) {
          set = true;
        }
        if (k < months[ms.getMonth()] && set) {
          k++;
        } else {
          set = false;
        }
        msg = set ?  k  : '&nbsp;';
        var style       = k == day && set ?  dayStyle       + ' xformsCalendarDaySelected' : dayStyle;
        var styleActive = k == day && set ?  dayStyleActive + ' xformsCalendarDaySelected' : dayStyleActive;
        
        calendar += '<td ' + (set ? ('onclick="xCalendar.getTime(' + year + ',' + month + ',' + k + ');"') : '') + ' class="' + style + '" ' + 
                         'onmouseover="this.className = \'' + styleActive + '\';" ' + 
                         'onmouseout="this.className = \'' + style + '\';">' +  msg + '</td>';
      }
      calendar += '</tr>';
    }
    calendar += '<tr>' + 
              '<td style="text-align: left;" colspan="2" class="xformsCalendarButton" onclick="xCalendar.show(xCalendar.getCurrent(), ' + (d.getTime() - daylength * 365 * 10) +  ');" ' + 
                        'onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                        'onmouseout="this.className = \'xformsCalendarButton\';">' + (year - 10) + '&nbsp;←</td>' + 
              '<td class="xformsCalendarButton" onclick="xCalendar.show(xCalendar.getCurrent(), ' + (d.getTime() - daylength * 365) +  ');" ' + 
                        'onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                        'onmouseout="this.className = \'xformsCalendarButton\';">' + (year - 1)  + '</td><td class="xformsCalendarAccented">' + year + 
                '</td><td class="xformsCalendarButton" onclick="xCalendar.show(xCalendar.getCurrent(), ' + (d.getTime() + daylength*365) +  ');" ' + 
                         'onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                         'onmouseout="this.className = \'xformsCalendarButton\';">' + (year + 1) + '</td>' + 
          '<td style="text-align: right;" colspan="2" class="xformsCalendarButton" onclick="xCalendar.show(xCalendar.getCurrent(), ' + (d.getTime() + daylength * 365 * 10) +  ');" ' + 
                        'onmouseover="this.className = \'xformsCalendarButtonActive\';" ' + 
                        'onmouseout="this.className = \'xformsCalendarButton\';">→&nbsp;' + (year + 10) + '</td>'
             + '</tr>';
    calendar += '</table>';

    this.cal_pane.innerHTML     = calendar;
    this.cal_pane.style.left    = (DOMUtl.posx(this.cal_cur) + this.cal_cur.scrollWidth ) + 'px';
    this.cal_pane.style.top     = DOMUtl.posy(this.cal_cur) + 'px';
    this.cal_pane.style.display = 'block';
  },

  getTime : function (year, month, day) {
    var tt = new Date(year, month, day);
    month++;
    var str = (day < 10 ? '0' + String(day) : day) + '.' + (month < 10 ? '0' + String(month) : month) + '.' + year;
    this.cal_cur.value = str;
    this.cal_cur.setAttribute('value', str);
    this.show(xCalendar.getCurrent(), tt.getTime());
    this.cal_pane.style.display = 'none';
  }
}
