$(function() {
  window.__currentHref = document.location.href.replace(/^(.*)\#.*$/,"$1"); // cutting anchor
  setLists(); // red markers
  setAjaxLinks();
  ieFix();
  var leftMenu = new Menu();
});

var Menu = function() {
  this.duration = 300;
  this.animate = !($.browser.msie && $.browser.version <= 7);
  this.setup();
}
Menu.prototype = {
  setup: function() {
    var self = this;
    self.closeSubmenus(false);
    $(".left-menu li a").click(function() {
      self.rebuildPath(this);
      self.closeSubmenus(true);
      self.showSubmenu(this);
    });
  },
  closeSubmenus: function(delay) {
    var self = this;
    $(".left-menu ul.level-3, .left-menu ul.level-4").each(function() {
      if (!$(this.parentNode).is(".current")) {
        if (!self.animate || !delay) {
          $(this).hide();
        } else {
          $(this).slideUp(self.duration);
        }
      }
    });
  },
  showSubmenu: function(a) {
    var self = this;
    var $ul = $("> ul", a.parentNode.parentNode);
    if ($ul.get(0)) {
      resetHomeAlign();
      if (!self.animate) {
        $ul.show();
      } else {
        $ul.slideDown();
      }
    }
  },
  rebuildPath: function(a) {
    var self = this;
    var $a = $(a);
    $("#left-part .left-menu li.current").removeClass("current");
    $("#left-part .left-menu li.active").removeClass("active");
    $("#left-part .left-menu a.green").removeClass("green");
    $a.parents("li").each(function() {
      $(this).addClass("current");
      $("> p a", this).addClass("green");
    });
    $a.parent().parent().addClass("active")
  }
};

var setLists = function(container) {
  container = container || document.body;
  $(".text ul, .text ol", container).each(function() {
    var $ul = $(this);
    if (!$ul.is(".content-ul")) {
      $ul.addClass("content-ul")
         .find("li")
         .each(function() {
           $(this).html("<span class='li-item'>"+this.innerHTML+"</span>");
         });
     }
  });
}

var setAjaxLinks = function() {
  $("a[rel]").click(function() {
    var a = this;
    jQuery.ajax({url: "/ajax/",
            dataType: "xml",
            type: "POST",
            data: { href: a.href },
            success: function(xml) { if (a.href != window.__currentHref) parseAjaxResponse(xml, a); },
            error: function() { document.location.href = a.href; }});
    return false;
  })
}
var parseAjaxResponse = function(xml, a) {
  window.__currentHref = a.href;
  var content = xml.getElementsByTagName('content')[0];
  if (content) {
    var node = _textContent(xml.getElementsByTagName('node')[0]);
    var hash = "node"+node;
    document.location.href = document.location.href.replace(/^(.*)\#.*$/,"$1")+"#"+hash;
    var speed = ($.browser.msie && $.browser.version <= 6) ? 0 : 300;
    $("#content").fadeOut(speed, function() {
      $(this).empty().html(_textContent(content)).fadeIn(speed, function() { setLists($("#content")); resizeFix(); });
    });
  }
}

var alignHomeBlocks = function() {
  var $lBlock = $("#left-part .left-products").addClass("left-products-home");
  var $cBlock = $("#center-part .home-news");
  var delta = $lBlock.offset().top - $cBlock.offset().top;
  if (delta < 0) {
    $lBlock.css({marginTop: -delta});
  } else {
    $cBlock.css({marginTop: delta});
  }
}
var resetHomeAlign = function() {
  var $lBlock = $("#left-part .left-products");
  var $cBlock = $("#center-part .home-news");
  $lBlock.removeClass("left-products-home").animate({marginTop: 0});
  $cBlock.animate({marginTop: 0});
}

var ieFix = function() {
  if ($.browser.msie && $.browser.version <= 7) {
    $("#left-part .left-menu ul.level-4").hide();
    pngFix();
  }
}

var resizeFix = function() {
  // IE fix for dynamic size changing
  if ($.browser.msie && $.browser.version == 6) {
    $("#foot").css({bottom: 0}).removeAttr("style");
  }
}


var pngFix = function() {
  if ($.browser.msie && $.browser.version <= 6) {
    $(".png").each(function() {
      if (this.tagName == "IMG") {
        this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
                          this.src +
                          '", sizingMethod="crop")';
        this.src = '/i/e.gif';
      } else {
        var method = (this.currentStyle.backgroundRepeat == 'no-repeat') ? 'crop' : 'scale';
        this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
                            this.currentStyle.backgroundImage.replace(/.*?url\(\"?(.*?)\"?\).*$/,"$1") +
                            '", sizingMethod="' + method + '")';
        this.style.backgroundImage = 'none';
      }
    });
  }
}

var _textContent = function(xml) {
  if (xml.text) {
    return xml.text;
  } else if (xml.textContent) {
    return xml.textContent;
  } else {
    return xml.firstChild.nodeValue;
  }
}

