/////		 Last Update @ 2005-03-03			  /////
/////		 Version: 5.0.2.6225				  /////
///////////////////////////////////////////////////////
//////   SuperMap IS WebControl Client JavaScript /////
//////         For Internet Explorer browser      /////
///////////////////////////////////////////////////////


// 获取 map image 对象.  //
function SMISGetMap(mapId)
{
	var _map = document.getElementById(mapId +"_MVIMAGE");
	return _map;
}

// Action 
function SMISActionStart(mapID, action)
{
	var _map = SMISGetMap(mapID);
	
	if(!BeforeActionStart(_map)){ return; }
	
	// 用于解决在 MyIE2 中出现的 ActionStart 之后，地图图片消失的异常bug。 //
	var v = _map.style.visibility;
	eval(mapID + "_SMISMapActionStart(_map, action)");
	_map.style.visibility = v;
	AfterActionStarted(_map);
}

function SMISMapActionStop(map)
{
	if(!BeforeActionStop(map)){ return; }
	
	map.onmousedown = null;
	map.onmousemove = null;
	map.onmouseup = null;
	map.onclick = null;
	map.ondblclick = null;
	map.style.cursor = "";
	map.bActionStarted = false;
	
	AfterActionStopped(map);
}

//***************************** Mouse Event ********************************

function SMISOnMouseDown()
{
	this.bActionStarted = true;

	var relativePos = SMISGetMouseMapRelativePosition(this);
	this.PointsParam.AddPoint(relativePos.x, relativePos.y);
	return false;
}


function SMISPanOnMouseDown()
{
	this.bActionStarted = true;

	// hide the customLayer. --ahnan 2004-07-08
	var CustomLayerID = this.id.replace("_MVIMAGE","") + "_divCustomLayer";
	var CustomLayer = document.getElementById(CustomLayerID);
	if(CustomLayer)
	{
		CustomLayer.style.visibility = "hidden";
	}
	
	var relativePos = SMISGetMouseMapRelativePosition(this);
	this.PointsParam.AddPoint(relativePos.x, relativePos.y);
	
	// set to absolute
	this.style.position = "absolute";
	this.style.left = this.offset.x;
	this.style.top = this.offset.y;
	return false;
}

function SMISOnMouseUp()
{
	var map = this;
	if(this.map)
	{
		map = this.map;
	}
	if(this.bActionStarted)
	{
		var relativePos = SMISGetMouseMapRelativePosition(this);
		SMISMapActionStop(map);
		this.PointsParam.AddPoint(relativePos.x, relativePos.y);
		var theform = document.forms[0];
		SMISCreateHiddenField(theform, map.id + "_Action", map.id + "_Action", map.CurrentAction);
		var pointsString = this.PointsParam.GeneratePointsString();
		SMISCreateHiddenField(theform, map.id + "_PointsParam", map.id + "_PointsParam",pointsString);
		
		map.DoPostBack();
	}
}

function SMISCircleOnMouseUp()
{
	if(this.bActionStarted)
	{
		var posEnd = SMISGetMouseMapRelativePosition(this);
		SMISMapActionStop(this);
		this.PointsParam.AddPoint(posEnd.x, posEnd.y);
//		var posStart = this.PointsParam.GetPoint(0);
//		var radius = Math.sqrt(Math.pow((posEnd.x - posStart.x), 2) +
//								 Math.pow((posEnd.y - posStart.y), 2));
//		this.PointsParam.AddPoint(radius,0);

		var theform = document.forms[0];
		SMISCreateHiddenField(theform, this.id + "_Action", this.id + "_Action", this.CurrentAction);
		var pointsString = this.PointsParam.GeneratePointsString();
		SMISCreateHiddenField(theform, this.id + "_PointsParam", this.id + "_PointsParam",pointsString);
		
		this.DoPostBack();
	}
}

function SMISRectOnMouseMove()
{
	if(this.bActionStarted)
	{
		var startPoint = this.PointsParam.GetPoint(0);
		if(startPoint)
		{
			startPoint.x += this.origin.x;
			startPoint.y += this.origin.y;
		}
		if(window.event.button == 1 &&  startPoint != null)
		{
			var absolutePos = SMISGetMouseAbsolutePosition();
			SMISDrawingRect(this, startPoint, absolutePos);
			return false;
		}
	}
}

//must return false ,otherwise mouseup event will not be invoked
function SMISPanOnMouseMove()
{	
	if(this.bActionStarted)
	{
		var startPoint = this.PointsParam.GetPoint(0);
		if(startPoint)
		{
			startPoint.x += this.origin.x;
			startPoint.y += this.origin.y;
		}
		if(window.event.button == 1 && startPoint != null)
		{
			var absolutePos = SMISGetMouseAbsolutePosition();
			
			if(!SMISCheckMousePosition(this))
			{
				return false;
			}
			
			SMISPaningImage(this,startPoint,absolutePos);
			return false;
		}
	}
}

//must return false ,otherwise mouseup event will not be invoked
function SMISCircleOnMouseMove()
{
	if(this.bActionStarted)
	{
		var startPoint = this.PointsParam.GetPoint(0);
		if(startPoint)
		{
			startPoint.x += this.origin.x;
			startPoint.y += this.origin.y;
		}
		if(window.event.button == 1 &&  startPoint != null)
		{
			var absolutePos = SMISGetMouseAbsolutePosition();
			SMISDrawingCircle(this, startPoint, absolutePos);
			return false;
		}
	}
}

