jQuery(function(){
    jQuery('#lang').click(function() {
        jQuery('#lang_wrapper').slideToggle('slow');
    });
    jQuery('#top_menu2 .white').hide();
    jQuery('#top_menu2 td:first .white').show();
    jQuery('.open_block').click(function() {
        jQuery(this).next().next().toggle('slow');
    });

	// Для всех элементов второго уровня
	jQuery('#top_menu2 .top_menu2_podmenu').each(function(){
		var __podM = jQuery(this);
		var __lies = jQuery('td>ul>li', __podM);
		
		// Вычисляем общую высоту (для этого кратковременно отображаем блок, подсчитываем высоту, скрываем блок...)
		__podM.css({ display: 'block', visibility: 'hidden' });
		var __height = 0;
		var __liesHeight = [];
		var __cnt = 0;
		__lies.each(function(){
			var __lHeight = jQuery(this).height();
			__height += __lHeight;
			__liesHeight[__cnt++] = __lHeight;
		});
		__podM.css({ display: 'none', visibility: '' });
		
		// Если высота меньше, скажем 300 пикселей, меню не разбиваем...
		if (__height >= 300) {
			
			// Высчитываем "точку переноса"
			var __nHeight = 0;
			var __break = 0;
			for (var i = 0; i < __liesHeight.length; i++) {
				__nHeight += __liesHeight[i];
				if (__nHeight >= (__height / 2)) {
					__break = i;
					break;
				}
			}
			
			// Дублируем пункты меню и удаляем ненужные
			jQuery('td>ul', this).eq(0).parent().css('vertical-align', 'top').parent().append(jQuery('td>ul', this).eq(0).parent().clone());
			jQuery('td:eq(0)>ul>li:gt(' + __break + ')', this).remove();
			jQuery('td:eq(1)>ul>li:lt(' + (__break + 1) + ')', this).remove();
		}
					
	});/* end menu */
	
	jQuery('td.top_men_p:last .block_level2').each(function(){ jQuery(this).width(145); });

    var k = jQuery('.punkt').prev();
    var l = jQuery('.punkt').next();
    jQuery('.punkt').mouseover(function(){
        jQuery('.top_menu2_podmenu').hide();
        jQuery('#top_menu2 table:first td').removeClass('onmouseov1').removeClass('onmouseov2').removeClass('onmouseov3').removeClass('bg_rep');
        jQuery(this).addClass('onmouseov2');
        var __width_2 = jQuery(this)[0].clientWidth;
        var __container = jQuery(this).prev().addClass('onmouseov1');
        var __submenu = __container.find('.top_menu2_podmenu').eq(0);
        __submenu.show();
        var __width_bl = __submenu[0].clientWidth;
        var __pos = __container.position();
     	var __size = ___getPageSize();   	
        var __nxt = jQuery(this).next().addClass('onmouseov3');
        
        var __width = 21 + __width_2 + __nxt[0].clientWidth;
        var __right = parseInt((__size[2] - __pos.left), 10);
        
        if (__width_bl > __right) __submenu.css('left', (__width - __width_bl) + 'px');
        
        jQuery(this).next().next().addClass('bg_rep');
        jQuery('.onmouseov1 .top_menu2_podmenu').show();
    });
    jQuery('#top_menu2 .punkt:eq(0)').mouseover(function(){
        jQuery('#top_menu2 td:first .white').hide();
    });
    jQuery('#top_menu2 .f_t').mouseleave(function(){
        jQuery('#top_menu2 td:first .white').show();
        jQuery('.top_menu2_podmenu').hide();
        jQuery('#top_menu2 td').removeClass('onmouseov1').removeClass('onmouseov2').removeClass('onmouseov3').removeClass('bg_rep');
    });

    var k = jQuery('.top_men_p').prev();
    var l = jQuery('.top_men_p').next();
    var left = '-'+((jQuery('#top_menu .separator:first').width())/2+4.5);
    jQuery('.top_men_p').mouseover(function(){
        jQuery('#top_menu .top_menu2_podmenu:last').css('width','117px');
        jQuery('#top_menu td:eq(1) div').removeClass('gr_bl');
        jQuery('.top_menu2_podmenu').hide();
        jQuery('#top_menu table:first td').removeClass('onmouseov1').removeClass('onmouseov2').removeClass('onmouseov3').removeClass('bg_rep');
        jQuery(this).addClass('onmouseov2');
        jQuery(this).prev().addClass('onmouseov1');
        jQuery(this).next().addClass('onmouseov3');
        jQuery(this).next().next().addClass('bg_rep');
        jQuery('.onmouseov2 .top_menu2_podmenu').css('left',left+'px').show();
    });
    jQuery('#top_menu .top_men_p:eq(0)').mouseover(function(){
        jQuery('#top_menu td:first div').addClass('gr_bl');
    });
    jQuery('#top_menu').mouseleave(function(){
        jQuery('#top_menu td:first div').removeClass('gr_bl');
        jQuery('#top_menu .top_menu2_podmenu').hide();
        jQuery('#top_menu td').removeClass('onmouseov1').removeClass('onmouseov2').removeClass('onmouseov3').removeClass('bg_rep');
    });
});

function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};

