$(function()
{	
	var slideshow 	= '';
	var gallery 	= false;
 		
	$('#gallery').galleria({
				
	        insert: '#img',
	        
	        history: false,

	
	        onImage: function(image, caption, thumb) {        
	        	
	            image.hide().fadeIn(500);
	            
	            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.2);
	            
	            $('#img').trigger('image-loaded');
	        },
	        
	        onThumb: function(thumb)
			{
				
	            var $li = thumb.parent(),
	                opacity = $li.is('.active') ? 1 : 0.2;
	            
	            $li.hover(
	                function() { $li.fadeTo(200, 1); },
	                function() { $li.not('.active').fadeTo(200, opacity); }
	            )
	        }        
    } ).find( 'li:first' ).addClass( 'active' );
		    
    function startFotoSlide()
	{
    	slideshow = window.setInterval(function(){
    		$.galleria.next();
		}, 2 * 1000);
		
		$( '#play' ).html( 'Pauze' ).attr( 'id', 'pauze');
		
		return false;
	}

	function stopFotoSlide()
	{
		window.clearInterval(slideshow);
		
		$( '#pauze' ).html( 'Start diashow' ).attr( 'id', 'play');
		
		return false;
	}
	
	$( '#play' ).toggle( function()
	{
		var id = $( this ).attr( 'id' );

		if( id == 'play' )
		{
			startFotoSlide();
		}
		else
		{
			stopFotoSlide();
		}
		return false;
		
	}, function()
	{
		var id = $( this ).attr( 'id' );
		
		if( id == 'play' )
		{
			startFotoSlide();
		}
		else
		{
			stopFotoSlide();
		}
		
		return false;
	});
	
	$( '#next' ).click( function()
	{
		stopFotoSlide();
		
		$.galleria.next();
		
		return false;
	});
	
	$( '.jcarousel-item' ).click( function()
	{
		stopFotoSlide();
		
		return false;
	});
	
	$( '#img' ).click( function()
	{
		stopFotoSlide();
		
		return false;
	});
	
	$( '.galleria_wrapper' ).click( function()
	{
		stopFotoSlide();
		
		return false;
	});
	
	$( '#prev' ).click( function()
	{
		stopFotoSlide();
		
		$.galleria.prev();
		
		return false;
	});
    
    $('#gallery li img').css('opacity', 0).each(function(){    
        if (this.complete || this.readyState == 'complete') { $(this).animate({'opacity': 1}, 300) } 
        else { $(this).load(function() { $(this).animate({'opacity': 1}, 300) }); }
    });	 
		 
	$( '#img' ).click( function(){
    	
		$('.jcarousel-next-horizontal').click();
		
 	});
	 
	$( '#footer' ).click( function()
 	{
 		$('.jcarousel-prev-horizontal').click();
 	}); 

	
    $('#gallery').jcarousel({
        scroll: 1,
        initCallback: initCallbackFunction
    });

    function initCallbackFunction(carousel) {
        $('#img').bind('image-loaded',function()
		{
            var idx =  parseInt( $('#gallery li.active img').attr('id').replace(/n/,"") ) - 2;
	
            carousel.scroll(idx);
            return false;
        });
    };		    
});