﻿jQuery(function(){
	 var liczbael;
    var slideshow;
	 var slideshowPause;
        slideshowPause = 6; // sekundy
		  liczbael = jQuery('#rotator2 li').length;
    jQuery('#rotator2 li').each(function(idx) {
        jQuery(this).data('index', (++idx));
    });
    jQuery('#rotator2').jcarousel({
        scroll: 1,
			vertical: true,
        initCallback: initCallbackFunction
    })

    function initCallbackFunction(carousel) {
        jQuery('#img').bind('image-loaded',function() {
            var idx =  jQuery('#rotator2 li.active').data('index') - 2;

            carousel.scroll(idx);
            return false;
        });
    };

    // load and fade-in thumbnails
    jQuery('#rotator2 li img').css('opacity', 0).each(function() {
        if (this.complete || this.readyState == 'complete') {
			jQuery(this).animate({'opacity': 1}, 300);
		  } else {
			jQuery(this).load(function() {
				jQuery(this).animate({'opacity': 1}, 300) });
			}
    });


    jQuery('#rotator2').galleria({
        // #img is the empty div which holds full size images
        insert: '#img',

        // enable history plugin
        history: false,
        // function fired when the image is displayed
        onImage: function(image, caption, thumb) {
            // fade in the image

            image.hide().fadeIn(1000);

            // animate active thumbnail's opacity to 1, other list elements to 0.6
            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6)
            jQuery('#img')
                .trigger('image-loaded')
                .hover(
					      function(){
								jQuery('#img .caption2').stop().animate({height: 50}, 250);
//						 		window.clearInterval(slideshow);
							},
                    function(){
                        if (!jQuery('#show-caption').is(':checked')) {

                            //jQuery('#img .caption').stop().animate({height: 0}, 250)
                        }
                    }
                )
					 .click(
					 		function(){
	                      if (thumb.attr("name").length > 0)
                         {
                               window.location.href = thumb.attr("name");
                         }
							}
					 )
        },
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb) {
            var jQueryli = thumb.parent(),
                opacity = jQueryli.is('.active') ? 1 : 0.6;

            // hover effects for list elements
            jQueryli.hover(
                function() { jQueryli.fadeTo(200, 1);
					 		if(liczbael>1){window.clearInterval(slideshow);}
//						 jQuery('li.active span').css('color', '#FF0000');
					  },
                function() { jQueryli.not('.active').fadeTo(200, opacity);
  				 		 if(liczbael>1){slideshow = window.setInterval(function(){jQuery.galleria.next()}, slideshowPause * 1000)}
					 }
            )
        }
    }).find('li:first').addClass('active') // display first image when Galleria is loaded

    jQuery('#img .caption2').css('height', 0)

    jQuery('#slideshow').hide()

    // this one is for Firefox, which loves to leave fields checked after page refresh
    jQuery('#toggle-slideshow, #show-caption').removeAttr('checked')

	 jQuery('#img .caption2').stop().animate({height: 50}, 250)

    jQuery('#slideshow-pause').change(function(){
		 if(liczbael>1){
         slideshowPause = this.value

        // clear interval when timeout is changed
         window.clearInterval(slideshow)

        // and set new interval with new timeout value
         slideshow = window.setInterval(function(){
             jQuery.galleria.next()
         }, slideshowPause * 1000) // must be set in milisecond
		 }
    })
    jQuery('#slideshow').fadeIn()
           // set interval when slideshow is enabled
   	 if(liczbael>1){
		    slideshow = window.setInterval(function(){
				    jQuery.galleria.next()
		    }, slideshowPause * 1000)
		 }
});