function SMISPolygonOnMouseMove()
{
	if(this.bActionStarted)
	{
		var map = SMISGetInnerMap(this);
		var line = document.getElementById("SMISLine");
		if(line)
		{
			var absolutePos = SMISGetMouseAbsolutePosition();
			SMISDrawingPolygon(map, absolutePos, map.PolygonClosed);
		}
		return false;
	}
}

function SMISPolygonOnMouseClick()
{
	this.bActionStarted = true;
	
	var map = SMISGetInnerMap(this);
	var line = document.getElementById("SMISLine");
	if(!line)
	{
		SMISEnableVML();
		line = document.createElement("<v:polyline points=\"0,0\"/>");
		line.style.position = "absolute";
		line.style.visibility = "visible";
		line.id = "SMISLine";
		line.style.zIndex = this.parentElement.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.0></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid'></v:stroke>");
		document.body.appendChild(line);
		line.appendChild(fill);		
		line.appendChild(stroke);
		line.onmousemove = SMISPolygonOnMouseMove;
		line.onclick = SMISPolygonOnMouseClick;
		line.ondblclick = SMISPolygonOnDblClick;
		line.map = map;
		line.lineOffset = new SMISPoint(line.offsetLeft, line.offsetTop);
	}
	var relativePos = SMISGetMouseMapRelativePosition(map);
	map.PointsParam.AddPoint(relativePos.x, relativePos.y);
	
	return false;
}

function SMISPolygonOnDblClick()
{
	var map = SMISGetInnerMap(this);
//	var relativePos = SMISGetMouseMapRelativePosition(map);
	SMISMapActionStop(this);
	SMISMapActionStop(map);
//	map.PointsParam.AddPoint(relativePos.x, relativePos.y);
	var theform = document.forms[0];
	SMISCreateHiddenField(theform, map.id + "_Action", map.id + "_Action", map.CurrentAction);
	var pointsString = map.PointsParam.GeneratePointsString();
	SMISCreateHiddenField(theform, map.id + "_PointsParam", map.id + "_PointsParam",pointsString);
	map.DoPostBack();
}

//***************************** End Mouse Event ****************************

//Paning Image
function SMISPaningImage(map, startPoint, curPoint)
{
//	window.status = "startPoint:" + startPoint.x + "," + startPoint.y + "; curPoint:" + curPoint.x + "," + curPoint.y;
//	window.status += "map.id: " + map.id + "; map.style.left:" + map.style.left;
//	window.status += "map.offset.x: " + map.offset.x + ";map.offset.y:" + map.offset.y;
	var clipTop, clipRight, clipBottom, clipLeft;
	var currentLeft, currentTop, currentRight, currentBottom;
	
	// Calculate absolute current coordinates of the image
	currentLeft = map.offset.x + (curPoint.x - startPoint.x);
	currentTop = map.offset.y + (curPoint.y - startPoint.y);

	if(map.map)
	{
		map.style.left = currentLeft; 
		map.style.top = currentTop;	
		SMISClipPolygon(map, true, curPoint.x - startPoint.x, curPoint.y - startPoint.y);
		return;
	}

	try
	{
		currentRight = currentLeft + map.width;
		currentBottom = currentTop + map.height;
	}
	catch(e)
	{
		currentRight = currentLeft + map.map.width;
		currentBottom = currentTop + map.map.height;
	}
			
	// Check to see if image goes out of bounds, and if so, set the clipping 
	// parameters
/* */	
	if ( currentTop > map.offset.y )
		clipTop = 0;
	else
		clipTop = map.offset.y - currentTop;
			
	if (currentRight > (map.offset.x + map.width)) 
		clipRight = (map.offset.x + map.width) - currentLeft;
	else 
		clipRight = currentRight - currentLeft;
		
	if (currentBottom > (map.offset.y + map.height)) 
		clipBottom = (map.offset.y + map.height) - currentTop;
	else 
		clipBottom = currentBottom - currentTop;
			
	if (currentLeft > map.offset.x) 
		clipLeft = 0;
	else 
		clipLeft = map.offset.x - currentLeft;
/* */
	// Set the map image's style parameters to actually move the image	
	map.style.left = currentLeft; 
	map.style.top = currentTop;	
	map.style.clip = 'rect(' + clipTop + ' ' +  clipRight + ' ' + clipBottom + ' ' + clipLeft +')';
//	window.status += "currentLeft:" + currentLeft + ",currentTop:" + currentTop;
}

//***************************** Drawing ************************************
//Enable VML support
function SMISEnableVML()
{
	// todo: support ie5.0, ie5.5
	if(document.namespaces)
	{
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml")
	}
	if(document.styleSheets.length < 1)
	{
	var _oStyle = document.createElement("style");
	document.body.appendChild(_oStyle);
	}
	document.styleSheets.item(0).addRule("v\\:*", "behavior:url(#default#VML)");
}

