/* Scroller functions */

function Scroller(buttons_container, content_container) {

    /* This variable contains an alement that wraps scroller buttons */
    this.buttons_container = buttons_container;
    /* This variable contains an element that wraps scrolled content */
    this.content_container = content_container;
    /* Those variable contain left and right buttons */
    this.button_left = null;
    this.button_right = null;
    /* This variable contains collection of other possible content divs */
    this.content = new Array();
    /* This variable contains number of currently active content div */
    this.active_content = 0;


    this.findButtons = function() {
        var self = this;
        this.buttons_container.childElements().each(function(button) {
            if (button.hasClassName('left')) {
                self.button_left = button;
                self.button_left.observe('click', function(event){
                    self.scrollLeft();
                });
            } else if (button.hasClassName('right')) {
                self.button_right = button;
                self.button_right.observe('click', function(event){
                    self.scrollRight(event);
                });
            }
        });
    }

    this.findContent = function() {
        this.content = this.content_container.childElements();
        var max_height = 0;
        this.content.each(function(slide){
            var height = slide.getHeight();
            if (height > max_height) {
                max_height = height;
            }
        });
        this.content_container.setStyle({'height': max_height + 'px'});
    }

    this.scrollLeft = function(event) {
        if (this.active_content > 0) {
            this.active_content -= 1;
            this.content[this.active_content + 1].slideUpRight({duration:0.5});
            this.content[this.active_content].slideDownRight({duration:1.0});
            /* Change button img src if first element active */
            if (this.active_content == 0) {
                this.buttonDeactivate(this.button_left);
            }
            /* Activate oposing button */
            this.buttonActivate(this.button_right);
        }
    }

    this.scrollRight = function(event) {
        if (this.active_content < this.content.length - 1) {
            this.active_content += 1;
            this.content[this.active_content - 1].slideUpLeft({duration:0.5});
            this.content[this.active_content].slideDownLeft({duration:1.0});
            /* Change button img src if first element active */
            if (this.active_content == this.content.length - 1) {
                this.buttonDeactivate(this.button_right);
            }
            /* Activate oposing button */
            this.buttonActivate(this.button_left);
        }
    }

    this.buttonActivate = function(button_container) {
        button_container.firstDescendant().setAttribute('src', button_container.firstDescendant().getAttribute('src').replace('inactive.png', 'active.png'));
    }

    this.buttonDeactivate = function(button_container) {
        button_container.firstDescendant().setAttribute('src', button_container.firstDescendant().getAttribute('src').replace('active.png', 'inactive.png'));
    }


    /* Initialise */
    /* Execute buttons and content search functions */
    this.findButtons();
    this.findContent();
}


/* Initailise scrollers on the page */
document.observe("dom:loaded", function() {
    $$('div.scroller-container').each(function(scroller_container) {
        var id_prefix = scroller_container.getAttribute('id').replace('-scroller', '');
        var content_id = id_prefix + '-content';
        new Scroller(scroller_container, $(content_id));
    });
});
