$(document).ready(function() {

    //Product gallery
    $('.product-gallery-image:gt(0)').hide();

	$('#product-gallery-next').click(function(){
		var img = $('.product-gallery-image:visible');
		var img_index = img.index();
		var img_index_next = img.next().index();
		if (img_index_next != -1) {
			$(img).next('.product-gallery-image').fadeIn('slow');
			$(img).hide();
		}

	});

	$('#product-gallery-prev').click(function(){
		var img = $('.product-gallery-image:visible');
		var img_index = img.index();
		var img_index_prev = img.prev().index();
		if (img_index_prev != -1) {
			$(img).prev('.product-gallery-image').fadeIn('slow');
			$(img).hide();
		}

	});

	//Slideshow
	$('.slideshow-gallery').each(function(i){
		//Add unique class to this slideshow set (ul)
		var slideshow = 'slideshow-'+ (i+1);
		var slideshow_class = '.' + slideshow;
		$(this).addClass(slideshow);
		
		//Hide all li's but the first for this set
		$(slideshow_class + ' li:gt(0)').hide();
		
		//Rotate through this slideshow's li's
		var slideshow_img = $('.autoplay ' + slideshow_class + ' li');
		setInterval(function(){
			$(slideshow_img).filter(':visible').fadeOut(1000,function(){
				if ($(this).next(slideshow_img).size()) {$(this).next().fadeIn(1000);}
				else {$(slideshow_img).eq(0).fadeIn(1000);}
			});
		}, 5000);

	});
	
	//Slideshow - click to play
	$('.slideshow-play').click(function(){
		$(this).fadeOut();
		$(this).parent('.slideshow-container').removeClass('click2play').addClass('autoplay');
		
		var slideshow_img = $(this).closest('.slideshow-container').find('.slideshow-gallery li');
		setInterval(function(){
			$(slideshow_img).filter(':visible').fadeOut(1000,function(){
				if ($(this).next(slideshow_img).size()) {$(this).next().fadeIn(1000);}
				else {$(slideshow_img).eq(0).fadeIn(1000);}
			});
		}, 5000);
	});
	
	//Rotating testimonials
	$('#home-testimonials p:gt(0)').hide();

	var testimonial = $('#home-testimonials p');
	setInterval(function(){
		$(testimonial).filter(':visible').fadeOut(1000,function(){
			if($(this).next(testimonial).size()){$(this).next().fadeIn(1000);}
			else{$(testimonial).eq(0).fadeIn(1000);}
		});
	}, 10000);	

	//Product gallery (diagrams) lightbox
	$('#gallery-table a').lightBox({
		imageLoading: 	'/scripts/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 	'/scripts/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 	'/scripts/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 	'/scripts/lightbox/lightbox-btn-next.gif',
		imageBlank: 	'/scripts/lightbox/lightbox-blank.gif',
		fixedNavigation:true
	});

});
