/**
 * A generic javascript toolbar object for use in Image Web Server sites.
 * The toolbar supports
 * 1) Normal push buttons with images
 * 2) Exclusive push buttons with images
 * 3) Pop down combo boxes
 * 4) Check boxes
 * 5) Spaces
 * 6) HTML labels
 * 7) Custom html objects
 *
 * When instantiating a toolbar, if you pass an object id (for e.g. create 
 * a DIV <DIV id='toolbarDivID'></DIV> and pass 'toolbarDivID' as the ID)
 * the toolbar will dynamically get written to the innerHTML of that object. 
 * If you pass a null objectID, you must call build() directly, and write 
 * the results into the page, e.g. document.writeln(MyNCSToolbar.build());
 * 
 * Example:
 *
 * <SCRIPT LANGUAGE=javascript>
 * 
 * var MyNCSToolbar = new NCSToolbar("UID_VIEW_TOOLBAR", null, true);
 * 
 * function testFunction1(uid)
 * {
 * 	alert("testFunction1 : " + uid);
 * }
 * 
 * function testFunction2(uid, value)
 * {
 * 	alert("testFunction2 : " + uid + "\nValue : " + value);
 * }
 * 
 * function testFunction3(uid, value)
 * {
 * 	alert("testFunction3 : " + uid + "\nValue : " + value);
 * }
 * 
 * MyNCSToolbar.addLabel("<a href='javascript:alert(\"Custom label callback\");'>This</a> is a <b>HTML</b> <u>label</u>", "UID_LABEL");
 * MyNCSToolbar.addButton("Pan image",				"roam.png", "roamSelect.png", true, testFunction1, "UID_PAN");
 * MyNCSToolbar.addButton("Zoom image",			"zoom.png", "zoomSelect.png", true, testFunction1, "UID_ZOOM");
 * MyNCSToolbar.addSpace("SPACER1", "UID_SPACER1");
 * MyNCSToolbar.addButton("Add a vector circle",	"circle.png", "circleSelect.png", false, testFunction1, "UID_ANNOT_CRICLE");
 * MyNCSToolbar.addSpace("SPACER1", "UID_SPACER2");
 * var strings = new Array();
 * strings[0] = "String 1";
 * strings[1] = "String 2";
 * strings[2] = "String 3";
 * MyNCSToolbar.addPopdownCombo("Select an option :", strings, testFunction3, "UID_POPDOWNCOMBO1");
 * MyNCSToolbar.addCheckBox("Select true or false", testFunction2, "UID_CHECK_1");
 * MyNCSToolbar.addSpace("SPACER1", "UID_SPACER3");
 * MyNCSToolbar.addCustomObject("<INPUT type='button' onclick='alert(\"Custom object callback function\");' value='Custom Object' id=button1 name=button1>", "UID_CUSTOMOBJ");
 * document.writeln(MyNCSToolbar.build());
 * MyNCSToolbar.setCurrentItem("UID_ZOOM");
 * MyNCSToolbar.getTable().border="0px";
 * MyNCSToolbar.getTable().cellPadding="1px";
 * MyNCSToolbar.getTable().cellSpacing="2px";
 * MyNCSToolbar.getTable().bgColor="lightgrey";
 * </SCRIPT>
 *
 */

function NCSCreateJSToolbar( NCSView, target )
{
	var ECWToolbarCB = function(uid, value)
	{
		if (uid == "UID_VIEW_PAN")
		{
			NCSView.SetPointerMode(0);
		}
		else if (uid == "UID_VIEW_ZOOM")
		{
			NCSView.SetPointerMode(1);
		}
		else if (uid == "UID_VIEW_ZOOMBOX")
		{
			NCSView.SetPointerMode(3);
		}
		else if (uid == "UID_VIEW_POINTER")
		{
			NCSView.SetPointerMode(2);
		}
		else if (uid == "UID_VIEW_RESET")
		{
			NCSView.SetExtentsAll();
		}
	}

	var ECWToolbar1 = new NCSToolbar("UID_VIEW_TOOLBAR", target, false, 10);
	ECWToolbar1.addButton("Pan around the image by click-dragging the hand on the image",
						  "/ecwplugins/lib/bitmaps/buttons/roam.png", "/ecwplugins/lib/bitmaps/buttons/roamSelect.png", true, ECWToolbarCB, "UID_VIEW_PAN");
	ECWToolbar1.addButton("Zoom in or out of the image interactively by click-dragging the magnifier on the image",
						  "/ecwplugins/lib/bitmaps/buttons/zoom.png", "/ecwplugins/lib/bitmaps/buttons/zoomSelect.png", true, ECWToolbarCB, "UID_VIEW_ZOOM");
	ECWToolbar1.addButton("Zoom into the image by click-dragging a zoom box over the image",
						  "/ecwplugins/lib/bitmaps/buttons/zoombox.png", "/ecwplugins/lib/bitmaps/buttons/zoomboxSelect.png", true, ECWToolbarCB, "UID_VIEW_ZOOMBOX");
	ECWToolbar1.addButton("Select pointer mode to profile or select image features",
						  "/ecwplugins/lib/bitmaps/buttons/pointer.png", "/ecwplugins/lib/bitmaps/buttons/pointerSelect.png", true, ECWToolbarCB, "UID_VIEW_POINTER");
	ECWToolbar1.addSpace ("SPACE", "UID_VIEW_SPACE1");
	ECWToolbar1.addButton("Reset the image to maxiumum extents",
						  "/ecwplugins/lib/bitmaps/buttons/reset.png", "/ecwplugins/lib/bitmaps/buttons/resetSelect.png", false, ECWToolbarCB, "UID_VIEW_RESET");
	ECWToolbar1.addSpace ("SPACE", "UID_VIEW_SPACE2");

	return ECWToolbar1;
}

