// SLIDESHOW:
// config for slideshow:
// view: "carousel", default fading
// navigation: "withPagination", default no navigation

// needs:
// - /js/jquery/jquery-1.4.2.min.js
// - /js/jquery/jquery.cycle.min.js
// - /js/jquery/jquery.easing.1.2.js
// - /css/slideshow.css

(function($) {
	var initialize = function(){
			$('.slideshow').each(startSlideshow);
		},			
		startSlideshow = function(){
			var $item = $(this)
				$opts = {};
			
			if ($item.hasClass('withPagination') || $item.hasClass('withBubblePagination')) {
                
                // If we have less than 2 Slides, skip the pagination
                if ($item.children('.slide').length < 2) return;
                    
                var classname = $item.hasClass('withBubblePagination') ? 'px-bubbleList' : 'pagination';
                
				// add pagination wrapper
				$item.append('<ul class="' + classname + '"></ul>');
				
				$opts.pager = '.' + classname;
				$opts.pagerAnchorBuilder = function(index, slide) {
			        return '<li>' + $(slide).attr('title') + '</li>'; 
			    };
				$opts.updateActivePagerLink = function(selector, index, activeClass){
					$pagination = $item.find('.' + classname);
			    	$pagination.find('li').removeClass('active');		    	
			    	$($pagination.find('li')[index]).addClass('active');
			    };
				// With this callback we check if the current and next slide element has a data-background attribute
				$opts.before = function(currSlideElement, nextSlideElement, options, forwardFlag) {
					var $currSlideElement = $(currSlideElement),
						$nextSlideElement = $(nextSlideElement),
						$nextNextSlideElement = $(nextSlideElement).next();
					
					var changeDataToBackground = function(element) {
						if (!element) return;
						var elementBackground = element.data('background');
						
						if (elementBackground) {
							// if data-background is set, remove it and set style accordingly
							element.css('backgroundImage', 'url(' + elementBackground + ')');
							element.removeAttr('data-background');
							element.removeData('background');
						}
					};
					
					$currSlideElement && changeDataToBackground($currSlideElement);
					$nextSlideElement && changeDataToBackground($nextSlideElement);
					$nextNextSlideElement && changeDataToBackground($nextNextSlideElement);
				};
				
				// fade in pagination in order to prevent flapping
				$pagination = $item.find('.' + classname).fadeIn('slow');
			}
			
			if($item.hasClass('withPagerButton')){
				// add nav buttons
				$pagerButtons = $item.append('<a class="pagerButton nextSlide">nächstes</a><a class="pagerButton previousSlide">vorheriges</a>');
				
				// fade in pagination in order to prevent flapping
				$pagerButtons = $item.find('.pagerButton').fadeIn('slow');
				
				$opts.next = $('.nextSlide', $item);
				$opts.prev = $('.previousSlide', $item);
			}			
			
			if($item.hasClass('vertScroll')){
				$opts.fx = 	'scrollVert';
			} else if($item.hasClass('horScroll')){
				$opts.fx = 	'scrollHorz';
			} else {
				$opts.fx = 'fade';
			}
			
			if (typeof($item.attr('data-timeout')) != 'undefined') {
			    $opts.timeout = parseInt($item.attr('data-timeout'), 10);
			}
			
			initializeSlideshow($item,$opts);
		},
		
		initializeSlideshow = function(slideshowElement,options){
			var $slideshowElement = $(slideshowElement);
			
			var defaultOptions = {
				fx: 'fade',
				speed: 'slow',
				timeout: 7000,
				slideExpr: '.slide',
				next: null,
				prev: null
			};
			
			var slideshowOptions = $.extend({}, defaultOptions, options);
			var slideshow = $slideshowElement.cycle(slideshowOptions);
			
			$slideshowElement.mouseenter(function(){
				$slideshowElement.cycle('pause');
			});
			$slideshowElement.mouseleave(function(){
				$slideshowElement.cycle('resume');
			});
		};

    $.fn.slideshow = function(options) {
        return this.each(function(index, element) {
            startSlideshow.apply(element);
        });
    };

	$(function(){
		initialize();
	});
})(jQuery);
