$(document).ready(function(){

	/* Clickable list items */

	if($(".clickable li").length) {

		$(".clickable > li").css("cursor", "pointer")

		$(".clickable > li").hover(function(){
			$("> .inner", this).addClass("hover");
		},function(){
			$("> .inner", this).removeClass("hover");
		});

		$(".clickable li").click(function(){
			document.location = $("a.more:first", this).attr("href");
		});

	}

	/* NGG Gallery */

	if($(".ngg-galleryoverview").length) {

		$(".ngg-galleryoverview").prepend('<div class="large"></div>');

		$(".ngg-galleryoverview").each(function(){
			load_img($("> .ngg-gallery-thumbnail-box:first > .ngg-gallery-thumbnail > a", this), $(".large", this));
			$("> .ngg-gallery-thumbnail-box:first > .ngg-gallery-thumbnail", this).addClass("ngg-gallery-thumbnail-on");
		});

		$(".ngg-galleryoverview .ngg-gallery-thumbnail-box .ngg-gallery-thumbnail a").click(function(){
			var parent = $(this).parents(".ngg-galleryoverview");
			$(".ngg-gallery-thumbnail-on", parent).removeClass("ngg-gallery-thumbnail-on");
			$(this).parent(".ngg-gallery-thumbnail").addClass("ngg-gallery-thumbnail-on");
			load_img(this, $(".large", parent));
			return false;
		});

	}

});

function load_img(source, destination) {
	var path = $(source).attr('href');
	$('img:first', destination).remove();
	//$(destination).css("background-image", "/images/loading.gif");
	$(destination).addClass("loading");
	var imgsrc = path;
	var newimg = new Image();
	newimg.src = imgsrc;
	newimg.onload = function() {
		var randomnumber = Math.floor(Math.random()*11);
		$(destination).append('<img src="'+imgsrc+'" />');
		newimg.onload = function(){};
		$(destination).removeClass("loading");
	}
	newimg.src = imgsrc;
	//$(destination).removeClass("loading");
}