/**
 * Create a toolbar object. uid is a unique ID for the object, objectID is the id 
 * tag of a html element for the toolbar to be written to (may be null) and the
 * status bar argument will show a status bar directly under the toolbar.
 *
 * The toolbar is layed out in a table, normally from left to right with one row.
 * If nCols is passed as a positive integer, the table will be layed out with nCols
 * number of colums, and each item added will flow into the next row. Pass nCols = -1
 * or not at all to disable this feature.
 */
function NCSToolbar(uid, objectID, bShowStatusBar, nCols)
{
	if (arguments.length > 0)
	{
		this.init(uid, objectID);
    }
	this.items = new Array();
	this.bShowStatusBar = bShowStatusBar;
	if (nCols != null)
	{
		this.colums = nCols;
    }
    else 
    {
    	this.colums = 100;
    }
}
function NCSToolbar_AddButton(name, imageNormal, imageSelect, bExclusive, action, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.imageSelect = imageSelect;
	item.imageNormal = imageNormal;
	item.bExclusive = bExclusive;
	item.action = action;
	item.uid = uid;
	item.type = 0;
	this.addObject(this);
	if (this.element)
    {
    	this.build();
    }
}
function NCSToolbar_AddSpace(name, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.uid = uid;
	item.type = 1;
	if (this.element) 
    {
	   this.build();
    }
}
function NCSToolbar_AddCheckbox(name, action, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.uid = uid;
	item.type = 2;
	item.action = action;
	if (this.element)
	{
	   this.build();
    }
}
function NCSToolbar_AddPopdownCombo(name, strings, action, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.uid = uid;
	item.type = 3;
	item.action = action;
	item.strings = strings;
	if (this.element)
	{
	   this.build();
    }
}
function NCSToolbar_AddLabel(name, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.uid = uid;
	item.type = 4;
	if (this.element)
	{
	   this.build();
    }
}
function NCSToolbar_AddCustomObject(name, uid)
{
	var item = new Object;
	this.items[this.items.length] = item;
	item.name = name;
	item.uid = uid;
	item.type = 5;
	if (this.element)
	{
	   this.build();
    }
}
function NCSToolbar_Click(itemNum)
{	
	if (this.items[itemNum].type == 0 ||
		this.items[itemNum].type == 1)
	{
		this.items[itemNum].action(this.items[itemNum].uid);
	}
	else if (this.items[itemNum].type == 2)
	{
		var element = document.getElementById(this.items[itemNum].uid);
		this.items[itemNum].action(this.items[itemNum].uid, element.checked);
	}
	else if (this.items[itemNum].type == 3)
	{
		var element = document.getElementById(this.items[itemNum].uid);
		selectedIndex = element.selectedIndex;
		this.items[itemNum].action(this.items[itemNum].uid, element.options[selectedIndex].text );
	}
	else if (this.items[itemNum].type == 4)
	{
	}
}
function NCSToolbar_MouseDown(itemNum)
{
	this.click(itemNum);
	
	if (this.items[itemNum].type == 0 || 
		this.items[itemNum].type == 1 )
	{
		// If this is an exclusive button, turn all the others off also
		if (this.items[itemNum].bExclusive) 
		{
			for (i=0; i<this.items.length; i++)
			{
				if (this.items[i].bExclusive)
				{
					var element = document.getElementById(this.items[i].uid);
					if (element == null) alert("Null element at id : " + i);
					element.src = this.items[i].imageNormal;
				}
			}
		}
		var element = document.getElementById(this.items[itemNum].uid);
		element.src = this.items[itemNum].imageSelect;
	}
}
function NCSToolbar_MouseUp(itemNum)
{
	if (this.items[itemNum].type == 0 || 
		this.items[itemNum].type == 1 )
	{
		if (this.items[itemNum].bExclusive)
		{
		}
		else 
		{
			var element = document.getElementById(this.items[itemNum].uid);
			element.src = this.items[itemNum].imageNormal;
		}
	}
}
function NCSToolbar_UpdateStatus(itemNum)
{
	if (this.bShowStatusBar)
	{
	   var status = document.getElementById("statusText");
	   status.innerHTML = this.items[itemNum].name;
    }
    else {
        window.status = this.items[itemNum].name;
    }
}
function NCSToolbar_SetCurrentItem(itemUID)
{
	var i = 0;
	for (i=0; i< this.items.length; i++)
	{
		var item = this.items[i];
		if (item.uid == itemUID)
		{
			this.mouseDown(i);
			this.mouseUp(i);			
			break;
		}
	}
}
function NCSToolbar_GetTable()
{
	return document.getElementById(this.uid);
}
function NCSToolBar_IsMultipleOf(x,y)
{
	if (x<y) return false;
	if (x==y) return true;
	// if x a multiple of y? ie, is 4 a multiple of 2, yes. Is 6 a multiple of 4 - no.
    var remainder = (x/y) - Math.floor(x/y);
    return (remainder == 0.0 ? true : false);
}
function NCSToolbar_Rebuild()
{
	if (this.colums == -1)
	{
		this.colums = this.items.length;
    }
	text = "<TABLE id='" + this.uid + "' BORDER=0 CELLSPACING=0 CELLPADDING=0>";

	var nCurrentRowItem = 0;

	for (i=0; i< this.items.length; i++)
	{
		var item = this.items[i];

        if (nCurrentRowItem == 0 || NCSToolBar_IsMultipleOf(nCurrentRowItem, this.colums))
        {
           text += "<TR>";
        }
        nCurrentRowItem ++;

		if (item.type == 0) {
			text += "<TD>";
			text += "<image id='" + item.uid + "' title='" + item.name + "' src='" + item.imageNormal + "' border=0 onMouseOut='" + this.myself + ".mouseUp(" + i + ");' onMouseUp='" + this.myself + ".mouseUp(" + i + ");' onMouseDown='" + this.myself + ".mouseDown(" + i + ");'  onMouseOver='" + this.myself + ".updateStatus(" + i + ");' >";
            text += "</TD>";
		}
		else if (item.type == 1)
		{
			text += "<td>&nbsp;&nbsp;</td>"
		}
		else if (item.type == 2)
		{
			text += "<TD><INPUT type='checkbox' id='" + item.uid + "' name=checkbox1 onClick='" + this.myself + ".click(" + i + ");'>" + item.name + "</TD>";
		}
		else if (item.type == 3)
		{
			text += "<TD>" + item.name + " <SELECT id='" + item.uid + "' name=select1 onChange='" + this.myself + ".click(" + i + ");'>";
			for (itemNum=0; itemNum<item.strings.length; itemNum++)
			{
				text += "<OPTION>" + item.strings[itemNum] + "</OPTION>";
			}
			text += "</SELECT></TD>";
		}
		else if (item.type == 4)
		{
			text += "<td><LABEL>" + item.name + "</LABEL></td>"
		}
		else if (item.type == 5)
		{
			text += "<td>" + item.name + "</td>"
		}
		
        if (NCSToolBar_IsMultipleOf(nCurrentRowItem, this.colums) || (i == this.items.length))
        {
        	text += "</TR>";
        }
	}

	if (this.bShowStatusBar)
	{
		var colspan = (this.colums != -1 ? this.colums : this.items.length);
		var colspan = 4;
		text += "<TR><TD colspan= " + colspan  + "><FONT face=Arial size=2><DIV id='statusText'>This is the status bar</DIV></FONT></TD</TR>";
	}

	text += "</TABLE>";
	
	if (this.element)
	{
		this.element.innerHTML = text;
    }

	return text;
}

