QuickEdit.Toolbar = Class.create();

Object.extend(QuickEdit.Toolbar.prototype, {
    
    initialize: function() {
        this.element = $('toolbar') || document.body.appendChild(document.createElement('div'));
        this.element.className = arguments[0] || 'toolbar';
        this.hide();
        this.activeEditable = null;
    },
    
    show: function(ae) {
        this.activeEditable = ae;
        Element.show(this.element);
    },
    
    hide: function() {
        this.activeEditable;
        Element.hide(this.element);
    },
    
    add: function(tbElement) {
        this.element.appendChild(tbElement.element);
    },
    
    remove: function(tbElement) {
        this.element.removeChild(tbElement.element);
    },
    
    clear: function() {
        for (var i = 0; i < this.element.childNodes.length; i++) {
            this.element.removeChild(this.element.childNodes[0]);
        }
    }
    
});

QuickEdit.ToolbarButton = Class.create();

Object.extend(QuickEdit.ToolbarButton.prototype, {
    
    initialize: function(img, title, cmd, keyCode) {
        this.cmd = cmd;
        this.element = document.createElement('div');
        this.element.title = title;
        this.element.style.backgroundImage = "url('" + img + "')";
        this.element.unselectable = 'on';
        this.keyCode = keyCode || false;
        
        this.element.className = arguments[4] || 'toolbar_button';
        this.moClassName = arguments[5] || 'toolbar_button_over';
        
        this.eventClick = this.doClick.bind(this);
        Event.observe(this.element, 'click', this.eventClick);
        
        this.eventOver = this.doMouseOver.bind(this);
        Event.observe(this.element, 'mouseover', this.eventOver);
        
        this.eventOut = this.doMouseOut.bind(this);
        Event.observe(this.element, 'mouseout', this.eventOut);
        
        if (this.keyCode) {
            QuickEdit.Shortcuts.add(this.keyCode, this.cmd.exec.bind(this.cmd));
        }
    },
    
    doClick: function(event) {
        this.cmd.exec(this);
        Event.stop(event);
    },
    
    doMouseOver: function(event) {
        Element.addClassName(this.element, this.moClassName);
    },
    
    doMouseOut: function(event) {
        Element.removeClassName(this.element, this.moClassName);
    }
});

QuickEdit.ToolbarSelect = Class.create();

Object.extend(QuickEdit.ToolbarSelect.prototype, {
    
    initialize: function(values, title, cmd, type) {
        this.cmd = cmd;
        this.type = type ? type : 'text';
        this.element = document.createElement('select');
        this.element.className = arguments[4] || 'toolbar_select';
        this.element.title = 'title';
        this.element.options[0] = new Option('- ' + title + ' -', '');
        this.element.unselectable = 'on';
        for (var i = 0; i < values.length; i++) {
            if (this.type == 'color') {
                this.element.options[i + 1] = new Option('', values[i]);
                this.element.options[i + 1].style.background = values[i];
            } else {
                this.element.options[i + 1] = new Option(values[i][0], values[i][1]);
                if (values[i][2]) {
                    this.element.options[i + 1].className = values[i][2];
                }
            }
        }
        
        this.eventChange = this.doChange.bind(this);
        Event.observe(this.element, 'change', this.eventChange);
    },
    
    doChange: function(event) {
        if (this.element.selectedIndex > 0) {
            this.cmd.exec(this.element.options[this.element.selectedIndex].value);
            this.element.selectedIndex = 0;
        }
        
        Event.stop(event);
    }
    
});

QuickEdit.ToolbarSpacer = Class.create();

Object.extend(QuickEdit.ToolbarSpacer.prototype, {
    
    initialize: function() {
        this.element = document.createElement('div');
        this.element.className = arguments[0] || 'toolbar_spacer';
    }
    
});


