(function($) {
  $.fn.equalize = function(options) {
    var settings = {
      items: 'li',
      item__is: 'a, b'
    };
    if(options) {
      $.extend(settings, options);
    }
    return this.each(function() {
      var wrap = $(this),
        items = $(settings.items, wrap),
        items__is = $(settings.item__is, items),
        wrap_w = wrap.width(),
        sum_w = 0,
        sum_pad = 0,
        item_pad = 0;
      items__is.each(function() {
        sum_w += $(this).outerWidth(true);
      });
      sum_pad = wrap_w - sum_w;
      item_pad = sum_pad / items.size();
      items__is.each(function(i, el) {
        var item__i = $(this),
          w_percent = Math.round(100 * ((item__i.outerWidth(true) + item_pad) / wrap_w));
        items.eq(i).width(w_percent + '%');
      });
    });
  };
})(jQuery);

    	ResizeEvents.eventElement.bind(
		'x-initial-sizes x-text-resize x-window-resize x-window-width-resize x-window-height-resize',
		function (eventObj, emPixels, textHeight, windowWidth, windowHeight){
			// append results of resize event
            var a = $('#mainMenuBottom ul:eq(0) li:eq(0) a').width()+$('#mainMenuBottom ul:eq(0) li:eq(1) a').width()+$('#mainMenuBottom ul:eq(0) li:eq(2) a').width()+$('#mainMenuBottom ul:eq(0) li:eq(3) a').width()+$('#mainMenuBottom ul:eq(0) li:eq(4) a').width()+$('#mainMenuBottom ul:eq(0) li.active').width();
            var b = $('#search').outerWidth();

            var c = $('#mainMenuTop').width();

            var d = (c - b - a)/13;
            d = parseInt(d);
            $('#mainMenuBottom ul:eq(0) li a').css('padding-left',d);
            $('#mainMenuBottom ul:eq(0) li a').css('padding-right',d);
            $('#mainMenuBottom ul:eq(0) li.active').css('padding-left',d);
            $('#mainMenuBottom ul:eq(0) li.active').css('padding-right',d);
            $('#mainMenuBottom ul:eq(0) li.active a').css('padding-left',0);
            $('#mainMenuBottom ul:eq(0) li.active a').css('padding-right',0);

		}
	);
	ResizeEvents.initialise();

