$(document).ready(function() {
	fillwidth('#menu','#menu > ul.menu > li',-1,false);

	if (window.map_initialize) map_initialize();
	
	// equal-height each heading and cell on each row of a product grid
	if ($('#ranges > li > ul').length) {
		$('#ranges > li > ul').each(function(){$(this).find('.content').equalHeights();});
	}

	// nivoslider
	if ($('#slider').length) $('#slider').nivoSlider({effect:'fade',pauseTime:5000,captionOpacity:1,controlNav:false,directionNav:false});
	
	// fancybox
	if ($(".gallery a").length) $(".gallery a").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleShow'			: true,
		'scrolling'			: 'no'
	});
	
	// 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";
		}
	}
});

function fillwidth(parentselector,childrenselector,parentwidthfudge,debug){
	// fill a menu out to the full width available
	// get the width of parent
	// add up the widths of all the children
	// apportion the difference between all the children
	// give any leftover X pixels to the first X elements
	var logtext='';
	var menuwidth=$(parentselector).width()+parentwidthfudge; // need to knock a px off or it doesn't fit for some reason
	if (debug) logtext+='menuwidth: '+menuwidth+'\n';
	var tops=$(childrenselector); // use the LIs here, as they have borders
	if (debug) logtext+='tops.length: '+tops.length+'\n';
	var topswidth=0;
	tops.each(function(){
		topswidth+=$(this).outerWidth()
	});
	if (debug) logtext+='topswidth: '+topswidth+'\n';
	var diff=menuwidth-topswidth;
	if (debug) logtext+='diff: '+diff+'\n';
	var wideneachby=Math.floor(diff/tops.length);
	if (debug) logtext+='wideneachby: '+wideneachby+'\n';
	var padd_both=Math.floor(wideneachby/2);
	if (debug) 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);
	if (debug) logtext+='leftover: '+leftover+'\n';
	var topscounter=1;
	if (debug) logtext+='padd_left: '+padd_left+'\n';
	if (debug) logtext+='padd_right: '+padd_right+'\n';
	tops.each(function(){
		if (debug) logtext+='topscounter: '+topscounter+'\n';
		var oldpadleft=$(this).css('paddingLeft');
		if (debug) logtext+='\toldpadleft:'+oldpadleft+'\n';
		var newpadleft=parseInt(oldpadleft)+padd_left;
		if (debug) logtext+='\tnewpadleft:'+newpadleft+'\n';
		$(this).css('paddingLeft',newpadleft+'px');
		
		var oldpadright=$(this).css('paddingRight');
		if (debug) logtext+='\toldpadright:'+oldpadright+'\n';
		var newpadright=parseInt(oldpadright)+padd_right;
		if (debug) 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
			if (debug) logtext+='\tnewpadright(revised):'+newpadright+'\n';
		}
		$(this).css('paddingRight',newpadright+'px');
		
		topscounter++;
	})
	var finaltopswidth=0;
	tops.each(function(){
		finaltopswidth+=$(this).outerWidth()
	})
	if (debug) logtext+='finaltopswidth: '+finaltopswidth+'\n';
	if (debug) console.log(logtext);
} // end function fillwidth
/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 * 
 * TJS revision 1.1
 * 2010-06-14
 * Solves the problem of height-matching an element that has padding and/or border, with one that has no padding and/or border. 
 * It inspects the outerHeight rather than the height, but adjusts only the height by whatever shortfall was found.
 */
(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).outerHeight() > tallest) {
				tallest = $(this).outerHeight();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			shortfall=tallest-$(this).outerHeight();
			targetheight=$(this).height()+shortfall;
			$(this).height(targetheight)/*.css("overflow","hidden")*/;/* overflow:auto triggers scrollbars upon :active of a link */
		});
	}
})(jQuery);

