$(document).ready(function() {
	// fill the top menu out to the full width available
	// get the width of #menu
	// add up the widths of all the .top
	// apportion the difference between all the .top
	// give any leftover X pixels to the first X elements
	// first we need to Cufon-ize the menu items or the widths will change later
	Cufon.replace('#menu > li > a',{hover: true}); // doesn't actually nmake any difference - I suppose Cufon resizes later
	var logtext='';
	var menuwidth=817;//$('#menu').width();
	logtext+='menuwidth: '+menuwidth+'\n';
	var tops=$('#menu > li'); // use the LIs here, as they have borders
	logtext+='tops.length: '+tops.length+'\n';
	var topswidth=0;
	tops.each(function(){
		topswidth+=$(this).outerWidth()
	});
	logtext+='topswidth: '+topswidth+'\n';
	var diff=menuwidth-topswidth;
	logtext+='diff: '+diff+'\n';
	var wideneachby=Math.floor(diff/tops.length);
	logtext+='wideneachby: '+wideneachby+'\n';
	var padd_both=Math.floor(wideneachby/2);
	logtext+='padd_both: '+padd_both+'\n';
	var padd_left=Math.floor(padd_both);
	var padd_right=Math.ceil(padd_both);
	if (wideneachby%2==1) padd_right+=1;
	var leftover=diff-(wideneachby*tops.length);
	logtext+='leftover: '+leftover+'\n';
	var tops=$('#menu > li > a'); // want to resize the As rather than the LIs, as they both have borders and we don't want to split them
	var topscounter=1;
	logtext+='padd_left: '+padd_left+'\n';
	logtext+='padd_right: '+padd_right+'\n';
	tops.each(function(){
		logtext+='topscounter: '+topscounter+'\n';
		var oldpadleft=$(this).css('paddingLeft');
		logtext+='\toldpadleft:'+oldpadleft+'\n';
		var newpadleft=parseInt(oldpadleft)+padd_left;
		logtext+='\tnewpadleft:'+newpadleft+'\n';
		$(this).css('paddingLeft',newpadleft+'px');
		
		var oldpadright=$(this).css('paddingRight');
		logtext+='\toldpadright:'+oldpadright+'\n';
		var newpadright=parseInt(oldpadright)+padd_right;
		logtext+='\tnewpadright:'+newpadright+'\n';
		if (topscounter<=leftover) {
			if (diff>=0) newpadright+=1;
			else newpadright-=1; // in IE6 the menu *starts off* too wide, so need to subtract rather than add. Still ends up a px or 3 too short but that's good enough for such an old browser
			logtext+='\tnewpadright(revised):'+newpadright+'\n';
		}
		$(this).css('paddingRight',newpadright+'px');
		
		topscounter++;
	})
	var tops=$('#menu > ul > li'); // use the LIs here, as they have borders
	var finaltopswidth=0;
	tops.each(function(){
		finaltopswidth+=$(this).outerWidth()
	})
	logtext+='finaltopswidth: '+finaltopswidth+'\n';
	//alert(logtext);
	
	// top menu flyouts
	if (!($.browser.msie && $.browser.version<7)) {
		// This doesn't work properly in IE6
		$(document).ready(function() {
			function show() {
				$(this).find(".flyout").show();//fadeIn('fast');//slideDown();show();
				//alert('now resize the width of #machinery-flyout (or its holder) down to the width of the child ul')
			}
			function hide() { 
				$(this).find(".flyout").hide();//fadeOut('fast');//slideUp();hide();
			}
			$(".with-flyout").hoverIntent({
				sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
				interval: 50,   // number = milliseconds for onMouseOver polling interval
				over: show,     // function = onMouseOver callback (required)
				timeout: 300,   // number = milliseconds delay before onMouseOut
				out: hide       // function = onMouseOut callback (required)
			});
		});
	}

	// Cufon-ize headings
	Cufon.replace('.cufon',{hover: false});
	Cufon.replace('.cufon-hover',{hover: true});
	Cufon.replace('.flyout h2',{hover: false});
	Cufon.replace('h1',{hover: false});
	Cufon.replace('.menu-section h2 a',{hover: true});
	Cufon.replace('.menu-section h2',{hover: false});
	Cufon.replace('#footer-links-images h2',{hover: false});
	
	// nivoslider
	if (!($.browser.msie && $.browser.version<7)) {
		// This doesn't work properly in IE6
		if ($('#slider').length && typeof $.fn.nivoSlider=='function') {
			$('#slider').nivoSlider({effect:'fade',pauseTime:5000,captionOpacity:0,controlNav:true,directionNav:false});
			// center the control nav - pure CSS doesn't have a way of doing this without adding extra elements (i.e. altering nivo)
			var controlnavwidth=$('.nivo-controlNav').width()-5; // want to position it according to its VISIBLE width, not the extra margin-right (5px) on the last-child
			var leftness=$('.nivo-controlNav').css('left');
			leftness=leftness.replace('px','');
			var newleftness=leftness-controlnavwidth/2
			newleftness=newleftness+'px';
			$('.nivo-controlNav').css('left',newleftness);
		}
	}
		
	// fancybox
	if ($(".gallery a").length) $(".gallery a").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleShow'			: true,
		'scrolling'			: 'no'
	});
	
	// map
	if (window.map_initialize) map_initialize();
	
	// look for anchors with href and rel="external", and add target="_blank" to them so that they open in a new window
	// from http://articles.sitepoint.com/article/standards-compliant-world/3
	if (!document.getElementsByTagName) return;  
	var anchors = document.getElementsByTagName("a");  
	for (var i=0; i<anchors.length; i++) {  
		var anchor = anchors[i];  
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
});

