//-------- LAYER SUPPORT FUNCTIONS --------------

// clip layer display to clipleft, cliptip, clipright, clipbottom


function clipTTLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getTTLayer(name);
	  if (parent.isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }	  else {
		    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight;
			layer.width	= newWidth;
			//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
			//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
			//alert(cliptop + " " + clipleft);
			layer.top	= cliptop  + "px";
			layer.left	= clipleft + "px";
			//layer.display= "none";
		}

}


// Create a DHTML layer
function createTTLayer(name, inleft, intop, width, height, visible, content) {
	  var layer;
	  if (parent.isNav4) {
	  
	    document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
	    document.writeln(content);
	    document.writeln('</layer>');
	  } else {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
}

// get the layer object called "name"
function getTTLayer(name) {
	  if (parent.isNav4)
	    return(document.layers[name]);
	  else if (parent.isIE4) {
	    layer = eval('document.all.' + name + '.style');
	    return(layer);
	  } else if (parent.is5up) {
		var theObj = document.getElementById(name);
		return theObj.style
	  }
	  else
	    return(null);
}

// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getTTLayer(name);		
	
	if (layer != null) {
	
	 	if (parent.isNav4){
    		layer.visibility = "hide";
		} else {
   			 layer.visibility = "hidden";
		}
	}
}

// move layer to x,y
function moveTTLayer(name, x, y) {	
  	var layer = getTTLayer(name);		
	if (layer != null) {
	  	if (parent.isNav4)
    		layer.moveTo(x, y);
	 	else if (parent.isIE) {
    		layer.left = x + "px";
   			layer.top  = y + "px";
		} else {
			layer.height = 20;
			layer.width	= 20;
    		layer.left = x + "px";
   			layer.top  = y + "px";
	  	}
	}
}

