/*
COPYRIGHT 1995-2003 ESRI

TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the 
Copyright Laws of the United States.

For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373

email: contracts@esri.com
*/

var esriClientActionFunction = null;

///////////////////////////////////// Object Creation Functions /////////////////////////////////////
function ToolbarGroupObject(name, toolbars)
{
	this.name = name;
	this.toolbars = toolbars.split(",");;
}

function ToolbarItemObject(name, defaultImage, selectedImage, hoverImage, disabledImage, 
clientAction, isClientActionCustom, showLoading, disabled, buddyItem, selectAutoPostBack, cursor)
{
	this.name = name;
	this.defaultImage = defaultImage;
	this.selectedImage = selectedImage;
	this.hoverImage = hoverImage;
	this.disabledImage = disabledImage;
	this.clientAction = clientAction;
	this.isClientActionCustom = isClientActionCustom;
	this.showLoading = showLoading;
	this.disabled = disabled;
	this.buddyItem = buddyItem;
	this.selectAutoPostBack = selectAutoPostBack;
	this.cursor = cursor;
	this.preExecFunction = null;
}

function ToolbarObject(name, toolbarStyle, defaultStyle, selectedStyle, hoverStyle, disabledStyle, tools, commands, buddyControls, itemArray, group, currentToolField, callbackFunctionString)
{
	this.name = name;
	this.toolbarStyle = toolbarStyle;
	this.defaultStyle = defaultStyle;
	this.selectedStyle = selectedStyle;
	this.hoverStyle = hoverStyle;
	this.disabledStyle = disabledStyle;
	this.tools = 	tools.split(",");
	this.commands = commands.split(",");
	if (buddyControls != "")
		this.buddyControls = buddyControls.split(",");
	else
		this.buddyControls = null;
	this.items = itemArray;
	this.group = group;
	this.currentToolField = currentToolField;
	this.enableClientPostBack = false;
	this.pageID = "";
	this.buttonsSupportingClientPostBack = "";
	this.document = document;
	
	if (callbackFunctionString!=null) 
	    this.callBackFunctionString = callbackFunctionString;
	else
	    this.callBackFunctionString = null;
	    
	for (var i = 0; i < this.tools.length; i++)
	{
		var toolbarItemName = this.tools[i];
		if (toolbarItemName == "")
			continue;
		var imageTag = this.name + toolbarItemName + "Image";
		var img = document.images[imageTag];
		if (img != null)
		    alphaBlend(img);
	}
    
	for (var i = 0; i < this.commands.length; i++)
	{
		var toolbarItemName = this.commands[i];
		if (toolbarItemName == "")
			continue;
		var imageTag = this.name + toolbarItemName + "Image";
		var img = document.images[imageTag];
		if (img != null)
		    alphaBlend(img);
	}
		
	this.selectTool = function()
	{
		var f = document.forms[docFormID];
		var mode = f.elements[this.currentToolField].value;
		var	selectedStyle = this.selectedStyle;
		var disabledStyle = this.disabledStyle;
		var	style = this.defaultStyle;
		var tools = this.tools;
		var toolbarItemName,imageTag,cell;
		if (tools == null)
			return;
		for (var i = 0; i < tools.length; i++)
		{
			toolbarItemName = tools[i];
			if (toolbarItemName == "")
				continue;
			imageTag = this.name + toolbarItemName + "Image";
			cell = this.name + toolbarItemName;
			var toolbarItem = this.items[toolbarItemName];
			var cellElement = document.getElementById(cell);
			if ( cellElement == null || cellElement.style.display == "none")
			    continue;
			if (mode == toolbarItemName) 
			{ //selected
				cellElement.style.cssText = selectedStyle;
				if (this.toolbarStyle != "TextOnly")
				{
					var img = document.images[imageTag];
					switchImageSourceAndAlphaBlend(img,toolbarItem.selectedImage);
				}
			}
			else if (toolbarItem.disabled)
			{
				cellElement.style.cssText = disabledStyle;
				if (this.toolbarStyle != "TextOnly")
				{
					var img = document.images[imageTag];
					switchImageSourceAndAlphaBlend(img,toolbarItem.disabledImage);
				}
			}
			else
			{//not selected or disabled
				cellElement.style.cssText = style;
				if (this.toolbarStyle != "TextOnly")
				{
					var img = document.images[imageTag];
					switchImageSourceAndAlphaBlend(img,toolbarItem.defaultImage);
				}
			}
		}
	}
	
	this.refreshGroup = function()
	{
		//refresh other toolbars in group
		var group = this.group;
		if (group == null || group == "")
		return;	 
		var toolbarGroup = ToolbarGroups[group];
		if (toolbarGroup == null)
			return;
		var toolbars = toolbarGroup.toolbars;
		if (toolbars == null)
			return;
		for (var x = 0; x < toolbars.length; ++x)
		{
			if (toolbars[x] != this.name)
			{
				Toolbars[toolbars[x]].selectTool();
			}
		}
	}
	
	this.refreshCommands = function()
	{
		var f = document.forms[docFormID];
		var	style = this.defaultStyle;
		var disabledStyle = this.disabledStyle;
		var commands = this.commands;
		var toolbarItemName,imageTag,cell;
		for (var i = 0; i < commands.length; i++)
		{
			toolbarItemName = commands[i];
			imageTag = this.name + toolbarItemName + "Image";
			cell = this.name + toolbarItemName;
			var toolbarItem = this.items[toolbarItemName];
			var cellElement = document.getElementById(cell);
			if ( cellElement == null || cellElement.style.display == "none")
			    continue;
			if (toolbarItem.disabled)
			{
				cellElement.style.cssText = disabledStyle;
				if (this.toolbarStyle != "TextOnly")
				{
					var img = document.images[imageTag];
					switchImageSourceAndAlphaBlend(img,toolbarItem.disabledImage);
				}
			}
			else
			{//not selected or disabled
				cellElement.style.cssText = style;
				if (this.toolbarStyle != "TextOnly")
				{
					var img = document.images[imageTag];
					switchImageSourceAndAlphaBlend(img,toolbarItem.defaultImage);
				}
			}
		}
	}
}

