var top_menu_height = 16;
if (is_gecko) top_menu_height--;
var menu_mouse_over = 0;

function show_menu(id)
{
  var menu_div = _ge('popup_menu_'+id);
  var anchor_div = _ge('anchor_'+id);

  if (menu_div && anchor_div)
  {
    var x = getLeft(anchor_div);
    var y = getTop(anchor_div);

    menu_div.style.left = x;
    menu_div.style.top = y + top_menu_height;
    menu_div.style.display = 'block';
  }

  return false;
}

function hide_menu(id)
{
  var menu_div = _ge('popup_menu_'+id);
  var anchor_div = _ge('anchor_'+id);

  if (menu_div && anchor_div)
  {
    if (menu_mouse_over == id)
    {
      if (menu_div.style.display != 'block')
        menu_div.style.display = 'block';
    }
    else
    {
      menu_div.style.display = 'none';
    }
  }

  return false;
}

function menu_element_over(id, flag)
{
  return menu_div_over(id, flag);
}

function menu_div_over(id, flag)
{
  if (flag)
  {
    menu_mouse_over = id;

    show_menu(id);
  }
  else
  {
    if (menu_mouse_over == id)
    {
      menu_mouse_over = 0;

      hide_menu(id);
    }
  }

  return false;
}

function toggle_wait()
{
  return false;
}

function toggle_visibility(id)
{
  var e = _ge(id);

  if (e)
  {
    e.style.visibility = e.style.visibility != 'visible' ? 'visible' : 'hidden';
  }
}

function calendar_goto(y)
{
  window.location.href = '/'+cms_paths_ar['calendar']+'/'+y+'/';
  return false;
}

function toggle_feedback_form()
{
  var e = _ge('feedback_form_div');

  if (e) e.style.display = e.style.display != 'block' ? 'block' : 'none';

  if (feedback_message_div && e.style.display == 'block')
  {
    feedback_message_div.style.display = 'none';
  }
}

/* current song */

var cs_timer_id = 0;

function init_current_song()
{
  cs_timer_id = setTimeout("get_current_song()", 30000);
}

function get_current_song()
{
  if (cs_timer_id) clearTimeout(cs_timer_id);

  var url = '/xml/playlist/current_song.xml';
  diXMLRequest.get(url, get_current_song_response);
}

function get_current_song_response(xmlDoc)
{
  var rs;

  try {
    rs = xmlDoc.documentElement.getElementsByTagName('result');
  } catch(e) {}

  if (!rs || typeof str_cut_end == 'undefined') return;

  for (var i = 0; i < rs.length; i++)
  {
    var name = rs[i].getAttribute('name');

    switch (name)
    {
      case 'song':
        var cursong_artist = rs[i].getAttribute('artist');
        var cursong_title = rs[i].getAttribute('title');
        var e1 = _ge('current_song_artist_span');
        var e2 = _ge('current_song_title_span');

        if (e1 && e2)
        {
          discroller1.set_content(cursong_artist);
          discroller2.set_content(cursong_title);

          //e1.innerHTML = '<nobr>'+cursong_artist+'</nobr>';
          //e2.innerHTML = '<nobr>'+cursong_title+'</nobr>';
        }

        /*
        var cursong_full = rs[i].getAttribute('artist')+' - '+rs[i].getAttribute('title');
        var cursong = str_cut_end(cursong_full, cur_song_cut_len);
        var e = _ge('current_song_span');
        if (e)
        {
          e.innerHTML = '<nobr>'+cursong+'</nobr>';
        }
        var e2 = _ge('popup_div_current_song_title');
        if (e2)
        {
          e2.innerHTML = '<nobr>'+cursong_full+'</nobr>';
        }
        */
        break;

      case 'segment':
        var cursegment_full = rs[i].getAttribute('title');
        var cursegment = str_cut_end(cursegment_full, cur_segment_cut_len);
        var e = _ge('current_segment_span');
        if (e)
        {
          e.innerHTML = '<nobr>'+cursegment+'</nobr>';
        }
        var e2 = _ge('popup_div_current_segment_title');
        if (e2)
        {
          e2.innerHTML = '<nobr>'+cursegment_full+'</nobr>';
        }
        var e3 = _ge('current_segment_img');
        if (e3)
        {
          e3.src = rs[i].getAttribute('pic');
          e3.style.width = rs[i].getAttribute('pic_w');
          e3.style.width = rs[i].getAttribute('pic_h');
        }
        break;
    }
  }

  init_current_song();
}

