var map = null;
var panAction = null;
var zoomInAction = null;
var zoomOutAction = null;
var pointQueryAction = null;
var former_index = 0;
var max_btn_number = 33;
var currentMapName="";                                          
var timeSlider=null;
var xmlhttprequest=null;
var jsTable=null;
function getZhenYuan(time)
{
    xmlhttprequest=_GetXmlHttpRequest();
    xmlhttprequest.open("get","zhenyuan.ashx?Time="+time,false);
    xmlhttprequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttprequest.onreadystatechange=ongetZhenYuanComplete;
    xmlhttprequest.send(null);
}
function addSingleMark(x,y,strScrip)
{
    var innerHtml="<img src='images/marker.gif' alt='"+strScrip+"'/>";
    map.CustomLayer.InsertMark("singleMark",x,y,10,10,innerHtml);   
}
function ongetZhenYuanComplete(result)
{
    var readyState=xmlhttprequest.readyState;
    if (readyState==4)
    {
        var status=xmlhttprequest.status;
        if(status==200)
        {
            var strLayersName= xmlhttprequest.responseText;
            if(strLayersName!= null)
            {
                addMark(eval(strLayersName));                
                if(jsTable==null)
                {
                    jsTable=new JSTable("JSTable");
                    jsTable.initlize([{"caption":"编号","value":"id"},{"caption":"名称","value":"name"},{"caption":"震级","value":"class1"},{"caption":"时间","value":"time1"}],eval(strLayersName));
                }
            }
        }
        else
        {            
            alert(xmlhttprequest.responseText);
        }
        xmlhttprequest = null;
    }

}
function addMark(markers)
{
    map.CustomLayer.ClearMarks();
    for(var i=0;i<markers.length;i++)
    {
        var marker=markers[i];
        var innerHTML="<img ";
        if(parseInt(marker.class1)==6)
        {
            innerHTML+="style='width:20px;height:20px'";
        }
        else if(parseInt(marker.class1)==5)
        {
            innerHTML+="style='width:15px;height:15px'";
        }
        else
        {
            innerHTML+="style='width:10px;height:10px'";
        }
        
        innerHTML+=" src='images/signed.gif' alt='震源："+marker.name+"'/>";
        map.CustomLayer.InsertMark(marker.id, marker.x, marker.y, 10, 10, innerHTML);    
    }
    
}
function switchMap(e)
{
    if(e=="linan")
    {
        document.getElementById("tab_focus_t").src="images/tab_normal_l0.gif";
        document.getElementById('zhenyuan').src='images/zhenyuan_up.gif';
        document.getElementById('tab_focus').src='images/tab_focus_l1.gif';
        document.getElementById('linan').src='images/linan_down.gif';
        document.getElementById("tab_focus_p").src="images/tab_focus_r0.gif";
        OnPageLoad('shangwang');
    }
    else
    {
        document.getElementById("tab_focus_t").src="images/tab_focus_l0.gif";
        document.getElementById('zhenyuan').src='images/zhenyuan_down.gif';
        document.getElementById('tab_focus').src='images/tab_focus_r1.gif';
        document.getElementById('linan').src='images/linan_up.gif';
        document.getElementById("tab_focus_p").src="images/tab_normal_r0.gif";
        OnPageLoad('zhenyuan_new');    
    }
}

function OnPageLoad(mapName){
    if(map==null)
    {
        var params = new Object();
	    params.mapHandler = "http://www.supermap.com.cn/mapHandler3/";
	    params.mapName = mapName;
	    currentMapName=mapName;
        params.mapScales = [1/6278912,1/3139456,1/1569728,1/784864];
        params.imageFormat = "gif";
        params.x = -1082645.1410411300;
        params.y = 2522135.1399492200;

	    params.zoomLevel = 3;
	    params.fixedView = false;
	    params.buffer = 256; // 预先进行下载的视图范围。 
	    map = new SuperMap.IS.MapControl($("myMap"), params); 
	    map.Init();
        refreshLegend(mapName);
	    map.AttachEvent("onendzoom", RefreshSlider);
    }
    else
    {
        map.Destroy();
	    map = null;
    
        var params = new Object();
	    params.mapHandler = "http://www.supermap.com.cn/mapHandler3/";
	    params.mapName = mapName;
        currentMapName=mapName;
        params.mapScales = [1/6278912,1/3139456,1/1569728,1/784864];
        params.imageFormat = "gif";
        params.x = -1082645.1410411300;
        params.y = 2522135.1399492200;

	    params.zoomLevel = 3;
	    params.fixedView = false;
	    params.buffer = 256; // 预先进行下载的视图范围。 
	    map = new SuperMap.IS.MapControl($("myMap"), params); 
	    map.Init();
	    map.AttachEvent("onendzoom", RefreshSlider);   
        refreshLegend(mapName); 
	 }
	 if(mapName=="zhenyuan_new")
	 {
	    if(timeSlider==null)
	    {
            timeSlider=new TimeSlider(document.getElementById("timeSlider"));
		    timeSlider.initlize();
        }
        else
        {
            timeSlider.show();
			timeSlider.setCurrentTime(new Date());
        }
	 }
	 else
	 {
	    if(timeSlider!=null)
	    {
	        timeSlider.hide();
	    }
	 }
}
function refreshLegend(e)
{
    var legend_div=document.getElementById("legend");
    if(e=="zhenyuan_new")
    {
        legend_div.style.left="777px";
        legend_div.style.top="524px";
        legend_div.childNodes[0].src="images/tlyq.bmp";
    }
    else
    {
        legend_div.style.left="726px";
        legend_div.style.top="524px";
        legend_div.childNodes[0].src="images/tlfd.bmp";
    }
}
 