function SMISDrawingRect(map, startPoint, curPoint)
{
	var rect = document.getElementById("SMISRect");
	if(!rect) 
	{
		SMISEnableVML();
		var rect = document.createElement("<v:rect/>");
		rect.id = "SMISRect";
		rect.style.position = "absolute";
		rect.style.visibility = 'visible';
		rect.style.zIndex = map.parentElement.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.1 color='#316AC5'></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid' Color='#316AC5' weight='1px'></v:stroke>");
		document.body.appendChild(rect);
		rect.appendChild(fill);		
		rect.appendChild(stroke);	
	}
	curPoint.x = Math.max(map.origin.x, Math.min(curPoint.x, map.origin.x + map.offsetWidth+2));
	curPoint.y = Math.max(map.origin.y, Math.min(curPoint.y, map.origin.y + map.offsetHeight+2));
	rect.style.top = Math.min(startPoint.y, curPoint.y);
	rect.style.left = Math.min(startPoint.x, curPoint.x);
	rect.style.width = Math.abs(curPoint.x - startPoint.x);
	rect.style.height =  Math.abs(curPoint.y - startPoint.y);
}

function SMISDrawingCircle(map, startPoint, curPoint)
{
	var circle = document.getElementById("SMISCircle");
	if(!circle)
	{
		SMISEnableVML();
		circle = document.createElement("<v:arc startangle='0' endangle='360'/>");
		circle.style.position = "absolute";
		circle.style.visibility = 'visible';
		circle.id = "SMISCircle";
		circle.style.zIndex = map.parentElement.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.3></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid' Color='blue'></v:stroke>");
		document.body.appendChild(circle);
		circle.appendChild(fill);
		circle.appendChild(stroke);
	}

	curPoint.x = Math.max(map.origin.x, Math.min(curPoint.x, map.origin.x + map.offsetWidth+2));
	curPoint.y = Math.max(map.origin.y, Math.min(curPoint.y, map.origin.y + map.offsetHeight+2));

	var radius = Math.sqrt(Math.pow((curPoint.x - startPoint.x), 2) + Math.pow((curPoint.y - startPoint.y), 2))

	circle.style.left = startPoint.x - radius;
	circle.style.top = startPoint.y - radius;
	
	circle.style.width = 2*radius;
	circle.style.height =  circle.style.width;
	
	var clipTop, clipRight, clipBottom, clipLeft;
	currentLeft = parseInt(circle.style.left);
	currentTop = parseInt(circle.style.top);
	currentRight = currentLeft + parseInt(circle.style.width);
	currentBottom = currentTop + parseInt(circle.style.height);

//	status = " currentLeft,top,right,bottom:" + currentLeft + "," + currentTop + "," + currentRight + "," + currentBottom + ",";
//	status += " map.origin.x,y,r,b:" + map.origin.x + "," + map.origin.y + "," + (eval(map.origin.x)+eval(map.width)) + "," + (map.origin.y+map.height);
			
	if ( currentTop > map.origin.y )
		clipTop = 0;
	else
		clipTop = map.origin.y - currentTop;
			
	if (currentRight > (map.origin.x + map.width)) 
		clipRight = (map.origin.x + map.width) - currentLeft;
	else if (currentLeft < 0)	// 圆的范围已经超出页面的左边界 
		clipRight = currentRight - currentLeft + 3;		// +3是因为要考虑圆弧本身的宽度。 
	else
		clipRight = currentRight;
		
	if (currentBottom > (map.origin.y + map.height)) 
		clipBottom = (map.origin.y + map.height) - currentTop;
	else if (currentTop < 0)	// 圆的范围已经超出页面的上边界 
		clipBottom = currentBottom - currentTop + 3;	// +3是因为要考虑圆弧本身的宽度。 
	else
		clipBottom = currentBottom;
			
	if (currentLeft > map.origin.x) 
		clipLeft = 0;
	else 
		clipLeft = map.origin.x - currentLeft;
	
//	status += " circle.style.clip:" + clipLeft + "," + clipTop + "," + clipRight + "," + clipBottom;

	circle.style.clip = 'rect(' + clipTop + ' ' +  clipRight + ' ' + clipBottom + ' ' + clipLeft +')';

}

function SMISDrawingPolygon(map,curPoint,polygonClosed)
{
	var line = document.getElementById("SMISLine");


	curPoint.x = Math.max(map.origin.x, Math.min(curPoint.x, map.origin.x + map.offsetWidth+2));
	curPoint.y = Math.max(map.origin.y, Math.min(curPoint.y, map.origin.y + map.offsetHeight+2));

	var offset = line.lineOffset;
	curPoint.x = curPoint.x - offset.x;
	curPoint.y = curPoint.y - offset.y;
	var tempOffset = new SMISPoint();
	tempOffset.x = map.origin.x - offset.x;
	tempOffset.y = map.origin.y - offset.y;
	if (polygonClosed) {
		firstPoint = map.PointsParam.GetPoint(0);
		firstPoint.x = firstPoint.x + tempOffset.x;
		firstPoint.y = firstPoint.y + tempOffset.y;
		line.points.value = map.PointsParam.GetPolyPointsString(tempOffset) + 
				" " + curPoint.x + "," + curPoint.y + " " +
				firstPoint.x + "," + firstPoint.y;
	} else {
		line.points.value = map.PointsParam.GetPolyPointsString(tempOffset) + 
				" " + curPoint.x + "," + curPoint.y;
	}
}

//***************************** End Drawing ******************************



function hideBusyFlag(mapControlID)
{
	if(mapControlID == null || typeof(mapControlID) == "undefined")
	{
		return;
	}

	var divLoading = document.getElementById(mapControlID+"_divLoading");
	if( divLoading != null && divLoading.style != null)
	{
		divLoading.style.visibility = "hidden";
		divLoading.style.zIndex = "-1";
	}
}

