/* =========================================================

// jquery.innerfade.js

// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/

 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').innerfade({ 
 *	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
 *	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
 *	  timeout: Time between the fades in milliseconds (Default: '2000'), 
 *	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
 * 		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
 *	  children: optional children selector (Default: null)
 *  }); 
 *

// ========================================================= */


 (function($) {
	
	$("#photos-description").html($("#photos > li > p").html());

    $.fn.innerfade = function(options) {
        return this.each(function() {
            $.innerfade(this, options);
        });
    };

    $.fn.getinnerfade = function() {
        if (this.length > 0) {
            return $(this[0]).data('innerfade');
        } else {
            return null;
        }
    }

    $.innerfade = function(container, options) {
        var base = this;
        
		base.init = function(){
			$(container).data('innerfade', base);
	        base.settings = {
	            'animationtype': 'fade',
	            'speed': 'normal',
	            'type': 'sequence',
	            'timeout': 3000,
	            'containerheight': 'auto',
	            'runningclass': 'innerfade',
	            'children': null
	        };
	        base.timer = null;
			base.playing = true;

	        if (options)
	        $.extend(base.settings, options);

	        if (base.settings.children === null)
	        	base.$elements = $(container).children();
	        else
	        	base.$elements = $(container).children(base.settings.children);

	        if (base.$elements.length > 1) {
	            $(container).css('position', 'relative').css('height', base.settings.containerheight).addClass(base.settings.runningclass);
	            for (var i = 0; i < base.$elements.length; i++) {
	                $(base.$elements[i]).css('z-index', String(base.$elements.length - i)).css('position', 'absolute').hide();
	            };
	            if (base.settings.type == "sequence") {
					base.last = 0;
					base.current = 0;
	                base.play();
	                $(base.$elements[0]).show();
	            } else if (base.settings.type == "random") {
	                var last = Math.floor(Math.random() * (base.$elements.length));
	                setTimeout(function() {
	                    do {
	                        current = Math.floor(Math.random() * (base.$elements.length));
	                    }
	                    while (last == current);
	                    $.innerfade.next(base.$elements, base.settings, current, last);
	                },
	                base.settings.timeout);
	                $(base.$elements[last]).show();
	            } else if (base.settings.type == 'random_start') {
	                base.settings.type = 'sequence';
	                var current = Math.floor(Math.random() * (base.$elements.length));
	                setTimeout(function() {
	                    $.innerfade.next(base.$elements, base.settings, (current + 1) % base.$elements.length, current);
	                },
	                base.settings.timeout);
	                $(base.$elements[current]).show();
	            } else {
	                alert("Innerfade-Type must either be 'sequence ', 'random ' or 'random_start '");
	            }
	        }
		}

		base.stop = function(){
			base.clearTimer();
			base.playing = false;
		}
		
		base.play = function(){
			base.playing = true;
			base.timer = setTimeout((function() {
	            base.next();
	        }), base.settings.timeout);
		}
		
		base.clearTimer = function(){
			if(base.timer){
				window.clearInterval(base.timer);
			}
		}

		base.prev = function() {
			base.animate("previous");
	    };

		base.next = function() {
			base.animate();
	    };
	
		base.animate = function(direction){
			if(direction != "previous") direction = "next";
			
			base.clearTimer();

			var current = base.current;
			var last = base.last;
			
			if (base.settings.type == "sequence") {
	            if(direction == "next"){
					if ((current + 1) < base.$elements.length) {
						last = current;                
						current = current + 1;
		                
		            } else {
		                current = 0;
		                last = base.$elements.length - 1;
		            }
				} else {
					if ((current - 1) > -1) {
						last = current
		                current = current - 1;
		            } else {
		                current = base.$elements.length - 1;
		                last = 0;
		            }
				}
	        }
			base.current = current;
			base.last = last;
			
			
			

	        if (base.settings.animationtype == 'slide') {
	            $(base.$elements[last]).slideUp(base.settings.speed);
	            $(base.$elements[current]).slideDown(base.settings.speed);
	        } else if (base.settings.animationtype == 'fade') {
				if(current !== last){
	            $(base.$elements[last]).filter(":visible").fadeOut(base.settings.speed);
	            $(base.$elements[current]).fadeIn(base.settings.speed,
	            function() {
	                removeFilter($(this)[0]);
	            });
				$("#photos-description").html($(base.$elements[current]).find('p').html());
				}
	        } else
	        alert("Innerfade-animationtype must either be 'slide ' or 'fade '");
	
	        
	        if(base.playing) base.play();
			
		}
	
		base.init();
    };


    

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
    if (element.style.removeAttribute) {
        element.style.removeAttribute('filter');
    }
}

