$(function() {
	$('.top-nav-search-input')
		.focusin(function() {
			if ($(this).val() == $(this).attr('alt')) {
				$(this).addClass('top-nav-search-input-focused').val('')
			}
		})
		.focusout(function() {
			if ($(this).val() == '') {
				$(this).removeClass('top-nav-search-input-focused').val($(this).attr('alt'))
			}
		});
	
	$('.top-menu-item > a')
		.mouseenter(function() {
			if (t) clearTimeout(t);
			var id = $(this).attr('id').split('menu')[1];
			menuObj = $('#submenu' + id);
			if (menuObj.length) {
				if (currentMenuObj && menuObj.attr('id') != currentMenuObj.attr('id')) {
					hideMenu(true)
				}
				currentMenuObj = menuObj.animate({top: '50px'}, {easing: 'easeInOutBack', duration: 800});
			}
		})
		.mouseleave(function() {
			if (currentMenuObj)
				t = setTimeout('hideMenu()', 300);
		})
		.each(function() {
			var id = $(this).attr('id').split('menu')[1];
			menuObj = $('#submenu' + id);
			if (menuObj.length)
				menuObj.css("margin-left", $(this).position().left - menuObj.position().left + "px").show();
		});

	$('.top-menu-submenu')
		.mouseenter(function() {
			if (t) clearTimeout(t);
		})
		.mouseleave(function() {
			t = setTimeout('hideMenu()', 300);
		})
		.each(function() {
			$(this).css("top", $(this).offset().top - $(this).height() + "px").css("z-index", "2");
			$(this).attr("top_init", $(this).css("top"));
		});

	$('.faq-item-question a').click(function() {
		$(this).parent().next().slideToggle(200);
		$(this).parent().toggleClass('faq-item-question-open');
		return false
	});

	$('.vacancy-item-title a').click(function() {
		$(this).parent().next().slideToggle(200);
		$(this).parent().toggleClass('vacancy-item-title-open');
		return false
	});

	$('.product-title a').click(function() {
		var product = $(this).parents('.product');
		var product_body = $('.product-body', product);
		
		var offset = product.position().left - product.parent().position().left;
		if (product.hasClass('product-open')) {
			product_body.animate({'height': 'toggle'}, 400, function() {
				product.removeClass('product-open');
				if (product.attr('swapped')) {
					product.toggleClass('product-even').next('.product').insertBefore(product);
					product.removeAttr('swapped')
				}
				product.nextAll('.product').toggleClass('product-even');
			});

		} else {
			if (offset > 0) {
				product.prev('.product').insertAfter(product);
				product.next().nextAll('.product').toggleClass('product-even')
				product.addClass('product-even').attr('swapped', '1');
			} else {
				product.nextAll('.product').toggleClass('product-even')
			}
			
			var product_title = $('.product-title', product);
			var padding_top = product_title.position().top - product_title.parent().position().top;
			product_title.parent().css('padding-top', padding_top + 'px').css('vertical-align', 'top')
			product.addClass('product-open');
			product_body.animate({'height': 'toggle'}, 400)
		}
		return false
	})
	
	alignVert();

	var h = window.location.hash;
	if (h) {
		$('.product-title a', $(h)).triggerHandler('click');
	}

})

var ie;
var ie_v;
if (/MSIE ([0-9]{1,}[\.0-9]{0,})/.test(navigator.userAgent)) {
	ie_v = new Number(RegExp.$1);
	ie = ie_v < 8;
}

var t = null;
var currentMenuObj = null;

function hideMenu(immediately) {
	if (currentMenuObj) currentMenuObj.animate({top: currentMenuObj.attr("top_init")}, {easing: 'easeInOutBack', duration: immediately?300:800});
	currentMenuObj = null;
	t = null
}

function alignVert() {
	if (ie) {
		$('.product-folder-rows .product-title').each(function() {
			$(this).parent().css('padding-top', $(this).parent().prev().height() / 2 - $(this).parent().height() / 2 + 15 + 'px')
		})
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent(function() {
	alignVert();
})
