var MultiplexedDiv = Class.create({
    initialize:function(div_id, args) {
        this.div = $(div_id);
        this.items = this.div.select('[class="multiplex"]');
        this.idx = 0;
        this.timerId = 0;
        this.items.each(function(item) {
            item.setStyle({display:'none'});
            });
        if (!args.hide_first)
        {
            this.items[0].setStyle({display:'block'});
        }
        if (args.velocity)
        {
            this.velocity = args.velocity;
        }
        else
        {
            this.velocity = 1000;
        }
        if (args.acceleration)
        {
            this.acceleration = args.acceleration;
        }
        else
        {
            this.acceleration = 0;
        }
        if (args.repeat)
        {
            this.repeat = args.repeat;
        }
        else
        {
            this.repeat = -1;
        }
        if (args.end)
        {
            this.endfn = args.end;
        }
        if (args.scope)
        {
            this.scope = args.scope;
        }
        else
        {
            this.scope = div_id;
        }

        this.id1 = args.id1;
        this.id2 = args.id2;
        this.state = true;
    },

    start:function()
    {
        var _self = this;
        this.timerId = setTimeout(function(ms){
            _self.rotate();
            }, 500);
    },

    rotate:function()
    {
        var _self = this;
        var item;
        var effect_time;

        item = this.items[this.idx];
        effect_time = 1000/this.velocity;

        $(item.id).fade({duration:effect_time, queue:{position:'end', scope:this.scope}});
        this.idx = (this.idx+1)%this.items.length;
        item = this.items[this.idx];
        $(item.id).appear({duration:effect_time, queue:{position:'end', scope:this.scope}});

        // Have an upper cap on velocity = 10000 
        // Values greater than this cause the fade timer to cup
        if (this.velocity + this.acceleration < 10000)
            this.velocity += this.acceleration;
        
        // One loop is complete
        if(this.idx == this.items.length-1 && this.repeat > 0)
        {
            this.repeat--;
        }

        if( this.repeat != 0 )
        {
            this.timerId = setTimeout(function(ms){
                _self.rotate();
                }, 100);
        }
        else
        {
            _self.stop();
        }
    },

    stop:function()
     {
         if(this.timerId > 0)
             clearTimeout(this.timerId);

         if(this.endfn)
             eval(this.endfn);
     },
    
    change:function(e)
    {
        var effect_time = 0.3;
        var elem = e.findElement();

        if (e.type == "mouseover")
        {
            item = this.items[this.id1];
            // The picture must be visible first of course!
            if(item.getStyle('display') == 'none') return;

            var queue = Effect.Queues.get(this.div.id);

            var descendants = item.descendants();
            // Hack to check if the "mouseover" element is a descendant
            if(elem == descendants[0] || elem == item)
            {
                // It's the picture!
                $(item.id).fade({duration:effect_time, queue:{position:'end', scope:this.div.id}});
                item = this.items[this.id2];
                $(item.id).appear({duration:effect_time, queue:{position:'end', scope:this.div.id}});
            }
        }
        else if (e.type == "mouseout")
        {
            item = this.items[this.id2];
            if(item.getStyle('display') == 'none') return;

            var queue = Effect.Queues.get(this.div.id);

            var descendants = item.descendants();
            // Hack to check if the "mouseover" element is a descendant
            if(elem == descendants[0] || elem == item)
            {
                // It's the picture!
                $(item.id).fade({duration:effect_time, queue:{position:'end', scope:this.div.id}});
                item = this.items[this.id1];
                $(item.id).appear({duration:effect_time, queue:{position:'end', scope:this.div.id}});
            }
        }
    }

    });


function waitForEmpty(queue_scope, endfn)
{
    var queue = Effect.Queues.get(queue_scope);
    // Wait till queue is empty
    if (queue.effects.length > 0)
        setTimeout(function(){waitForEmpty(queue_scope,endfn);}, 500);
    else
        eval(endfn);
}


