/*
 * jQuery teaserSlide
 * Copyright 2011 Martin Modler
 * Version 0.4
 */

(function($) {

	$.fn.teaserSlide = function(settings) {

		var config = {
			speed:				4000,
			fadeSpeed:			2000,
			innerSelector:		'div',
			teaserSlideHeight:	'469px',
			teaserSlideSpecial:	'default'
		};

		if (settings) $.extend(config, settings);

		if (config.teaserslideSpecial == 'home') {
			$(config.innerSelector, this).shuffle();
		}

		var teaserDivs = $(config.innerSelector, this);
		var numberOfElements = teaserDivs.length;
		
		if (numberOfElements > 1) {

			// container css
			$(this).css('position', 'relative').css('height', config.teaserSlideHeight);

			// hide all exept first
			for(i=0; i < numberOfElements; i++){
				teaserDiv = teaserDivs.eq(i);
				$(teaserDiv).css('z-index', String(numberOfElements-i)).css('position', 'absolute');
				if (i == 0) $(teaserDiv).show();
				if (i > 0) $(teaserDiv).hide();
			}

			var teaserSlideActive = 0;
			var teaserSlideHide = 0;

			setInterval(function() {

				if (teaserSlideActive < numberOfElements - 1) teaserSlideActive++;
				else teaserSlideActive = 0;

				if (teaserSlideActive > 0) teaserSlideHide = teaserSlideActive - 1;
				else teaserSlideHide = numberOfElements - 1;

				$(teaserDivs.eq(teaserSlideHide)).fadeOut(config.fadeSpeed);
				$(teaserDivs.eq(teaserSlideActive)).fadeIn(config.fadeSpeed);

			}, config.speed);
		}
 
	};

 })(jQuery);

(function(d){d.fn.shuffle=function(c){c=[];return this.each(function(){c.push($(this).clone(true))}).each(function(a,b){d(b).replaceWith(c[a=Math.floor(Math.random()*c.length)]);c.splice(a,1)})};d.shuffle=function(a){return d(a).shuffle()}})(jQuery);

