/*
 +-------------------------------------------------------------------+
 |                   J S - C A L E N D A R   (v2.1)                  |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: May 27, 2003                Last modified: Dec. 29, 2005 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+

 EXAMPLE #1:  myCal = new CALENDAR();
              document.write(myCal.create());

 EXAMPLE #2:  myCal = new CALENDAR("2004-12");
              document.write(myCal.create());

 EXAMPLE #3:  myCal = new CALENDAR();
              myCal.year = 2004;
              myCal.month = 12;
              document.write(myCal.create());
==========================================================================================================
*/
  var cal_ID = 0;

  function CALENDAR(date) {
//========================================================================================================
// Configuration
//========================================================================================================
    this.tFontFace = 'Verdana, Arial, Helvetica'; // title: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.tFontSize = 11;                 // title: font size (pixels)
    this.tFontColor = '#000000';         // title: font color
    this.tBGColor = '#ffffff';           // title: background color

    this.hFontFace = 'Verdana, Arial, Helvetica'; // heading: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.hFontSize = 11;                 // heading: font size (pixels)
    this.hFontColor = '#003399';         // heading: font color
    this.hBGColor = '#ffffff';           // heading: background color

    this.dFontFace1 = 'Verdana, Arial, Helvetica'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.dFontSize1 = 9;                 // days: font size (pixels)
    this.dFontColor1 = '#000000';         // days: font color
    this.dBGColor1 = '#fbfbfb';           // days: background color

    this.dFontFace2 = 'Verdana, Arial, Helvetica'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.dFontSize2 = 9;                 // days: font size (pixels)
    this.dFontColor2 = '#000000';         // days: font color
    this.dBGColor2 = '#f5f5f5';           // days: background color

    this.wFontFace = 'Verdana, Arial, Helvetica'; // weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.wFontSize = 9;                 // weeks: font size (pixels)
    this.wFontColor = '#cccccc';         // weeks: font color
    this.wBGColor = '#ffffff';           // weeks: background color

    this.saFontColor = '#ff0000';        // Saturdays: font color

    this.suFontColor = '#ff0000';        // Sundays: font color

    this.tdBGColor = '#cccc99';          // today: background color

    this.borderColor = '#ffffff';        // border color
    this.hilightColor = '#FFFF00';       // hilight color (works only in combination with link)

    this.link = '';                      // page to link to when day is clicked

    this.offset = 2;                     // week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)

    // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
    this.weekdays = new Array('Сб', 'Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт');

    // months: must start with January
    this.months = new Array('ЯНВАРЬ' , 'ФЕВРАЛЬ', 'МАРТ', 'АПРЕЛЬ', 'МАЙ', 'ИЮНЬ', 'ИЮЛЬ', 'АВГУСТ', 'СЕНТЯБРЬ', 'ОКТЯБРЬ', 'НОЯБРЬ', 'ДЕКАБРЬ');

    // don't change from here
    this.year = 0;
    this.month = 0;
    this.size = 0;
    this.mDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    if(!date) {
      var obj = new Date();
      this.year = obj.getYear();
      if(this.year < 1900) this.year += 1900;
      this.month = obj.getMonth() + 1;
    }
    else {
      var d = date.split('-');
      this.year = d[0];
      this.month = d[1];
    }

//========================================================================================================
// Functions
//========================================================================================================
    this.set_styles = function() {
      cal_ID++;
      var html = '<style> .cssTitle' + cal_ID + ' { ';
      if(this.tFontFace) html += 'font-family: ' + this.tFontFace + '; ';
      if(this.tFontSize) html += 'font-size: ' + this.tFontSize + 'px; ';
      if(this.tFontColor) html += 'color: ' + this.tFontColor + '; ';
      if(this.tBGColor) html += 'background-color: ' + this.tBGColor + '; ';
      html += '} .cssHeading' + cal_ID + ' { ';
      if(this.hFontFace) html += 'font-family: ' + this.hFontFace + '; ';
      if(this.hFontSize) html += 'font-size: ' + this.hFontSize + 'px; ';
      if(this.hFontColor) html += 'color: ' + this.hFontColor + '; ';
      if(this.hBGColor) html += 'background-color: ' + this.hBGColor + '; ';
      html += '} .cssDaysL' + cal_ID + ' { ';
      if(this.dFontFace1) html += 'font-family: ' + this.dFontFace1 + '; ';
      if(this.dFontSize1) html += 'font-size: ' + this.dFontSize1 + 'px; ';
      if(this.dFontColor1) html += 'color: ' + this.dFontColor1 + '; ';
      if(this.dBGColor1) html += 'background-color: ' + this.dBGColor1 + '; ';
      html += '} .cssDaysD' + cal_ID + ' { ';
      if(this.dFontFace2) html += 'font-family: ' + this.dFontFace2 + '; ';
      if(this.dFontSize2) html += 'font-size: ' + this.dFontSize2 + 'px; ';
      if(this.dFontColor2) html += 'color: ' + this.dFontColor2 + '; ';
      if(this.dBGColor2) html += 'background-color: ' + this.dBGColor2 + '; ';
      html += '} .cssWeeks' + cal_ID + ' { ';
      if(this.wFontFace) html += 'font-family: ' + this.wFontFace + '; ';
      if(this.wFontSize) html += 'font-size: ' + this.wFontSize + 'px; ';
      if(this.wFontColor) html += 'color: ' + this.wFontColor + '; ';
      if(this.wBGColor) html += 'background-color: ' + this.wBGColor + '; ';
      html += '} .cssSaturdays' + cal_ID + ' { ';
      if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
      if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
      if(this.saFontColor) html += 'color: ' + this.saFontColor + '; ';
      html += '} .cssSundays' + cal_ID + ' { ';
      if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
      if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
      if(this.suFontColor) html += 'color: ' + this.suFontColor + '; ';
      html += '} .cssToday' + cal_ID + ' { ';
      if(this.tdBGColor) html += 'background-color: ' + this.tdBGColor + '; ';
      html += '} .cssHilight' + cal_ID + ' { ';
      if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
      if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
      if(this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
      if(this.hilightColor) html += 'background-color: ' + this.hilightColor + '; ';
      html += 'cursor: default; ';
      html += '} </style>';

      return html;
    }

    this.leap_year = function(year) {
      return (!(year % 4) && (year < 1582 || year % 100 || !(year % 400))) ? true : false;
    }

    this.get_weekday = function(year, days) {
      var a = days;
      if(year) a += (year - 1) * 365;
      for(var i = 1; i < year; i++) if(this.leap_year(i)) a++;
      if(year > 1582 || (year == 1582 && days >= 277)) a -= 10;
      if(a) a = (a - this.offset) % 7;
      else if(this.offset) a += 7 - this.offset;

      return a;
    }

    this.get_week = function(year, days) {
      var firstWDay = this.get_weekday(year, 0);
      return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
    }

    this.table_cell = function(content, cls, date) {
      var size = Math.round(this.size * 1.5);
      var html = '<td align=center width=24 height=20 class="' + cls + '"';
      var clsName = cls.toLowerCase();

      if(this.link && content != '&nbsp;' && clsName.indexOf('day') != -1) {
        html += ' onMouseOver="this.className=\'cssHilight' + cal_ID + '\'"';
        html += ' onMouseOut="this.className=\'' + cls + '\'"';
        html += ' onClick="document.location.href=\'' + this.link + '?date=' + date + '\'"';
      }
      html += '>' + content + '</td>';

      return html;
    }

    this.table_head = function(title) {
      var html, ind, wDay, i;

      html = '<tr><td></td><td colspan=7 class="cssTitle' + cal_ID + '"><strong>' + title + '</strong></td></tr><tr>';
      html += this.table_cell('&nbsp;', 'cssHeading' + cal_ID);
      for(i = 0; i <= 6; i++) {
        ind = (i + this.offset) % 7;
        wDay = this.weekdays[ind];
        html += this.table_cell(wDay, 'cssHeading' + cal_ID);
      }
      html += '</tr>';

      return html;
    }

    this.create = function() {
      var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
          inThisMonth, weekNr, wBGColor, wdays, days, ind, cls, content, date, i;

      this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
      if(this.wFontSize > this.size) this.size = this.wFontSize;

      obj = new Date();
      curYear = obj.getYear();
      if(curYear < 1900) curYear += 1900;
      curMonth = obj.getMonth() + 1;
      curDay = obj.getDate();

      if(this.year < 1 || this.year > 3999) html = '<b>Year must be 1 - 3999!</b>';
      else {
        if(this.leap_year(this.year)) this.mDays[1] = 29;
        for(i = days = 0; i < this.month - 1; i++) days += this.mDays[i];

        start = this.get_weekday(this.year, days);
        stop = this.mDays[this.month-1];

        html = this.set_styles();
        html += '<table border=0 cellspacing=1 cellpadding=0>';
        title = this.months[this.month-1];
        html += this.table_head(title);
        daycount = 1;

        if((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
        else inThisMonth = false;

        weekNr = this.get_week(this.year, days);
        wBGColor = this.wBGColor;

        while(daycount <= stop) {
          html += '<tr>';
          if(!weekNr) {
            if(this.year == 1) content = '&nbsp;';
            else if(this.year == 1583) content = 52;
            else content = this.get_week(this.year - 1, 365);
          }
          else if(this.month == 12 && weekNr >= 52 && wdays < 4) content = 1;
          else content = weekNr;
          html += this.table_cell(content, 'cssWeeks' + cal_ID);

          for(i = wdays = 0; i <= 6; i++) {
		 if(i==1||i==3||i==5) cls = 'cssDaysL';
		else  cls = 'cssDaysD';
		var cls1;
		
            ind = (i + this.offset) % 7;
            if(ind == 0) cls1 = 'cssSaturdays';
            else if(ind == 1) cls1 = 'cssSundays';
		
		
            if((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
            else {
              content = daycount;
              if(inThisMonth && daycount == curDay) cls1 = 'cssToday';
              else if(this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
              daycount++;
              wdays++;
            }
			cls1=" "+cls1 + cal_ID;
            date = this.year + '-' + this.month + '-' + (daycount - 1);
            html += this.table_cell(content, cls + cal_ID + cls1, date);
          }


			html += '</tr>';
          weekNr++;
        }
        html += '</table>';
      }
      return html;
    }
  }

