$(document).ready(function() {
	// Menu
	if (($.browser.msie && $.browser.version >= 8) || 
		!($.browser.msie)) { 
		var imgCounter = 1;
	
		$("header nav ul li").hover(
			function() {
				$(this).children("ul").fadeIn();
				imgCounter++;
			},
			function() {
				$(this).children("ul").fadeOut();
			}
		);
	}
	
	if ($('.sub-header').length) {
		// Sub Menu highlight
		curPage = window.location.pathname;
		curParams = window.location.search;
		fullURL = curPage + curParams;
		$('.sub-header nav ul li a').each(function(){
			if (fullURL.indexOf($(this).attr('href')) != -1){
				$(this).addClass('active');
			}
		});
		$('.sub-header nav ul ul li a').each(function(){
			link = $(this).attr('href').toLowerCase();
			url = window.location.pathname.toLowerCase();
			linkParams = link.replace(url, '').toLowerCase().replace('?', '');
			urlParams = window.location.search.toLowerCase().replace('?', '');
			if(urlParams.indexOf(linkParams) != -1){
				$(this).addClass('active');
				$(this).parent('li').parent('ul').siblings('a').addClass('active');
			}
		});
		$('.sub-header nav ul li:last-child a').addClass('last-item');
		$('.sub-header nav ul ul').css('display', 'none');
		$('.sub-header nav ul li a.active').parent('li').find('ul').css('display', 'block');
	
		// Sub Menu links size fixing
		subHeadWidth = $('.sub-header').width();
		curWidth = 0;
		elCount = 0;
		remWidth = 0;
		$('.sub-header nav ul:first-child').children('li').each(function(){
			curWidth += $(this).children('a').outerWidth(true);
			++elCount;
		});
		remWidth = subHeadWidth - curWidth;
		pad = (remWidth / elCount) / 2;
		padMod = 0;
		pad = Math.floor(pad) + padMod;
		
		$('.sub-header nav ul:first-child').children('li').each(function(){
			$(this).children('a').css('padding-left', pad + 'px').css('padding-right', pad + 'px');
			remWidth = remWidth - (pad * 2);
		});
		$('.sub-header nav ul:first-child').children('li:last-child').children('a').css('padding-right', (pad + remWidth) + 'px');
	}
	
	// Background
	$(window).resize(resizeBg);
	resizeBg();
	
	// Rollovers
	$('#cart a.checkout img').hover(
		function() { $(this).attr('src', '/images/buttons/cart-checkout-button-over.jpg'); }, 
		function() { $(this).attr('src', '/images/buttons/cart-checkout-button.jpg'); }
	);
	$('#browse-form form button').hover(
		function() { $(this).find('img').attr('src', '/images/buttons/browse-form-button-over.jpg'); }, 
		function() { $(this).find('img').attr('src', '/images/buttons/browse-form-button.jpg'); }
	);
});

function resizeBg() {
	//windowWidth = ($('.container').width() > $(window).width())?$('.container').width():$(window).width();
	windowWidth = $(window).width();
	windowHeight = $(document).height() - 3; 
	// -3px handles issue of IE6, IE7 endlessly lengthening and resizing the "document" object; 
	// slight white pad at bottom of screen is unnoticeable.
	ratio = windowWidth / windowHeight; 
	if (windowHeight > windowWidth) {
		containerHeight = $('.container').height();
		if (containerHeight > windowHeight) {
			$("img#bg").height(containerHeight);
		} else {
			$("img#bg").height(windowHeight);
		}
		$("img#bg").width(windowHeight * ratio);
	} else {
		containerWidth = $('.container').width();
		if (containerWidth > windowWidth) {
			$("img#bg").width(containerWidth);
		} else {
			$("img#bg").width(windowWidth);
		}
		$("img#bg").height(windowWidth / ratio);
	}
}