function showBusyFlag(mapControlID)
{
	if(mapControlID == null || typeof(mapControlID) == "undefined")
	{
		return;
	}

	var divLoading = document.getElementById(mapControlID+"_divLoading");
	if( divLoading != null && divLoading.style != null)
	{
		divLoading.style.zIndex = "999";
		divLoading.style.visibility = "visible";
	}
}

function SetMapAbsPosition(mapID)
{
	// offset 为内部元素定位所需要的相对偏移量 //
	// origin 为绝对座标 //
	var mapControl = document.getElementById(mapID.replace("_MVIMAGE",""));
	var map = document.getElementById(mapID);

	map.origin = SMISGetAbsolutePosition(map);
/*	
	if(mapControl.style.position.toLowerCase() != "absolute")
	{
		map.offset = map.origin; 
	}
	else
	{
		map.offset = new SMISPoint(0, 0);
	}
*/
	map.offset = SMISGetOffsetPosition(mapControl);
}

// for layerControl 
function CheckVisibleAll(layerControlID)
{
	//LayerControl1:LayerCount
	//LayerControl1:V0
	var layerCount = eval(document.getElementById(layerControlID + ":LayerCount").value);
	var layerVisible;
	for(var i=0; i<layerCount; i++)
	{
		layerVisible = document.getElementById(layerControlID + ":V" + i);
		if(layerVisible && !layerVisible.disabled)
		{
			layerVisible.checked = event.srcElement.checked;
		}
	}
}

function CheckQueryableAll(layerControlID)
{
	//LayerControl1:LayerCount
	//LayerControl1:Q0
	var layerCount = eval(document.getElementById(layerControlID + ":LayerCount").value);
	var layerQueryable;
	for(var i=0; i<layerCount; i++)
	{
		layerQueryable = document.getElementById(layerControlID + ":Q" + i);
		if(layerQueryable)
		{
			layerQueryable.checked = event.srcElement.checked;
		}
	}
}


function SMISPrintMap(mapControlClientID, mapControlPage, printControlPage, mapUrl, imageHandlerEnabled)
{
	if( !mapControlClientID || !mapControlPage || !printControlPage )
	{
		return false;
	}
	
	var printWin;
	
	if ( !mapUrl )
	{
		mapUrl = document.getElementById(mapControlClientID+"_MVIMAGE").src;
	}
	var address = document.getElementById(mapControlClientID+"_MapServer_Address").value;
	var port	= document.getElementById(mapControlClientID+"_MapServer_Port").value;

	printUrl = printControlPage + "?Address=" + address + "&Port=" + port + "&MapControlClientID=" + mapControlClientID + "&MapControlPage=" + mapControlPage + "&ImageHandlerEnabled=" + imageHandlerEnabled;
	printWin = window.open(printUrl, "", "resizable,toolbar,menubar,scrollbars,status" );
}


function SMISAdjustCustomDivPosition(customDivID, alignStyle)
{
//	window.status += customDivID + "," + alignStyle;
	var customDiv = document.getElementById(customDivID);
	var offsetX, offsetY;
	if(typeof(customDiv) != "undefined")
	{
		switch(alignStyle)
		{
			case "LeftTop":		offsetX = 0;	offsetY = 0;	break;
			case "LeftMiddle":	offsetX = 0;	offsetY = customDiv.offsetHeight / 2;	break;
			case "LeftBottom":	offsetX = 0;	offsetY = customDiv.offsetHeight;		break;
			case "CenterTop":	offsetX = customDiv.offsetWidth / 2;	offsetY = 0;	break;
			case "CenterMiddle":offsetX = customDiv.offsetWidth / 2;	offsetY = customDiv.offsetHeight / 2;	break;
			case "CenterBottom":offsetX = customDiv.offsetWidth / 2;	offsetY = customDiv.offsetHeight;		break;
			case "RightTop":	offsetX = customDiv.offsetWidth;		offsetY = 0;	break;
			case "RightMiddle":	offsetX = customDiv.offsetWidth;		offsetY = customDiv.offsetHeight / 2;	break;
			case "RightBottom":	offsetX = customDiv.offsetWidth;		offsetY = customDiv.offsetHeight;		break;
			default:			offsetX = 0;	offsetY = 0;	break;
		}
		customDiv.style.left = SMISGetNumFromPixel(customDiv.style.left) - offsetX;
		customDiv.style.top = SMISGetNumFromPixel(customDiv.style.top) - offsetY; 
//		window.status = offsetX + "," + offsetY;
//		window.status += "customDiv.offsetWidth:" + customDiv.offsetWidth;
//		window.status += "customDiv.style.left:" + customDiv.style.left;
	}
}

function SMISRegisterToolControl(toolControlID, mapControlID)
{
	var input = document.getElementById(mapControlID + "_ToolControls");
	if(typeof(input) != "undefined" && input.value.indexOf(toolControlID+",") < 0)	// 防止重复注册 //
	{
		input.value += toolControlID + ",";
	}
}

