/**
 * @author lars.laade
 */
var Gallery = Class.create();
Gallery.prototype = {
    
    data: [],
    window: null,
    image: null,
    options: {
        close_txt: 'close'
    },
    
    initialize: function(data, id, options)
    {
            if (options){
                this.options = options;
            }
            this.data = data;
            this.build(id);
            $(id).observe('click', this.toggle.bind(this));

    },
    
    build: function(id){
        this.window = new Element('div',{
            'class': 'gallery_window'
        });
        
        this.image = new Element('img',{
            src: this.data['image_0'].big,
            alt: '',
            'class': 'mainimage',
            width: 478
        });
        this.window.insert(this.image);
        var thumblist = new Element('ul'); 
        
        for (var item in this.data)
        {
            var li, a, img;
            if (item.startsWith('image_')){
                li = new Element('li',{
                    id: 'thumbli_' + item.split('_')[1]
                });
                a = new Element('a',{
                    id: 'thumb_' + item.split('_')[1],
                    href: this.data[item].thumb,
                    title: this.data[item].title
                });
                li.insert(a);
                img = new Element('img',{
                    id: 'thumbimg_' + item.split('_')[1],
                    src: this.data[item].thumb,
                    alt: this.data[item].title
                });
                a.insert(img);
                thumblist.insert(li);
            }
        }
        this.window.insert(thumblist);
        var close_link = new Element('a',{
            'class': 'close',
            title: this.options.close_txt
        }).update(this.options.close_txt)
        this.window.insert(close_link);
        $$('.content')[0].insert(this.window);
        this.window.hide();
        Event.observe(close_link, 'click', this.toggle.bind(this));
        Event.observe(thumblist, 'click', this.select.bind(this));
    },
    
    toggle: function(e){
        if (!this.window.hasClassName('open'))
        {
            var id = 'image_' + $(e.target).readAttribute('id').split('_')[1];
            this.image.src = this.data[id].big;
            new Effect.Grow(this.window, {duration:0.5});
        }
        else
        {
            this.window.hide();
        }
        this.window.toggleClassName('open');
        e.stop();
        return false;
    },
    
    select: function(e){
        var id = 'image_' + $(e.target).readAttribute('id').split('_')[1];
        this.image.src = this.data[id].big;
        e.stop();
        return false;
    }
};

