$(document).ready(function() {
  $(".pcb-pic a").click(function() {
    var $this = $(this);
    $this.addClass("active-").siblings().removeClass("active-");
    $this.parent().prev().find("img").attr({src: $this.attr("rel")});
    return false;
  });
  $(".rubricator .toggle- a").click(function() {
    this.blur();
    $(this).parent().parent().parent().toggleClass("opened-");
    return false;
  });
  $(".rubricator .main- a").each(function() {
    prepareCatalogueSwitcher($(this), $(".rubricator .sm-list"));
  });
  $("a.term-helper").each(function() {
    prepareTipPopup($(this));
  });
  $(".cat-frame").hover(function() {
    $(this).addClass("cat-frame-hover");
  }, function() {
    $(this).removeClass("cat-frame-hover");
  });
  $(".rotator").each(function() {
    prepareRotator($(this), true);
  });
  $(".ss-rotator").each(function() {
    prepareRotator($(this), false);
  });
  $(".services").each(function() {
    var $this = $(this);
    var $links = $this.find("a[rel=nav-link]");
    $this.data("active", $links.parent().filter(".active-").find("a[rel=nav-link]"));
    $links.data("parent", $this);
    $links.each(function() {
      var $this = $(this);
      var $block = $("a[name=" + $this.attr("href").substr(1) + "]").parent();
      $block.css({display: "none"});
      $block.data("parent", $this.data("parent"));
      $this.data("block", $block);
      $this.click(function() {
        this.blur();
        var $this = $(this);
        var $oldActive = $this.data("parent").data("active");
        if($this.get(0).innerHTML != $oldActive.get(0).innerHTML) {
          $oldActive.parent().removeClass("active-");
          $oldActive.data("block").stop().animate({opacity: 0}, "fast", function() {
            $oldActive.data("block").hide();
            $this.data("block").stop().css({opacity: 0}).show().animate({opacity: 1}, "fast");
          });
          $this.parent().addClass("active-");
          $this.data("parent").data("active", $this);
          var dataHeight = $this.data("block").parent().outerHeight();
          var navHeight = $this.parents(".s-nav").outerHeight();
          var newHeight = dataHeight > navHeight + 10 ? dataHeight : navHeight + 10;
          $this.data("parent").animate({height: newHeight}, "fast");
        }
        return false;
      });
    });
    $(window).resize(function() {
      $(".services").css({height: $(".s-content").height()});
    });
    $this.data("active").data("block").show();
    $this.css({height: $this.height()});
    $this.data("links", $links);
  });
  $(".s-how .sh-switcher").click(function() {
    $(this).parent().addClass("active-").siblings().removeClass("active-");
    return false;
  });

  $(".tip-input").tipInput();
  $(".auth.popup .p-close, .order-call.popup .p-close, .cart.popup .p-close").click(function() {
    $(this).parents("div.popup").hide();
    return false;
  });
  $(".auth-link").showPopup({popup: $(".auth.popup"), position: "bottom"});
  $(".header-links .request-call").showPopup({popup: $(".order-call.popup"), position: "bottom"});
  $(".footer-links .request-call").showPopup({popup: $(".order-call.popup"), position: "top"});
  $(".header-links .cart-link").showPopup({popup: $(".cart.popup"), position: "bottom"});
  $(".footer-links .cart-link").showPopup({popup: $(".cart.popup"), position: "top"});
  $(".team-list").teamManager();
});
