$(document).ready(function(){
													 
	$('ul.gallery li img').addClass('noborder');
});

function startGallery() {
	if($('#thumbs ul.galleria').length > 0) {return false;}
	else {
		$('ul.gallery').galleria({
			history   : true, // 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
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				$('ul.gallery li img.selected').fadeTo(500,0.3).removeClass('selected');
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// tabs cause image heights to be 0. check and fix if needed.
				if(parseInt($(thumb[0]).css("height"))==0) {					
 					var $thumb = $(thumb[0]);
					var $parent = $(thumb[0]).parent();
					
					var w = Math.ceil( thumb[0].width / thumb[0].height * parseInt($parent.css("height")) );
					var h = Math.ceil( thumb[0].height / thumb[0].width * parseInt($parent.css("width")) );	

					if (w < h) {	
						$thumb.css({ height: 'auto', width: $parent.css("width"), marginTop: -(h-parseInt($parent.css("height")))/2 });
					} else {
						$thumb.css({ width: 'auto', height: $parent.css("height"), marginLeft: -(w-parseInt($parent.css("width")))/2 });
					}	
				}			
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500).wrapAll(document.createElement("div"));
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('div').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
		
		var imgNav = $((document.createElement('p'))).addClass('nav_img');
		imgNav.append($(document.createElement('a')).addClass('prev').attr('href', '#').html('&laquo; Previous Image').click(function(){$.galleria.prev(); return false;}));
			imgNav.append(" | ");
		imgNav.append($(document.createElement('a')).addClass('next').attr('href', '#').html('Next Image &raquo;').click(function(){$.galleria.next(); return false;}));
		$('#main_image').append(imgNav);
		
		$('.nav_page ul li:last').width(82).css('display', 'block');
	}
}