NCSToolbar.prototype                = new NCSControl();
NCSToolbar.prototype.constructor    = NCSToolbar;
NCSToolbar.superclass               = NCSControl.prototype;
NCSToolbar.prototype.build          = NCSToolbar_Rebuild;
NCSToolbar.prototype.rebuild          = NCSToolbar_Rebuild;
NCSToolbar.prototype.addButton		= NCSToolbar_AddButton;
NCSToolbar.prototype.addCheckBox	= NCSToolbar_AddCheckbox;
NCSToolbar.prototype.addPopdownCombo= NCSToolbar_AddPopdownCombo;
NCSToolbar.prototype.addSpace		= NCSToolbar_AddSpace;
NCSToolbar.prototype.addLabel		= NCSToolbar_AddLabel;
NCSToolbar.prototype.addCustomObject= NCSToolbar_AddCustomObject;
NCSToolbar.prototype.updateStatus	= NCSToolbar_UpdateStatus;
NCSToolbar.prototype.click			= NCSToolbar_Click;
NCSToolbar.prototype.mouseDown		= NCSToolbar_MouseDown;
NCSToolbar.prototype.mouseUp		= NCSToolbar_MouseUp;
NCSToolbar.prototype.setCurrentItem = NCSToolbar_SetCurrentItem;
NCSToolbar.prototype.getTable		= NCSToolbar_GetTable;
