
function ComponentToolbar(id, toolbarId, toolIdList, componentManagerObj) {
    
    ComponentToolbar.prototype.buttonOver = function buttonOver() {
        
        if (this.toolBarObj.toolIsEnabled(this) && 
            this.src.indexOf(this.getAttribute(this.toolBarObj.TOOL_ATTR_DOWN_IMAGE)) == -1) {
            this.src = this.getAttribute(this.toolBarObj.TOOL_ATTR_HOVER_IMAGE);
        }
    }
    
    ComponentToolbar.prototype.buttonOut = function() {

        if (this.src.indexOf(this.getAttribute(this.toolBarObj.TOOL_ATTR_HOVER_IMAGE)) > -1) {
            this.src = this.getAttribute(this.toolBarObj.TOOL_ATTR_UP_IMAGE);
        }
    }
    
    ComponentToolbar.prototype.setEvents = function() {
        
        var length = this.toolIdList.length;
        var toolObj;
        for (var i=0; i<length; i++) {
            toolObj = document.getElementById(this.toolIdList[i]);
            if (toolObj) {
                toolObj.toolBarObj = this;
                toolObj.onmousedown = this.setToolClick;
                toolObj.onmouseover = this.buttonOver;
                toolObj.onmouseout = this.buttonOut;
                toolObj.onmouseup = this.toolMouseUpEvent;
                
                if (toolObj.getAttribute(this.TOOL_ATTR_ENABLED).toLowerCase() == "false") {
                    toolObj.enabled = false;
                }
                else {
                    toolObj.enabled = true;
                }
                
                if (!this.toolIsEnabled(toolObj)) {
                    toolObj.src = toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_DISABLE_IMAGE);
                }
            }
        }
    }
    
    ComponentToolbar.prototype.setToolClick = function(toolObj) {
        this.toolBarObj.setTool(this);
    }
    
    ComponentToolbar.prototype.setTool = function(toolObj, isStartTool) {    
        
        if ((!this.toolbarEnabled || !this.toolIsEnabled(toolObj)) && !isStartTool) {
            return;
        }
        
        var toolId = toolObj.id.toLowerCase();
        var drawMode = toolObj.getAttribute("DrawMode");
        
        //The draw mode is always set on server on start up.
        if (!isStartTool) {
            drawMode;
            this.componentManagerObj.setDrawMode(drawMode);
        }
        
        //Set the appropriate image.        
        if (this.toolIsEnabled(toolObj) && toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_BUTTON_TYPE).toLowerCase() != toolObj.toolBarObj.TOOL_BUTTON_COMMAND) {
            if (toolObj.toolBarObj.activeTool) {
                toolObj.toolBarObj.activeTool.src = toolObj.toolBarObj.activeTool.getAttribute(toolObj.toolBarObj.TOOL_ATTR_UP_IMAGE);
            }
            toolObj.toolBarObj.activeTool = toolObj;
            this.stateObj.value = toolObj.getAttribute(this.TOOL_ATTR_COMMAND_NAME);
        }
        
        if (this.toolIsEnabled(toolObj)) {
            toolObj.src = toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_DOWN_IMAGE);
            if (!isStartTool) {
                toolObj.toolBarObj.sendRequest(toolObj);
            }
        }
    }
    
    ComponentToolbar.prototype.toolIsEnabled = function(toolObj) {
    
        return toolObj.enabled;
    }
    
    ComponentToolbar.prototype.enableTool = function(toolId) {
        
        var toolObj = this.getToolObj(toolId);
        
        if (toolObj) {
            if (toolObj.src.indexOf(toolObj.getAttribute(this.TOOL_ATTR_DISABLE_IMAGE)) > -1) {
                toolObj.src = toolObj.getAttribute(this.TOOL_ATTR_UP_IMAGE);
                toolObj.enabled = true;
            }
        }
    }
    
    ComponentToolbar.prototype.disableTool = function(toolId) {
        
        var toolObj = this.getToolObj(toolId);
        
        if (toolObj) {
            toolObj.src = toolObj.getAttribute(this.TOOL_ATTR_DISABLE_IMAGE);
            toolObj.enabled = false;
        }
    }
    
    ComponentToolbar.prototype.deactivateTool = function() {
        
        if (this.activeTool) {
            this.activeTool.src = this.activeTool.getAttribute(this.TOOL_ATTR_UP_IMAGE);
            this.activeTool = null;
            this.stateObj.value = "";
        }
    }
    
    ComponentToolbar.prototype.toolMouseUpEvent = function() {
        
        if (this.toolBarObj.toolIsEnabled(this)) {
            if (this.getAttribute(this.toolBarObj.TOOL_ATTR_BUTTON_TYPE).toLowerCase() == this.toolBarObj.TOOL_BUTTON_COMMAND) {
                this.src = this.getAttribute(this.toolBarObj.TOOL_ATTR_UP_IMAGE);
            }
        }
            
    }
    
    ComponentToolbar.prototype.setStartTool = function(toolId) {
        
        if (toolId && toolId.length > 0) {
            this.setTool(document.getElementById(toolId), true);
        }
    }
    
    ComponentToolbar.prototype.getToolObj = function(toolId) {
        
        var toolObj = null;
        if (toolId && toolId.length > 0) {
            toolObj = document.getElementById(toolId);
        }
        return toolObj;
    }
    
    ComponentToolbar.prototype.disable = function() {
        this.toolbarEnabled = false;
    }
    
    ComponentToolbar.prototype.enable = function() {
        this.toolbarEnabled = true;
    }
    
    ComponentToolbar.prototype.sendRequest = function(toolObj) {
        this.componentManagerObj.sendToolRequest(this, toolObj);
    }
    
    ComponentToolbar.prototype.init = function() {
    
        this.setEvents();
        this.componentManagerObj.registerToolbar(this);
    }
    
    //Constants
    
    this.TOOL_BUTTON_MODE = "toolbarmodebutton";
    this.TOOL_BUTTON_COMMAND = "toolbarcommandbutton";
    
    this.TOOL_ATTR_DISABLE_IMAGE = "DisableImage";
    this.TOOL_ATTR_UP_IMAGE = "UpImage";
    this.TOOL_ATTR_DOWN_IMAGE = "DownImage";
    this.TOOL_ATTR_HOVER_IMAGE = "HoverImage";
    this.TOOL_ATTR_BUTTON_TYPE = "ButtonType";
    this.TOOL_ATTR_COMMAND_NAME = "CommandName";
    this.TOOL_ATTR_ENABLED = "enabled";
    
    //properties
    this.id = id;
    this.toolIdList = toolIdList;
    this.activeTool = null;
    this.stateObj = document.getElementById(toolbarId + "_askToolState");
    this.componentManagerObj = componentManagerObj;
    this.toolbarEnabled = true;
    this.init();
}

