var CvX = 200;
var CvY = 130;
var lastUrlFocus = "";
var bUrlOn = false;
var InternalList = "";
var colors = [[255, 0 ,0],
			[0,255,0],
			[0,0,255],
			[255,0,255],
			[0,255,255],
			[255,255,0],
			[255, 127 ,0],
			[127,255,0],
			[0,127,255],
			[255,127,255],
			[127,255,255],
			[255,255,127],
			[255, 20 ,0],
			[255, 40 ,0],
			[255, 60 ,0],
			[255, 80 ,0],
			[255, 100 ,0],
			[255, 120 ,0],
			[255, 140 ,0],
			[255, 160 ,0],
			[255, 180 ,0],
			[255, 200 ,0],
			[255, 220 ,0],
			[255, 240 ,0],
			[255, 20 ,90],
			[255, 40 ,90],
			[255, 60 ,90],
			[255, 80 ,90],
			[255, 100 ,90],
			[255, 120 ,90],
			[255, 140 ,90],
			[255, 160 ,90],
			[255, 180 ,90],
			[255, 200 ,90],
			[255, 220 ,90],
			[255, 240 ,90],
			[0,255,0],
			[0,0,255],
			[255,0,255],
			[0,255,255],
			[255,255,0],
			[255, 127 ,0],
			[127,255,0],
			[0,127,255],
			[255,127,255],
			[127,255,255],
			[255,255,127],
			[0,0,0],
			[50,50,50],
			[100,100,100],
			[150,150,150],
			[200,200,200],
			[255,255,255]];
	
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
var mouseMoveCount = 0;
var ActionType = 1;
var InnerTooltip = new Array;
var InnerTooltipEmp = new Array;
var lastRmid = "";
var BaseLayers = "";
var CurrentController = "rooms";
var zoomLevel = 25;
var DrawingId = 2;
var FloorId = 2;
var currentDrawing = "";

function OpenDrawing(drawingName)
{
	if (drawingName != currentDrawing)
		{
		document.applets[0].OpenFile(drawingName);
		currentDrawing = drawingName;
		lastRmid = "";
		InitLayers();
		document.applets[0].Redraw();
		}
}

function SetDrawingId(id, floor, drawingName)
{
	if (DrawingId != id)
		{
		FloorId = floor;
		DrawingId = id;
		OpenDrawing(drawingName);
		}
}

function SetRoomAndDrawingId(room, id, floor, drawingName)
{
	SetDrawingId(id, floor, drawingName);
	HighlightRoom(room, 1);
	setTimeout( "document.applets[0].Redraw();", 2500);
}

function GetDrawingId()
{
	return DrawingId;
}

function SetActionType(action)
{
	ActionType = action;
}

function SetZoomLevel(zoom)
{
	zoomLevel = zoom;
}

function SetCurrentController(cont)
{
   	if ((cont.length) && (cont != "undefined"))
   		{
		CurrentController = cont;
		}
}

function ResetDrawing()
{
	InitLayers();
	lastRmid = "";
	document.applets[0].ZoomExtents();
	document.applets[0].Redraw();
}

function ZoomToLayer(wksfid)
{
	var wksfidLayer = "NODE_" + wksfid;
	document.applets[0].ZoomExtents(wksfidLayer);
        UpdateRooms(wksfidLayer);
}

function ZoomNamedLayer(layerlst)
{
	document.applets[0].ZoomExtents(layerlst, 95);	
	setTimeout( "document.applets[0].Redraw();", 250);
}

function UpdateRooms(layerList)
{
	InitLayers();
	document.applets[0].OnLayers(layerList);
	document.applets[0].Redraw();
}

function UpdateRoom(wksfid)
{
	var newLayer = "NODE_" + wksfid;
	InitLayers();
	document.applets[0].OnLayers(newLayer);
	document.applets[0].Redraw();
}

function ClearHighlight()
{
    InitLayers()
    document.applets[0].Redraw();
}

function ZoomExtents()
{
    document.applets[0].ZoomExtents();
}

function SetBaseLayers(layers)
{
	BaseLayers = layers;
}

function LoadEnd(junk)
{
      ResetDrawing()
//	showoffice();
	ClearInternalList();
	
	allLayers = document.applets[0].RetrieveOnLayers();
	// parse layer list to find space layers, set alt URL txt on each layer
	var layerArray = allLayers.split(';');
	for (var i = 0; i < layerArray.length; i++) 
		{
		if (layerArray[i].indexOf("NODE_") == 0) 
			{
			var nodeId = layerArray[i].substr(5);
			// single quotes & closing paren ensure we match node name at end of URL
			var urlSubstring = "NODE_" + nodeId + ";";
			InternalList = InternalList + urlSubstring;
			}
		}
	document.applets[0].FillAllPolylinesOnLayer(InternalList, colors[1][0], colors[1][1], colors[1][2], false);
	document.applets[0].OnLayers(BaseLayers);
	document.applets[0].OffLayers(InternalList);
	InternalList = "";
}

// "0;DATOS;VIEWER;GROS_;RM_;RM_TXT;VERT_"

function InitLayers()
	{
    ClearLastRoom();
	ClearInternalList();
	document.applets[0].OnLayers(BaseLayers);
//	TooltipSetVisible(false);
	}

function ClearAll()
{
	InitLayers();
	document.applets[0].ZoomExtents();
}

function ClearDrawing()
{
	ClearDrawing("rooms");
}

