// Favola Basic js

$(document).ready(function() {
	slider();
	overlay();
	gallery();
});

function slider(){
	if($('#slider')){
		// redefine Cycle's updateActivePagerLink function 
		$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
			$(pager).find('li').removeClass('selected').filter('li:eq('+currSlideIndex+')').addClass('selected');
		};
		$('#slidercontent').before('<ul class="slidernav"></ul>');
		$('#slidercontent').cycle({
			fx: 'scrollHorz',
			timeout: 8000, 
			pager: 'ul.slidernav',
			pagerAnchorBuilder: function(idx, slide) {
				return '<li><span></span><h1>' + $(slide).find('a').attr('title') + '</h1><p>' + $(slide).find('a img').attr('alt') + '</p></li>';
			},
			pauseOnPagerHover: true
		});
		$('#slidercontent').hover(function(){
			$(this).cycle('pause')
		},function(){
			$(this).cycle('resume')
		});
		$('#slidercontent figcaption a').click(function(){
			$(this).cycle('stop')
		});
	}
}

function overlay(){
	$('.gallery').each(function() {
		var rel = $(this).attr('id');
		$(this).find('a').each(function() {
			$(this).attr('rel', rel).fancybox({
				'titleShow'		: false,
				'padding'		: 5,
				'overlayOpacity': 0.75,
				'overlayColor'	: '#000',
				'centerOnScroll': true
			});
		});
	});
}

function gallery() {
	var galleryItem = $('#gallery-slider').find('.gallery-item');
	var galleryItemCount = galleryItem.length;
	
	var currentSlideCount = 1;
	var availableSlideCounts = (galleryItemCount > 8) ? galleryItemCount-8 : 1;
	
	$('#gallery-slider').find('.gallery').css({
		'width': (galleryItem.outerWidth()*(galleryItemCount+1)) + 'px',
		'position': 'relative',
		'left': 0
	}).end().find('#gallery-slider-left span').css({'opacity': '0.5', 'cursor': 'default'}).click(function() {
		if (currentSlideCount > 1) {
			$('#gallery-slider').find('.gallery').animate({
				'left': '+=' + (galleryItem.outerWidth()+2) + 'px'
			});
			currentSlideCount -= 1;
		}
	
		if (currentSlideCount == 1) {
			$('#gallery-slider-left span').css({
				'opacity': '0.5',
				'cursor': 'default'
			});
		} else if (currentSlideCount == availableSlideCounts) {
			$('#gallery-slider-right span').css({
				'opacity': '0.5',
				'cursor': 'default'
			});
		} else {
			$('#gallery-slider-left span').css({
				'opacity': '1',
				'cursor': 'pointer'
			});
			$('#gallery-slider-right span').css({
				'opacity': '1',
				'cursor': 'pointer'
			});
		}
	}).end().find('#gallery-slider-right span').click(function() {
		if (currentSlideCount < availableSlideCounts) {
			$('#gallery-slider').find('.gallery').animate({
				'left': '-=' + (galleryItem.outerWidth()+2) + 'px'
			});
			currentSlideCount += 1;
		}
	
		if (currentSlideCount == 1) {
			$('#gallery-slider-left span').css({
				'opacity': '0.5',
				'cursor': 'default'
			});
		} else if (currentSlideCount == availableSlideCounts) {
			$('#gallery-slider-right span').css({
				'opacity': '0.5',
				'cursor': 'default'
			});
		} else {
			$('#gallery-slider-left span').css({
				'opacity': '1',
				'cursor': 'pointer'
			});
			$('#gallery-slider-right span').css({
				'opacity': '1',
				'cursor': 'pointer'
			});
		}
	});
}
