/*
 * Ротация дочерних div'ов элемента
 * <div class="rotator" fade="1000" delay="3000">...</div>
 * где fade - общее время затухания/появления дочернего элемента, delay - интервал ротации
 * Date: 14.04.2009
 * @author politov@gmail.com
 */
$(document).ready(function(){
    $('div.rotator').each(function(){
        $(this).children('div:first').show();
        if ($(this).children('div').length <= 1) { return; }
        $(this).keypress(); // initialize params, yes this is ugly
        var div = $(this).get(0);
        var delay = div.delay;
        delay = (delay < 500) ? 2000 : delay;
        var effect = div.effect.toLowerCase().replace(/^\s+|\s+$/g,'');
        var fade = div.fade;
        fade = Math.round(fade/2);
        $(this).everyTime(delay, 'rotator', function(i){
            var div1 = $(this).children('div:visible');
            var n = $(this).children('div').size();
            var i = Math.round(Math.random() * (n-1));
            var div2 = $(this).children('div:eq('+i+')');
            if (effect!='' && (effect in $.effects)) {
                div1.effect(effect, {mode:'hide'}, fade);
                div2.oneTime(fade, function(){
                    $(this).effect(effect, {mode:'show'}, fade);
                });
            } else {
                div1.fadeOut(fade);
                div2.oneTime(fade, function(){
                    $(this).fadeIn(fade);
                });
            }
        });
    });
});