String.prototype.startsWith = function(str){
    return (this.lastIndexOf(str, 0) === 0);
}

function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
	jQuery(".ngg-navigation .page-numbers").removeClass("selected");
	jQuery(".ngg-navigation #page-number-" + idx).addClass('selected');
};

function mycarousel_initCallback(carousel) {
	jQuery('.ngg-navigation .page-numbers').click(function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.ngg-navigation .next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('.ngg-navigation .prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}
function mycarousel_buttonNextCallback(carousel, button, enabled) {
	if (enabled)
		jQuery('.ngg-navigation .next').removeClass("disabled");
	else
		jQuery('.ngg-navigation .next').addClass("disabled");
};
function mycarousel_buttonPrevCallback(carousel, button, enabled) {
    if (enabled)
		jQuery('.ngg-navigation .prev').removeClass("disabled");
	else
		jQuery('.ngg-navigation .prev').addClass("disabled");
};

var on_featured_image = false;

jQuery(document).ready(function($) {
	jQuery('.ngg-thumbnails').jcarousel({
		scroll: 1,
		visible: 1,
		initCallback: mycarousel_initCallback,
		buttonNextCallback:   mycarousel_buttonNextCallback,
		buttonPrevCallback:   mycarousel_buttonPrevCallback,
		itemVisibleInCallback: {
			onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
		},
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemFallbackDimension: 165
	});

	jQuery('.ngg-navigation .page-numbers').removeAttr("href");
	jQuery('.ngg-navigation .next').removeAttr("href");
	jQuery('.ngg-navigation .prev').removeAttr("href");
	jQuery('.ngg-gallery-thumbnail a').removeAttr("href");
	jQuery('.ngg-thumbnails .ngg-gallery-thumbnail-box').removeAttr('style');
	
	function SelectImage(el) {
		var thumb = $(el).children("img");
		$("#mainimage").attr("src", thumb.attr("data-large"));
		$("#mainimage").attr("alt", thumb.attr("alt"));
		
		var width = thumb.attr("data-large-width");
		var height = thumb.attr("data-large-height");
		if (width > 519) {
			if (width > 346) {
				$("#mainimage").css("width", "auto");
				$("#mainimage").css("height", "auto");
			} else {
				$("#mainimage").css("width",519);
				$("#mainimage").css("height", "auto");
			}
		}
		if (width > 346) {
			$("#mainimage").css("height", 346);
			$("#mainimage").css("width", "auto");
		} else {
			$("#mainimage").css("width", thumb.attr("data-large-width"));
			$("#mainimage").css("height", thumb.attr("data-large-height"));
		}
		$("#imagedescription").html(thumb.attr("data-descr"));
		on_featured_image = false;
		$('.selected_img').removeClass('selected_img');
		$(el).addClass('selected_img');
	}
	
	$("#ngg-thumbnails .ngg-gallery-thumbnail a").click(function() {SelectImage(this); return false;});
	
	$("#next_image").click(function() {
		var current = parseInt($('.selected_img').attr('data-number'));
		
		if (on_featured_image) {
			current = 0;
		}
		
		if ((current % 6 == 0) && (current != 0)) {
			$('.ngg-navigation .next').click();
		}
		var next = $('a[data-number="'+(current+1)+'"]');
		if (next.length > 0)
			SelectImage(next);
		return false;
	});
	$("#prev_image").click(function() {
		var current = parseInt($('.selected_img').attr('data-number'));
		if (current % 6 == 1) {
			$('.ngg-navigation .prev').click();
		}
		var prev = $('a[data-number="'+(current-1)+'"]');
		if (prev.length > 0)
			SelectImage(prev);
		return false;
	});
	
	var mainsrc = $("#mainimage").attr("src");
	if ((typeof mainsrc) ==  'undefined' || mainsrc == '' || mainsrc.startsWith('data:')) {
		$(".ngg-gallery-thumbnail a").first().click();
	} else {
		on_featured_image = true;
	}
});