function SMISSwitchCurrentTool(map, action)
{
	
	var toolControl;
	var toolControlsID = map.id.replace("_MVIMAGE","") + "_ToolControls";
	var toolControls = document.getElementById(toolControlsID);
	
	if(typeof(toolControls) != "undefined" && toolControls != null)
	{
		var arrToolControls = toolControls.value.split(",");
		for(var i=0; i<arrToolControls.length; i++)
		{
			if(arrToolControls[i] == "")
			{
				continue;
			}
			
			toolControl = document.getElementById(arrToolControls[i]);
			if(typeof(toolControl) == "object" && toolControl.ActiveSrc != "" && toolControl.InactiveSrc != "")
			{
				if(toolControl.ActionName == action)
				{
					toolControl.src = toolControl.ActiveSrc;
				}
				else
				{
					toolControl.src = toolControl.InactiveSrc;
				}
			}
		}
	}
}

function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
} 

function SMISGetAbsolutePosition(element, tagName, pos)
{
	var _i=0, _j=0;
	var __DEBUG	= false;
	if(typeof(pos) == "undefined" || pos == null)
	{
		pos = new SMISPoint(0, 0);
	}
	
	while ( ! (element == null || element.tagName == "BODY" || element.tagName.toLowerCase() == tagName) ) //*/
	{
		_i+=1;
		
		pos.x += eval(element.offsetLeft);
		pos.y += eval(element.offsetTop);	

		var bFlag = typeof(element.style.borderLeftWidth) != "unknown";
		if(bFlag && element.style.borderLeftWidth != null && element.style.borderLeftWidth != "")
		{
			pos.x += SMISGetNumFromPixel(element.style.borderLeftWidth);
			pos.y += SMISGetNumFromPixel(element.style.borderTopWidth);

			if(__DEBUG) alert( "bFlag true & borderLeftWidth is not null. \n pos.Y = " + pos.y);
		}
				
		if(__DEBUG)	alert("current element id = " + element.id + "\n current element tagName = " + element.tagName);
		if(__DEBUG)	alert(element.offsetParent.tagName + "," + element.offsetLeft);

		element = element.offsetParent;
	}
	return pos;
}

//The point object
function SMISPoint(x,y)
{
	this.x = parseInt(x);
	this.y = parseInt(y);
}

//Get Current Mouse Absolute Position
function SMISGetMouseAbsolutePosition()
{

	var __DEBUG = false;
	if(__DEBUG)
	{
		window.status = "event.x:" + event.x + ", event.y:" + event.y;
		window.status += "event.clientX:" + event.clientX + ", event.clientY:" + event.clientY;
	}
	
	var x = event.clientX + document.body.scrollLeft;
	var y = event.clientY + document.body.scrollTop;
	return new SMISPoint(x,y);
}

//Get Current Mouse Map Relative Position 
function SMISGetMouseMapRelativePosition(map)
{
	var x, y;
	if(event)
	{
		x = event.clientX + document.body.scrollLeft; 
		y = event.clientY + document.body.scrollTop; 
	}
	if(map && map.origin)
	{
		x -= map.origin.x;
		y -= map.origin.y;
	}
	return new SMISPoint(x,y);
}


function SMISGetNumFromPixel(str)
{
	if(typeof(str) == "undefined" || str == null || str == "")
		return 0;

	var strResult;
	strResult = str.toString();
	try{
		// 1 pt = 4/3 px; 1 in = 72 pt = 96 px; 
		if(strResult.indexOf("pt") > 0)
		{
			strResult = strResult.replace("pt", "");
			strResult = eval(strResult) * 4.0 / 3.0;
		}
		else if(strResult.indexOf("in") > 0)
		{
			strResult = strResult.replace("in", "");
			strResult = eval(strResult) * 96;
		}
		else
		{
			strResult = strResult.replace("px", "");
			strResult = eval(strResult);
		}
	}catch(e){alert("error:" + e + ";strResult=" + strResult);}

	return strResult;
}


function SMISGetInnerMap(object)
{
	var _map = null;
	if (object.map) {
		_map = object.map;
	} else {
		_map = object;
	}
	return _map;
}

//Create input hidden field, if exists, then update its value.
function SMISCreateHiddenField(theform, id, name, value)
{
	var _elem = document.getElementById(id);
	if(_elem == null)
	{
		_elem = document.createElement("<input />");
		_elem.type = "hidden";
		_elem.id = id;
		_elem.name = name;
		_elem.value = value;
		theform.appendChild(_elem);
	}
	else
	{
		_elem.name = name;
		_elem.value = value;
	}
	return _elem;
}

function SMISPointsParam()
{
	this.Points = new Array();
	this.AddPoint = SMISPointsParamAddPoint;
	this.GetPoint = SMISPointsParamGetPoint;
	this.GetPolyPointsString = SMISGetPolyPointsString;
	this.GeneratePointsString = SMISGeneratePointsString;

	//format:x,y x,y .......................
	function SMISGetPolyPointsString(offset)
	{
		var pointString = "";
		for (i = 0; i < this.Points.length; i++) 
		{
			if (i>0)
			{
				pointString += " "
			}
			pointString += (this.Points[i].x + offset.x) + "," + (this.Points[i].y + offset.y);
		}
		return pointString;
	}

	//format: numofpoints,x y,x y,...........
	function SMISGeneratePointsString()
	{
		var pointString = "" + this.Points.length + ",";
		for (i = 0; i < this.Points.length; i++) 
		{
			if (i>0) {	pointString += ",";	}
			pointString += this.Points[i].x + " " + this.Points[i].y;		
		}
		return pointString;	
	}
}

function SMISPointsParamAddPoint(x,y)
{
	this.Points[this.Points.length] = new SMISPoint(x,y);
}