function show_reply_form(before_obj_id, parent)
{
  var a = _ge(before_obj_id);

  if (!a || !comment_reply_div || !comment_parent) return false;

  a.parentNode.insertBefore(comment_reply_div, a);
  comment_reply_div.style.display = 'block';

  comment_parent.value = parent;

  var title_e = _ge('comment_title');
  if (title_e) title_e.value = '';

  var content_e = _ge('comment_content');
  if (content_e) content_e.value = '';

  var id_e = _ge('comment_id');
  if (id_e) id_e.value = '0';

  return false;
}

function project_filter_select_all_tags(on)
{
  for (var i = 0; i < tags_id_ar.length; i++)
  {
    var e = _ge('tag_'+tags_id_ar[i]);

    if (e) e.checked = on ? true : false;
  }
}

/* popups */

var selected_obj = null;
var selected_id = 0;
var div_prefix = 'popup_div_';

// íâ® ®âáâã¯ ®â ¢á¯«ë¢ îé¥£® ®ª­  ¤® ªãàá®à  ¬ëè¨
var div_offset_x = 10;
var div_offset_y = 10;
//

function engage(e)
{
  var event = e ? e : window.event;

  if (selected_obj) move_selected_obj(event);

  if (!is_ie4up && e.preventDefault) e.preventDefault();
  return true;
}

function move_selected_obj(event)
{
  selected_obj.style.display = 'block';

  var x = event.clientX + document.body.scrollLeft + div_offset_x;
  var y = event.clientY + document.body.scrollTop + div_offset_y;
  var w = selected_obj.offsetWidth;

  if (x + w > document.body.clientWidth - 10)
  {
    x = document.body.clientWidth - w - 10;
  }

  selected_obj.style.left = x+'px';
  selected_obj.style.top = y+'px';
}

function show_div(id)
{
  if (id == selected_id) return;

  var d = document.getElementById(div_prefix+id);

  if (d)
  {
    selected_obj = d;
    selected_id = id;
  }
}

function hide_div(id)
{
  if (id != selected_id) return;

  var d = document.getElementById(div_prefix+id);

  if (d)
  {
    selected_obj = null;
    selected_id = 0;
    d.style.display = 'none';
  }
}


function check_poll(choices_count)
{
  var prefix = arguments[1] || '';

  if (!poll_choices_ar) return false;

  for (var i = 0; i < poll_choices_ar.length; i++)
  {
    var e = _ge(prefix+'poll_choice_'+poll_choices_ar[i]);
    if (e && e.checked) return true;
  }

  alert('Âûáåðèòå âàðèàíò(û) îòâåòà, ÷òîáû ïðîãîëîñîâàòü!');
  return false;
}

function show_prev_polls()
{
  var e1 = _ge('show_prev_polls_div');
  var e2 = _ge('poll_rows_div');

  if (e1 && e2)
  {
    e1.style.display = 'none';
    e2.style.display = 'block';
  }

  return false;
}

function set_breakfast_date(dt)
{
  var e1 = _ge('breakfast_day');
  var e2 = _ge('breakfast_month');
  var e3 = _ge('breakfast_year');
  if (!e1) return;

  var dt2 = dt.split(/\./);
  e1.value = dt2[0];
  e2.value = dt2[1];
  e3.value = dt2[2];

  var e = _ge('bf_date['+dt+']');
  if (e) e.className = 'bf_date bf_selected';

  if (bf_selected_date)
  {
    var e2 = _ge('bf_date['+bf_selected_date+']');
    if (e2) e2.className = 'bf_date';
  }

  bf_selected_date = dt;
}

function show_reasons(date)
{
  var e = _ge('bf_reasons['+date+']');
  var e2 = _ge('bf_reasons_link['+date+']');

  if (e && e2)
  {
    e2.innerHTML = e.style.display != 'block' ? 'ñêðûòü' : 'ïîêàçàòü';
    e.style.display = e.style.display != 'block' ? 'block' : 'none';
  }
}

function show_reason_ending(id)
{
  var e = _ge('bf_reason['+id+']');
  var e2 = _ge('bf_re['+id+']');

  if (e && e2)
  {
    if (e.style.display != '')
    {
      e.style.display = '';
      e2.style.display = 'none';
    }
    else
    {
      e.style.display = 'none';
      e2.style.display = '';
    }
  }
}

function new_year_window()
{
  win=window.open('/newyear.php','newyear','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,width=370,height=405');
}

function show_photo_upload_details()
{
  var e = _ge('photo_upload_details');

  if (e)
  {
    e.style.display = 'block';
  }
}
