window.addEvent('domready', function()
{
    //new SuperScroller(document.id('content'));

    var appointment_list = document.id('sidebar-appointment-list');

    if (appointment_list != null)
    {
        new CollapsibleAppointments(appointment_list.getElements('li'));
    }

    var nav_lis = document.id('navigation').getElements('li.level-1');
    nav_lis.each(function(item)
    {
        item.addEvents({
            'mouseenter': function()
            {
                this.addClass('sfhover');
            },
            'mouseleave': function()
            {
                this.removeClass('sfhover');
            }
        });
    });

    init_sidebar_image_cycle();
});


var CollapsibleAppointments = new Class(
{
    initialize: function(elms)
    {
        if (elms.length <= 0)
        {
            return;
        }

        elms.each(function(item, index)
        {
            item.getElement('.excerpt').slide('hide');
            item.addClass('closed');

            var toggle_link = item.getElement('h2.title');
            toggle_link.addEvent('click', this.toggle_excerpt.bind(this));
            toggle_link.setStyles({'outline': 'none', '-moz-outline-style': 'none', 'cursor': 'pointer'});
        }.bind(this));
    },

    toggle_excerpt: function(evt)
    {
        evt.stop();
        var container_li = evt.target.getParent('li')
        var excerpt = container_li.getElement('.excerpt');
        excerpt.slide('toggle');
        container_li.toggleClass('closed');
        container_li.toggleClass('open');
    }
});


var SuperScroller = new Class(
{
    initialize: function(elm)
    {
        this.elm = elm;
        if (this.elm.getScrollSize().y <= this.elm.getSize().y)
        {
            return;
        }

        this.scrolling_interval = 50;
        this.scrolling_interval_id = 0;

        this.elm.setStyle('overflow', 'hidden');

        var up_arrow = this.create_arrow_button('up').inject(this.elm, 'after');
        var down_arrow = this.create_arrow_button('down').inject(this.elm, 'after');

        up_arrow.store('direction', 'up');
        up_arrow.setStyles(
        {
            left: 670,
            top: parseInt(elm.getStyle('top')) + 65
        });
        up_arrow.addEvents(
        {
            'mousedown': function()
            {
                $clear(this.scrolling_interval_id);
                this.scrolling_interval_id = this.scroll.periodical(this.scrolling_interval, this, 'up');
            }.bind(this),
            'mouseup': function()
            {
                $clear(this.scrolling_interval_id);
            }.bind(this)
        });

        down_arrow.setStyles(
        {
            left: 670,
            top: 440
        });
        down_arrow.addEvents(
        {
            'mousedown': function()
            {
                $clear(this.scrolling_interval_id);
                this.scrolling_interval_id = this.scroll.periodical(this.scrolling_interval, this, 'down');
            }.bind(this),
            'mouseup': function()
            {
                $clear(this.scrolling_interval_id);
            }.bind(this)
        });

        document.addEvent('mouseup', function(evt)
        {
            $clear(this.scrolling_interval_id);
        }.bind(this));
    },

    create_arrow_button: function(direction)
    {
        var scroll_arrow = new Element('span',
        {
            'id': 'scroll-' + direction + '-arrow',
            'styles': {'z-index': 30}
        });

        return scroll_arrow;
    },

    scroll: function(direction)
    {
        if (direction == 'up')
        {
            this.elm.scrollTo(0, this.elm.getScroll().y - 10)
        }
        else
        {
            this.elm.scrollTo(0, this.elm.getScroll().y + 10)
        }
    }
});







function init_sidebar_image_cycle()
{
    var images = document.id('sidebar').getElements('.sidebar-image');

    if (images.length <= 1)
    {
        return;
    }

    images.each(function(img, img_index)
    {
        if (img_index > 0)
        {
            img.set('tween', {duration: 1000});
            img.setStyle('opacity', 0);
        }
    });

    cycle_images.delay(5000, null, [images, 0]);
}

function cycle_images(images, img_index)
{
    old_img_index = img_index;
    if (img_index == images.length - 1)
    {
        img_index = 0;
    }
    else
    {
        img_index++;
    }

    images[old_img_index].fade(0);
    images[img_index].fade(1);

    cycle_images.delay(5000, null, [images, img_index]);
}