function ClearDrawing(cont)
{
	SetCurrentController(cont);
	ClearLastRoom();
	ClearInternalList();
	document.applets[0].ZoomExtents();
	//setTimeout( "document.applets[0].Redraw();", 250);
}


function officeclick(wksfid)
{
    var callitem;
//    if (CurrentController == "employees")
//		callitem = "/employees/getroom/"+wksfid;
  //  else
		callitem = "/rooms/getroom/"+wksfid;
	var ps = 'FloorId=' +FloorId;
    new Ajax.Updater('left', callitem, {onSuccess: function(request){ClearAndHighlightRoom(wksfid,1, true)}, parameters: ps , asynchronous:true, evalScripts:true});	
//    InitLayers();
//    var newLayer = "NODE_" + wksfid;
//    document.applets[0].OnLayers(newLayer);
//    document.applets[0].Redraw();
}
	
function GrabInner(request, wksfid)
{
	//TooltipLocation(tempX+5, tempY-20);
	if (CurrentController == "employees")
		InnerTooltipEmp[wksfid] =  request.responseText;
	else
		InnerTooltip[wksfid] =  request.responseText;
}


function ToolTipInfoXY(foci, tx, ty)
{
}

function TooltipLocation(x, y)
  {
  // var DivRef = document.getElementById('TooltipDiv');
  // DivRef.style.top = y;
  // DivRef.style.left = x;
  }
  
function TooltipSetVisible(state)
  {
  }

function ClearLastRoom()
{
	if (lastRmid.length > 0)
		{
		var layer = Nodize(lastRmid);
		document.applets[0].OffLayers(layer);
		lastRmid = "";
		setTimeout( "document.applets[0].Redraw();", 250);
		}
}

function UnHighlightRoom(rmid)
{
	if (rmid == lastRmid)
		ClearLastRoom();
}

function SimpleHighlightRoom(rmid, clr)
{
	ClearInternalList();
	HighlightRoom(rmid, clr);
}

function HighlightRoom(rmid, clr)
{
	if (rmid != lastRmid)
		{
		var layer = Nodize(rmid);
		if (clr == 255)
			document.applets[0].FillAllPolylinesOnLayer(layer, 254, 254, 254,false);
		else
			document.applets[0].FillAllPolylinesOnLayer(layer, colors[clr][0], colors[clr][1], colors[clr][2],false);
		ClearLastRoom();
		document.applets[0].OnLayers(layer);
		lastRmid = rmid;
		setTimeout( "document.applets[0].Redraw();", 250);
		}
}

function ClearAndHighlightRoom(rmid, clr)
{
	ClearAndHighlightRoom(rmid, clr, false);
}

function ClearAndHighlightRoom(rmid, clr, zoom)
{
	var layer = Nodize(rmid);
	ClearInternalList();
	
	if (rmid != lastRmid)
		{
		ClearLastRoom();
		HighlightRoom(rmid, clr);
		}
		
	if (zoom)
		document.applets[0].ZoomExtents(layer, zoomLevel);
	setTimeout( "document.applets[0].Redraw();", 250);
}

function HighlightWksfid(wksfid)
{
	SimpleHighlightRoom(wksfid,1);
}

function Nodize(rmid)
{
	return "NODE_" + rmid;
}

function HighlightList(rooms)
{
    InitLayers();
	var strArray = [];
	strArray = rooms.split("name\":");
	for (var i=1; i < strArray.length; i++)
		{
		var subStr = [];
		subStr = strArray[i].split("\"");
		AddToInternalList(subStr[1])
		}
	ZoomAndHighlightInternalList(1)
	setTimeout( "document.applets[0].Redraw();", 250);
}

function ZoomAndHighlightInternalList(clr)
{
	document.applets[0].ZoomExtents(InternalList, 95);	
	HighlightInternalList(clr);
}

function HighlightInternalList(clr)
{
	if (clr >= 0)
		document.applets[0].FillAllPolylinesOnLayer(InternalList, colors[clr][0], colors[clr][1], colors[clr][2], false);
	document.applets[0].OnLayers(InternalList);
	setTimeout( "document.applets[0].Redraw();", 250);
}

function AddToInternalList(rmid)
{
	InternalList = InternalList + "NODE_" + rmid + ";";
}

function AddToInternalListClr(rmid, clr)
{
	var layer = Nodize(rmid);
	document.applets[0].FillAllPolylinesOnLayer(layer, colors[clr][0], colors[clr][1], colors[clr][2], false);
	InternalList = InternalList + "NODE_" + rmid + ";";
}

function DefineColorKey()
{
	var DivRef = document.getElementById('TooltipDiv');
    
 	var colortable = "";
	colortable = "<table align=\"center\"><tr><td><b>Employee Count Key</b></td></tr>"
	colortable = colortable + "<tr><td bgcolor=\"#00FF00\">Unoccupied</td></tr>"
	colortable = colortable + "<tr><td bgcolor=\"#0000FF\"><font color=\"white\">One Employee</font></td></tr>"
	colortable = colortable + "<tr><td bgcolor=\"#FF00FF\">Two Employees</td></tr>"
	colortable = colortable + "<tr><td bgcolor=\"#00FFFF\">Three Employees</td></tr></table>"
	DivRef.innerHTML=colortable;
	lastUrlFocus = "colorkey";
    DivRef.style.display = "block";
}

function ClearInternalList()
{
	document.applets[0].OffLayers(InternalList);
	InternalList = "";
	TooltipSetVisible(false);
}