


// ---- 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=95% 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];

   wordend = '';   
   switch (newsCache[date])
   {
    case 1:
    	wordend = 'ü';
    	break;
    case 2:
    case 3:
    case 4:
    	wordend = 'è';
    	break;
    default:
    	wordend = 'åé';
    	break;
   }
   
   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) ?
       ('<A HREF="'+ section + date + '/" TITLE="' + newsCache[date] + ' íîâîñò' + wordend + '">') : '') +
      calendar[i][j] +
      ((newsCache[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();
}


// ---- Menu

function getOnPageLeft(obj)
{
 var left = 0;
 
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   left += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else
  if (obj.x)
   left += obj.x;
 
 return left;
}

function getOnPageTop(obj)
{
 var top = 0;
 
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   top += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else
  if (obj.y)
   top += obj.y;
 
 return top;
}

var submenuTimeout = null;
var hilightedLink = null;

function showSubmenu(submenuId, parentEl)
{
 if (submenuId == null || submenuId == '')
  return false;

 self.clearTimeout(submenuTimeout);

 if (hilightedLink != null)
  hilightedLink.className = 'MENU';

 hilightedLink = parentEl;
 hilightedLink.className = 'CURRENT_MENU';

 with (self.document.all)
 {
  submenu.style.visibility = redline.style.visibility = 'hidden';

  if (submenuCache[submenuId] != null)
  {
   var out = '';
   var numsubmenu = submenuCache[submenuId].length;
   
   submenu.innerHTML = '';
  
   for (i = 0; i < numsubmenu; ++i)
   {
	out +=
	 '<TR><TD STYLE="padding-left:12px; padding-top:7px; padding-right:12px; padding-bottom:8px;">' +
	 '<A HREF="' + submenuCache[submenuId][i]["link"] + '" CLASS=' +
	  ((submenuCache[submenuId][i]["id"] == activeSubmenu) ? "CURRENT_" : "") + 'SUBMENU' +
     ' onMouseOver="clearTimeout(submenuTimeout); self.status = \'\'; return true;"' +
     ' onMouseOut="hideSubmenu(); self.status = \'\'; return true;">' +
     submenuCache[submenuId][i]["name"] + '</A></TD></TR>\n';
    
    if (i < (numsubmenu - 1))
     out +=
	  '<TR><TD STYLE="background:#DE0223 url(\'/img/hr_submenu.gif\') repeat-x;">' +
      '<IMG SRC="/img/empty.gif" WIDTH=133 HEIGHT=1 BORDER=0 ALT=""><BR>' +
      '</TD></TR>\n';
   }

   if (out != '')
   {
	submenu.innerHTML = '<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>' + out + '</TABLE>';

    submenu.style.left = getOnPageLeft(parentEl) - ((submenu.offsetWidth - parentEl.offsetWidth) / 2);
    submenu.style.top = getOnPageTop(parentEl) + ((document.all) ?
     ((navigator.appName == 'Opera') ? 23 : 21) : 20);

    redline.style.width = submenu.offsetWidth - 40;
	redline.style.left = submenu.offsetLeft + 20;
	redline.style.top = getOnPageTop(parentEl) + ((document.all) ?
	 ((navigator.appName == 'Opera') ? 23 : 21) : 20);

    if (redline.offsetWidth >= submenu.offsetWidth)
    {
     redline.style.width = submenu.offsetWidth - 20;
	 redline.style.left = getOnPageLeft(parentEl) + 10;
    }
    
    submenu.style.visibility = redline.style.visibility = 'visible';
   }
  }
 }
}

function hideSubmenu()
{
 self.clearTimeout(submenuTimeout);

 submenuTimeout =
  self.setTimeout('self.document.all.submenu.style.visibility = self.document.all.redline.style.visibility = \'hidden\'; if (hilightedLink != null) hilightedLink.className = \'MENU\';', 2000);
}

function blinkTopmenu()
{
 setTimeout('document.all.topmenu.style.visibility = \'hidden\';', 500);
 setTimeout('document.all.topmenu.style.visibility = \'visible\';', 800);
 setTimeout('document.all.topmenu.style.visibility = \'hidden\';', 1200);
 setTimeout('document.all.topmenu.style.visibility = \'visible\';', 1400);
}

