// JavaScript Document
/*
Galleria: http://github.com/aino/galleria/blob/master/docs/options.rst
JCarousel: http://sorgalla.com/projects/jcarousel/
with some code from http://www.leonkessler.com/blog/?p=9 and http://blog.themeforest.net/tutorials/how-to-integrate-the-jquery-galleria-and-jcarousel-plugins/
*/


		jQuery(document).ready(function() {
		
		/*$('#gallery li').each(function(idx) {
        	$(this).data('index', (++idx));
   		 });*/
		
		 	//carousel init
    		jQuery('ul#gallery').jcarousel({
				scroll: 1,
				//visible: 6,
				initCallback: mycarousel_initCallback,
				wrap: 'both',
				buttonNextHTML: null,
				buttonPrevHTML: null
 
    });
});
 
// Now lets call the galleria code
			jQuery(function($) { $('ul#gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#img', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // image effects for the main image are added here

				//$('#img').trigger('image-loaded')
 
				// 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');
			},
			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'); // display first image when Galleria is loaded
	});

		
		//init carousel controls
		function mycarousel_initCallback(carousel) {
		
			// bind "image-loaded" event to the #img container (we trigger it inside onImage function)
    	/*$('#img').bind('image-loaded',function() {

            var idx =  $('#gallery li.active').data('index');
            
            carousel.scroll(idx);
			
            return false;
		
        });*/
			
			//if thumb is clicked, stop autoplay
			carousel.clip.click(function() {
       			//window.clearInterval(slideshow);
				//window.clearInterval(carouselslide);
				//carousel.startAuto(0);
   			 });

		
			$('.jcarousel-control a').bind('click', function() {
				carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				return false;
			});

		
			$('#mycarousel-next').bind('click', function() {
				carousel.next();
				//window.clearInterval(carouselslide);
				//carousel.startAuto(0);
				return false;
			});
		
			$('#mycarousel-prev').bind('click', function() {
				carousel.prev();
				//window.clearInterval(carouselslide);
				//carousel.startAuto(0);
				return false;
			});
			
			
			//autoplay
		/*var slideshow = window.setInterval(function(){
        	$.galleria.next() }, 6000)
		
		var carouselslide = window.setInterval(function(){
			carousel.next()}, 6000)*/
												  
			
		};
