$(document).ready(function() {
   
   if($('#gallery').length) initGallery();
   
});


function initGallery(){
	
//	$('#legende').hide();
//	
//	if(!$('#legende').is(':empty')){
//		$('#imgFull').bind('mouseenter', function(){ $('#legende').fadeIn();} );	
//		$('#imgFull').bind('mouseleave', function(){ $('#legende').fadeOut();} );	
//	}
	
	
	$(window).bind('resize', checkPosition);
	
	$('#navBack, #navNext').bind('mouseover', hoverNav);
	$('#navBack, #navNext').bind('mouseout', normalNav);
	
	$('#navNext').bind('click', scrollNext);
	$('#navBack').bind('click', scrollBack);
	
		
	var pages = Math.ceil($('#thumbWrapper img').length/6);
	var currentPage = 1;
	
			
	function hoverNav(){
		$(this).find('img').attr('src', $(this).find('img').attr('src').replace('_n', '_o'));
	}
	
	function normalNav(){
		$(this).find('img').attr('src', $(this).find('img').attr('src').replace('_o', '_n'));
	}
	
	function scrollNext(){
		if(currentPage < pages){
			currentPage++;
			$('#thumbWrapper').animate({ marginLeft: -(currentPage-1)*$('#thumbsList').width() }, 500 );
		}
	}
	
	function scrollBack(){
		if(currentPage > 1){
			currentPage--;
			$('#thumbWrapper').animate({ marginLeft: -(currentPage-1)*$('#thumbsList').width() }, 500 );
					
		}
	}	
	
	function checkPosition(){
		$('#thumbWrapper').css('marginLeft', -(currentPage-1)*$('#thumbsList').width());
	}
	
	
	$('#thumbWrapper img').css('opacity', 0.5);
	
	var firstThumb = $('#thumbWrapper a')[0];
	$(firstThumb).addClass('current');
	$(firstThumb).find('img').css('opacity', 1);
	
	$('#thumbWrapper a').bind('mouseover', onOver);
	$('#thumbWrapper a').bind('mouseout', onOut);
	$('#thumbWrapper a').bind('click', loadImg);
	
	function onOver(){
		 $(this).find('img').animate({opacity:1}, 300);
	}
	
	function onOut(){
		if(!$(this).hasClass('current'))
		 	$(this).find('img').animate({opacity:0.5}, 300);
	}
	
	function loadImg(){
		var newImg = $(this).attr('href');
		$('#legende').html($(this).attr('title'))
		
		$('#thumbWrapper a').each( function() {
			$(this).removeClass('current');
		})
		
		
		$(this).addClass('current');
		
		$('#thumbWrapper a').each( function() {
			if(!$(this).hasClass('current'))
			 	$(this).find('img').animate({opacity:0.5}, 300);
		})		
		
//		if($(this).attr('title') != ''){
//			$('#imgFull').bind('mouseenter', function(){ $('#legende').fadeIn();} );	
//			$('#imgFull').bind('mouseleave', function(){ $('#legende').fadeOut();} );	
//		} else {
//			$('#imgFull').unbind('mouseenter');			
//		}
		
		$(function () {
		        var newimg = new Image();
		        $(newimg).load(function () {
		            $(this).hide();
		            $('#imgFull').removeClass('loading').append(this);
		            $(this).fadeIn(600,function() {
		            	 $('#imgFull img').each(function(index) { 
		            	 	if(index <  $('#imgFull img').length && $('#imgFull img').length > 1)
			            	 	$(this).remove();
		            	 })
		            });
		        }).error(function () {
		            // notify the user that the image could not be loaded
		        }).attr('src', newImg);
		    });		

		return false;
	}
	
	
	
	
}
