// ---- Cookie handling

function setCookie(sName, sValue)
{
 document.cookie = sName + '=' + escape(sValue) + ';';
}

function getCookie(sName)
{
 // cookies are separated by semicolons
 var aCookie = document.cookie.split('; ');

 for (var i=0; i < aCookie.length; ++i)
 {
  // a name/value pair (a crumb) is separated by an equal sign
  var aCrumb = aCookie[i].split('=');

  if (sName == aCrumb[0])
   return unescape(aCrumb[1]);
 }

 return null; // a cookie with the requested name does not exist
}

// ---- Calendar

var today = new Date();

var year = getCookie('rtv_calendar_year');
if (year == null)
 year = today.getFullYear();

var month = getCookie('rtv_calendar_month');
if (month == null)
 month = today.getMonth();
else
 --month;

var month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if ((year % 4) == 0)
 month_days[1] = 29;

var year_months = new Array('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь',
 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь');

// redraw calendar corresponding to the month & year
function showCalendar()
{
 var calendar = new Array();

 first_day = new Date(year, month, 1);

 day = first_day.getDay();

 if (day == 0)
  day = 7;

 --day;

 maxdate = month_days[month];

 i = 1;

 while (i <= maxdate)
 {
  week = new Array();

  for (j = 0; j < 7; ++j)
  {
   if (((i == 1) && (j < day)) || (i > maxdate))
    week[j] = 0;
   else
   {
    week[j] = i;
    ++i;
   }
  }

  calendar[calendar.length] = week;
 }
 
 var out = '<TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0 STYLE="border: 1px solid #8CA2B5;">\n' +
  '<TR><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  'Пн\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  'Вт\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  'Ср\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  'Чт\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  'Пт\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY">\n' +
  '<B>Сб</B>\n' +
  '</TD><TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDAY" STYLE="border-right:0px;">\n' +
  '<B>Вс</B>\n' +
  '</TD></TR>\n';

 for (i = 0; i < calendar.length; ++i)
 {
  out += '<TR>';

  for (j = 0; j < 7; ++j)
  {
   var date = '' + year + (((month + 1) < 10) ? '0' : '') + (month + 1) +
    ((calendar[i][j] < 10) ? '0' : '') + calendar[i][j];

   out += '<TD ALIGN=CENTER VALIGN=MIDDLE CLASS="CALENDAR_WEEKDATE"' +
    (((year == today.getFullYear()) && (month == today.getMonth()) &&
     (calendar[i][j] == today.getDate())) ? ' STYLE="border:1px solid #FFB600;"' : '') + '>\n' +
    ((calendar[i][j] == 0) ? '&nbsp;' :
     (
      ((newsCache[date] != null)||(daysCache[date] != null)||(eventsCache[date] != null)||(toursCache[date]!=null) ? 
       ('<A HREF="events.php?date=' + date + '" TITLE="' + ((newsCache[date] != null) ?
       ('новостей: ' + newsCache[date] + ' \n') : '')+
       ((daysCache[date] != null) ?
       ('дней рождения: ' + daysCache[date] + ' \n') : '')+
       ((toursCache[date] != null) ?
       ('турниров: ' + toursCache[date] + ' \n') : '')+
       ((eventsCache[date] != null) ?
       ('других событий: ' + eventsCache[date] + '') : '')+'">'):'') +
      calendar[i][j] +
      ((newsCache[date] != null) ? '</A>' : ((daysCache[date] != null) ? '</A>' : ((toursCache[date] != null) ? '</A>' : ((eventsCache[date] != null) ? '</A>' : ''))))
     )
    ) + '\n</TD>';
  }

  out += '</TR>\n';
 }
 
 out += '</TABLE>';
 
 document.all.calendar.innerHTML = out;
 
 setCookie('rtv_calendar_month', month + 1);
 document.all.calendar_month.innerHTML = year_months[month];
 
 setCookie('rtv_calendar_year', year);
 document.all.calendar_year.innerHTML = '&nbsp;' + year + '&nbsp;';
}

// decrease the month
function decCalendarMonth()
{
 if (month == 0)
 {
  --year;
  month = 11;
 }
 else
  --month;

 showCalendar();
}

// increase the month
function incCalendarMonth()
{
 if (month == 11)
 {
  ++year;
  month = 0;
 }
 else
  ++month;

 showCalendar();
}

// decrease the year
function decCalendarYear()
{
 if (year > 1970)
  --year;

 showCalendar();
}

// increase the year
function incCalendarYear()
{
 ++year;

 showCalendar();
}

// reset to today
function resetCalendar()
{
 month = today.getMonth();
 year = today.getFullYear();

 showCalendar();
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//set bookmarks
function bookmarksite(title,url){
if (navigator.appName === "Microsoft Internet Explorer")

window.external.AddFavorite(url, title);

else

if(window.opera && window.print) { // Opera Browser

var elem = document.createElement('a');

elem.setAttribute('href',url);

elem.setAttribute('title',title);

elem.setAttribute('rel','sidebar');

elem.click();

}

else { // Other Browsers

window.sidebar.addPanel(title, url, "");

}

}

// Splash opacity management

 var splashNum = 0;

 function fadeSplash()
 {
  if (splashImgs[++splashNum] == null)
   splashNum = 0;

  splashBottom = document.getElementById('splash_bottom');
  splashBottom.src = splashImgs[splashNum];

  fadeOpacity.addRule('v2i', 1, 0, 80);
  fadeOpacity('splash_top', 'v2i');

  setTimeout('splashTop = document.getElementById(\'splash_top\'); splashTop.src = splashImgs[' + splashNum + ']; fadeOpacity.back(\'splash_top\');', 4000);
  setTimeout('fadeSplash()', 5500);
 } 