// replace layer's content with new content
function replaceTTLayerContent(name, content, uLeft,uTop,rgt,btm) {
	/* var idx = content.indexOf('<BR>',0);
	var newBWidth;
	if (content.length - idx - 4 > idx)
		newBWidth = (content.length - idx - 4) * 7;
	else
		newBWidth = idx * 7;
	*/
	content = '<img src="' + content + '">';
	  if (parent.isNav4) {
		    var layer = getTTLayer(name);
			if (layer != null) {
			    layer.document.open();
			    layer.document.writeln(content);
			    layer.document.close();
			}
	  }  else if (parent.isIE) {
			if (eval("document.all." + name) != null) {
		  		content = content.replace(/\'/g,"\\'");
			    var str = "document.all." + name + ".innerHTML = '" + content + "'";
			    eval(str);
	  		} 
	  } else if (parent.isNav) {
		content += "\n";
		if (document.getElementById){
			rng = document.createRange();
			el = document.getElementById(name);
			rng.setStartBefore(el);
			htmlFrag = rng.createContextualFragment(content);
			while (el.hasChildNodes()){
				el.removeChild(el.lastChild);
			}
			el.appendChild(htmlFrag);
		}
	  }
clipTTLayer(name,uLeft,uTop,rgt,btm);
}

// set layer background color
function setTTLayerBackgroundColor(name, color) {		
  	var layer = getTTLayer(name);		
	if (layer != null) {
	    if (parent.isNav) 
    		layer.bgColor = color;
		else 
    		layer.backgroundColor = color;
	}
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getTTLayer(name);		
	if (layer != null) {
	  	if (parent.isNav4)
    		layer.visibility = "show";
		else
   		 	layer.visibility = "visible";
	}
}


function createToolTips(toolName, xVal) {
	var fWidth = 0;
	var ttMsg;

	var docWidth = getDocumentWidth();
	var xValMax = docWidth-1;
	switch (toolName) {
	/*	replaceTTLayerContent('TTMSGBox', ttMsg, xVal,15);
	'TTMSGBox' is the text msg box created in top5.htm as a layer
	ttMsg is the msg based on the Case to display in TTMSGBOX
	xVal is the horizontal position value above the button calculated by getLeft
	in toolbar5.js 
	15 is the height (y position).  To make the box smaller, increase 15
	 
	 replaceTTLayerContent('TTMSGBox', 'ovtip.gif', xVal,15,xVal+102,67)
	       xVal is left, 15 is top ,xVal+102 is right (left + width of button = right),  and 67 is bottom (15 + height = bottom)
	 */
	    case 'HOME':
			window.status='Click here for the Home page';
			replaceTTLayerContent('TTMSGBox', 'images/logotip.gif', xVal+260,0,xVal+415,50);
			break;
		case 'BACK':
			window.status='Go back to previous page';
			replaceTTLayerContent('TTMSGBox', 'images/backtip.gif', xVal+160,3,xVal+315,53);
			break;
		 case 'MAPMAKER':
			window.status='Click here to erase your map and start over';
			replaceTTLayerContent('TTMSGBox', 'images/mapmakertip.gif', xVal+120,0,xVal+275,50);
			break;
		case 'OVMAP':
			//ttMsg = " Show or hide<BR>overview map  ";
			window.status='Show or hide the overview map';
			replaceTTLayerContent('TTMSGBox', 'images/ovtip.gif', xVal,10,xVal+205,67);
			break;
		case 'OVWIN':
		
			    replaceTTLayerContent('TTMSGBox', '../natlas/images/ovbigtip.gif', xVal,10,xVal+215,95);
	
			break;
		case 'OVWIN_FULL':
			    replaceTTLayerContent('TTMSGBox', '../natlas/images/ovbigtip_full.gif', xVal,10,xVal+215,95);
	
			break;
		case 'PAN':
			//ttMsg = "Drag mouse on map to outline a new area<BR>or Click on map to Re-Center";
			window.status='Click or click-and-drag on map to re-center';
			if (parent.state == 'PAN')
			   replaceTTLayerContent('TTMSGBox', 'images/movetip.gif', xVal-60,0,xVal+150,50);
			else
			   replaceTTLayerContent('TTMSGBox', 'images/movetip_off.gif', xVal-75,11,xVal+150,60);
	
			break;
		case 'ZOOMIN':
			//ttMsg = "Drag mouse on map to outline a new area<BR>or Click on map to Re-Center and Zoom In";
			window.status='Click or click-and-drag on map to re-center and zoom in';
			if (parent.state == 'ZOOMIN')
			   replaceTTLayerContent('TTMSGBox', 'images/zoomintip.gif', xVal-60,0,xVal+134,50);
			else
			   replaceTTLayerContent('TTMSGBox', 'images/zoomintip_off.gif', xVal-75,11,xVal+144,60);
			
			//replaceTTLayerContent('TTMSGBox', ttMsg, xVal,15);
			break;
		case 'ZOOMOUT':
			//ttMsg = "Drag mouse on map to outline a new area<BR>or Click on map to Re-Center and Zoom Out";
			window.status='Click or click-and-drag on map to re-center and zoom out';
			if (parent.state == 'ZOOMOUT')
			   replaceTTLayerContent('TTMSGBox', 'images/zoomouttip.gif', xVal-65,3,xVal+129,67);
			else
			   replaceTTLayerContent('TTMSGBox', 'images/zoomouttip_off.gif', xVal-75,10,xVal+144,67);
			
			//replaceTTLayerContent('TTMSGBox', ttMsg, xVal,15);
			break;
		case 'STATEZOOM':
			//ttMsg = "select a state to<BR>Zoom to that State";
			window.status='select a state to Zoom to that State';
			replaceTTLayerContent('TTMSGBox', 'images/statezoomtip.gif', xVal-15,2,xVal+140,67);
			
			break;
		case '50ST':
			//ttMsg = "Click to view<BR>50 States";
			window.status='Click to view 50 states';
			replaceTTLayerContent('TTMSGBox', 'images/50sttip.gif', xVal-15,0,xVal+75,67);
			break;
		case 'IDENTIFY':
			window.status='Click button, then map, to learn about map location';
			if (parent.state == 'IDENTIFY')
			   replaceTTLayerContent('TTMSGBox', 'images/identifyontip.gif', xVal-95,20,xVal+150,49);
			else
			   replaceTTLayerContent('TTMSGBox', 'images/identifyofftip.gif', xVal-115,20,xVal+186,50);
			break;
		case 'REDRAW':
			window.status='Click to draw the checked layers';
			replaceTTLayerContent('TTMSGBox', 'images/redrawtip.gif', xVal+75,4,xVal+280,29);
			break;
        
		case 'FINDFEATURE':
			window.status='Find features by searching selected map layers';
			replaceTTLayerContent('TTMSGBox', 'images/findfeaturetip.gif', 25,xVal-32,286,xVal);
			break;
		case 'FIND':
			window.status='Find Information or Location on the Map';
			fWidth = getFrameWidth(parent.ToolFrame)+xVal;
			replaceTTLayerContent('TTMSGBox', 'images/findfplacetip.gif',25,xVal-32,286,xVal );
			
			break;
		case 'FIRST':
			window.status='How to draw your first map';
			fWidth = getFrameWidth(parent.ToolFrame)+xVal;
			replaceTTLayerContent('TTMSGBox', 'images/firsttip.gif', xVal-40,0,xVal+133,53);
			
			break;
		case 'RESET':
			window.status='Click to clear selected layers from map';
			replaceTTLayerContent('TTMSGBox', 'images/resettip.gif', xVal-150,0,xVal+133,53);
			break;
        case 'LAYER':
			window.status='Show the list of map layers';
			fWidth = getFrameWidth(parent.ToolFrame)+xVal;
			replaceTTLayerContent('TTMSGBox', 'images/layertip.gif', fWidth-3,0,fWidth+113,53);
			break;
		case 'LEGEND':
			window.status='Show the map legend';
			fWidth = getFrameWidth(parent.ToolFrame)+xVal;
			
			replaceTTLayerContent('TTMSGBox', 'images/legendtip.gif', fWidth-6,0,fWidth+80,53);
			break;
		
		case 'FINDTAB':
			window.status='Find Information or Location on the Map';
			fWidth = getFrameWidth(parent.ToolFrame)+xVal;
			replaceTTLayerContent('TTMSGBox', 'images/findtip.gif',fWidth-30,0,fWidth+66,51 );
			
			break;
        case 'GO':
			window.status='Find any geographic name in the U.S.';
			replaceTTLayerContent('TTMSGBox', 'images/gotip.gif', xVal-122,0,xVal,42);
			break;
		
		case 'HELP':
			window.status='Get detailed help on the mapmaker';
			replaceTTLayerContent('TTMSGBox', 'images/helptip.gif', xVal-120,0,xVal,50);
			break;
		case 'PRINT':
			window.status='Print the current map and legend';
			replaceTTLayerContent('TTMSGBox', 'images/printtip.gif', xVal-120,0,xVal,50);
			break;
		case 'EMAIL':
			window.status='Save/Email Map';
			replaceTTLayerContent('TTMSGBox', 'images/emailtip.gif', xVal-125,0,xVal,50);
			break;
		case 'SOUTH':
			window.status='Click to move south on the map';
			xValMax = xVal + 143;
			if (parent.isIE) {
				if (xValMax > docWidth) {
					xValMax = docWidth-1;
					xVal = docWidth - 144;
				}
			}
			replaceTTLayerContent('TTMSGBox', 'images/southtip.gif', xVal,0,xValMax,16);
			break;
		case 'NORTH':
			window.status='Click to move north on the map';
			xValMax = xVal + 143;
			if (parent.isIE) {
				if (xValMax > docWidth) {
					xValMax = docWidth-1;
					xVal = docWidth - 144;
				}
			}
			replaceTTLayerContent('TTMSGBox', 'images/northtip.gif', xVal,0,xValMax,16);
			break;
		case 'NORTHEAST':
		    
			window.status='Click to move northeast on the map';
			replaceTTLayerContent('TTMSGBox', 'images/netip.gif',xVal-172,0,xVal,16);
			break;
		case 'NORTHWEST':
			window.status='Click to move northwest on the map';
			replaceTTLayerContent('TTMSGBox', 'images/nwtip.gif', 0,0,162,16);
			break;
		case 'SOUTHWEST':
			window.status='Click to move southwest on the map';
			replaceTTLayerContent('TTMSGBox', 'images/swtip.gif', 0,0,162,16);
			break;
		case 'SOUTHEAST':
			window.status='Click to move southeast on the map';
			
			replaceTTLayerContent('TTMSGBox', 'images/setip.gif', xVal-172,0,xVal,16);
			break;
		case 'WEST':
			window.status='Click to move west on the map';
			replaceTTLayerContent('TTMSGBox', '../natlas/images/westtip.gif', 0,xVal,139,xVal+16);
			break;
		case 'EAST':
		    
			   replaceTTLayerContent('TTMSGBox', '../natlas/images/easttip.gif', parent.MapFrame.iWidth-149,xVal,parent.MapFrame.iWidth,xVal+16);
			
			window.status='Click to move east on the map';
			break;
		}	
			
	
	showLayer('TTMSGBox');
	}
	
function getRealLeft(elem)
{
   xPos = eval(elem).offsetLeft;
   tempEl = eval(elem).offsetParent;
     while (tempEl != null) {
          xPos += tempEl.offsetLeft;
          tempEl = tempEl.offsetParent;
     }
   return xPos;
}

function getRealTop(elem)
{
   yPos = eval(elem).offsetTop;
   tempEl = eval(elem).offsetParent;
     while (tempEl != null) {
          yPos += tempEl.offsetTop;
          tempEl = tempEl.offsetParent;
     }
   return yPos;
}
function getDocumentWidth(){
	//opera Netscape 6 Netscape 4x Mozilla 
	if (window.innerWidth){ 
		docwidth = window.innerWidth; 
	} 
	//IE Mozilla 
	if (document.body.clientWidth){ 
		docwidth = document.body.clientWidth; 
	} 
	return docwidth;
}
function getDocumentHeight(){
	//opera Netscape 6 Netscape 4x Mozilla 
	if (window.innerHeight){ 
		docheight = window.innerHeight; 
	} 
	//IE Mozilla 
	if (document.body.clientHeight){ 
		docheight = document.body.clientHeight; 
	} 
	return docheight;
}
function getMouseX(e)
{
// e gives access to the event in all browsers
	if (!e) var e = window.event;
		//if (document.layers) {
	if (parent.isNav) {
		mouseX=e.pageX;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
	}

	return mouseX;
}	
function getMouseY(e)
{
// e gives access to the event in all browsers
	if (!e) var e = window.event;
		//if (document.layers) {
	if (parent.isNav) {
		mouseY=e.pageY;
	} else {
		mouseY=event.clientY + document.body.scrollTop;
	}

	return mouseY;
}
function getFrameWidth(obj){
	//opera Netscape 6 Netscape 4x Mozilla 
	if (obj.window.innerWidth){ 
		docwidth = obj.window.innerWidth; 
	} 
	//IE Mozilla 
	if (obj.document.body.clientWidth){ 
		docwidth = obj.document.body.clientWidth; 
	} 
	return docwidth;
}