///////////////////////////////////// Object Creation Functions /////////////////////////////////////

///////////////////////////////////// Mouse Action Functions /////////////////////////////////////
function ToolbarMouseDown(toolbarName, toolbarItemName, buttonType, e)
{
	if (buttonType != "DropDownBox" && !isLeftButton(e))
		return;
	var f = document.forms[docFormID];
	var imageTag = toolbarName + toolbarItemName + "Image";
	var cell = toolbarName + toolbarItemName;
	var toolbar = Toolbars[toolbarName];
	if (toolbar == null) return;
	if (toolbar.items[toolbarItemName].disabled)
        return;
        
	if (buttonType == "Tool")
	{
	  //change toolbar's selected tool
		f.elements[toolbar.currentToolField].value = toolbarItemName;
		//clientAction for each buddy control
		var clientAction =  toolbar.items[toolbarItemName].clientAction;
		if (clientAction != null)
		{
			var clientActions = "";
		  if (!toolbar.items[toolbarItemName].isClientActionCustom)
		  {
                //*- CUSTOM CODE FOR // Map IT Change
				//*- added djones modified for SP2
				var tmpClientActions = clientAction.split("|||");
				clientAction = tmpClientActions[0];
				if(tmpClientActions[1] != null){
				    eval(tmpClientActions[1]);
				}
				//*-/				

				var buddies = toolbar.buddyControls;
				if (buddies != null)
				{
					for (var i = 0; i < buddies.length; i++)
					{
						var modeField = f.elements[buddies[i] + "_mode"];
						if (modeField != null)
						  modeField.value = toolbarItemName;
						var cursor = toolbar.items[toolbarItemName].cursor;
						if (cursor != null)
						  clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + ",'" + cursor + "'); ";
						else
						  clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + "); ";
					}
				}
			}
			else
			{
			    //*- CUSTOM CODE FOR // Map IT Change
			    //*- added djones , modified for SP2
				var tmpClientActions = clientAction.split("|||");
				if(tmpClientActions[1] != null)
				{
				    clientAction = tmpClientActions[0];
				    if(tmpClientActions[1] != null){
				        
				        var buddies = toolbar.buddyControls;
				        if (buddies != null)
				        {
					        for (var i = 0; i < buddies.length; i++)
					        {
						        var modeField = f.elements[buddies[i] + "_mode"];
						        if (modeField != null)
						          modeField.value = toolbarItemName;
						        var cursor = toolbar.items[toolbarItemName].cursor;
						        if (cursor != null)
						          clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + ",'" + cursor + "'); ";
						        else
						          clientActions = clientActions + clientAction + " ( '" + buddies[i] + "' , '" + toolbarItemName + "', " + toolbar.items[toolbarItemName].showLoading + "); ";
					        }
				        }
				        eval(tmpClientActions[1]);
				    }
				}
			    else
			    {
				    clientActions = clientAction;
				}
				//-*
			}
	        if (toolbar.items[toolbarItemName].preExecFunction != null)
	            clientActions += toolbar.items[toolbarItemName].preExecFunction;   	
			var clientActionFunction = new Function(clientActions);
			clientActionFunction.call(null);
			//select this tool and unselect others
			Toolbars[toolbarName].selectTool();
			Toolbars[toolbarName].refreshGroup();
		}
	}
	else if (buttonType == "Command")
	{
	    if (toolbar.items[toolbarItemName].preExecFunction != null)
	        eval(toolbar.items[toolbarItemName].preExecFunction);
	        
		//if (toolbar.items[toolbarItemName].showLoading) showLoading();
		document.body.style.cursor = "wait";
		//reset toolbar images: IE does not refresh toolbar properly if this is not done.
		Toolbars[toolbarName].refreshCommands();
		Toolbars[toolbarName].selectTool();
		//set selected style and image
		var cellElement = document.getElementById(cell);
		cellElement.style.cssText = toolbar.selectedStyle;
		if (toolbar.toolbarStyle != "TextOnly")
		{
			var img = document.images[imageTag];
            switchImageSourceAndAlphaBlend(img,toolbar.items[toolbarItemName].selectedImage);
		}
		var clientAction =  toolbar.items[toolbarItemName].clientAction;
		if (clientAction != null && clientAction.length>0)
        {
//			esriClientActionFunction = new Function(clientAction);
//			 window.setTimeout("esriClientActionFunction.call(null);", 0);	
            esriClientActionFunction	 = clientAction;
            window.setTimeout("eval(esriClientActionFunction);", 0);     
	    }
	    else
	        window.setTimeout("postBack('" + toolbarName + "','" +  toolbarItemName + "');", 0);
		    	
		document.body.style.cursor = "default";
	}
	else if (buttonType == "DropDownBox")
	{
	  var selectDropDown = toolbarName + toolbarItemName + "DropDownBox";
	  var selectValueField = toolbarName + toolbarItemName + "Value";
    f.elements[selectValueField].value = f.elements[selectDropDown].value;

        window.setTimeout("postBack('" + toolbarName + "','" +  toolbarItemName + "');", 0);
	}
}

