function getId(id)
{
  var tmp = document.getElementById(id);
  return tmp;
}

function downloadPage(url, param, id, afterResponse, func)
{
  var	message = '<table style="width:98%; height:98%;" align="center">'+
  '<tr>'+
  '<td style="text-align:center;">'+
  '<img src="images/loading.gif" alt="Загрузка" />'+
  '</td>'+
  '</tr>'+
  '</table>';
  if(getId(id) || func)
  {
    if (getId(id)) {
      getId(id).innerHTML = message;
    }
    if (window.XMLHttpRequest)     req = new XMLHttpRequest();
    else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req)
    {
      req.onreadystatechange = function()
      {
        if (req.readyState == 4)
        {
          if (req.status == 200)
          {
            if (func)
              eval(func + '(\'' + req.responseText + '\');');
            else
              getId(id).innerHTML = req.responseText; //unescape(req.responseText);
            if(afterResponse!=undefined) eval(afterResponse);
            refreshScreen();
          }
          else
            alert (req.status);
        }
      }
      //alert(param);
      req.open('POST', url + ( (navigator.userAgent.indexOf('Opera 8')>=0 || navigator.userAgent.indexOf('Opera/8')>=0) ? '?'+param : ''), true);
      try
      {
        req.setRequestHeader('Accept-Charset', 'windows-1251');
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=windows-1251');
      }
      catch (er){}
      req.send(param);
    }
  }
  else alert('Ошибка. Перезагрузите страницу.');
}

//===========================================================================
function escapeRus(str)
{ // ??нициализируем таблицу перевод
  var trans = [];
  for(var i=0x410; i<=0x44F; i++)
  {
    trans[i] = i - 0x350; // А-Яа-я
  }
  trans[0x401] = 0xA8;    // Ё
  trans[0x451] = 0xB8;    // ё

  var ret = [];
  // Составляем массив кодов символов, попутно переводим кириллицу
  for (var i=0; i<str.length; i++)
  {
    var n = str.charCodeAt(i);//2B
    if ((typeof trans[n] != 'undefined'))
    {
      n = trans[n];
    }
    if (n <= 0xFF)
    {
      ret[i] = escape(String.fromCharCode(n));
    }
    if (n==8470)
    {
      ret[i] = '%B9';
    }
    if (n==43)
    {
      ret[i] = '%2B';
    }
  }
  return ret.join('');
}


function showDiv (div)
{
  if (getId(div))
  {
    if (getId(div).style.left.replace('px', '') < 10)
    {
      getId(div).style.left = Number (getId(div).style.left.replace('px', '')) + 20 +'px';
      setTimeout("showDiv('"+div+"')",2);
    }
  }
}

function hideDiv (div, left)
{
  if (getId(div))
  {
    if (getId(div).style.left.replace('px', '') > 40 - Number (getId(div).style.width.replace('px', '')))
    {
      getId(div).style.left = Number (getId(div).style.left.replace('px', '')) - 20 +'px';
      setTimeout("hideDiv('"+div+"','"+left+"')",2);
    }
  }
}


function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function showContent(show)
{
  if (getId('blockDiv'))
  {
    $('#contentDiv').css({
      'height':$('#mainDiv')[0].offsetHeight-100+'px'
    });
    getId('blockDiv').style.display='block';
    getId('contentDiv').style.display='block';
    downloadPage('index.php','ajax='+show,'contentDiv', 'getId(\'contentDiv2\').style.height=getClientHeight()-112-(getId(\'menuTable'+show+'\')?18:0)+\'px\'');
  }
}

function lightMenuOver(menuId,x)
{
  if (getId('menu_'+menuId+'_center_'+x).className != 'activeMenu')
  {
    getId('menu_'+menuId+'_center_'+x).className = 'overMenu';
    getId('menu_'+menuId+'_left_'+x).style.backgroundImage = 'url(images/news_left_red.gif)';
    getId('menu_'+menuId+'_right_'+x).style.backgroundImage = 'url(images/news_right_red.gif)';
  }
}

