
var Gallery = Class.create({
    initialize: function(location, collection) {
        this.imageLocation = location;
        this.imageCollection = collection;
        this.load_count = 0;
        this.images = [];
        this.image_captions = [];
        
        new Ajax.Request(this.imageCollection, {
            onComplete: function(response){
                var photos = response.responseText.evalJSON();

                for(i=0;i<(photos.length);i++){
                    this.images[i] = photos[i].photo_id;
                    this.image_captions[i] = photos[i].photo_caption;

                }
                
            }
        });
    },

    checkConstraints: function(){
        //check left constraints
        left_offset = $('image_0').cumulativeOffset().left - $('image_container').cumulativeOffset().left;
        if($('image_0').cumulativeOffset().left >$('image_container').cumulativeOffset().left){

            new Effect.Move('images', {
                x: -left_offset,
                y: 0,
                mode: 'relative',
                transition: Effect.Transitions.spring
            });
        }

        //check right constraints
        image_offset_right = $('image_'+(images.length-1)).cumulativeOffset().left+$('image_'+(images.length-1)).getWidth();
        container_offset_right = $('image_container').cumulativeOffset().left+$('image_container').getWidth();
        right_offset = container_offset_right - image_offset_right;
        if(image_offset_right < container_offset_right){

            new Effect.Move($('images'), {
                x: right_offset,
                y: 0,
                mode: 'relative',
                transition: Effect.Transitions.spring
            });
        }
    },

    loadGallery: function(){
        for(i=0;i<(this.images.length);i++){
            
            var img = new Element('img',{
                'class':'shadow',
                id:'image_'+i,
                alt:this.image_captions[i],
                src:this.imageLocation+'thumb=true&id='+this.images[i]
            });
            img.setStyle({
                height:'150px',
                marginRight:'15px'
            });

            var link = new Element('a',{
                href:this.imageLocation+'photo.php?id='+this.images[i],
                rel:'lightbox[photos]',
                title:this.image_captions[i]
            });
            link.setStyle({
                display:'inline-block'
            });

            new Draggable('images',{
                constraint:'horizontal',
                onEnd:this.checkConstraints
            });

            link.appendChild(img);
            $('images').appendChild(link);

        }
    }

});
