var pages = [
	'index.html',
	'overview.html',
	'about.html',
	'companies.html',
	'products.html',
	'services.html',
	'contact.html'
];

var o = this;
var page, index;
var primary, secondary;

window.addEvent('domready', function () {
	page = location.pathname.split('/').pop();
	index = Math.max(0, pages.indexOf(page));
	primary = $$('#nav_primary a');
	secondary = $$('#nav_secondary a');
	setup(primary, true);
	setup(secondary, false);
});

function setup ($list, $isPrimary) {
	$list.each(function ($el, $i) {
		// abort if current		
		if ($isPrimary && ($i + 1) == index) {
			var i = $el.getChildren('img')[0];
			var m = imageParts(i);
			i.src = [m[0], 1, m[2]].join('');
			return;
		}
		
		// add events
		$el.addEvent('mouseenter', onNavMouseEvent.bindWithEvent(o, [$el, 1]));
		$el.addEvent('mouseleave', onNavMouseEvent.bindWithEvent(o, [$el, 0]));
		
		// preload images
		m = imageParts($el.getChildren('img')[0]);
		var a = new Image();
		var b = new Image();
		a.src = [m[0], 0, m[2]].join('');
		b.src = [m[0], 1, m[2]].join('');
	});
}

function imageParts ($img) {
	var m = /^(.+)([01])(\.gif)$/.exec($img.src);
	m.shift();
	return m;
}

function onNavMouseEvent ($e, $el, $type) {
	var i = $el.getChildren('img')[0];
	var m = imageParts(i);
	i.src = [m[0], $type, m[2]].join('');
}
