$(document).ready(function() {	
	if (document.getElementById('gallery')) {
		$('#gallery li:first').attr('class','active');
		$('#gallery ul').galleria({
			history   : false,				// activates the history object for bookmarking, back-button etc.
			clickNext : true, 				// helper for making the image clickable
			insert    : '#hauptansicht',			// the containing selector for our main image
			onImage   : function(image,caption,thumb) {	// let's add some image effects for demonstration purposes
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(500);
				}
				caption.css('display','none').fadeIn(500);						// fade in the image &amp; caption
				var _li = thumb.parents('li');									// fetch the thumbnail container
				_li.siblings().children('img.selected').fadeTo(500,0.3);		// fade out inactive thumbnail
				thumb.fadeTo('fast',1).addClass('selected');					// fade in active thumbnail
				image.attr('title',thumb.attr('title'));						// add a title for the clickable image
			},
			onThumb : function(thumb) { 										// thumbnail effects goes here
				var _li = thumb.parents('li');									// fetch the thumbnail container
				var _fadeTo = _li.is('.active') ? '1' : '0.3';					// if thumbnail is active, fade all the way.
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(500);		// fade in the thumbnail when finnished loading
				thumb.hover(													// hover effects
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});  /**/
		$(document).keydown(function(e){
			if (e.keyCode == 37) { 
			   $('.prev').click();
			   return false;
			}
			else if (e.keyCode == 39) { 
			   $('.next').click();
			   return false;
			}
		});
		$('#gallery').css({overflow: 'hidden'});			//Remove scrollbars	
		var lastLi = $('#galerie').find('ul:last-child');	//Find last image container
		var left =0;
		$('#gallery').mousemove(function(e){				//When user move mouse over menu
			var left = (e.pageX - $('#gallery').offset().left) * ($('#gallery ul').width()+30-$('#gallery').width()) / $('#gallery').width();
			$('#gallery').scrollLeft(left);
		});
	}
});
