(function($) {
	// Preload images passed as parameters
	var preload_cache = [];
	// arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement("img");
			cacheImage.src = arguments[i];
			preload_cache.push(cacheImage);
		}
	}
	
})(jQuery);

// initialize the main navigation
function nav_init() {
	// get nav right position and width and store the values with data() method, init hover
	for (var i = 1; i <= 3; i++) {
		$("#nav"+i).data("navpos", {idx: i, right: parseInt($("#nav"+i).css("right")), width: $("#nav"+i).width()});
		$("#nav"+i).hoverIntent({
			over: nav_show,
			out: nav_hide,
			timeout: 200
		});
	}
}
// show nav bar
function nav_show() {
	// get nav data back
	var navpos = $(this).data("navpos");
	if (navpos && navpos.right != 0) {
		// show nav (right position to zero)
		$(this).stop().animate({right:0}, {duration:400, easing:"swing"});
		$(this).children("div:first").removeClass("arrow-white-left");
	}
}
// hide nav bar
function nav_hide() {
	// get nav data back
	var navpos = $(this).data("navpos");
	if (navpos && navpos.right != 0) {
		// hide nav (right position to negative width minus 19 to show the nav arrow)
		$(this).stop().animate({right:-navpos.width+19}, {duration:400, easing:"swing"});
		$(this).children("div:first").addClass("arrow-white-left");
	}
}
// Show the artist picture and name based on the element id (id="type-folder" ex:"illus-bagieu")
function artist_list_on(el, url_root) {
	if (url_root.substr(-1, 1) != '/') url_root += '/';
	$(".main-picture").addClass("loading");
	var tmp = $(el).attr("id").split(/[\|:]/, 2);
	var type = tmp[0];
	var folder = tmp[1];
	var name = $(el).html();
	$("#art-name").html(name);
	$("#art-image").attr("src", url_root+"img/"+type+"/"+folder+".jpg").attr("alt", name);
}
// Hide the artist picture and name
function artist_list_out(url_root) {
	if (url_root.substr(-1, 1) != '/') url_root += '/';
	$(".main-picture").removeClass("loading");
	$("#art-name").html("&nbsp;");
	$("#art-image").attr("src", url_root+"p/blank.gif").attr("alt", "");
}
// Tooltip
function show_artist_tooltip() {
	// store the title for later use and empty the title
	$("#art-image-link").data("title", $("#art-image-link").attr("title"));
	$("#art-image-link").attr("title", "");
	$("#art-image-tooltip .tooltip").html($("#art-image-link").data("title"));
	tt_height = $("#art-image-tooltip").height();
	// get container positions and dimensions
	var mp_offset = $(".main-picture").offset();
	var mp_width = $(".main-picture").width();
	var mp_height = $(".main-picture").height();
	var tt_height = $("#art-image-tooltip").height();
	$(".main-picture").bind("mousemove", function(e) {
		var newX = Math.round(e.pageX - mp_offset.left);
		var newY = Math.round(e.pageY - mp_offset.top);
		if (newX >= 0 && newY >= 0 && newX <= mp_width && newY <= mp_height) {
			$("#art-image-tooltip").css("top",(newY-tt_height-10)+"px").css("left",(newX-22)+"px");
		}
	});
	// show tooltip
	$("#art-image-tooltip").slideDown();
}
function hide_artist_tooltip() {
	$("#art-image-tooltip").slideUp();
	$(".main-picture").unbind("mousemove");
	// get title back
	$("#art-image-link").attr("title", $("#art-image-link").data("title"));
	$("#art-image-link").removeData("title");
}

