
jQuery.preloadImages = function() {
	for(var i = 0; i < arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

$(document).ready(function() {
	$('.nav_item a').hover(function() {
		$(this).children('img').attr('src', ToggleNavImage($(this).children('img').attr('src')));
	}, function() {
		$(this).children('img').attr('src', ToggleNavImage($(this).children('img').attr('src')));
	});

	$('a.article_popup').click(function() {
		var dimensions = $(this).attr('dimensions');
		if (dimensions) {
			var dimensionParts = dimensions.split(",");
			var width = dimensionParts[0];
			var height = dimensionParts[1];
		}

		if (!IsNumeric(width)) width = 800;
		if (!IsNumeric(height)) height = 500;
		
		width = parseInt(width);
		height = parseInt(height);

		// Code to center the window
		width += 32;
		height += 96;
		wleft = (screen.width - width) / 2;
		wtop = (screen.height - height) / 2;
		if (wleft < 0) {
			width = screen.width;
			wleft = 0;
		}
		if (wtop < 0) {
			height = screen.height;
			wtop = 0;
		}

		window.open(this.href, 'articlewin', 'scrollbars=yes,width=' + width + ',height=' + height + ',left=' + wleft + ',top=' + wtop);
		return false;
	});
});

function ToggleNavImage(currentSource) {
	var isOver = !(currentSource.indexOf('-OVER.gif') == -1);
	return currentSource.replace((isOver ? /\-OVER\.gif/ : /\.gif/), (isOver ? '.gif' : '-OVER.gif'));
}

function IsNumeric(value) {
	return isFinite( (value * 1.0) );
}
