var bigSlideShow;
var scrollRSSDiv;

BigSlideShow = Class.create();
BigSlideShow.prototype = {
    /**
	* C-r.
	* @param imagesContaier container with json data
	* @param currentImage current image contaiber name
	* @param currentCaption
        * @param auto
	*/
    initialize: function(imagesContaier, currentImage, currentCaption, auto) {
        this.currentImage = $(currentImage);
        this.currentCaption = $(currentCaption);
        // auto/manual switcher
        this.auto = $(auto);
		
        // put images to array for futher work
        this.images = new Array();
        $$('#'+imagesContaier+' span').each(function(s) {
            this.images.push({
                src: s.innerHTML,
                caption:s.readAttribute('title')
            });
        }.bind(this));
        this.index = 0;
        this.showImage(this.index);
		
        // auto/switcher event
        this.auto.observe('click', this.autoClick.bindAsEventListener(this));
    },
    autoClick:function() {
        var currentMode = this.auto.readAttribute('mode');
        if (currentMode=='auto') {
            this.setManualMode();
        } else {
            this.setAutoMode();
        }
    },
    setAutoMode: function() {
        this.auto.writeAttribute('mode', 'auto');
        this.auto.update('Switch to Manual Slideshow');
        this.timer = setInterval($('bssnext').onclick, '5000');
    },
    setManualMode: function() {
        this.auto.writeAttribute('mode', 'manual');
        this.auto.update('Switch to Automatic Slideshow');
        clearInterval(this.timer);
        this.timer = null;
    },
    next:function() {
        this.index++;
        if (this.index>=(this.images.length)) {
            this.index = 0;
        }
        this.showImage(this.index);
    },
    prev:function() {
        this.index--;
        if (this.index<0) {
            this.index = this.images.length - 1;
        }
        this.showImage(this.index);
    },
    /**
	* Show image of given index.
	*/
    showImage: function(index) {
        this.currentImage.writeAttribute('src', this.images[index].src);
        this.currentCaption.update(this.images[index].caption);
    }
}

/* Initial settings */
function initializeGreenMe() {	
    if ($('bss')) {
        bigSlideShow = new BigSlideShow('bssimages', 'bsscurrentimage', 'bsscurrentcaption', 'bssauto');
    }
    if ($('rssFeed')) {
        scrollRSSDiv = new ScrollRSS();
    }
}

Event.observe(window, 'load', initializeGreenMe);
