var ClickDivReveal = Class.create();
ClickDivReveal.prototype = {
    controlDivs : null,
    targetDivs : null,
    callbacks : null,
    controlSelectedClass : null,
    toggleVisibility : null,
    oneAtATime : null,
    eventType : null,
    initialized : false,
    
    _toggle : null,
    
    initialize : function(config) {
        if (!this.initialized) {
            if (config.controlSelector) {
                this.controlDivs = $$(config.controlSelector);
            } else {
                this.controlDivs = config.controlDivs;
            };
            if (config.targetSelector) {
                this.targetDivs = $$(config.targetSelector);
            } else {
                this.targetDivs = config.targetDivs;
            };
            
            this.eventType = config.eventType || 'click';
            
            this.callbacks = config.callbacks || null;
            this.controlSelectedClass = config.controlSelectedClass || 'selected';
            this.toggleVisibility = config.toggleVisibility || false;
            this.oneAtATime = config.oneAtATime || false;
            
            this._toggle = this.toggle.bindAsEventListener(this);
                        
            this.createEvents();
            this.initialized = true;
        }
    },
    
    createEvents : function(){
        for (var i=0, c=this.controlDivs.length; i < c; i++) {
            var controlDiv = this.controlDivs[i];
            controlDiv.targetDiv = this.targetDivs[i];
            controlDiv.callback = this.callbacks ? (this.callbacks[i] || this.callbacks[0]) : null;
            if (this.targetDivs[i].visible()) {
                controlDiv.shown = true;
            } else {
                controlDiv.shown = false;
            };
            Event.observe(controlDiv, this.eventType, this._toggle);
        };
    },
    
    toggle : function(event) {
        var elem = event.element();
        if (elem.shown && this.toggleVisibility) {
            this.hide(elem);
        } else {
            this.show(elem);
        };
        event.stop();
    },

    show : function(target) {
        target.addClassName(this.controlSelectedClass);
        var targetDiv = target.targetDiv;
        if (target.callback) {
            target.callback(targetDiv);
        }
        targetDiv.show();
        if (this.oneAtATime) {
            this.hideOther(target);
        }
        target.shown = true;
    },

    hide : function(target) {
        target.removeClassName(this.controlSelectedClass);
        var targetDiv = target.targetDiv;
        if (targetDiv) {
            targetDiv.hide();
            target.shown = false;
        };
    },
    
    hideOther : function(keep) {
        for (var i=0, c=this.controlDivs.length; i < c; i++) {
            var controlDiv = this.controlDivs[i];
            if (controlDiv != keep && controlDiv.shown) {
                this.hide(controlDiv);
            };
        };
    }
}

Event.observe(window, 'load', function() {
    var sampleReveal = new ClickDivReveal({
        eventType : 'mousedown',
        oneAtATime : true,
        toggleVisibility : false,
        controlSelector : '.select_div',
        targetSelector : '.target_div',
        controlSelectedClass : 'hoverbutton_selected',
        callbacks : [
            function(target) {
                var thumb = target.down('img');
                thumb.classObject.setMainImage(thumb, thumb.mainImage);
            }
        ]        
    });
    // Can also pass arrays of elements instead of selectors if you can't use selectors for some reason.
    // controlDivs : [],
    // targetDivs : []    
});