/**
 * 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.
 */
 var schrgWin=null;
function NCSToolbar(uid, objectID, bShowStatusBar, nCols) {
    if (arguments.length > 0) {
		this.init(uid, objectID);
    }
    this.ncsView=null;
    this.currentUID=null;
    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, this.items[itemNum].uid);
	}
	else if (this.items[itemNum].type == 2)
	{
		var element = document.getElementById(this.items[itemNum].uid);
		this.items[itemNum].action(this, 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, 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_OLD()
{
	if (this.colums == -1)
	{
		this.colums = this.items.length;
    }
	text = "<TABLE id='" + this.uid + "' BORDER=0 CELLSPACING=1 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 += "<TR><TD>";
			//if (item.uid=="UID_VIEW_EXPORT" || item.uid=="UID_VIEW_PRINT") {
			if (item.uid=="UID_VIEW_EXPORT" || item.uid=="UID_VIEW_GEOREFEXPORT") {
        text += "<a id=\""+(item.uid=="UID_VIEW_EXPORT"?"expLink":(item.uid=="UID_VIEW_GEOREFEXPORT"?"expGeoRefLink":"priLink"))+"\" href=\"printexport.php\" onMouseOut='" + this.myself + ".mouseUp(" + i + ");return true;' onMouseUp='" + this.myself + ".mouseUp(" + i + ");' onMouseOver='"+((item.uid=="UID_VIEW_EXPORT" || item.uid=="UID_VIEW_GEOREFEXPORT")?"ECWExportURL":"ECWPrintURL")+"("+(item.uid=="UID_VIEW_GEOREFEXPORT"?"true":"")+");" + this.myself + ".updateStatus(" + i + ");return true;' "+(item.uid=="UID_VIEW_PRINT"?"target='_blank'":"")+">"+
                "<image id='" + item.uid + "' title='" + item.name + "' src='" + item.imageNormal + "' border=0>"+
                "</a>";
      }
      else text += "<image id='" + item.uid + "' title='" + item.name + "' src='" + item.imageNormal + "' border=0 onMouseOut='" + this.myself + ".mouseUp(" + i + ");return true;' onMouseUp='" + this.myself + ".mouseUp(" + i + ");' onMouseDown='" + this.myself + ".mouseDown(" + i + ");'  onMouseOver='" + this.myself + ".updateStatus(" + i + ");' >";
      text += "</TD></TR>";
		}
		else if (item.type == 1)
		{
			text += "<tr><td>&nbsp;&nbsp;</td></tr>"
		}
		else if (item.type == 2)
		{
			text += "<TR><TD><INPUT type='checkbox' id='" + item.uid + "' name=checkbox1 onClick='" + this.myself + ".click(" + i + ");'>" + item.name + "</TD></TR>";
		}
		else if (item.type == 3)
		{
			text += "<TR><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></TR>";
		}
		else if (item.type == 4)
		{
			text += "<tr><td><LABEL>" + item.name + "</LABEL></td></tr>"
		}
		else if (item.type == 5)
		{
			text += "<tr><td>" + item.name + "</td></tr>"
		}
	/*
        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;
}
function NCSToolbar_Rebuild()
{
	if (this.colums == -1)
	{
		this.colums = this.items.length;
    }
	text = "";//"<TABLE id='" + this.uid + "' BORDER=0 CELLSPACING=1 CELLPADDING=0>";

	var nCurrentRowItem = 0;

	for (i=0; i< this.items.length; i++)
	{
		var item = this.items[i];
        nCurrentRowItem ++;

		if (item.type == 0) {
			text += "<span>";//"<TR><TD>";
			if (item.uid=="UID_VIEW_EXPORT" || item.uid=="UID_VIEW_GEOREFEXPORT") {
        text += "<a id=\""+(item.uid=="UID_VIEW_EXPORT"?"expLink":(item.uid=="UID_VIEW_GEOREFEXPORT"?"expGeoRefLink":"priLink"))+"\" href=\"printexport.php\" onMouseOut='" + this.myself + ".mouseUp(" + i + ");return true;' onMouseUp='" + this.myself + ".mouseUp(" + i + ");' onMouseOver='"+((item.uid=="UID_VIEW_EXPORT" || item.uid=="UID_VIEW_GEOREFEXPORT")?"ECWExportURL":"ECWPrintURL")+"("+(item.uid=="UID_VIEW_GEOREFEXPORT"?"true":"")+");" + this.myself + ".updateStatus(" + i + ");return true;' "+(item.uid=="UID_VIEW_PRINT"?"target='_blank'":"")+">"+
                "<image id='" + item.uid + "' title='" + item.name + "' src='" + item.imageNormal + "' border=0>"+
                "</a>";
      }
      else text += "<image id='" + item.uid + "' title='" + item.name + "' src='" + item.imageNormal + "' border=0 onMouseOut='" + this.myself + ".mouseUp(" + i + ");return true;' onMouseUp='" + this.myself + ".mouseUp(" + i + ");' onMouseDown='" + this.myself + ".mouseDown(" + i + ");'  onMouseOver='" + this.myself + ".updateStatus(" + i + ");' >";
      text += "</span>";//"</TD></TR>";
		}
		else if (item.type == 1)
		{
			text += "<span>&nbsp;&nbsp;</span>";//"<tr><td>&nbsp;&nbsp;</td></tr>"
		}
		else if (item.type == 2)
		{
			//text += "<TR><TD><INPUT type='checkbox' id='" + item.uid + "' name=checkbox1 onClick='" + this.myself + ".click(" + i + ");'>" + item.name + "</TD></TR>";
			text += "<span><INPUT type='checkbox' id='" + item.uid + "' name=checkbox1 onClick='" + this.myself + ".click(" + i + ");'>" + item.name + "</span>";
		}
		else if (item.type == 3)
		{
			//text += "<TR><TD>" + item.name + " <SELECT id='" + item.uid + "' name=select1 onChange='" + this.myself + ".click(" + i + ");'>";
			text += "<span>" + 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></span>";//</TD></TR>";
		}
		else if (item.type == 4)
		{
			//text += "<tr><td><LABEL>" + item.name + "</LABEL></td></tr>"
			text += "<span><LABEL>" + item.name + "</LABEL></span>"
		}
		else if (item.type == 5)
		{
			//text += "<tr><td>" + item.name + "</td></tr>"
			text += "<span>" + item.name + "</span>"
		}
	/*
        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 += "<span><FONT face=Arial size=2><DIV id='statusText'>This is the status bar</DIV></FONT></span>";
	}

	//text += "</TABLE>";

	if (this.element)
	{
		this.element.innerHTML = text;
    }
	return text;
}
NCSToolbar.StrgDown=false;
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;
NCSToolbar.prototype.setNCSView		= function(NCSView) {
  this.ncsView=NCSView;
}
NCSToolbar.prototype.ECWSetPointerMode=function(mode) {
  this.ncsView.SetPointerMode(mode);
  /*
  if (getCookie("NCSSampleCookie") == "Geolink" ) {
    document.ECWView2.SetPointerMode(mode);
  }
  */
}
function ECWToolbarCB(toolBar, uid, value){
  if (uid == "UID_VIEW_PAN")
	{
		toolBar.ncsView.SetPointerMode(0);
		toolBar.currentUID=uid;
	}
	else if (uid == "UID_VIEW_ZOOM")
	{
		toolBar.ncsView.SetPointerMode(1);
		toolBar.currentUID=uid;
	}
	else if (uid == "UID_VIEW_ZOOMBOX")
	{
		toolBar.ncsView.SetPointerMode(3);
		toolBar.currentUID=uid;
	}
	else if (uid == "UID_VIEW_POINTER")
	{
		toolBar.ncsView.SetPointerMode(2);
	}
	else if (uid == "UID_VIEW_RESET")
	{
		//toolBar.ncsView.SetExtentsAll();
    setViewExtentsAll(toolBar.ncsView);		
	}
	else if (uid == "UID_VIEW_PRINT")
	{
	  ECWPrintImage(toolBar.ncsView, "print");
	}
	else if (uid == "UID_VIEW_EXPORT")
	{
	  ECWExportImage(toolBar.ncsView, "export");
	}	
	else if (uid == "UID_VIEW_SHOP")
	{
	  f_order(toolBar.ncsView);
	}
	else if (uid == "UID_VIEW_CAPTURE")
	{
	   captureView();
	}
  else if (uid == "UID_ANNOT_POLYGON" || uid == "UID_ANNOT_POLYLINE")
        {
          theStatus.showMeasure("","");
          theStatus.showArea("");
          ECWVectorLayer.clear();
	  ECWVectorLayer.objectNumPoints = 0;
	  if (ECWVectorLayer.captureCoord)
	    ECWVectorLayer.captureCoord.clear();
	  toolBar.ECWSetPointerMode(2);
	  toolBar.currentUID=uid;
	}
	else if (uid=="UID_SHOW_POINTS") {
          var position=getPosition("Ort");
          moveLayer("XY",position.x,position.y);
          showLayer("XY");
          hideLayer("Ort");
	}
	else if (uid == "UID_IMAGE_COMBO")
	{
		var actExtent=currentExtent;
		toolBar.ncsView.DeleteAllLayers();
		//var ImgInfos = null;
		var ImgInfos = oImgDict.getValue(value);
		var ImgCmnt = new String(ImgInfos[1]);
		while(ImgCmnt.search("#") > -1)
		{
			ImgCmnt = ImgCmnt.replace('#', String.fromCharCode(10,13));
		}
		//document.all.Comment.innerText = ImgCmnt;

		if (toolBar.ncsView.AddLayer("ECW", ImgInfos[0], "RasterLayer", "") < 0 )
		{
			alert(toolBar.ncsView.GetLastErrorText());
		}
		else {
              ECWToggleLogo(true);
              ECWLoadOverview(ImgInfos[0]);
              ECWToolbar1.setCurrentItem(toolBar.currentUID);
              if (actExtent) {
                setExtent(actExtent);
              }
            }
	}
	else if (uid == "UID_HILFE") {
    setMeineHilfe();    
  }
  else if (uid=="UID_SRLB") {
    var xmin = toolBar.ncsView.GetTopLeftWorldCoordinateX();
    var ymin = toolBar.ncsView.GetBottomRightWorldCoordinateY();
    var xmax = toolBar.ncsView.GetBottomRightWorldCoordinateX();
    var ymax = toolBar.ncsView.GetTopLeftWorldCoordinateY();   
    var x=xmin+(xmax-xmin)/2;
	var y=ymin+(ymax-ymin)/2;
	//if (!schrgWin||schrgWin.closed)
	if (schrgWin&&!schrgWin.closed)schrgWin.close();
      schrgWin=window.open("/aerowest/aerowest.php?X="+x+"&Y="+y,"schrgWin","status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=no,hotkeys=yes,dependent=yes");
	/*else {
	  alert(schrgWin.zoomToScale);
	  schrgWin.zoomToScale(1000,x,y);
	}
	*/
	//schrgWin.focus();
  }
}
function setMeineHilfe() {  
  var aDiv=getObject("meineHilfe");
  if (aDiv) {
    aDiv.style.display="block";
    aDiv.innerHTML=unescape(hilfText);
    //moveLayer("meineHilfe",0,0);
  }
}
function closeDiv() {
  var aDiv=getObject("meineHilfe");
  if (aDiv) aDiv.style.display="none";
}
function getKeyCode(e) {
  var keynum;
  var keychar;
  var numcheck;
  //alert(window.event+"-->"+parent.frmHeader.event+"-->"+parent.frmHeader.event.keyCode);
  if(window.event) // IE
    return window.event.keyCode;
  else if (parent && parent.frmHeader && parent.frmHeader.event)
    return parent.frmHeader.event.keyCode;
  else if(e.which) // Netscape/Firefox/Opera
    return e.which
}
function KeyDown(e) {
  if (getKeyCode(e)==17)
    NCSToolbar.StrgDown=true;
}
function KeyUp(e) {
  if (getKeyCode(e)==17)
    NCSToolbar.StrgDown=false;
}
window.document.onkeydown=KeyDown;
window.document.onkeyup=KeyUp;
if (parent && parent.frmHeader) {
  parent.frmHeader.document.onkeydown=KeyDown;
  parent.frmHeader.document.onkeyup=KeyUp;
}
