$(function() {
	$(".thumbnails_unstyled").addClass("thumbnails");			
	$("ul.thumbnails").galleria({
		history: false,
		clickNext: true,
		insert: "#main_image",
		onImage: function(image, caption, thumb) {
			image.css("display", "none").fadeIn(1000);
			caption.css("display", "none").fadeIn(1000);
			var _li = thumb.parents("li");
			_li.siblings().children("img.selected").fadeTo(500, 0.3);
			thumb.fadeTo("fast", 1).addClass("selected");
			image.attr("title", "Next image");
		},
		onThumb: function(thumb) {
			var _li = thumb.parents("li");
			var _fadeTo = _li.is(".active") ? "1" : "0.3";
			thumb.css({display: "none", opacity: _fadeTo}).fadeIn(1500);
			thumb.hover(
				function() { 
					thumb.fadeTo("fast", 1);		
				},
				function() {
					_li.not(".active").children("img").fadeTo("fast", 0.3);
				}
			)
		}
	});
	
	prepareArrow = function(arrow) {
		arrow.css({display: "none", opacity: "0.5", "top": "3px"}).fadeIn(1500);			
		arrow.hover(
			function() {
				arrow.fadeTo("fast", 1);
			},
			function() {
				arrow.fadeTo("fast", 0.3);			
			}
		);	
	}
	
	var leftArrow = $("#left_arrow");
	prepareArrow(leftArrow);
	leftArrow.css({"left": "-60px"});
	leftArrow.click(function() {
		$.galleria.prev();	
	});
	
	var rightArrow = $("#right_arrow");
	prepareArrow(rightArrow);
	rightArrow.css({"right": "-50px"});					
	rightArrow.click(function() {
		$.galleria.next();
	});
});

$(document).bind("keydown", "left", function() {
	if (!KeyboardNavigation.widgetHasFocus()) {
		$.galleria.prev();
	}
});
$(document).bind("keydown", "right", function() {
	if (!KeyboardNavigation.widgetHasFocus()) {
		$.galleria.next();
	}
});

var KeyboardNavigation = {
	widgetHasFocus: function() {
		if(typeof _jaWidgetFocus != 'undefined' && _jaWidgetFocus) {
			return true;
		}
		return false;
	}
}

var ImageScroller = {
	scrollRight: function() {
		$(this).scrollImages({ direction: "right", imageWidth: 106, size: 7 });
	},			
	scrollLeft: function() {
		$(this).scrollImages({ direction: "left", imageWidth: 106, size: 7 });
	}
}