var obj = null;

function checkHover() {
	if (obj) {
		obj.find('ul').fadeOut('fast');	
	} //if
} //checkHover

$(document).ready(function() {
	$('#navmain > li').hover(function() {
		if (obj) {
			obj.find('ul').hide();
			obj = null;
		} //if

		
		$(this).find('ul').show();
		//if($(this).find('li').length > 3)
		//{
		//	$(this).removeClass('unique');
		//}
		
		var items = $(this).find('li');
		
		lenghtpx = 0;
		for (i = 0; i < items.length; i++)
			lenghtpx = lenghtpx + $(items[i]).width();
		
		if(lenghtpx + $(this).offset().left > $('#navmain').width() + $('#navmain').offset().left || $(this).attr('class')=='unique')
		{
			$(this).find('ul').css({'left':'auto', 'right':0});
			$(this).find('li').css({'float':'right'});
			$(this).find('li:first').removeClass('first').addClass('last');
			$(this).find('li:last').removeClass('last').addClass('first');
		}
	}, function() {
		obj = $(this);
		setTimeout(
			"checkHover()",
			150);
	});
});



