var HIDE_MENU = false;
var HIDE_3RD_LIV = true;
var MOUSE_PRESSED = false;
	
$(document).ready(function(){
	$('#navigation li.deactive').mouseover(function() { showMenu(this); });
	$('html').click(function() { hideAllMenu(); });
	$('html').mouseover(function() { setTimeout("testAndHidemenu()",200); });	
	$('ul#navigation li a').mouseover(function() { 
		HIDE_MENU = false;
		$('object').hide();
		$('embed').hide();
	});
	$('ul#navigation li a').mousedown(function() { 
		MOUSE_PRESSED = true; 
	});
	$('ul#navigation li a').mouseout(function() { 
		HIDE_MENU = true; 
	});
	$('ul.trd').hide();
	
	$('ul.trd').prev().mouseover(function() {
		if(!$(this).attr('moved')) {
			$(this).next().show();
			var h = $(this).next().height(); 
			$(this).parent().nextAll().each(function(index) {
				var top = parseInt($(this).css('top'));
				$(this).css({'top': (h+top)+'px'});
			});
			$(this).attr('moved',"yes");
			$(this).addClass('moved');
		}
		
	});	
	calculateOffset();
});

function testAndHidemenu() {
	if(HIDE_MENU) {
		hideAllMenu();
	}
}

function close3rdLiv() {
	if(HIDE_3RD_LIV) {
		$('.moved').each(function(index) {
			$(this).next().hide();
			$(this).removeAttr('moved');
			$(this).removeClass('moved');
			var h = $(this).next().height(); 
			$(this).parent().nextAll().each(function(index) {
				var top = parseInt($(this).css('top'));
				if(top>0) $(this).css('top',(top-h)+'px');
			});
		});
	}
}

function calculateOffset() {
	$('ul.trd').css({'position' : 'relative', 'right' : '-4px', 'top' : '-9px'});
	
	var elem = $('#navigation li.last_elem');
	this_left = this_width = 0;
	for(i=0;i<elem.length;i++) {
		var id = $(elem[i]).attr('id');
		var container_left = $(elem[i]).offset().left;
		var container_width = $(elem[i]).width();
		if ($('#'+id+' ul li:first').lenght > 0)
		{
			var this_left = $('#'+id+' ul li:first').offset().left;
			var this_width = $('#'+id+' ul li:first').width();
		}
		else
		{
			this_left = $('#'+id).offset().left;
			var this_width = $('#'+id).width();
		}
		
		var offsetting = parseInt(- (( this_left+this_width ) - ( container_left+container_width ) -2)); 
		$('#'+id+' ul li').css({'position' : 'relative', 'left' : offsetting});
		$('#'+id+' ul.trd').css({'position' : 'relative', 'right' : (offsetting-4)+'px', 'top' : '-9px'});
	}
}

function showMenu(elem,is_selected)
{
	$('.active ul').attr('class','hidden-menu');
	$('.active').addClass('deactive');
	$('.active').removeClass('active');
	
	$(elem).removeClass('deactive');
	$(elem).addClass('active');
		
	$('.deactive').unbind('mouseover');
	$('.deactive').mouseover(function() {
		showMenu(this,is_selected);			
	});		
	var id = $(elem).attr('id');
	$('#'+id+' ul').attr('class','shown-menu');
}

function hideAllMenu()
{
	if(!MOUSE_PRESSED) {
		$('.active ul').attr('class','hidden-menu');
		$('.active').addClass('deactive');
		$('.active').removeClass('active');
	
		$('.deactive').unbind('mouseover');
		$('.deactive').mouseover(function() {
			showMenu(this,false);		
		});
	
		close3rdLiv();
		HIDE_MENU = false;
		$('object').show();
		$('embed').show();
	}
}






