var BubbleCloser = new Class({
    max_len: 4,

    initialize:function(openMessage, closeMessage, max_len){
        this.firstOpenMessage = openMessage;
        this.openMessage = openMessage;
        this.closeMessage = closeMessage;
        this.init(openMessage);
        this.max_len = max_len?max_len:this.max_len;
    },

    init:function(message){
        if (message) {
            this.openMessage = message;
        } else {
            this.openMessage = this.firstOpenMessage;
        }
        this._closed = true;
        this.bubbles = $$('#time_line-listdiv > div');
        if (this.bubbles.length <= this.get_div_count())
            return;
        this.divOpen = new Element('div', {'class':'newsbotbut-open'});
        var divButton = Element('div', {'class':'button'}).inject(this.divOpen);
        var divButtonText = Element('div', {'class':'button-text'}).inject(divButton);
        this.textOpen = Element('a',{
            'href':'#',
            'text':this.openMessage
//            'style':'float: left; padding-right: 9px; text-decoration: none; color: #000; width: 85%; position: relative; text-align: right;'
        }).inject(divButtonText);
        var divButtonDot = Element('div', {'class':'button-dot'}).inject(divButton);

        var button = new Element('a', {
            'href':'#'
        }).inject(divButtonDot);

        this.imgButton = Element('img', {
            'width':'15px',
            'heigth':'15px',
            'alt':'',
            'src':'/img/b11-P01/bg-but-toopen.gif'
        }).inject(button);

        button.addEvent('click', this.action.bind(this));
        this.textOpen.addEvent('click', this.action.bind(this));

        this.close();
        this.divOpen.inject($('time_line-listdiv'));
   },

   get_div_count: function(){
       return this.max_len*2;
   },

    action:function(e){
        e.stop();
        if (this._closed)
            this.open();
        else
            this.close();
        this._closed = !this._closed;
        // ie7 hack
        $$('div.b11-P01-index2')[0].setStyle('display', 'none');
        $$('div.b11-P01-index2')[0].setStyle('display', 'block');
    },

    close:function(){
        this.bubbles.each(function(item, i){
            if (i > (this.get_div_count()-2)&& i<this.bubbles.length-1)
                item.addClass('hide');
        }.bind(this));
        this.textOpen.setProperty('text', this.openMessage);
        this.imgButton.set('src','/img/b11-P01/bg-but-toopen.gif');
    },

    open:function(){
        this.bubbles.each(function(item, i){
            if (i > (this.get_div_count()-2)&& i<this.bubbles.length-1)
                item.removeClass('hide');
        }.bind(this));
        this.textOpen.setProperty('text', this.closeMessage);
        this.imgButton.set('src','/img/b11-P01/bg-but-toclose.gif');
    }

    /*reinit:function(){
        this.init();
    }*/

});