function SMISPointsParamGetPoint(indx)
{
	if (this.Points[indx] != null) 
	{
		return new SMISPoint(this.Points[indx].x, this.Points[indx].y);
	} 
	else 
	{
		return null;
	}
}

function SMISCreatePolygon(mapId, points)
{
	var pointsValue = SMISExtractPoints(points);
	var map = SMISGetMap(mapId);
	var mapDiv = document.getElementById(mapId);
	var editPolygonId = "SMISEditPolygon";
	var line = document.getElementById(editPolygonId);
	if(!line)
	{
		SMISEnableVML();
		line = document.createElement("<v:polyline points=\"0,0\"/>");
		line.style.position = "absolute";
		line.style.visibility = "visible";
		line.id = editPolygonId;
		line.style.zIndex = mapDiv.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.1 color='blue'></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid' color='red'></v:stroke>");
		mapDiv.appendChild(line);
		line.appendChild(fill);		
		line.appendChild(stroke);
		line.onmousedown = SMISPanOnMouseDown;
		line.onmousemove = SMISPanOnMouseMove;
		line.onmouseup = SMISOnMouseUp;
		line.PointsParam = new SMISPointsParam()
		line.origin = map.origin;
		line.offset = map.offset;
		line.origin.x = map.origin.x;
		line.origin.y = map.origin.y;
		line.offset.x = map.offset.x;
		line.offset.y = map.offset.y;
		line.DoPostBack = map.DoPostBack;
		line.map = map;
		line.lineOffset = new SMISPoint(line.offsetLeft, line.offsetTop);
		line.points.value = pointsValue;
	}
	// var relativePos = SMISGetMouseMapRelativePosition(map);
	line.style.left = map.offset.x;
	line.style.top = map.offset.y;
	SMISClipPolygon(line);
}


function SMISCreateLine(mapId, points)
{
	var pointsValue = SMISExtractPoints(points);
	var map = SMISGetMap(mapId);
	var mapDiv = document.getElementById(mapId);
	var editLineId = "SMISEditLine";
	var line = document.getElementById(editLineId);
	if(!line)
	{
		SMISEnableVML();
		line = document.createElement("<v:polyline points=\"0,0\"/>");
		line.style.position = "absolute";
		line.style.visibility = "visible";
		line.id = editLineId;
		line.style.zIndex = map.parentElement.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.1 color='blue'></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid' color='red' Weight='3' ></v:stroke>");
		mapDiv.appendChild(line);
		line.appendChild(fill);		
		line.appendChild(stroke);
		line.onmousedown = SMISPanOnMouseDown;
		line.onmousemove = SMISPanOnMouseMove;
		line.onmouseup = SMISOnMouseUp;
		line.PointsParam = new SMISPointsParam()
		line.origin = map.origin;
		line.offset = map.offset;
		line.origin.x = map.origin.x;
		line.origin.y = map.origin.y;
		line.offset.x = map.offset.x;
		line.offset.y = map.offset.y;
		line.DoPostBack = map.DoPostBack;
		line.map = map;
		line.lineOffset = new SMISPoint(line.offsetLeft, line.offsetTop);
		line.points.value = pointsValue;
	}
	line.style.left = map.offset.x;
	line.style.top = map.offset.y;
}

function SMISCreatePoint(mapId, points)
{
	SMISCreatePoint_Img(mapId, points);
	return;
	var pointsValue = SMISExtractPoints(points);
	var map = SMISGetMap(mapId);
	var mapDiv = document.getElementById(mapId);
	var editPointId = "SMISEditPoint";
	var circle = document.getElementById(editPointId);
	if(!circle)
	{
		SMISEnableVML();
		circle = document.createElement("<v:oval style='width:12; height:12;' fillcolor='red' />");
		circle.style.position = "absolute";
		circle.style.visibility = 'visible';
		circle.id = editPointId;
		circle.style.zIndex = map.parentElement.style.zIndex + 200;
		var fill = document.createElement("<v:fill opacity=0.3></v:fill>");
		var stroke = document.createElement("<v:stroke dashstyle='solid' Color='blue'></v:stroke>");
		mapDiv.appendChild(circle);
//		document.body.appendChild(circle);
//		circle.appendChild(fill);
//		circle.appendChild(stroke);

		circle.onmousedown = SMISPanOnMouseDown;
		circle.onmousemove = SMISPanOnMouseMove;
		circle.onmouseup = SMISOnMouseUp;
		circle.PointsParam = new SMISPointsParam()
		circle.origin = map.origin;
		circle.offset = map.offset;
		circle.origin.x = map.origin.x;
		circle.origin.y = map.origin.y;
		circle.offset.x = map.offset.x;
		circle.offset.y = map.offset.y;
		circle.DoPostBack = map.DoPostBack;
		circle.map = map;
		circle.lineOffset = new SMISPoint(circle.offsetLeft, circle.offsetTop);
//		circle.points.value = pointsValue;
	}

	var x = eval(pointsValue.split(",")[0]);
	var y = eval(pointsValue.split(",")[1]);
	circle.style.left = map.offset.x = x;
	circle.style.top = map.offset.y = y;
}

