$('document').ready(function(){
	initGallery();
});


// initGallery
function initGallery(){
	var _gallery = $('.gallery-block>.gallery');
	var duration = 6000;
	
	_gallery.each(function(){
		var gallery = $(this);
		var _slides = gallery.find('li');
		var _links = $('.columns-block .column>h3');
		var _int;
		
		var _active = _links.index(_links.filter('.active:eq(0)'));
		if (!_slides.eq(_active).is('.active')) {
			_slides.removeClass('active');
			_slides.eq(_active).addClass('active');
		}
		
		_slides.hide();
		_slides.filter('.active').show();
		
		function rotate(){
			if (_active < _links.length-1) _active++
				else _active = 0;
			_links.filter('.active').removeClass('active');
			_slides.filter('.active').fadeOut().removeClass('active');
			_links.eq(_active).addClass('active');
			_slides.eq(_active).fadeIn().addClass('active');
		}
		
		_links.mouseenter(function(){
			if (_int) clearInterval(_int);
			if (!$(this).is('.active')){
				_links.filter('.active').removeClass('active');
				_slides.filter('.active').fadeOut().removeClass('active');
				$(this).addClass('active');
				_active = _links.index($(this));
				_slides.eq(_active).fadeIn().addClass('active');
			}
		})
		_links.mouseout(function(){
			if (_int) clearInterval(_int);
			_int = setInterval(rotate, duration)
		})
		_int = setInterval(rotate, duration);
		
	});
}