function ToolbarMouseOver(toolbarName, toolbarItemName)
{	
	var imageTag = toolbarName + toolbarItemName + "Image";
	var cell = toolbarName + toolbarItemName;
	var toolbar = Toolbars[toolbarName];
	if (toolbar == null) return;
	 		if (toolbar.items[toolbarItemName].disabled)
	 		    return;

	var f = document.forms[docFormID];	
  var cellElement = document.getElementById(cell);
	cellElement.style.cssText = toolbar.hoverStyle;
	if (toolbar.toolbarStyle != "TextOnly")
	{
		var img = document.images[imageTag];
		switchImageSourceAndAlphaBlend(img,toolbar.items[toolbarItemName].hoverImage);
	}
}

function ToolbarMouseOut(toolbarName, toolbarItemName) 
{
	var toolbar = Toolbars[toolbarName];
	if (toolbar == null) return;
	var control = toolbar.tools[0];
	var f = document.forms[docFormID];
	var mode = f.elements[toolbar.currentToolField].value;
	var imageTag = toolbarName + toolbarItemName + "Image";
	var cell = toolbarName + toolbarItemName;
	
	var style, image;
	if (mode == toolbarItemName) 
	{
		style = toolbar.selectedStyle;
		image = toolbar.items[toolbarItemName].selectedImage;
	}
	else
	{
	style = toolbar.defaultStyle;
	image = toolbar.items[toolbarItemName].defaultImage;
	}
	
	var cellElement = document.getElementById(cell);
	cellElement.style.cssText = style;
	if (toolbar.toolbarStyle != "TextOnly")
	{
		var img = document.images[imageTag];
		switchImageSourceAndAlphaBlend(img,image);
	}
}

///////////////////////////////////// Mouse Action Functions /////////////////////////////////////

