(function () {
    
    var W = Allthat.namespace('Allthat.Widget');
    
    W.Reveal = new Class({
        Implements : [Events, Options],
        options : {
            toggleSelector : null,
            revealSelector : null
        },
        
        initialize : function (options) {
            this.setOptions(options);
            
            // initialize DOM references
            this.revealElement = $$(this.options.revealSelector)[0];
            this.toggleElement = $$(this.options.toggleSelector)[0];
            
            // bind scope of the this.toggle function to "this" and remember it:
            this.boundedToggle = this.toggle.bind(this);
            
            // usefull shortcut to define from the outer classes, are reveal hidden or not
            this.visible = false;
            
            // bind event listeners
            this.toggleElement.addEvent('click', this.boundedToggle);
            
            this.fireEvent('initialize');
        },
        
        toggle : function () {
            if (this.revealElement.getStyle('display') == 'none') {
                this.visible = true;
                this.revealElement.setStyle('display', 'block');
                this.toggleElement.addClass('expanded');
                this.toggleElement.removeClass('collapsed');
            } else {
                this.visible = false;
                this.revealElement.setStyle('display', 'none');
                this.toggleElement.addClass('collapsed');
                this.toggleElement.removeClass('expanded');
            }
            
            this.fireEvent('toggle');
        }
    });	
    
    W.Reveal.REVEALS = {}; // place existing reveals here
    
    W.Reveal.create = function (options) {
        if (!W.Reveal.REVEALS[options.toggleSelector]) {
            var reveal = W.Reveal.REVEALS[options.toggleSelector] = new W.Reveal(options);
            reveal.toggle();
            return false;
        }
    };
    
})();