$(function() {
	
	/* menu */
	foMenu.init();
	
	 $(".lang .lang-arrow").toggle(
      function () {
        $('#sub-lang').css({display: 'block'});
		$(this).addClass("l-a-close");
      },
      function () {
        $('#sub-lang').css({display: 'none'});
		$(this).removeClass("l-a-close");
      }
    );
	
	/* fix line for ie */
	if ($.browser.msie) $('.v-line table').css({height: $('.v-line').height() - 140});
	 
});

/* main menu */
var foMenu = {
	selected: null,	
	timer: null,
	
	init: function() {
	
		/* */
		var w = $('#top-menu .horisontal').width();
		$('#top-menu .r').width(w - 17 + 'px');
		$('#top-menu .horisontal').css({position: 'absolute', width: w + 'px'});
		
		/* create absolute sub */
		$('#top-menu li.parent').each(function(i) {
			$(this).attr('id', 'menu_' + i);
			
			var sub = $(this).find('.sub');
			
			if (sub.length) {
				$('body').append('<div id="menu_sub_' + i + '" class="sub">' + sub.html() + '</div>');
				
				var tmp = $(this).find('a span span');
				
				$('#menu_sub_' + i)
					.css({
						 left: tmp.offset().left + 9 + 'px',
						 top: tmp.offset().top + (tmp.height()/2) + 5 + 'px'
					})
					.hover(
						function() {
							foMenu.clear_timer();
						},
						function() {
							foMenu.delay_hide($(this).attr('id').split('_')[2]);
					});
				
				sub.remove();
			}
		});
		
		/* menu hover */
		$('#top-menu li.parent').hover(
			function() {
				foMenu.show($(this).attr('id').split('_')[1]);
			},
			function() {
				foMenu.delay_hide($(this).attr('id').split('_')[1]);
			});

	},
	
	show: function(id) {
		
		if (this.selected == id) {
			this.clear_timer();
			return;
		}
		
		if (this.selected != id) {
			this.hide(this.selected);
		}
		
		$('#menu_' + id).addClass('sel');
		$('#menu_sub_' + id).show();
		
		this.selected = id;
	},
	
	delay_hide: function(id){
		this.timer = setTimeout("foMenu.hide(" + id + ")", 500);
	},
	
	hide: function(id) {
		if (!$('#menu_' + id).is('.now')) $('#menu_' + id).removeClass('sel');
		$('#menu_sub_' + id).hide();
		this.selected = null;
		this.clear_timer();
	},
	
	clear_timer: function() {
		clearTimeout(this.timer);
		this.timer = null;
	}
	
};
