var hint_timer;
var hint_state;

var bmenu_timer;
var bmenu_object;
var bmenu_state_link;
var bmenu_state_div;

var menu_top_inc = 22;
var menu_left_inc = 10;
var menu_frame_width = 0;
var menu_header_fill_width = -65;
var menu_zindex_const = 10;

var header_index = 0;
var h2_array = Array ();
var h3_array = Array ();
var h4_array = Array ();
var h5_array = Array ();

var links_count;
var links_txt = Array();


var block_counter = 0;

$(function(){ // shorthand for $(document).ready() BTW
  hint_state = false;
  bmenu_state_link = false;
  bmenu_state_div = false;
  bmenu_object = true;
  
  $("#btn_hint_frame").css("display", "block");
  
  $("div.leftmenu_container").each(function() {
    $(this).attr("value", block_counter.toString());
    block_counter++;
    if (!/MSIE (5\.5|6|7|8).+Win/.test(navigator.userAgent))
    {
      $(this).show();
      tmp = $(this).css("width");
      menu_frame_width = parseInt(tmp.substr(0,  tmp.length - 2));
    }
  });
  menu_header_fill_width += menu_frame_width;
  
  if (menu_ord.length == 0)
    for (i=0; i<block_counter; i++)
//      menu_ord[i] = block_counter - i - 1;
      menu_ord[i] = i;
    
  $("#div_buttons_active div a img").hover(
    function() {
      var imgpos;
      var order;
      clearTimeout (hint_timer);
      $(this).parent().parent().fadeTo("fast", 1);
      order = $(this).parent().parent().attr("id");
      order = order.match(/menu_button_(.)/i);
      order = parseInt(order[1]) - 1;
      $(".hint_td_middlecenter").html($(this).attr("hint_info"));
      imgpos = $("#btn_hint_table table").width();
      imgpos = 10 + order * (imgpos - 30) / 5;
      $("#btn_hint_pointer img").css("left", imgpos.toString());
      imgpos = 587 + (39 * order) - imgpos;
      $("#btn_hint_frame").css("left", imgpos.toString());
      if (!hint_state)
        hint_timer = setTimeout(hint_show, 800);
      //$("#btn_hint_frame").fadeIn("slow");
    },                               
    function() {
      clearTimeout (hint_timer);
      $(this).parent().parent().fadeTo("fast", 0.01);
      if (hint_state)
        hint_timer = setTimeout(hint_hide, 500);
      //$("#btn_hint_frame").fadeOut("slow");
    }          
  );

  $("#footer_stat_button img").click(
    function() {
      $(this).fadeOut(10);
      if ($(this).attr("src") == "/i/design/counter_btn_opened.gif") {
        $(this).attr("src", "/i/design/counter_btn_closed.gif");
        $("#footer_counter_place").fadeOut("slow");
      } else {
        $(this).attr("src", "/i/design/counter_btn_opened.gif");
        $("#footer_counter_place").fadeIn("slow");
      }
      $(this).fadeIn("slow");      
    }        
  );
  
//  alert (navigator.userAgent);
  if (!/MSIE (5\.5|6|7|8).+Win/.test(navigator.userAgent))
  {
    tmp = menu_left_inc * menu_ord.length + menu_frame_width + 10;
    $("#loading").hide();
    $("img.left-menu-filler").show();
    $("img.left-menu-filler").css("width", tmp.toString() + "px");
    $("div.leftmenu_container").each(function() {
      tmp = parseInt($(this).attr("value"));
      ord_id = -1;
      while (menu_ord[++ord_id] != tmp);
      var_top = menu_top_inc * ord_id;
      var_left = menu_left_inc * ord_id;
      var_zindex = ord_id + menu_zindex_const;
      $(this).css("top", var_top.toString() + "px");
      $(this).css("left", var_left.toString() + "px");    
      $(this).css("z-index", var_zindex.toString());
      if (ord_id == menu_ord.length - 1) {
        $(this).find("img.leftmenu_titlefill").css("width", menu_header_fill_width.toString() + "px");
        $(this).find("div.leftmenu_marker>img").attr("src", "/i/design/leftmenu-marker-active.gif");
        tmp = $(this).find("div.leftmenu_content").css("height");
        tmp = parseInt(tmp.substr(0,  tmp.length - 2)) + menu_top_inc * menu_ord.length - 25;
        $("img.left-menu-filler").css("height", tmp.toString() + "px");
      } else {
        tmp = $(this).find("div.leftmenu_header>h2").css("width");
        tmp = parseInt(tmp.substr(0,  tmp.length - 2)) - 19;
        $(this).find("img.leftmenu_titlefill").css("width", tmp.toString() + "px");
        $(this).find("div.leftmenu_marker img").attr("src", "/i/design/leftmenu-marker-inactive.gif");
        $(this).find("div.leftmenu_content").hide();
      }
      $(this).find("div.leftmenu_selector>img").hover(
        function() {
          tmp = parseInt($(this).parent().parent().css("z-index")) - menu_zindex_const;
          if (tmp < menu_ord.length - 1) {
            $(this).parent().parent().find("img.leftmenu_titlefill").css("width", menu_header_fill_width.toString() + "px");
            $(this).parent().parent().find("div.leftmenu_header>h2").css("text-decoration", "underline");
            $(this).css("cursor", "hand");
          } else {
            $(this).css("cursor", "default");
          }
        },                               
        function() {
          tmp = parseInt($(this).parent().parent().css("z-index")) - menu_zindex_const;
          if (tmp < menu_ord.length - 1) {
            tmp = $(this).parent().parent().find("div.leftmenu_header>h2").css("width");
            tmp = parseInt(tmp.substr(0,  tmp.length - 2)) - 19;
            $(this).parent().parent().find("img.leftmenu_titlefill").css("width", tmp.toString() + "px");
            $(this).parent().parent().find("div.leftmenu_header>h2").css("text-decoration", "none");
            $(this).css("cursor", "default");
          }
        }          
      );
      $(this).find("div.leftmenu_selector>img").click(function() {
        tmp = parseInt($(this).parent().parent().css("z-index")) - menu_zindex_const;
        if (tmp < menu_ord.length - 1) {
          i = menu_ord[tmp];
          for (; tmp<menu_ord.length - 1; tmp++)
            menu_ord[tmp] = menu_ord[tmp+1];
          menu_ord[menu_ord.length - 1] = i;
          alter_menu ();
        }
      });
    });
    
    
    // обсчет всех заголовков внутри контента
    
    $("#main_content_frame").find(":header").each(function() {
      var domEl = $(this).get(0);
      var this_tag = domEl.tagName.toLowerCase();
      
      if (this_tag == "h6" || this_tag == "h1")
        return;

      var tmp_object = $(this).parent();
      domEl = tmp_object.get(0);

      while
      (tmp_object.attr("id") != "main_content_frame" &&
          !((domEl.tagName.toLowerCase() == "div") &&
              (tmp_object.hasClass("special") || tmp_object.hasClass("noindex"))
           )
      )
      {
        tmp_object = tmp_object.parent();
        domEl = tmp_object.get(0);
      }
      
      if (tmp_object.attr("id") != "main_content_frame")
        return;
        
      $(this).attr('name', 'header_' + header_index.toString());
      switch (this_tag) {
        case "h2":
          h2_array.push(header_index);
        break;
        case "h3":
          h3_array.push(header_index);
        break;
        case "h4":
          h4_array.push(header_index);
        break;
        case "h5":
          h5_array.push(header_index);
        break;
      }
      if (this_tag == "h2" || this_tag == "h3" || this_tag == "h4" || this_tag == "h5")
        $(this).before("<a name='header_" + header_index.toString() + "'></a>");
      header_index++;
    });
    
    var h_ar = new Array(4); // двумерный массив
    var h_ap = new Array(4); // итераторы по веткам массива
    
    h_ar[0] = h2_array;
    h_ar[1] = h3_array;
    h_ar[2] = h4_array;
    h_ar[3] = h5_array;
    h_ap[0] = -1;
    h_ap[1] = -1;
    h_ap[2] = -1;
    h_ap[3] = -1;
    
    var cur_index = 0;
    var S = "";
        
    while (cur_index < header_index) {
      for (i=0; i<4 && h_ar[i][h_ap[i]+1] != cur_index; i++);
      h_ap[i]++;
      if (i > 0) {
        ph_pos = i - 1;
        if (h_ap[ph_pos] >= 0) {
          for (i = ph_pos-1; i>=0; i--)
            if (h_ap[i] > h_ap[ph_pos])
              ph_pos = i;        
          $(":header[name='header_" + cur_index.toString() + "']").attr("parent","header_" + h_ar[ph_pos][h_ap[ph_pos]].toString());
        }
      }
      cur_index++;
    }
    
    for (cur_index = 0; cur_index < header_index; cur_index++) {
      this_obj = $(":header[name='header_" + cur_index.toString() + "']");
      var domElm = this_obj.get(0);
      this_tag = domElm.tagName.toLowerCase();
      parent = this_obj.attr("parent");
      if (parent!=undefined) {
        tmp = "<a class='inline go_top' href='" + window.location.pathname + "#" + parent + "'>&nbsp;&uarr;&nbsp;</a>";
        tmp = this_obj.html() + tmp;
        this_obj.html(tmp);
      }
      
      links_txt.length = 0;

      if (this_tag != "h5") {
        $(":header[parent='header_" + cur_index.toString() + "']").each(function () {
          name = $(this).attr('name');
          text = $(this).html();
          links_txt.push ("&rarr; <a class='inline' href='" + window.location.pathname + "#" + name + "'>" + text + "</a>");
        });
        if (links_txt.length > 0) {
          tmp = '';
          if (links_txt.length < 7) {
            for (i=0; i<links_txt.length; i++)
              tmp += "<p>" + links_txt[i] + "</p>";
            tmp = "<div class='sublinks'>" + tmp + "</div>";
          } else {
            cols = 3;
            rem = links_txt.length % cols;
            div = (links_txt.length - rem) / cols + 1;
            for (j=0; j<cols; j++) {
              tmp += "<td valign=top><div class='sublinks'>";
              for (i=j*div; (i<(j+1)*div) && (i<links_txt.length); i++)
                tmp += "<p>" + links_txt[i] + "</p>";
              tmp += "</div></td>";
            }
            tmp = "<table cellspacing=0 cellpadding=0 border=0><tr>" + tmp + "</tr></table>";
          }
          this_obj.after(tmp);
        }
      }
    }
    
    
  }
  
  
  
  $("#bottom_menu_frame").find("a.menu_pop").each(function() {
    $(this).click(function() {
      return false;
    });
    $(this).hover(
      function() {
//        if (bmenu_object != true && bmenu_object != "#" + $(this).attr("value"))
//          clearTimeout (bmenu_timer);
        bmenu_state_link = true;
        bmenu_object = "#" + $(this).attr("value");
        left = $(this).offset().left - 40;
        $(bmenu_object).show();
        $(bmenu_object).css("left", left + "px");        
      },                               
      function() {
        bmenu_state_link = false;
        bmenu_timer = setTimeout(bmenu_hide, 1);
      }          
    );
    
  });
  $(".bmenu_pop_place>span").each(function() {
    $(this).hover(
      function() {
        clearTimeout (bmenu_timer);
        bmenu_state_div = true;
      },                               
      function() {
        bmenu_state_div = false;
        bmenu_hide();
      }          
    );
    
  });
  
});