function OnPageUnload() 
{
	map.Destroy();
	map = null;
}

function OnClickZoomIn()
{
	var mapLevel = map.GetZoomLevel();
	if(mapLevel < 8)
	{
		mapLevel += 1;
		map.SetZoomLevel(mapLevel);
		MoveSlider(mapLevel);
	}
}

//点击滑动条下面的缩小按钮来放大地图
function OnClickZoomOut()
{
	var mapLevel = map.GetZoomLevel();
	if(mapLevel > 1)
	{
		mapLevel -= 1;
		map.SetZoomLevel(mapLevel);
		MoveSlider(mapLevel);
	}
}

function OnClickPan(tag)
{
	var x = map.GetMapCenterX();
	var y = map.GetMapCenterY();

	switch(tag)
	{
		case 1://east
			x += 100000;
			map.PanToMapCoord(x, y);
			break;
		case 2://west
			x -= 100000;
			map.PanToMapCoord(x, y);
			break;
		case 3://south
			y -= 100000;
			map.PanToMapCoord(x, y);
			break;
		case 4://north
			y += 100000;
			map.PanToMapCoord(x, y);
			break;
	}
}

//当地图的显示级别变化的时候来调整滑动条的位置
function MoveSlider(zoomlevel)
{
	var mapSlider = document.getElementById("mapSlider");
	switch(zoomlevel)
	{/*
		case 1:
			mapSlider.style.pixelTop = 341;
			break;
		case 2:
			mapSlider.style.pixelTop = 325;
			break;*/
		case 1:
			mapSlider.style.pixelTop = 277;
			break;
		case 2:
			mapSlider.style.pixelTop = 261;
			break;
		case 3:
			mapSlider.style.pixelTop = 245;
			break;
		case 4:
			mapSlider.style.pixelTop= 229;
			break;
	}
}

function RefreshSlider(eventArg)
{
	var zoomLevel = eventArg.param.zoomLevel;
	MoveSlider(zoomLevel);
}

function UpdateInfo(eventArg)
{
	var info = $("info");
	if (!info) { return false; }

	if(eventArg.param == null){
		info.innerHTML = '<font color="red">error: ' + eventArg.error + '</font>';
		return false;
	}

	info.innerHTML = 'mapName = ' + eventArg.param.mapName 
		+ ', zoomLevel=' + eventArg.param.zoomLevel
		+ ', mapCenter = ' + eventArg.param.mapCenter.ToString()
		+ ', pixelCenter = ' + eventArg.param.pixelCenter.ToString();
}

function DoPanUp() { map.DynamicPan(0, -10, 20); }

function DoPanDown() { map.DynamicPan(0, 10, 20); }

function DoPanLeft(str) { map.DynamicPan(-10, 0, 20); }

function DoPanRight() { map.DynamicPan(10, 0, 20); }

function DoZoomIn() { map.ZoomIn(); }

function DoZoomOut() { map.ZoomOut(); }

function ViewEntire() {
	map.SetCenterAndZoom(-1187645.110411300,2372135.1399492200,1)	
}

function SetZoomInAction() {
	if (!zoomInAction){
	    zoomInAction = new SuperMap.IS.ZoomInAction();
	}
	map.SetAction(zoomInAction);
}

function SetZoomOutAction() {
	if (!zoomOutAction) {
		zoomOutAction = new SuperMap.IS.ZoomOutAction();
	}
	map.SetAction(zoomOutAction);
}

function SetPanAction() {
	if (!panAction) {
		panAction = new SuperMap.IS.PanAction();
	}
	map.SetAction(panAction);
}

function SetDrawLineAction() {
	var drawLineAction = new SuperMap.IS.DrawLineAction();
	map.SetAction(drawLineAction);
}

function SetPointQueryAction() {
	var queryManager = map.GetQueryManager();
	queryManager.RegisterHandler(onQueryComplete, onQueryError);

	if(!pointQueryAction){
	    pointQueryAction = new SuperMap.IS.PointQueryAction(["School@changchun"], ["smid", "name"], 200);
	}

	map.SetAction(pointQueryAction);
}