jQuery(function($) {
  if (!$('html').hasClass('js')) {
    $('html').addClass('js');
  }
  setInterval(function() {
	  var body = $('body'),
	    sensor = $('#sensor'),
	    width = sensor.width(),
	    bodyClass = '';
	  function switchBodyClass() {
	    if (width > 1580) {
	      bodyClass = 'large';
	    } else if (width > 1250) {
	      bodyClass = 'medium';
	    } else {
	      bodyClass = 'small';
	    }
	    body.removeClass('large medium small').addClass(bodyClass);
	    if ($('.g-columned').size()) {
	      $('.g-columned').uncolumnlists();
	      if (width > 1260) {
	        $('.g-columned').makeacolumnlists({cols:6,colWidth:'16.5%',equalHeight:'false',startN:1});
	      } else  {
	        $('.g-columned').makeacolumnlists({cols:4,colWidth:'24.5%',equalHeight:'false',startN:1});
	      }
	    }
	  }
	  switchBodyClass();
	  return function() {
	    if (sensor.width() === width) {return}
	    width = sensor.width();
	    switchBodyClass();
	  }
  }(), 100);

	$('.b-nav-main-top').equalize({items: '.b-nav-main-top__item'});

  /**
   * Add large layout made-in block
   */
  (function() {
    $('#footer .b-footer_i tr').append('<td class="b-footer__r"><div class="b-madeIn">сделано<br /><a class="b-madeIn__logo_text" href="http://www.moguchi.ru/" target="_blank"><img height="12" width="73" alt="Могучи" src="/images/Moguchi.png"><img class="b-madeIn__logo" src="/images/Moguchi-logo.png" width="47" height="47" alt="" ></a></div></td>');
  })();

    /**
     * Add markers to colored ordered list
     *
     * Used in profnastil
     */
    if ($('.b-vlist_order-colored').size() > 0) {
      var colors = ['f08300', 'cb3753', '1693cc', '5dc81a', 'a225d7'];
      (function() {
        $('.b-vlist_order-colored').each(function() {
          $('>li', this).each(function(i) {
            $(this).prepend('<i class="b-vlist_order-colored__marker" style="background-color: #' + colors[i] + '">' + (i+1) + '</i>');
          });
        });
      })();
    }

		/**
		 * Replace self-links to plain text in Profnastil
		 * !!! To be refactored !!!
		 */
		if ($('.b-profnastil-scope__table').size() > 0) {
			(function() {
        $('.b-profnastil-scope__table').find('a[href^="' + window.location.pathname + '"]').replaceWith(function() {
        	return this.innerHTML;
        });
			})();
		}

		/**
		 * Make columned list of the vacancy description
		 */
		if ($('.b-vacancies-list').size() > 0) {
			(function() {
				var descrs = $('.b-vacancy__descr__body'),
					labels = $('.b-vacancy__descr__label'),
					bodies = $('.b-vacancy__descr__body');
        $('ul', descrs).makeacolumnlists({cols:2,colWidth:'40%',equalHeight:'false',startN:1});
        labels.click(function() {
        	$(this).siblings('dt,dd').removeClass('b-vacancy__descr__label_sel b-vacancy__descr__body_sel').end().addClass('b-vacancy__descr__label_sel').next('.b-vacancy__descr__body').addClass('b-vacancy__descr__body_sel');
        });
        })();
		}

		if ($('.b-content-price').size() > 0) {
			(function() {
				$('.b-content-price').each(function() {
					var table = $(this);  
          table.wrap('<div class="b-content-price-wrap"/>');
          $('td[colspan]', table).each(function() {
          	var td = $(this);
						if (td.innerWidth() == td.parent().innerWidth()) {
							td.addClass('b-content-price-section-title');
						}
          });
				});
				/*
	      $('.b-content-price').each(function() {
					var j = 1,
						rowspanBuffer = 0;
					$('.b-content-price td:first-child').each(function(i) {
						var td = $(this);
						if (rowspanBuffer > 0) {
							if (j % 2 == 0) {
								td.parent().addClass('even');
							}
							if (--rowspanBuffer == 0) {
								j++;
							}
							return true;
						}
						if (td.attr('rowspan')) {
				      rowspanBuffer = td.attr('rowspan') - 1;
							if (j % 2 == 0) {
								td.parent().addClass('even');
							}
							return true;
						}
						if (j % 2 == 0) {
							td.parent().addClass('even');
						}
						j++;
					});
	      });
				*/
			})();
		}

		if ($('.b-all-prods').size()) {
			(function() {
				var wrap = $('.b-all-prods'),
					button = $('.b-all-prods-button', wrap),
					popup = $('.b-all-prods-popup', wrap),
					close = $('.b-all-prods-popup-close', wrap);
				if ($('.b-section-nav').size() || $('.b-quicknav').size()) {
        	wrap.addClass('b-all-prods_before_nav');
				}
				function popupOpen() {
        	button.addClass('b-all-prods-button_pushed');
        	var buttonOffset = button.offset();
        	popup.css({
        		left: buttonOffset.left - wrap.offset().left + 'px'
        	});
					popup.slideDown(400, function() {
             popup.data('closed', false);
					});
				}
				function popupClose() {
					popup.slideUp(200, function() {
						popup.data('closed', true);
					});
          button.removeClass('b-all-prods-button_pushed');
				}
        popup.data('closed', true);
				button.hover(function() {
					if (!button.hasClass('b-all-prods-button_pushed')) {
             button.addClass('b-all-prods-button_hover');
					}
				}, function() {
					button.removeClass('b-all-prods-button_hover');
				});
        button.click(function() {
        	if (popup.data('closed')) {
          	popupOpen();
        	} else {
        		popupClose();
        	}
        });
				close.click(function() {
					popupClose();
				});
			})();
		}

		if ($('.b-metalloprokat-nav').size()) {
			(function() {
				$('.b-metalloprokat-nav').find('a').hover(function() {
					$(this).closest('td').addClass('b-metalloprokat-nav__item_hover');
				}, function() {
					$(this).closest('td').removeClass('b-metalloprokat-nav__item_hover');
				});
			})();
		}

		$('.b-nav-slider').each(function() {
			var slider = $(this),
				imgs = $('.b-nav-slider__img', slider);
			$('.b-nav-slider__link', slider).each(function() {
				$('<span class="b-nav-slider__text-sh" />').text($('.b-nav-slider__text', this).text()).insertBefore($('.b-nav-slider__text', this));
			});
	    slider.on('hover', '.b-nav-slider__link', function(evt) {
	    	var cur_img = $('.b-nav-slider__img', this);
				switch (evt.type) {
					case 'mouseenter':
						imgs.not(cur_img).stop().animate({
							height: 0
						}, {
							easing: 'easeOutQuad'
						});
			      cur_img.animate({
			      	height: '69px'
			      }, {
							easing: 'easeOutQuad',
			      	queue: false
			      });
						break;
					case 'mouseleave':
						imgs.stop().animate({
							height: 0
						}, {
							easing: 'easeOutQuad'
						});
						break;
				}
			});
		});




});
