(function($) {

    $.fn.innerfade = function(options) { return this.each(function() { $.innerfade(this, options); }); };
	//sequence-random
    $.innerfade = function(el, options) {
        var opt = {'animation':'fade', 'speed': 'normal', 'type': 'sequence', 'timeout': 2000, 'children': null };
        if (options) $.extend(opt, options);
        if (opt.children === null) var elements = $(el).children();
        else var elements = $(el).children(opt.children);
        if (elements.length > 1) {
            $(el).css('position', 'relative').css('height', $(elements[0]).height());
            for(var i = 0; i < elements.length; i++) { $(elements[i]).css('z-index', String(elements.length-i)).css('position','absolute').hide(); };
            if(opt.type == "sequence") {
                setTimeout(function() { $.innerfade.next(elements, opt, 1, 0, el); }, opt.timeout);
                $(elements[0]).show();
            } else if(opt.type == "random") {
            	var last = Math.floor(Math.random()*(elements.length));
                setTimeout(function() {
                    do { current = Math.floor(Math.random()*(elements.length)); } while (last == current);             
					$.innerfade.next(elements, opt, current, last, el);
                }, opt.timeout);
                $(elements[last]).show();
			}
		}
    };

    $.innerfade.next = function(elements, opt, current, last, el) {
        if(opt.animation == 'slide') {
            $(elements[last]).slideUp(opt.speed);
            $(elements[current]).slideDown(opt.speed);
        } else if (opt.animation == 'fade') {
			$(el).animate({'height': $(elements[current]).height()}, opt.speed);
            $(elements[last]).fadeOut(opt.speed);
            $(elements[current]).fadeIn(opt.speed, function() { removeFilter($(this)[0]); });
        } 
        if (opt.type == "sequence") {
            if((current+1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length-1;
            }
        } else if(opt.type == "random") {
            last = current;
            while(current == last) current = Math.floor(Math.random() * elements.length);
        }
        setTimeout((function() { $.innerfade.next(elements, opt, current, last, el); }), opt.timeout);
    };

})(jQuery);

function removeFilter(element) { if(element.style.removeAttribute){ element.style.removeAttribute('filter'); } }
