//------------------------------------------------------------------------------
var Menu = new Class({
    initialize:function(submenus)
    {
        this.submenus = submenus || new Array();
    },

    activate:function(id)
    {
        var e;

        for (var i = 0; i < this.submenus.length; ++i)
        {
            if ((e = $(this.submenus[i])) && e.style)
            {
                var p = $(e.parentNode);
                var left = p.getLeft();
                var top = p.getTop() + p.offsetHeight;

                e.style.left = left + 'px';
                e.style.top = top + 'px';
                e.style.visibility = (e.id == id ? 'visible' : 'hidden');
            }
        }
    }
});


//------------------------------------------------------------------------------
var SlideShow = new Class({
    initialize:function(root, frame_class, duration, interval, fps)
    {
        this.root = $(root);
        this.frames = this.root.getElements('.' + frame_class);
        this.duration = duration;
        this.interval = interval;
        this.fps = fps;

        this.chain_items = null;  // defer creation to .create_chain_items()
        this.chain = new Chain();

        this.create_chain_items();
    },

    create_chain_items:function()
    {
        this.chain_items = new Array();

        this.frames.each(this.create_effect, this);
    },

    create_effect:function(frame, index)
    {
        // alternative to using bind
        var duration = this.duration;
        var interval = this.interval;
        var root = this.root;

        var on_start = function()
        {
            var pos = root.getPosition();

            with (frame.style)
            {
                position = 'absolute';
                top = pos.y + 'px';
                left = pos.x + 'px';
                zIndex = index + 1;
            }
        }

        var fadein = new Fx.Style(frame, 'opacity',
            {duration:this.duration, onStart:on_start, fps:this.fps});

        var fadeout = new Fx.Style(frame, 'opacity',
            {duration:this.duration, fps:this.fps});

        this.chain_items.push(function() {  fadein.start(0, 1) });
        this.chain_items.push(function() {  fadein.set(1) });
        this.chain_items.push(function() { fadeout.start(1, 0) });
    },

    fill_chain:function()
    {
        for (var i = 0; i < this.chain_items.length; ++i)
            this.chain.chain(this.chain_items[i]);
    },

    run:function()
    {
        // this.chain.chains is undefined before first call, so check it first
        if (!this.chain.chains || !this.chain.chains.length)
            this.fill_chain();

        this.chain.callChain();
    },

    start:function()
    {
        this.run.periodical(this.interval, this);
    }
});


//------------------------------------------------------------------------------
function add_scrollers(root_class, target_class, trigger_class, fps)
{
    if (!fps)
        fps = 25;

    $$('.' + root_class).each(
        function(root)
        {
            var targets = root.getElements('.' + target_class);
            var triggers = root.getElements('.' + trigger_class);
            var scroller = new Fx.Scroll(root, {fps:fps});

            targets.each(
                function(target)
                {
                    var valid_triggers = triggers.filter(
                        function(trigger)
                        {
                            return trigger.getProperty('href') ==
                                '#' + target.id
                        }
                    );

                    valid_triggers.each(
                        function(trigger)
                        {
                            trigger.addEvent('click', function() {
                                scroller.toElement(target);
                            });

                            // WORKAROUND firefox: remove flickering
                            trigger.setProperty('href', 'javascript:void(0)');
                        }
                    );
                }
            );
        }
    );
}


//------------------------------------------------------------------------------
function create_menu(submenus)
{
    return new Menu(submenus);
}