function lightMenuOut(menuId,x)
{
  if (getId('menu_'+menuId+'_center_'+x).className != 'activeMenu')
  {
    getId('menu_'+menuId+'_center_'+x).className = 'outMenu';
    getId('menu_'+menuId+'_left_'+x).style.backgroundImage = 'url(images/news_left_white.gif)';
    getId('menu_'+menuId+'_right_'+x).style.backgroundImage = 'url(images/news_right_white.gif)';
  }
}
function lightMenuClick(menuId,id)
{
  x=0;
  while(getId('menu_'+menuId+'_center_'+x))
  {
    if (x!=id)
    {
      getId('menu_'+menuId+'_center_'+x).className = 'outMenu';
      getId('menu_'+menuId+'_left_'+x).style.backgroundImage = 'url(images/news_left_white.gif)';
      getId('menu_'+menuId+'_right_'+x).style.backgroundImage = 'url(images/news_right_white.gif)';
    }
    else
    {
      getId('menu_'+menuId+'_center_'+x).className = 'activeMenu';
      getId('menu_'+menuId+'_left_'+x).style.backgroundImage = 'url(images/news_left_red.gif)';
      getId('menu_'+menuId+'_right_'+x).style.backgroundImage = 'url(images/news_right_red.gif)';
    }
    x++;
  }
}

function showGuest(page)
{
  downloadPage('index.php','ajax=right_guest&page='+page,'rightDiv');
}

function showNews(page,type,tag)
{
  downloadPage('index.php','ajax=news_content&news_page='+page+'&ext2_news='+type+'&tag='+escapeRus(tag),'newsDiv');
}

function autorizateUser(nick, pass)
{
  if (nick=='')
    alert ('Введите ник');
  else if (pass=='')
    alert ('Введите пароль');
  else
    getId('authForm').submit();
//downloadPage('index.php','ajax=right_account&mode=logon&nick='+escapeRus(nick)+'&pass='+pass,'rightDiv');
}

function gimn_vote(id_song) {
  //alert (id_song);
  if ($.cookie('voteSong')) {
    downloadPage('index.php','ajax=air_gate&func=gimn_vote&p[0]='+id_song+'&p[1]=1','hiddenVoteDiv',null,'parseGimnVoteResult');
  }
  else {
    $.cookie('voteSong', '234512412412514512451234', {
      expires: 365
    });
    downloadPage('index.php','ajax=air_gate&func=gimn_vote&p[0]='+id_song,'hiddenVoteDiv',null,'parseGimnVoteResult');
  }
//downloadPage('index.php','ajax=air_gate&func=airGetTable','hiddenVoteDiv',null,'parseGimnVoteResult');
//	$.cookie('c_guestUserName', document.getElementById('autor').value, {expires: 365});
}

function parseGimnVoteResult (result) {
  if (jQuery.browser.msie) {
    data = new ActiveXObject( 'Microsoft.XMLDOM');
    data.async = false;
    data.loadXML(result);
  }
  else data = result;
  
  if ($('error123', data).text()) {
    alert ($('error123', data).text());
  }
  if ($('results',data)[0]) {
    var allVotes = $('allVoteCount', data).text();
    $('.songTd').text('0');
    $('results result', data).each(function(){
      drowVoteResult ($(this).attr('id_song'), $(this).text(), allVotes);
    });
  }
}

function drowVoteResult(id, colvo, allVotes) {
  var percWidth;
  percWidth = Math.round(colvo/allVotes*100);
  $('#songTd'+id).html('<table style="height: 41px; width: 500px;"><tr><td id="tdVoteResult'+id+'" style="text-align: center; width: '+percWidth+'%; background-color: #bf0000;">'+colvo+'</td><td></td></table>');
//  alert (id);
//  alert (colvo);
}
