function equalHeight(group) {
   tallest = 0;
   group.each(function() {
	  thisHeight = $(this).height();
	  if(thisHeight > tallest) {
		 tallest = thisHeight;
	  }
   });
   group.height(tallest);
}

function equalWidth(group,additionalWidth) {
   widest = 0;
   group.each(function() {
	  thisWidth = $(this).width();
	  if(thisWidth > widest) {
		 widest = thisWidth;
	  }
   });
   if (!additionalWidth) {
	   var additionalWidth = 0;
   }
   widest = widest + additionalWidth;
   group.width(widest);
}

$(document).ready(function () {
	
	$('.iepngfix, .ui-slider-handle').supersleight({
		shim: 'images/clear.gif'
	});
	
	var minMenuTextLeft = 10;
/*	var list = new Array();
	$(".menuRootLink span").each(function() {
		var pos = $(this).offset();
		var left = pos.left;
		list.push(left);
	});
	var lowest = Math.min.apply(Math,list);
	if (lowest < minMenuTextLeft) {
		$("ul.leftMenu, div#leftMenuContainer, td#leftSpacer").each(function(){
			if (lowest < 0) {
				lowest = -lowest;
			}
			var newWidth = $(this).width() + lowest + minMenuTextLeft;
			$(this).width(newWidth);
			var minWidth = newWidth + "px";
			$("td#leftSpacer").css("min-width",minWidth);
			$("td#leftSpacer").css("min-width","260px"); // TEMPORARY OVERRIDE! why is this script not working?
		});
	}*/
	$("ul.leftMenu li").hoverIntent({
		over	:	function () {
						$(this).find("a.menuRootLink").addClass("selected");
						$(this).addClass("over");
						$(this).children('ul.dropdown').show();
						var parentwidth = $(this).outerWidth();
						parentwidth = parentwidth - 2;
						$(this).children('ul.dropdown').css('left',parentwidth);
						var dropdownWidth = [];
						$(this).children('ul.dropdown').children('li').each(function() {
							dropdownWidth.push($(this).width());
						});
						var largest = dropdownWidth.sort(function(a,b){return a - b}).slice(-1);
						$(this).children('ul.dropdown').width(largest);
						/*$(this).children("ul.dropdown").each(function(){
							var absPosition = $(this).parent().position();
							var bottomY = absPosition.top + $(this).innerHeight() + $("#leftMenuContainer").position().top;
							var windowBottom = $(window).height();
							if (bottomY > windowBottom) {
								var offset = 5 + (bottomY - windowBottom);
								offset = offset;
								var topPos = '-' + offset + 'px';
								$(this).css('top',topPos);
							}
						});*/
					},
		timeout :	100,
		out		:	function () {
						$(this).find("a.menuRootLink").removeClass("selected");
						$(this).removeClass("over");
						/*$(this).children("ul.dropdown").each(function(){
							$(this).css('top','0px');
						});*/
						$(this).find('ul.dropdown').hide();
					}
	});
	
	/* equalHeight($(".column")); */
	
	$("input:text:not([class]), input:password:not([class])").addClass("textInput");
	
	/*$(window).scroll(function () {
		var x = $(this).scrollLeft();
		if (x > 0) {
			var offset = '-' + x + 'px';
			$('#headerContainer').css('left',offset).width($(document).width());
		} else {
			$('#headerContainer').css('left','0px').width("100%");
		}
	});
	*/
	
	$(".boxesInner").each(function() {
		var boxesInnerWidth = $(this).width();
		$(this).find("*").each (function() {
			if ($(this).width() > boxesInnerWidth) {
				$(this).removeAttr("width").width("100%");
			}
		});
	});
	
	$(".videocat").click(function() {
		var thisid = $(this).attr("id").split("_");
		var show = "#videolist_" + thisid[1];
		var hide = ".videolist:not(" + show + ")";
		$(hide).hide();
		$(show).show();
	});
	
	function resizeVideo() {
		var origWidth = 736;
		var origHeight = 412;
		var vidWidth = $("#homeVideo").width();
		var vidHeight = parseInt(vidWidth * (origHeight/origWidth));
		var sideHeight = vidHeight + 4;
		$("#homeVideo iframe").height(vidHeight);
		
		var origWidth2 = 640;
		var origHeight2 = 518;
		var vidContainerWidth2 = $("div.contentMinWidth:first").width() - 255;
		var vidWidth2 = vidContainerWidth2 - 5;
		var vidHeight2 = parseInt(vidWidth2 * (origHeight2/origWidth2));
		if (vidContainerWidth2 < (origWidth2 + 5)) {
			$("div.video_gallery_player, div.video_gallery_player embed").width(vidWidth2);
			$("div.video_gallery_player, div.video_gallery_player embed").height(vidHeight2);
		} else {
			$("div.video_gallery_player, div.video_gallery_player embed").width(origWidth2);
			$("div.video_gallery_player, div.video_gallery_player embed").height(origHeight2);
		}
		
		//$("#homeVideo iframe").height(vidHeight2);
	}
	
	$(window).resize(resizeVideo);
	resizeVideo();
	
	$("div#login, div#checkout").hover(function() {
		var newBG = $("div.bg_l, div.bg_r",this).css("background-image").replace("_off.", "_on.");
		$("div.bg_l, div.bg_r",this).css("background-image", newBG);
	}, function() {
		var newBG = $("div.bg_l, div.bg_r",this).css("background-image").replace("_on.", "_off.");
		$("div.bg_l, div.bg_r",this).css("background-image", newBG);
	});
	
	$("td#searchSubmit").hover(function() {
		var newBG = $(this).css("background-image").replace("_off.", "_on.");
		$(this).css("background-image", newBG);
	}, function() {
		var newBG = $(this).css("background-image").replace("_on.", "_off.");
		$(this).css("background-image", newBG);
	});
	
	$('a[rel*=lightbox]').lightBox();
	
	$("div.ghostContainer").each(function() {
		equalWidth($("table.assocBar",this),20);
	});
	
	/*$.localScroll({
		duration: 1,
		offset: {top:-172}
	});
	
	$.localScroll.hash({
		duration: 1,
		reset: true,
		offset: {top:-172}
	});*/
	
	/*var menuBottom = $('div#headerContainer').height() + $('div#leftMenuContainer').height();
	var normalHeight = $('div#leftMenuContainer').height();
	var normalWidth = $('div.menuOverlay div').offset().left + $('div.menuOverlay div').width();
	var top = $('div#headerContainer').height();
	function resizeAndMove(windowBottom, offset) {
		var newHeight = normalHeight - offset;
		if ($('div#leftMenuContainer').parent().attr('id') != 'menuWrap') {
			$('div#leftMenuContainer').wrap('<div id="menuWrap" />');
		}
		$('div#leftMenuContainer').css('position','relative');
		$('div#leftMenuContainer').css('top','0px');
		$('div#menuWrap').css('overflow','hidden');
		$('div#menuWrap').css('position','relative');
		$('div#menuWrap').height(newHeight);
		$('div#menuWrap').css('width',normalWidth);
		$('div#menuWrap').mousemove(function(e) {
			var yFromBottom = windowBottom - (e.pageY - $(window).scrollTop());
			if (yFromBottom < 200) {
				var moveMenu = ((yFromBottom - offset) - 100) / 2;
				if (moveMenu < 0) {
					$('div#leftMenuContainer').css('top',moveMenu);
					var backgroundPos = '0px ' + (moveMenu-35) +'px';
					$('div#leftMenuContainer').parents('body').css('background-position',backgroundPos);
				}
			} else {
				$('div#leftMenuContainer').css('top','0px');
				$('div#leftMenuContainer').parents('body').css('background-position','0px -35px');
			}
		}).mouseleave(function() {
			$('div#leftMenuContainer').css('top','0px');
			$('div#leftMenuContainer').parents('body').css('background-position','0px -35px');
		});
		$("ul.leftMenu").children('li').hover(function() {
			$('ul.dropdown:first',this).show();
			var newWidth = $('ul.dropdown:first',this).offset().left + $('ul.dropdown:first',this).width();
			$('div#menuWrap').css('width',function() {
				return newWidth;
			});
		}, function() {
			$('ul.dropdown:first',this).hide();
			$('div#menuWrap').css('width',function() {
				return normalWidth;
			});
		})
	}
	function finishResizeAndMove() {
		if ($('div#leftMenuContainer').parent().attr('id') == 'menuWrap') {
			$('div#menuWrap').unbind();
			$('div#leftMenuContainer').unwrap();
		}
		$('div#leftMenuContainer').unbind();
		$('div#leftMenuContainer').css({
			'position': 'absolute',
			'top': function() {
				return top;
			}
		});
		$('div#leftMenuContainer').parents('body').css('background-position','0px -35px');
	}*/
	$(window).resize(function() {
		/*var newmenuBottom = $('div#headerContainer').height() + $('div#leftMenuContainer').height();*/
		var viewportHeight = $(window).height();
		/*var offset = newmenuBottom - viewportHeight;
		if (offset > 0) {
			resizeAndMove(viewportHeight,offset);
		} else {
			finishResizeAndMove();
		}*/
		var printHeight = '$1' + (viewportHeight - 75) + '$3';
		var printLink = $('div#printpage a.thickbox').attr('href');
		var printwidth = printLink.replace(/(.*height=)(\d+)(&?.*)/i, printHeight);
		$('div#printpage a.thickbox').attr('href',printwidth);
		if ($('div#footerNames').width() > 600) {
			$('div#footerNames').css('width',600);
		} else {
			$('div#footerNames').css('width','auto')
		}
		if ($('table#addresses_all').width() > $('div#footeraddresses').width()) {
			$('table#addresses_all').hide();
			$('table.addresses4').show();
		} else {
			$('table.addresses4').hide();
			$('table#addresses_all').show();
		}
		if ($(window).width() < 1000) {
			$("div#mainContainer").css('width', '1000px');
		} else {
			$("div#mainContainer").css('width', '100%');
		}
	});
	if ($('div#footerNames').width() > 600) {
		$('div#footerNames').css('width', '600px');
	} else {
		$('div#footerNames').css('width','auto')
	}
	if ($('table#addresses_all').width() > $('div#footeraddresses').width()) {
		$('table#addresses_all').hide();
		$('table.addresses4').show();
	} else {
		$('table.addresses4').hide();
		$('table#addresses_all').show();
	}
	/*var windowBottom = $(window).height();
	var offset = menuBottom - windowBottom;
	if (offset > 0) {
		resizeAndMove(windowBottom,offset);
	} else {
		finishResizeAndMove();
	}*/
	var viewportHeight = $(window).height();
	var printHeight = '$1' + (viewportHeight - 75) + '$3';
	var printLink = $('div#printpage a.thickbox').attr('href');
	var printwidth = printLink.replace(/(.*height=)(\d+)(&?.*)/i, printHeight);
	$('div#printpage a.thickbox').attr('href',printwidth);
	
	/*$('div.boxbody:has(table.mapBox)').each(function() {
		var maps = $('table.mapBox',this).length;
		$('div.anchorDiv:even',this).css('float','left');
		$('br',this).remove();
		$('table.mapBox',this).css({
			'width': '49%',
			'float': 'left',
			'margin-bottom': '10px'
		}).find('tr').append('<td width="40">&nbsp;</td>');
		$(this).append('<br style="clear: both;" />');
	});*/

});
