$(function () {
	$('a[rel="external"]').click(function () {
		window.open(this.href);
		return false;
	});
});

// The end of the body, but without waiting for page onload.
// This outta be moved into jQuery
var bodyEnd = function() {
	var fns = [];
	return {
		invoke: function() {
			var i;
			for (i = 0; i < fns.length; i++) {
				fns[i].call();
			}
		},
		add: function(f) {
			fns.push(f);
		}
	};
}();

// Thanks to Douglas Crockford.
function walkTheDOM(node, func) {
	func(node);
	node = node.firstChild;
	while (node) {
		walkTheDOM(node, func);
		node = node.nextSibling;
	}
}

// Pad the nav elements out so that the menu bar spans the whole width.
function stretchNav(id) {
	var i, j, ul, uls, temp, lis, full, padding, extra, span, parent;
	
	uls = $('#' + id + ' ul');
	parent = $('#' + id)[0];
	for (j = 0; j < uls.length; j += 1) {
		ul = uls[j];
		if (ul.parentNode === parent) {
			lis = [];
			temp = ul.getElementsByTagName('li');
			
			full = 0;
			for (i = 0; i < temp.length; i++) {
				if (temp[i].parentNode == ul) {
					lis.push(temp[i]);
					full += temp[i].clientWidth;
				}
			}
	
			padding = Math.floor((ul.clientWidth - full) / lis.length);
			extra = ul.clientWidth - (full + padding * lis.length);
		
			for (i = 0; i < lis.length; i++) {
				span = lis[i].getElementsByTagName('span')[0];
				if (padding >= 0) {
					span.style.width = (span.clientWidth + padding + ((i < extra) ? 1 : 0) ) + 'px';
				}
			}
			
			if (ul.getElementsByTagName('ul').length > 0) {
				// and IE6 needs at +3 in order to display the last drop-down.
				ul.style.width = (ul.clientWidth + 3) + 'px';
			}
		}
	}
	
//	ul = document.getElementById(id).getElementsByTagName('ul')[0];
	
	
}

// Set up an ordered list of fragment links to ids, as a set of tabs for the id'd divs.
function setUpTabs(id, selectedClass) {
	
	function show(id) {
		var i, current;
		for (i = 0; i < panes.length; i++) {
			if (panes[i].id == id) {
				panes[i].style.display = 'block';
				$(lis[i]).addClass(selectedClass);
			} else {
				panes[i].style.display = 'none';
				$(lis[i]).removeClass(selectedClass);
			}
		}
	}
	
	function showByIndex(index) {
		show(panes[index].id);
	}
	
	function tab(li) {
		var a, split, id;
		a = li.getElementsByTagName('a')[0];
		split = a.href.split('#');
		if (split.length > 1) {
			id = split[1];
			a.onclick = function() {
				show(id);
				this.blur();
				return false;
			};
			return document.getElementById(id);
		}
	}
	
	var i, lis, panes;
	
	selectedClass = selectedClass || 'selected';
	panes = [];
	lis = document.getElementById(id).getElementsByTagName('li');
	for (i = 0; i < lis.length; i++) {
		panes[i] = tab(lis[i]);
	}
	
	showByIndex(0);
}

// Set up the carousel for home and store pages.
function setUpFeaturedStores(moveby, containerClass) {
	$('#featured-stores-carousel-container').addClass(containerClass);
	stepcarousel.setup({
		galleryid: 'featured-stores-carousel', //id of carousel DIV
		beltclass: 'featured-stores-carousel-belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'featured-stores-carousel-panel', //class of panel DIVs each holding content
		panelbehavior: {
			speed: 500,
			wraparound: false,
			persist: false
		},
		defaultbuttons: {
			enable: true,
			moveby: moveby,
			leftnav: [
				'/images/featured-left.png',
				-39, 0
			],
			rightnav: [
				'/images/featured-right.png',
				0, 0
			]
		},
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	});

	// wacky safari hack
	if (navigator.userAgent.indexOf('Safari') >= 0) {
		// Carousel doesn't work in safari unless we try to load an external
		// javascript file that doesn't exist.  I haven't tried to figure out why.
		document.write('<script type="text/javascript" src="this-file-does-not-exist.js"><\/script>');
	}
}

bodyEnd.add(function(){
	stretchNav('site-header-nav');
});
