$(function() {
	
	$(navSelector + ' ul').each(function() {
		$('li:first', this).addClass('first');
		$('li:last', this).addClass('last');
	});
	
	$(navSelector + ' ul.level2').each(function() {
	
		var listLevel = $(this).attr('class').replace('level', '');
		var listWidth = $(this).width();
		var parentWidth = $(this).parents('li:first').find('img:first').width();
		var listPosition;
		
		switch (level2Postion) {
			case 'left':
				listPosition = 0;
				break;
			case 'center':
				listPosition = (parentWidth -  listWidth) / 2;
				break;
			case '':
				listPosition = $(this).css('left');
				break;
			default:
				listPosition = level2Postion;
				break;
		}
		
		$(this).css('left', listPosition);
	
		if (level2ActiveOn) {
			if ($(this).parents('li:first').find('>a').hasClass('active')) $(this).fadeIn(0);
		}
		
	});
	
	if (rolloverMenus) {
		
		$(navSelector + ' ul li').mouseenter(function() {
		
			if ($('a>img', this).length > 0) {
				var imagePathInfo = $('a:first>img', this).attr('src').replace('_over', '').split('.');
				var imagePath = imagePathInfo.slice(0, -1).join('.');
				var imageExtension = imagePathInfo[imagePathInfo.length - 1];
				
				$('a:first>img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
			}
				
			$('a:first', this).addClass('over');
			
			$(this).parent().find('li>ul').fadeOut(0);
			$('>ul', this).fadeIn(0);
			
		}).mouseleave(function() {
		
			if ($('a>img', this).length > 0) {
				var imagePathInfo = $('a:first>img', this).attr('src').replace('_over', '').split('.');
				var imagePath = imagePathInfo.slice(0, -1).join('.');
				var imageExtension = imagePathInfo[imagePathInfo.length - 1];
			
				if (!$('a', this).hasClass('active')) {
					$('a:first>img', this).attr('src', imagePath + '.' + imageExtension);
				}
				
			}
			
			$('>ul', this).fadeOut(0);
			$('a:first', this).removeClass('over');
			
			if (level2ActiveOn) $(this).parent().find('li>a.active').parent().find('>ul').fadeIn(0);
			
		});
		
	};
	
});

var navSelector = '#mainnav';
var rolloverMenus = true;
var level2Postion = '';
var level2ActiveOn = false;