function onQueryComplete(resultSet)
{
	$("querypoint").innerHTML = "";

	if (!resultSet || resultSet.totalCount < 1) {
		onQueryError("查询结果为空。");
		return false;
	}

	for (var i = 0; i < resultSet.recordsets.length; i++) {
		for (var j = 0; j < resultSet.recordsets[i].records.length; j++) {
			var record = resultSet.recordsets[i].records[j];
			$("querypoint").innerHTML
				+= '<div class="query" onclick="'
				+ 'if(window.curMapControl.GetZoomLevel()<5){'
				+ 'window.curMapControl.SetCenterAndZoom('
				+ record.center.x + ',' + record.center.y + ',5);'
				+ '}else{'
				+ 'window.curMapControl.ViewByPoint('
				+ record.center.x + ',' + record.center.y + ');'
				+ '}'
				+ 'window.curMapControl.CustomLayer.InsertMark(\'marker\','
				+ record.center.x + ',' + record.center.y + ',10,10,' 
				+ '\'<img src=\\\'images/marker.gif\\\' alt=\\\'' + record.fieldValues[1] + '\\\' style=\\\'cursor:hand\\\' /> \');'
				+ '">'
				+ record.fieldValues[0] + '&nbsp;&nbsp;'
				+ record.fieldValues[1] + '</div>';
		}
	}
}

function onQueryError(responseText) {
	$("querypoint").innerHTML = '提示:' + responseText; 
}

// 切换按钮。 Switch button//
function SwitchBtn(index, action)
{
	// when argument index is ignored
	/*
	if(typeof(index) == "undefined" || typeof(index) == "string") {
		action = index;
		index = Eve(event).id.replace("img0","").replace("img","");
	}
	*/
	var prefix = "btn";

	var current = "";
	var rest = "";
	if (index == -1) { //reset all buttons to off status
		for (var i = 1; i < 10; i++) {
			if (i != index && getImg("img0" + i)) {
				getImg("img0" + i).src
					= "images/" + prefix + "_0" + i + "_off.jpg";
			}
		}

		for (var i = 10; i <= max_btn_number; i++) {
			if (i != index && getImg("img" + i)) {
				getImg("img" + i).src
					= "images/" + prefix + "_" + i + "_off.jpg";
			}
		}

		former_index = 0;
	}

	switch (action) {
		case "Over":
			current = "on";

			rest = "off";
			SwitchBtn_ChangeImg(current, rest, prefix, index, former_index);
			break;

		case "Out":
			current = "off";

			rest = "off";
			SwitchBtn_ChangeImg(current, rest, prefix, index, former_index);
			break;

		case "Down":
			current = "down";

			rest = "off";
			// 判断鼠标操作类型。 Judge the type of the mouse operating  
			if (index == 4 || index == 5 || index == 6 || index == 10
				|| (index >= 13 && index <= 17)
				|| index == 22 || index == 26 || (index >= 28 && index <= 32)) {
				for (var i = 1; i < 10; i++) {
					if (i != index && getImg("img0" + i)) {
						getImg("img0" + i).src
							= "images/" + prefix + "_0" + i + "_off.jpg";
					}
				}

				for (var i = 10; i <= max_btn_number; i++) {
					if (i != index && getImg("img" + i)) {
						getImg("img" + i).src
							= "images/" + prefix + "_" + i + "_off.jpg";
					}
				}

				SwitchBtn_ChangeImg(current, rest, prefix, index, former_index);
				former_index = index;
			}
			else {
				SwitchBtn_ChangeImg(current, rest, prefix, index, former_index);
			//former_index = index;
			}

			break;
	}
}

function SwitchBtn_ChangeImg(current, rest, prefix, index, former_index)
{
	if (former_index != index) {
		if (getImg("img" + index)) {
			getImg("img" + index).src = "images/" + prefix + "_" + index + "_" + current + ".jpg";
		}

		if (getImg("img0" + index)) {
			getImg("img0" + index).src = "images/" + prefix + "_0" + index + "_" + current + ".jpg";
		}
	}
	else {
		if (current != "off") {
			if (getImg("img" + index)) {
				getImg("img" + index).src = "images/" + prefix + "_" + index + "_" + current + ".jpg";
			}

			if (getImg("img0" + index)) {
				getImg("img0" + index).src = "images/" + prefix + "_0" + index + "_" + current + ".jpg";
			}
		}
		else {
			if (getImg("img" + index)) {
				getImg("img" + index).src = "images/" + prefix + "_" + index + "_" + "down" + ".jpg";
			}

			if (getImg("img0" + index)) {
				getImg("img0" + index).src = "images/" + prefix + "_0" + index + "_" + "down" + ".jpg";
			}
		}
	}
}

function getImg(img)
{
	if (document.all) {
		return document.all(img);
	} else if (document.getElementById) {
		return document.getElementById(img);
	}
}