$(document).ready(function() {
	// adds new class name to maintain degradability
	$('.gallery_unstyled').addClass('gallery');
	
	$('ul.gallery').galleria({
		history: false, // activates the history object for bookmarking, back-button etc.
		clickNext: true, // helper for making the image clickable
		insert: '#main_image', // the containing selector for our main image
		
		// let's add some image effects for demonstration purposes
		onImage: function(image, caption, thumb) {
			
			// fade in the image & caption
			image.css('display', 'none').fadeIn(1000);
			caption.css('display', 'none').fadeIn(1000);
			
			var _div = image.parent("div");
			var r = image.width() / image.height();
			var or = r < 1 ? "v" : "h";
			image.addClass(or);
			_div.removeClass("h").removeClass("v").addClass(or);
			
			var bframe = $('<img id="bframe" src="uploads/images/graphics/black_frame_' + or + '.png">');
			_div.prepend(bframe);
			
			// add a title for the clickable image
			//image.attr('title', '>>');
			
			var tmp = "";
			if (thumb.attr('title')) {
				tmp += thumb.attr('title');
			}
			if (thumb.attr('alt')) {
				tmp += '<br>'+thumb.attr('alt');
			}
			caption.html(tmp);
		},
		
		// thumbnail effects goes here
		onThumb: function(thumb) {
			var img = $(thumb["0"]);
			
			// fetch the thumbnail container
			var _li = img.parents('li');
			
			var r = img.width() / img.height();
			var or = r < 1 ? "v" : "h";
			
			var shadow = $('<img class="shadow ' + or + '" src="uploads/images/graphics/thb_shadow_on.png">');
			shadow.click(function() {
				$.galleria.activate(thumb.attr("src"));
			});
			
			_li.prepend(shadow);
			
			// hover effects
			shadow.hover(
				function() {
					shadow.attr("src", "uploads/images/graphics/thb_shadow_off.png");
					if (!shadow.attr('tittle')) {
						shadow.attr('title', img.attr('alt'));
					}
				},
				function() {
					shadow.attr("src", "uploads/images/graphics/thb_shadow_on.png");
				}
			);
		}
	});
	
	var ul = $('ul.gallery')["0"];
	if (ul) {
		Gallery.nbOfImgs = ul.childElementCount;
		$(ul.firstElementChild).addClass('active');
	}
});
var Gallery = {
	current: 0
	, nbOfImgs: 0
	, imgPerRow: 3
	, imgPerColumn: 3
	, min: 0
	, max: null
	, check: function() {
		if (Gallery.max == null) {
			Gallery.max = parseInt(Gallery.nbOfImgs / (Gallery.imgPerRow * Gallery.imgPerColumn));
		}
	}
	, next: function() {
		Gallery.check();
		if (Gallery.current >= Gallery.min && Gallery.current < Gallery.max) {
			Gallery.current++;
			$("ul.gallery").animate({
				top: "-=310px"
			}, "slow");
		}
	}
	, prev: function() {
		Gallery.check();
		if (Gallery.current > Gallery.min && Gallery.current <= Gallery.max) {
			Gallery.current--;
			$("ul.gallery").animate({
				top: "+=310px"
			}, "slow");
		}
	}
};
