$(document).ready(function()
{
    var timer;

    var hideMenu = function (){
        timer = setTimeout(function(){
            $(".sub_menu").hide();
            $(".link_h.active:not('.current')").removeClass("active");
        }, 300);
    }

    $(".b-menu>.link_h").hover( function(e)
	{
        clearTimeout(timer);

        $(".sub_menu").hide();
        $(".link_h.active:not(.current)").removeClass("active");

        $(this).addClass('active');

          //теперь нельзя привязываться к .active, т.к. есть еще .active.current
		var activeElement = e.target.parentNode.parentNode;

		if ($(this).attr("rel") > 3)
		{
			$('.sub_menu').css({ 'margin-left': '0px' });
			$('.sub_menu', activeElement).addClass('rev');

			var wM = $(activeElement).width();
			var wSm = $('.sub_menu', activeElement).width() + 2;

//			alert( wM + " :: " + wSm );
			// 104 362
			// IE 7 104 101

			if ( navigator.userAgent.indexOf('MSIE 7') >= 0 )
				if ( $(this).attr("rel") == 4 )
					{ wSm += 365; }
				else if ( $(this).attr("rel") == 5 )
					{ wSm += 217; }

			var x = wM - wSm;
			$('.sub_menu', activeElement).css({ 'margin-left': x });				
		}


        if (activeElement == $(".active.current").get(0))
			$(".active>.sub_menu").show();
        else {
		     $(".current>.sub_menu").hide();
			 $(".active:not(.current)>.sub_menu").show();
		}
    }, hideMenu);

//	$("#once_view").mouseover(function() { alert('over'); });

	$(".link_h.once_rev").ready(function ()
	{
		$('.sub_menu').css({ 'margin-left': '0px' });
		$('.once_rev .sub_menu').addClass('rev');

		var wM = $('.link_h.once_rev').width();
		var wSm = $('.once_rev .sub_menu').width() + 2;

//		alert(wM + " :: " + wSm);
		// 104 362  myWindows
		// 111 407	myLinux
		// 94 341  natWindows
		
//		alert(wM + " :: " + wSm + "\n" + navigator.userAgent);

// Наташа, мало вправо
// Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)
// Мой ок
// Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
// Линь, много влево
// Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3

		if ( navigator.userAgent.indexOf('Firefox/3.') >= 0 )
		{
			if ( navigator.userAgent.indexOf('Linux') >= 0 )
				{ wM -= 7; wSm -= 45; }
			else if ( navigator.userAgent.indexOf('.NET CLR') >= 0 )
				{ wM += 10; wSm += 21; }
		}
		else if ( navigator.userAgent.indexOf('MSIE 7') >= 0 )
			{ wSm += 104; }

		var x = wM - wSm;
		$('.once_rev .sub_menu').css({ 'margin-left': x });
		
    });

    $('.sub-menu a').hover( function(){
        clearTimeout(timer);
    }, hideMenu);
/*
	$("#once_view").ready(function ()
	{
		var evt = document.createEvent("MouseEvents");
		evt.initEvent("mouseover", true, true);

		document.getElementById("once_view").dispatchEvent(evt);
	});
*/
});