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

// jquery.innerfade.js

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

// 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($) {

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

    $.innerfade = function(container, options) {
        var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'next',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null,
			'output_title':     null,
			'width':			400,
			'height':			300,
			'next':				null,
			'prev':				null
        };
        if (options) $.extend(settings, options);
        var elements =  (settings.children === null) ? $(container).children() : $(container).children(settings.children);
		
		if(settings.next !== null){
			$(settings.next).click(function() {
				$.innerfade.value.settings.type = 'next';	
				$.innerfade.move(true);
				return false;
			});
		}
		
		if(settings.prev !== null){
			$(settings.prev).click(function() {
				$.innerfade.value.settings.type = 'prev';	
				$.innerfade.move(true);
				return false;
			});
		}
		

		$.innerfade.value.elements = elements;
		$.innerfade.value.settings = settings;
		$.innerfade.value.last = 1;
		$.innerfade.value.current = 0;
		
        if (elements.length > 1) {
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('visibility', 'visible').hide();
				$(elements[i]).find('img').height(settings.height);
				$(elements[i]).find('img').width(settings.width);
            };
			$.innerfade.value.interval = setInterval(function() {$.innerfade.move(false);}, settings.timeout);
            $(elements[0]).show();
				
			if (settings.output_title !== null){
				$(settings.output_title).html($(elements[0]).find('img').attr('title'));	
			}	

		}
    };

    $.innerfade.move = function(fast) {
		var last = $.innerfade.value.last;
		var current = $.innerfade.value.current;
		var settings = $.innerfade.value.settings;
		var elements = $.innerfade.value.elements;
		var interval = $.innerfade.value.interval;
		
		if(interval !== null){
			clearInterval(interval);
			$.innerfade.value.interval = null;	
		}
		
		
        if (settings.type == "next") {
            if ((current + 1) < elements.length) {
				last = current;
                current = current + 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        }
		else{
			var first = current;
            if ((current - 1) < 0) {
				last = current;
                current = elements.length - 1;
            } else {
				last = current;
                current = current - 1;
            }		
		}
		
		if (settings.animationtype == 'fade') {
			var speed = fast ? 0 : settings.speed;
            $(elements[last]).fadeOut(speed);
			if (settings.output_title !== null){
				$(settings.output_title).html($(elements[current]).find('img').attr('title'));	
			}	

            $(elements[current]).fadeIn(speed, function() {
				removeFilter($(this)[0]);
			});
        } 
		

		$.innerfade.value.current = current;
		$.innerfade.value.last = last;
		$.innerfade.value.interval = setInterval((function() {$.innerfade.move(false);}), settings.timeout);
    };
	
	$.innerfade.value = {
		'settings':null,
		'current':null,
		'last':null,
		'elements':null,
		'interval':null
	};

})(jQuery);

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