﻿/* 
* functions to create toolbar / modified to allow mouse over events
*/


function NCSCreateJSToolbar(NCSView, target)
{

    var ECWToolbarCB = function(uid, value)
    {

        var map = document.ECWView1;

        if (uid == "UID_VIEW_PAN")
        {
            map.SetPointerMode(0);
        }
        else if (uid == "UID_VIEW_ZOOM")
        {
            map.SetPointerMode(1);
        }
        else if (uid == "UID_VIEW_ZOOMBOX")
        {
            map.SetPointerMode(3);
        }
        else if (uid == "UID_VIEW_POINTER")
        {
            map.SetPointerMode(2);
        }

        else if (uid == "UID_VIEW_RESET")
        {
            map.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",
"images2/pan_off.gif", "images2/pan.gif", true, ECWToolbarCB, "UID_VIEW_PAN");
    ECWToolbar1.addButton("Zoom in or out of the image interactively by click-dragging the magnifier on the image",
"images2/zoom_off.gif", "images2/zoom.gif", true, ECWToolbarCB, "UID_VIEW_ZOOM");
    ECWToolbar1.addButton("Zoom into the image by click-dragging a zoom box over the image",
"images2/zoomBox_off.gif", "images2/zoomBox.gif", true, ECWToolbarCB, "UID_VIEW_ZOOMBOX");
    ECWToolbar1.addButton("Select pointer mode to profile or select image features",
"images2/pointer_off.gif", "images2/pointer.gif", true, ECWToolbarCB, "UID_VIEW_POINTER");
    ECWToolbar1.addSpace("SPACE", "UID_VIEW_SPACE1");
    ECWToolbar1.addButton("Reset the image to maxiumum extents",
"images2/reset_extents_off.gif", "images2/reset_extents.gif", 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.active = false;
    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;

                    this.items[i].active = false;
                }
            }
        }

        this.items[itemNum].active = true;

        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)
        {
            // if this isn't the currently selected item, make the image normal again
            if (!this.items[itemNum].active)
            {
                var element = document.getElementById(this.items[itemNum].uid);
                element.src = this.items[itemNum].imageNormal;

            }


        }
        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;
    }

    // show active image for mouseover effect
    var element = document.getElementById(this.items[itemNum].uid);
    element.src = this.items[itemNum].imageSelect;

}




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);

            var element = document.getElementById(this.items[i].uid);
            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 valign='middle' id='" + this.uid + "' BORDER=0 CELLSPACING=5 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;


