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

// jquery.innerfademod.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({ 
 *    animation type: 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'), 
 *      container height: Height of the containing element in any css-height-value (Default: 'auto'),
 *    running class: CSS-Class which the container get’s applied (Default: 'inner fade'),
 *    children: optional children selector (Default: null)
 *  }); 
 *

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


(function($) {

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

    $.innerfademod = function(container, options) {
        var settings = {
            'speed':            350,
            'timeout':          350,
            'containerheight':  'auto',
            'runningclass':     'innerfademod',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css({
                    'position': 'relative',
                    'height': settings.containerheight
                }).addClass(settings.runningclass);
            
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };

            setTimeout(function() {
                $.innerfademod.next(elements, settings, 1, 0);
            }, settings.timeout);
            
            $(elements[0]).show();
        }
    };

    $.innerfademod.next = function(elements, settings, current, last) {
        if (current + 1 == elements.length) {
            var speed = settings.speed;
        } else {
            var speed = settings.speed - ( settings.speed * (current / elements.length) );
        }
        
        $(elements[last]).fadeOut(speed);
        $(elements[current]).fadeIn(speed, function() {
            removeFilter($(this)[0]);
        });
        
        if ((current + 1) < elements.length) {
            current = current + 1;
            last = current - 1;
        } else {
            current = 0;
            last = elements.length - 1;
        }
        
        //http://www.wolframalpha.com/input/?i=y+%3D+1000+-+%281000+*+%28x%2F23%29%29
        var timeout = settings.timeout - ( settings.timeout * (current / elements.length) ) + 50;
        
        //console.log('timeout: ', timeout);
        //console.log('current: ', current);
        //console.log('elements.length: ', elements.length);
        
        if (current != 0) {
            setTimeout((function() {
                $.innerfademod.next(elements, settings, current, last);
            }), timeout);
        }
    };

})(jQuery);

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