function SMISCreatePoint_Img(mapId, points)
{
	var pointWidth = 30;
	var pointHeight = 30;

	var pointsValue = SMISExtractPoints(points);
	var map = SMISGetMap(mapId);
	var mapDiv = document.getElementById(mapId);
	var editPointId = "SMISEditPoint";
	var circle = document.getElementById(editPointId);
	if(!circle)
	{
		circle = document.createElement("<img src='images/Edit_Point.gif' width=" + pointWidth + " height=" + pointHeight + "/>");
		circle.style.position = "absolute";
		circle.style.visibility = 'visible';
		circle.id = editPointId;
		circle.style.zIndex = map.parentElement.style.zIndex + 200;
		mapDiv.appendChild(circle);

		circle.onmousedown = SMISPanOnMouseDown;
		circle.onmousemove = SMISPanOnMouseMove;
		circle.onmouseup = SMISOnMouseUp;
		circle.PointsParam = new SMISPointsParam()
		circle.origin = new SMISPoint();
//		circle.offset = map.offset;	// 传的是引用。ft。// 
		circle.offset = new SMISPoint();
		circle.origin.x = map.origin.x;
		circle.origin.y = map.origin.y;
		circle.offset.x = map.offset.x;
		circle.offset.y = map.offset.y;
		circle.DoPostBack = map.DoPostBack;
		circle.map = map;
		circle.lineOffset = new SMISPoint(circle.offsetLeft, circle.offsetTop);
	}

	var x = eval(pointsValue.split(",")[0]) + eval(map.offset.x) - pointWidth/2;	/* 为Edit_Point.gif图片的1/2宽度 */
	var y = eval(pointsValue.split(",")[1]) + eval(map.offset.y) - pointHeight/2;	/* 为Edit_Point.gif图片的1/2宽度 */

	circle.style.left = circle.offset.x = x;
	circle.style.top = circle.offset.y = y;
}

function SMISExtractPoints(points)
{
	var pointsValue = "";
	if(points)
	{
		if( typeof(points) == "object")
		{
			for(var i=0; i<points.length - 1; i++)			{
				pointsValue += points[i].x + "," + points[i].y + ",";
			}
			pointsValue += points[i].x + "," + points[i].y;
		}
		else
		{
			pointsValue = points;
		}
	}
	return pointsValue;
}

function SMISCheckMousePosition(map)
{
	var __DEBUG = false;
	var absolutePos = SMISGetMouseAbsolutePosition();
	var width, height;
	if(map.map)
	{
		width = map.map.width;
		height = map.map.height;
	}
	else 
	{
		width = map.width; 
		height = map.height;
	}
	
	if ( absolutePos.x <= map.origin.x ||
		absolutePos.y <= map.origin.y ||
		absolutePos.x >= map.origin.x + width ||
		absolutePos.y >= map.origin.y + height )
	{
		return false;
	}
	else
	{
		return true;
	}
}

function IsInAbsOrRelStyle(obj)
{
	var objTemp = obj;
	while(objTemp)
	{
 		if(objTemp.style)
		{
			var temp = objTemp.style.position.toLowerCase();
			if( temp == "absolute" || temp == "relative")
			{
				return true;
			}
		}
		objTemp = objTemp.parentElement;
	}
	return false;
}

function SMISGetOffsetPosition(element, tagName, pos)
{
	var _i = 0, _j = 0;
	if(typeof(pos) == "undefined" || pos == null)
	{
		pos = new SMISPoint(0, 0);
	}
	
	while ( ! (element == null || element.tagName == "BODY" || element.tagName.toLowerCase() == tagName) ) //*/
	{
		if(element.style)
		{
			var temp = element.style.position.toLowerCase();
			if(temp == "absolute" || temp == "relative")
			{
				if(_i > 0) { break;	}
				else {	element = element.offsetParent;	continue;}
			}
		}
		
		_i += 1;
		
		pos.x += eval(element.offsetLeft);
		pos.y += eval(element.offsetTop);	
		
		var bFlag = typeof(element.style.borderLeftWidth) != "unknown";
		if(bFlag && element.style.borderLeftWidth != null && element.style.borderLeftWidth != "")
		{
			pos.x += SMISGetNumFromPixel(element.style.borderLeftWidth);
			pos.y += SMISGetNumFromPixel(element.style.borderTopWidth);
		}

		element = element.offsetParent;
	}
	return pos;
}

function SMISClipPolygon(polygon, inMoving, offsetX, offsetY)
{
	if(!polygon.points)
	{
		return;
	}
	
	var topY = leftX = 0;
	var rightX = polygon.map.width + polygon.map.offset.x;
	var bottomY = polygon.map.height + polygon.map.offset.y;
	var clipTop, clipRight, clipBottom, clipLeft;
	var tempMinX, tempMinY, tempMaxX = rightX, tempMaxY = bottomY;

	if(!inMoving || !polygon.minPoint)	// 初始化 // 
	{
		var pointsArray = polygon.points.value.split(",");
		var p;
		for(var i = 0; i < pointsArray.length; i++)
		{
			p = SMISGetNumFromPixel(pointsArray[i]);
			if(i == 0){	tempMinX = tempMaxX = p;}
			if(i == 1){	tempMinY = tempMaxY = p;}
			if(i%2 == 0)	// x 坐标。 // 
			{
				if( p < tempMinX){ tempMinX = p;}
			}
			else			// y 坐标。 // 
			{
				if( p < tempMinY){ tempMinY = p;}
			}
		}
		polygon.minPoint = new SMISPoint(tempMinX, tempMinY);
	}
	else	// 移动过程中。 // 
	{
		tempMinX = polygon.minPoint.x + offsetX * 2;
		tempMinY = polygon.minPoint.y + offsetY * 2;
	}

	var tempOffsetX = tempMinX / 2 + polygon.map.offset.x - 2;		//polygon.offsetLeft
	var tempOffsetY = tempMinY / 2 + polygon.map.offset.y - 2;		//polygon.offsetTop
	clipTop = tempMinY < 0 ? (topY - tempMinY)*0.5 : 0;
	clipRight = (rightX - tempOffsetX);
	clipBottom = (bottomY - tempOffsetY);
	clipLeft = tempMinX < 0 ? (leftX - tempMinX)*0.5 : 0;

	polygon.style.clip = "rect(" + clipTop + "px " + clipRight + "px " + clipBottom + "px " + clipLeft + "px)";
}
 
