// Adding to index to each photo list item
$(function(){ 
	$('#gallery li').each(function(idx) {
        $(this).data('index', (++idx));
    });


	// Initializing jcarousel carousel
	$('#gallery').jcarousel({
		scroll:12,
		visible: 12,
		vertical: false,
        buttonNextHTML: null,
        buttonPrevHTML: null,
		initCallback: initCallbackFunction
    });

	// Function for centering active item
    function initCallbackFunction(carousel) {
		$('#main_image').bind('image-loaded',function() {
            var idx =  $('#gallery li.active').data('index') - 5;
            carousel.scroll(idx);
            return false;
        });
        
    // hotkeys plugin: use arrows to control the gallery
	$(document).bind('keydown', 'right', function (evt){ $.galleria.next(); });
	$(document).bind('keydown', 'left', function (evt){ $.galleria.prev(); });
	  
    };

// Initializing galleria
	$('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
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
				
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
				
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
				
			// add a title for the clickable image
			image.attr('title','Next image >>');
	
			// add a title for the clickable image
			image.attr('alt', thumb.attr('alt'))	
			
			$('.caption').css('display', 'none').css('opacity', '0.6'); 

		//onImage 'image-loaded'
            /*$('.caption').fadeIn(300).animate({opacity:.6},5000).fadeOut(300).stop();*/
			$('#main_image').trigger('image-loaded')
			.hover(
                    function(){ $('.caption').stop().animate({height: 60, opacity: 0.6}, 250) },
                    function(){ $('.caption').stop().animate({height: 0, opacity: 0}, 250) 
                    
                   }
			);
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// 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);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
			
			
		}).find('li:first').addClass('active')
 
 /*
	$('div#controls').css({opacity:0.4}).fadeIn(300);
	$('div.play').css({opacity:0.4}).fadeIn(300);
	$('div.pause').css({opacity:0.4}).fadeIn(300);*/

	var slideshow;
	
    $('a.play').click(function(){
			slideshow = setInterval(function(){
                $.galleria.next()}, 3000);
    })
		

    $('a.pause').click(function(){
            // clear interval when slideshow if disabled
        clearInterval(slideshow);
    })

});
z
