var Gallery = new Class({
    Implements:[Options, Events],
    
    element:null,
    element_image:null,
    element_copy:null,
    
    images:[],
    image_count:0,
    current_image:0,
    
    next_link:null,
    prev_link:null,
    
    options:{
        onDeactivateItems:$empty,
        onImageChanged:$empty
    },
    
    initialize:function(element, options)
    {
        var self = this; 
        
        if(!$chk(element)){ return; }
        this.element = element;
        
        var image = this.element.getElement(".image");
        if(!$chk(image)){ return; }
        
        this.element_image = image.getElement("img");
        if(!$chk(this.element_image))
        {
            this.element_image = new Element("img", {"src":"", "alt":""});
            this.element_image.addEvent("click", function(e){ e.stop(); self.nextImage(); });
            image.grab(this.element_image);
        }
        
        this.element_copy = this.element.getElement(".copy");
        if(!$chk(this.element_copy))
        {
            this.element_copy = new Element("div", {"class":"copy"});
            this.element_copy.inject(this.element_image, "after")
        }
        
        if(options != undefined && options != null)
        {
            this.setOptions(options);
        }
        
        var prevLink = this.element.getElement(".link-prev");
        if($chk(prevLink))
        {
            this.prev_link = prevLink;
            prevLink.addEvent("click", function(e){ e.stop(); self.prevImage(); });
            prevLink.fade("hide");
        }
        
        var nextLink = this.element.getElement(".link-next");
        if($chk(nextLink))
        {
            this.next_link = nextLink;
            nextLink.addEvent("click", function(e){ e.stop(); self.nextImage(); });
            nextLink.fade("hide");
        }
        
        this.build();
    },
    
    build:function()
    {
        var data = this.element.getElement(".data");
        if(!$chk(data)){ return; }
        
        data.addClass("hidden");
        
        data.getElements("ul li").each(function(item){
            this.buildItem(item);
        }, this);
        
        this.viewImage(0);
    },
    
    prevImage:function()
    {
        var next = Math.max(this.current_image - 1, 0);
        this.viewImage(next);
    },
    
    nextImage:function()
    {
        var next = Math.min(this.current_image + 1, this.image_count);
        this.viewImage(next);
    },
    
    viewImage:function(index)
    {
        if(this.image_count > index)
        {
            var f = this.images[index];
            if(typeof f == 'function')
            {
                this.current_image = index;
                
                this.element_image.fade(0);
                f(null);
                this.element_image.fade(1);
                
                if(this.current_image == 0 && this.prev_link != null)
                {
                    this.prev_link.fade(.3);
                }
                else if(this.prev_link != null)
                {
                    this.prev_link.fade("in");
                }
                
                if(this.current_image == this.image_count - 1 && this.next_link != null)
                {
                    this.next_link.fade(.3);
                }
                else if(this.next_link != null)
                {
                    this.next_link.fade("in");
                }
                
                this.fireEvent("imageChanged", index);
            }
        }
    },
    
    buildItem:function(item)
    {
        var self = this;
        
        var image_title = item.getElement("h1.image_title");
        var title = $chk(image_title) ? image_title.get("html") : "";
        
        var image_file = item.getElement("img.image_file");
        var image_thumbnail_file = item.getElement("img.image_thumbnail_file");
        var image_summary = item.getElement(".image_summary");
        
        var thumbs = this.element.getElement(".thumbnails ul");
        if(!$chk(thumbs))
        {
            var container = new Element("div", {"class":"thumbnails"});
            thumbs = new Element("ul");
            container.grab(thumbs);
            this.element.grab(container);
        }
        
        var li = new Element("li");
        var a = new Element("a", {"href":"#", "title":title});
        a.grab(image_thumbnail_file);
        
        this.addEvent("deactivateItems", function(){ self.deactivateItem(li, a); });
        
        var activationFunction = function(e){
            if(self.activateItem(li, a, image_title, image_file, image_summary))
            {
                if($chk(e)){ e.stop(); }
            }
        };
        
        var index = this.image_count;
        a.addEvent("click", function(e){ e.stop(); self.viewImage(index); });
        
        li.grab(a);
        thumbs.grab(li);
        
        this.images.push(activationFunction);
        
        this.image_count++;
    },
    
    activateItem:function(thumbnail_li, thumbnail_link, titleContainer, imageFile, summaryContainer)
    {
        this.fireEvent("deactivateItems");
        
        var title = $chk(titleContainer) ? titleContainer.get("html") : "";
        var summary = $chk(summaryContainer) ? summaryContainer.get("html") : "";
        
        this.element_image.set("src", imageFile.get("src"));
        this.element_copy.set("html", summary);
        
        thumbnail_li.setStyle("opacity", 1);
        thumbnail_li.addClass("active");
        
        return true;
    },
    
    deactivateItem:function(thumbnail_li, thumbnail_link)
    {
        thumbnail_li.removeClass("active");
        thumbnail_li.setStyle("opacity", .9);
    }
    
});
