var activeNav2,
	activeNav3,
	nav2Open = false,
	nav3Open = false,
	hideNavTimeout,
	nav2DelayHide = 1000,
	imageRotatorTimeout,
	imageRotatorDelay = 3000,
	imageRotatorAnimationTime = 500;

$('#nav1').find('a')
	.live('mouseenter', function() {
		//check to see if the second nav bar should be shown
		var bOpenNav2 = $(this).hasClass('openNav2');
		if(bOpenNav2) {
			if(nav3Open) {
				closeNav3();
			} else {
				openNav2();
			}
		} else {
			if(nav3Open) {
				closeNav2and3();
			} else {
				closeNav2();
			}
		}
		
		//hide the old active second nav
		$('#nav1 .active').removeClass('active');
		$('.nav2.active').removeClass('active');
		
		//show the new active second nav
		var activeId = $(this).attr('id').substring(5);
		activeNav2 = $("#nav2_"+activeId);
		activeNav2.addClass('active');
		$(this).addClass('active')
	})
	.live('mouseleave', function() {
		if(!nav2Open) {
			$('#navWrapper').find('.active').removeClass('active')
		}
	})
	
$('.nav2').find('a').live('mouseenter', function() {
	var bOpenNav3 = $(this).hasClass('openNav3');
	if(bOpenNav3) {
		openNav3();
		
		$('.nav2 a.active').removeClass('active');
		$('.nav3.active').removeClass('active');
			
		//show the new active second nav
		var activeId = $(this).attr('id').substring(5);
		activeNav3 = $("#nav3_"+activeId);
		activeNav3.addClass('active');
		activeNav3.find('.imageSwitcher').find('.active').removeClass('active')
		activeNav3.find('.imageSwitcher').find('.default').addClass('active')
		$(this).addClass('active')
	} else {
		if(nav3Open) {
			closeNav3();
		}
		
		$('.nav2 a.active').removeClass('active');
		$(this).addClass('active')
	}
})

$('.nav3').find('.changeImage').live('hover', function() {
	var imagePath = $(this).attr('rel'),
		imageSwitcher = $(this).parent().parent().parent().parent().find('.imageSwitcher');
	imageSwitcher.find('.active').removeClass('active');
	imageSwitcher.find('.switchedImg').attr('src', imagePath).addClass('active');
})
	
$('#navWrapper')
	.live('mouseenter', function() {
		clearTimeout(hideNavTimeout)
	})
	.live('mouseleave', function() {
		if(nav2Open) {
			if(nav3Open) {
				closeNav3();
				hideNavTimeout = setTimeout("closeNav2()", nav2DelayHide);
			} else {
				hideNavTimeout = setTimeout("closeNav2()", nav2DelayHide);
			}
		}
	})

function openNav2() {
	nav2Open = true;
	$('#navWrapper').css('height', '84px');
	$('#nav2Wrapper').css('height', '46px');
	$('#nav3Wrapper').css('height', '56px');
}

function closeNav2() {
	nav2Open = false;
	$('#navWrapper').css('height', '48px');
	$('#nav2Wrapper').css('height', '10px');
	$('#nav3Wrapper').css('height', '20px');
	$('#navWrapper').find('.active').removeClass('active')
}

function openNav3() {
	nav3Open = true;
	$('#navWrapper').css('height', '288px');
	$('#nav3Wrapper').animate({'height': '260px'}, 50, 'linear');
}

function closeNav3() {
	nav3Open = false;
	$('#nav3Wrapper').animate({'height': '56px'}, 50, 'linear', function() {
		$('#navWrapper').css('height', '84px');
	});
}

function closeNav2and3() {
	nav2Open = false;
	nav3Open = false;
	$('#nav2Wrapper').css('height', '10px');
	$('#nav3Wrapper').animate({'height': '20px'}, 50, 'linear', function() {
		$('#navWrapper').css('height', '48px');
		$('#navWrapper').find('.active').removeClass('active')
	});
}

function imageRotator() {
	imageRotatorTimeout = setTimeout("changeImage('true')", imageRotatorDelay)
}

function changeImage(autoChange) {
	var nNumberImages = $('#imgRotator').find('a').length,
		oActiveImg = $('#imgRotator').find('a.active'),
		oActiveDot = $('#imgRotator').find('.dots').find('.dot.active'),
		nActiveIndex = oActiveImg.index(),
		nLastIndex = nNumberImages - 1,
		nNextIndex = nActiveIndex + 1,
		oNextImg,
		oNextDot;
		
	if(nNextIndex > nLastIndex) {
		nNextIndex = 0;
	}
	
	oNextImage = $('#imgRotator').find('a:eq('+nNextIndex+')')
	oNextDot = $('#imgRotator').find('.dots').find('.dot:eq('+nNextIndex+')')
	
	oNextImage.show();
	oActiveDot.removeClass('active');
	oActiveImg.fadeOut(imageRotatorAnimationTime, function() {
		oNextImage.addClass('active');
		$(this).removeClass('active');
		oNextDot.addClass('active');
		if(autoChange) {
			imageRotator();
		}
	})
}

$(document).ready(function() {
	imageRotator();
})
