var News = Class.create({
    initialize: function() {
        this._currentIndex = 1;
        
        this.MIN_INDEX = 0;
        this.MAX_INDEX = 2;
        
        this._updater = new PeriodicalExecuter(
            function() { this.toggleStoryBlock(this._currentIndex.toString()); }.bind(this), 
            5
        );
    },
    
    toggleStoryBlock: function(id) {
    	var newsStoryBlockId = id.startsWith('news-story-button_') ? id.replace('news-story-button_', '') : id;
        
        $$('.newsStoryElement_image').each(function(img) {
        	img.setStyle( { display: 'none' });
        }.bind(this));
        $('newsStoryElement_image_' + newsStoryBlockId).setStyle( { display: 'block' });
        
        $$('.newsStoryElement_headline').each(function(headline) {
        	headline.setStyle( { display: 'none' });
        }.bind(this));
        $('newsStoryElement_headline_' + newsStoryBlockId).setStyle( { display: 'block' });
        
        $$('.newsStoryElement_intro').each(function(intro) {
        	intro.setStyle( { display: 'none' });
        }.bind(this));
        $('newsStoryElement_intro_' + newsStoryBlockId).setStyle( { display: 'block' });
        
        $$('.news-story-button-container').each(function(buttonContainer) {
        	buttonContainer.addClassName('news-story-button-off');
        }.bind(this));
        $('news-story-button-container_' + newsStoryBlockId).removeClassName('news-story-button-off');
        $('news-story-button-container_' + newsStoryBlockId).addClassName('news-story-button-on');
        
        $$('.newsStoryElement_readMoreBig').each(function(storyLink) {
        	storyLink.setStyle( { display: 'none' });
        }.bind(this));
        $('newsStoryElement_readMoreBig_' + newsStoryBlockId).setStyle( { display: 'block' });
        
        var nextIndex = this._currentIndex + 1;
        this._currentIndex = nextIndex > this.MAX_INDEX ? this.MIN_INDEX : nextIndex;
    },
    
    stopAutoToggle: function() {
    	this._updater.stop();
    }
});

newsStories = new News();

document.observe('dom:loaded', function() 
{   
    // add event handler to the update qty select elements
    $$('.news-story-button').each(function(link) {
        link.observe(
           'click',
           function() { 
           	    newsStories.toggleStoryBlock(this.id);
           	    newsStories.stopAutoToggle(); 
            }
        );
        
        link.onclick = function() { return false; }
    }.bind(this));
});              