jQuery(document).ready(function ($) {
	// enable toggle of client votes
	var toggleClientVotes = function () {
		if (!!toggleClientVotes.timer) {
			window.clearTimeout(toggleClientVotes.timer);
		}
	
		var current = $('#client_votes .client_vote:visible');
		
		current.fadeOut('fast', function () {
			if (current.next('.client_vote').length > 0) {
				current.next('.client_vote').fadeIn('fast');
			} else {
				current.siblings('.client_vote').first().fadeIn('fast');
			}
		});
		
		toggleClientVotes.timer = window.setTimeout(toggleClientVotes, 10000);
	}
	
	$('#client_votes .client_vote').first('.client_vote').show();
	toggleClientVotes.timer = window.setTimeout(toggleClientVotes, 10000);
	
	
	// enable image gallery on homepage
	$('.sabio_gallery').SabioGallery({
		itemSelector: 'img'
	});
	
	// enable image gallery in teasers
	$('.teaser_gallery').SabioGallery({
		itemSelector: 'img',
		interval: 3000
	});
	
	// enable image gallery without controls
	$('.slideshow_gallery').SabioGallery({
		itemSelector: 'img',
		interval: 2000,
		fading: 500,
		showControls: false
	});
	
	
	// create slider gallery
	var $container = $('#content-slider div.scroll-container').css('position', 'relative');
	
	if ($container.length > 0) {
		// store required dom elements
		var $panels = $container.children('div.panel');
		var $scroll =  $container.parent('div.scroll');
		
		//buttons text
		var texts = {
			de: {
				'next': 'Weiter',
				'back': 'Zurück'
			},
			en: {
				'next': 'Next',
				'back': 'Back'
			}
		};
		
		var lang = 'de';
		if($('html').hasClass('en')) {
			lang = 'en';
		}
		
		var getText = function(id) {
			return texts[lang][id];
		};
		
		// set active panel
		var activePanel = 0;
		var setButtonVisibility = function (activePanel) {
			var $btnLeft = $scroll.parent().find('a.scroll-button.left');
			var $btnRight = $scroll.parent().find('a.scroll-button.right');
			
			if (activePanel === 0) {
				$btnLeft.hide();
				$btnRight.show();
			} else if (activePanel === ($panels.length - 1)) {
				$btnLeft.show();
				$btnRight.hide();
			} else {
				$btnLeft.show();
				$btnRight.show();
			}
		};
		
		// calculate container width
		var totalPanelWidth = 0;
		
		$panels.each(function () {
			totalPanelWidth += $(this).outerWidth();
		});
		$container.width(totalPanelWidth);
		
		// append scroll buttons		
		
		$scroll
			.after('<a href="#" class="scroll-button left">' + getText('back') + '</a>')
			.after('<a href="#" class="scroll-button right">' + getText('next') + '</a>');
		
		setButtonVisibility(activePanel);

		// make buttons clickable
		$scroll.parent().find('a.scroll-button, a.link-next').click(function (evt) {
			var btn = $(this);
			
			if (btn.is('.left')) {
				activePanel = ((activePanel > 0 ? activePanel : $panels.length) - 1);
			} else {
				activePanel = (activePanel < ($panels.length - 1)) ? (activePanel + 1) : 0;
			}
			
			$container.animate({
				'left': $panels.eq(activePanel).position().left * -1
			});
			
			setButtonVisibility(activePanel);
			
			evt.preventDefault();
			return false;
		});
		
		// show content
		$('#content-slider')
			.css('display', 'none')
			.css('visibility', 'visible')
			.show(); 
	};
	// end of slider gallery
	
	
	// enable fancybox
	$("a.wpGallery").fancybox({
		'showNavArrows' : false,
		'titlePosition' : 'inside'
	});
});