/* 在 SmartNavigation 开启的情况下，建议直接在 HTML 页面中使用 <Body ... Onload="functionName"> 的方式进行注册。 */
function SMISAddFuncToWindowEvent(functionName, eventName)
{
	var p_szLoad = functionName;
	var eventRef = document.body.onload;
	if(eventName == "OnResize")
	{
		eventRef = document.body.onresize;
	}

	// Geneaology :: derived from technique at: http://devshed.com 
	if ( eventRef )
	{
		var szLoad 		=	eventRef.toString()							;
		var szFunc		=	p_szLoad.replace("()","");
		if(szLoad.indexOf(szFunc) >= 0)	// 已经注册到 onload 中了。
		{
			return;
		}
		var szMergeLoad	=  	''									
							+	szLoad.substring(	szLoad.indexOf(     '{' ) + 1
												  , szLoad.lastIndexOf( '}' ) ) 
							+	';\n'											
							+	p_szLoad + ';\n'							;
		eventRef	=	new Function( szMergeLoad )							;
	}
	else
	{
		eventRef	=	p_szLoad.replace("()","") 									;
	}

	if(eventName == "OnResize")
	{
		document.body.onresize = eval(eventRef);
	}
	else
	{
		document.body.onload = eventRef;
	}
}
/** 
 *  当页面采用相对定位时，浏览器窗口的缩放会导致 MapControl 的绝对位置发生变化从而导致索引框位置错误。
 *  为解决这个问题，建议 body.onresize 事件被触发时调用该函数。 例如：
 *  修改页面的 <body> 元素，往其中加入 <body onresize="SMISMapSetPosition('WHMapControl')">
 *  对 OverviewControl 也要做类似的处理：
 *  <body onresize="SMISMapSetPosition('WHMapControl'); SMISOverviewSetPosition('OverviewControl1')">
**/
function SMISMapSetPosition(mapID)
{
	if(mapID == null || mapID == "") {	return;	}

	var mapImageID			= mapID + '_MVIMAGE';
	var mapLoadingID		= mapID + '_divLoading';
	var mapQuickPanLeftID	= mapID + '_QuickPanDiv_L';
	var mapQuickPanRightID	= mapID + '_QuickPanDiv_R';
	var mapQuickPanUpID		= mapID + '_QuickPanDiv_U';
	var mapQuickPanDownID	= mapID + '_QuickPanDiv_D';

	SetMapAbsPosition(mapImageID);

	var map = document.getElementById(mapImageID);
	if(!map) { return; }

	var mapLoading = document.getElementById(mapLoadingID);
	if(mapLoading)
	{
		mapLoading.style.left = map.offset.x + (SMISGetNumFromPixel(map.style.width) - SMISGetNumFromPixel(mapLoading.clientWidth))/2;
		mapLoading.style.top  = map.offset.y + (SMISGetNumFromPixel(map.style.height) - SMISGetNumFromPixel(mapLoading.clientHeigth))/2;
	}
	
	var divQuickPan_L = document.getElementById(mapQuickPanLeftID);
	var divQuickPan_R = document.getElementById(mapQuickPanRightID);
	var divQuickPan_U = document.getElementById(mapQuickPanUpID);
	var divQuickPan_D = document.getElementById(mapQuickPanDownID);
	if(divQuickPan_L)
	{
		divQuickPan_L.style.left = map.offset.x;
		divQuickPan_L.style.top  = map.offset.y + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.height))/2 - divQuickPan_L.clientHeight/2;
		divQuickPan_L.style.visibility = 'visible';
	}
	if(divQuickPan_R)
	{
		divQuickPan_R.style.left = map.offset.x + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.width)) - divQuickPan_R.clientWidth;
		divQuickPan_R.style.top  = map.offset.y + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.height))/2 - divQuickPan_R.clientHeight/2;
		divQuickPan_R.style.visibility = 'visible';
	}
	if(divQuickPan_U)
	{
		divQuickPan_U.style.left = map.offset.x + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.width))/2 - divQuickPan_U.clientWidth/2;
		divQuickPan_U.style.top  = map.offset.y;
		divQuickPan_U.style.visibility = 'visible';
	}
	if(divQuickPan_D)
	{
		divQuickPan_D.style.left = map.offset.x + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.width))/2 - divQuickPan_D.clientWidth/2;
		divQuickPan_D.style.top  = map.offset.y + (SMISGetNumFromPixel(_WHMapControl_MVIMAGE.style.height)) - divQuickPan_D.clientHeight;
		divQuickPan_D.style.visibility = 'visible';
	}
	
	try
	{
		eval( mapID + "_SetDivCustomLayer()" );
	}
	catch(e)
	{
		// alert(show_props(e));
	}
}
