/*
 * Prototype image slider by David Chambers
 * http://davidchambersdesign.com/prototype-image-slider/
 * { last updated: 14 November 2009 }
 *
 * Requires Prototype:
 * http://www.prototypejs.org/
 *
 * Requires script.aculo.us:
 * http://script.aculo.us/
 */

var Slider = Class.create({
    initialize: function (element, duration, n) {
        var children, l, dimensions, w = 0, h = 0, wrap, nav;
        element = $(element);
        children = element.childElements();
        l = children.length;
        if (n == undefined || isNaN(n)) {
            n = 0
        }
        else if (n < 0) {
            n = l + n;
            if (n < 0) {
                n = 0
            }
        }
        else if (n >= l) {
            n = 0
        }
        children.each(function (child) {
            dimensions = child.getDimensions();
            if (dimensions.width > w) {
                w = dimensions.width
            }
            if (dimensions.height > h) {
                h = dimensions.height
            }
        });
        wrap = new Element('div');
        wrap.setStyle({
            marginLeft: n * -w + 'px',
            width: children.length * w + 'px',
            height: h + 'px'
        });
        children.each(function (child) {
            child.setStyle({
                cssFloat: 'left',
                width: w + 'px'
            });
            wrap.appendChild(child)
        });
        element.setStyle({
            width: w + 'px',
            overflow: 'hidden'
        });
        element.appendChild(wrap);
        nav = {
            ul: new Element('ul'),
            prev: {
                li: new Element('li', { 'class': 'prev' }),
                a: new Element('a', { href: '#prev' }).update('\u2190 previous')
            },
            next: {
                li: new Element('li', { 'class': 'next' }),
                a: new Element('a', { href: '#next' }).update('next \u2192')
            }
        };
        nav.prev.li.appendChild(nav.prev.a);
        nav.next.li.appendChild(nav.next.a);
        if (n == 0) {
            nav.prev.li.hide()
        }
        if (n == l - 1) {
            nav.next.li.hide()
        }
        nav.ul.appendChild(nav.prev.li);
        nav.ul.appendChild(nav.next.li);
        element.appendChild(nav.ul);

        this.duration = (duration == undefined ? 1.0 : duration);
        this.wrap = wrap;
        this.nav = nav;
        this.w = w; // width of each slide
        this.h = h; // height of each slide
        this.l = l; // number of slides in slide show
        this.n = n; // number of the active slide

        Event.observe(nav.prev.a, 'click', this.prev.bind(this));
        Event.observe(nav.next.a, 'click', this.next.bind(this));
    },

    isFirst: function () {
        return (this.n == 0)
    },

    isLast: function () {
        return (this.n == this.l - 1)
    },

    slide: function () {
        new Effect.Morph(this.wrap, {
            style: 'margin-left: -' + this.n * this.w + 'px;',
            duration: this.duration
        })
    },

    prev: function (e) {
        this.n--;
        this.slide();
        this.nav.next.li.show();
        if (this.isFirst()) {
            this.nav.prev.li.hide()
        }
        e.stop()
    },

    next: function (e) {
        this.n++;
        this.slide();
        this.nav.prev.li.show();
        if (this.isLast()) {
            this.nav.next.li.hide()
        }
        e.stop()
    }
});

document.observe('dom:loaded', function () {
    $$('div.slider').each(function (e) {
        new Slider(e);
    });
});
