/* =========================================================
//
// jquery.innerfadex.js
//
// Date: 2008-06-03
// Author: ISOLA Communications Co.,Ltd.
// Mail: support@isola.co.jp
// Web: http://www.isola.co.jp
// based on the work of Torsten Baldes http://medienfreunde.com
// ========================================================= */
(function($) {
	
	var status = "start";  // start:stop

	var currentId = null;
	var elements;

	$.fn.clearfade = function() {
        return this.each(function() {
            $.clearfade();
        });
	};

	$.clearfade = function() {
		status = "stop";
		debug("clearfade start");
		if (currentId)
			clearTimeout(currentId);
		clearAll();
		debug("clearfade end");
		return this;
	}

	function clearAll() {
		if (elements) {
	        for (var i = 0; i < elements.length; i++) {
    	        $(elements[i]).hide();
        	};
		}
	}

	$.fn.showTarget = function (startid) {
        return this.each(function() {
            $.showTarget(startid);
        });
	}

	$.showTarget = function(startid) {
		clearAll();
		debug("showtarget start");
		if (elements) {
			debug("show");
			$(elements[startid]).show();
		}
		debug("showtarget end");
		return this;
	}

    $.fn.innerfadex = function(options) {
        return this.each(function() {
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
		status = "start";
        var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null,
            'startelement':     null
        };

		debug("innerfade start");
		
		$.innerfade.init(container, settings, options);

		if (elements.length > 1) {
			if (settings.type == "sequence") {
				if (settings.startelement) {
		        	currentId = setTimeout(function() {
		                $.innerfade.next(elements, settings, settings.startelement, settings.startelement-1);
		            }, settings.timeout);
		            $(elements[settings.startelement]).show();
				} else {
		        	currentId = setTimeout(function() {
		                $.innerfade.next(elements, settings, 1, 0);
	    	        }, settings.timeout);
	                $(elements[0]).show();
				}
			} else if (settings.type == "random") {
				if (settings.startelement) {
					var last = settings.startelement;
	            	currentId = setTimeout(function() {
	                    do { 
							current = Math.floor ( Math.random ( ) * ( elements.length ) );
						} while (last == current );
						$.innerfade.next(elements, settings, current, last);
            	    }, settings.timeout);
	                $(elements[last]).show();
				} else {
					var last = Math.floor ( Math.random () * ( elements.length ) );
	            	currentId = setTimeout(function() {
	                    do { 
							current = Math.floor ( Math.random ( ) * ( elements.length ) );
						} while (last == current );
						$.innerfade.next(elements, settings, current, last);
            	    }, settings.timeout);
    	            $(elements[last]).show();
				}
			} else if ( settings.type == 'random_start' ) {
				settings.type = 'sequence';

				var current = Math.floor ( Math.random () * ( elements.length ) );
				if (settings.startelement) {
					currentId = setTimeout(function(){
						$.innerfade.next(elements, settings, (current + 1) %  elements.length, settings.startelement);
					}, settings.timeout);
					$(elements[settings.startelement]).show();
				} else {
					currentId = setTimeout(function(){
						$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
					}, settings.timeout);
					$(elements[current]).show();
				}
			}
    	}
		debug("innerfade end");
    };

	$.innerfade.init = function(container, settings, options) {
		

        if (options) {
            $.extend(settings, options);
		}

        if (settings.children === null) {
            elements = $(container).children();
		} else {
            elements = $(container).children(settings.children);
		}

        if (elements.length > 1) {

			$(container)
				.css('position','relative')
				.css('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();
				debug("hide:" + i);
	        };
		}

	}



    $.innerfade.next = function(_elements, settings, current, last) {
		if (settings.animationtype == 'slide') {
            $(_elements[current]).slideDown(
				settings.speed
				,function () {
					if (status == 'stop')
						$(this).hide();
				}
			);
            $(_elements[last]).slideUp(
				settings.speed
				,function () {
					if (status == 'stop')
						$(this).hide();
				}
			);
		} else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
					removeFilter($(this)[0]);
			});
		}

		if (settings.type == "sequence") {
            if ((current + 1) < _elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = _elements.length - 1;
            }
		} else if (settings.type == "random") {
            last = current;
            while (current == last)
        	    current = Math.floor(Math.random() * elements.length);
        } else {
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
		}

		debug(elements);
        currentId = setTimeout((function() {
            $.innerfade.next(_elements, settings, current, last);
        }), settings.timeout);
		debug(currentId);
    };


  function debug(message) {
	  if(window.console) {
    		//console.debug(message);
	  }
  }

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}