function alter_menu () {
  $("div.leftmenu_container").each(function() {
    tmp = parseInt($(this).attr("value"));
    ord_id = -1;
    while (menu_ord[++ord_id] != tmp);
    var_top = menu_top_inc * ord_id;
    var_left = menu_left_inc * ord_id;
    var_zindex = ord_id + menu_zindex_const;
    $(this).find("div.leftmenu_header>h2").css("text-decoration", "none");
    $(this).find("div.leftmenu_selector>img").css("cursor", "default");
    if (ord_id == menu_ord.length - 1) {
      $(this).find("img.leftmenu_titlefill").css("width", menu_header_fill_width.toString() + "px");
      $(this).find("div.leftmenu_marker>img").attr("src", "/i/design/leftmenu-marker-active.gif");
      $(this).find("div.leftmenu_content").show();
      tmp = $(this).find("div.leftmenu_content").css("height");
      tmp = parseInt(tmp.substr(0,  tmp.length - 2)) + menu_top_inc * menu_ord.length - 25;
      $("img.left-menu-filler").animate({ 
        height: tmp.toString() + "px"
      }, 100 );
    } else {
      tmp = $(this).find("div.leftmenu_header>h2").css("width");
      tmp = parseInt(tmp.substr(0,  tmp.length - 2)) - 19;
      $(this).find("img.leftmenu_titlefill").css("width", tmp.toString() + "px");
      $(this).find("div.leftmenu_marker img").attr("src", "/i/design/leftmenu-marker-inactive.gif");
      $(this).find("div.leftmenu_content").hide();
    }
    $(this).css("z-index", var_zindex.toString());
    $(this).animate({ 
        left: var_left.toString() + "px",
        top: var_top.toString() + "px"
      }, 400 );
  });
}

function hint_show () {
  if (!hint_state) {
    $("#btn_hint_frame").fadeTo("slow", 0.75);
    hint_state = true;
  }
}

function hint_hide () {
  if (hint_state) {
    $("#btn_hint_frame").fadeTo("slow", 0.01);
    hint_state = false;
  }
}

function bmenu_hide () {
  if (!(bmenu_state_div || bmenu_state_link))
    $(bmenu_object).hide();
  else
    clearTimeout (bmenu_timer);
}

