﻿/*---------------------------------------------------------------
// AspStudio_Codepage="65001"
// 上面这行是软件使用的代码页标记，请不要删除。详情请参考帮助文件。
//--------------------------------------------------------------*/
var map;
var markers=[];
var xmlrows;
var icon;

var curPage=1;
var curCount;
var curTotel;
var gSize;
var nPlace;
var htmlDiv;
var r;

var firstload=0;
var iorder="xh desc";
var loadtext;
var bound;
var boundexpand={
	"maxx":0,"minx":0,"maxy":0,"miny":0
};

var infoopen=false;
var infoid=0;
var tooltip=document.createElement("div");
tooltip.setAttribute("id","markerTooltip");
tooltip.setAttribute("class","markerTooltip");

function getMap(){
	window.setTimeout(getMap2,150);
};
function getMap2(){
	var styleObj=document.getElementById("more").style;
	styleObj.display="none";

	document.getElementById("mapview").style.height=getWindowHeight()-185+'px';	
		if (GBrowserIsCompatible()) {
		map=new GMap2(document.getElementById("mapview"));
                map.addControl(new google.maps.LocalSearch(), 
                new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(1,1)));

		G_NORMAL_MAP.getName=function(_4){return (_4)?"\u5730\u56fe":"\u5730\u56fe";};
		G_SATELLITE_MAP.getName=function(_5){return (_5)?"\u536b\u661f":"\u536b\u661f\u56fe";};
		G_HYBRID_MAP.getName=function(_6){return (_6)?"\u6df7\u5408":"\u6df7\u5408\u56fe";};


		map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(5,-58)));
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		//map.addMapType(G_PHYSICAL_MAP);	
		
		map.addControl(new DragZoomControl());
		map.addControl(new TextualloadControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
		tooltip.style.visibility="hidden";

		icon=new GIcon();
		icon.iconSize=new GSize(20,34);
		icon.shadowSize=new GSize(37,34);
		icon.iconAnchor=new GPoint(9,34);
		icon.infoShadowAnchor=new GPoint(18,25);
		icon.infoWindowAnchor=new GPoint(9,2);
		resizeMap();
		setAdvert();
		setNewMap();
	}
};

function reSetNewMap(){
	var iMenu = document.forms[0].jumpMenu;
	varArray=iMenu.options[iMenu.selectedIndex].value.split('|');
	if(varArray.length>1){
		for(var x=0;x<varArray.length;x++){
			switch(x){
			case 0: areaid=varArray[x];break;
			case 1: llat=varArray[x];break;
			case 2: llng=varArray[x];break;
			case 3: lzoom=varArray[x];break;
			}
		}
		map.clearOverlays();
		markers=[];
		setAdvert();
		setNewMap();
	}
	else{
		document.location.href=iMenu.options[iMenu.selectedIndex].value;
	}
}

function setAdvert(){
	var divad = document.getElementById("top2")
	var xml = GXmlHttp.create();
	var dom = "<root><cmd>advert</cmd><adopt>T</adopt><areaid>"+areaid+"</areaid></root>";
	try{
		xml.open("POST","querypos.asp",true);
		xml.onreadystatechange = function(){
		  if(xml.readyState==4){
		    if (xml.status==200){
				var ret=xml.responseXML;
				if(ret.documentElement.getElementsByTagName("result")[0].getAttribute("ret") == "ok"){
					var rows = ret.getElementsByTagName("row");
					if(rows.length>0){
						//divad.innerHTML = URLDecode(rows[0].getAttribute("adtext"));
						addinnerHTML(divad, rows[0].getAttribute("advertid"));
					}
					window.setTimeout(rechangeadvert,100);
				}
		    }}
		};
	    xml.send(dom);
	}catch(e){}
}

function addinnerHTML(el,adid){
	var adtxt = "<iframe name='ad_frame' id='ad_frame' scrolling=no frameborder=0 src='showadvert.asp?id="+adid+"' width=729 height=90></iframe>";
	el.innerHTML = adtxt;
}

function rechangeadvert(){
	var divad = document.getElementById("message")
	var xml = GXmlHttp.create();
	var dom = "<root><cmd>advert</cmd><adopt>R</adopt><areaid>"+areaid+"</areaid></root>";
	try{
		xml.open("POST","querypos.asp",true);
		xml.onreadystatechange = function(){
		  if(xml.readyState==4){
		    if (xml.status==200){
				var ret=xml.responseXML;
				if(ret.documentElement.getElementsByTagName("result")[0].getAttribute("ret") == "ok"){
					var rows = ret.getElementsByTagName("row");
					if(rows.length>0)
						divad.innerHTML = URLDecode(rows[0].getAttribute("adtext"));
					window.setTimeout(rechangeadvert,30000);
				}
		    }}
		};
	    xml.send(dom);
	}catch(e){}
}

function setNewMap(){
	var vlat=parseFloat(llat);
	var vlng=parseFloat(llng);
	var vzoom=parseInt(lzoom);
	var maptype;
	var VarArrayChk;		
	VarArrayChk=document.location.href.match('vlat');		
	if(VarArrayChk!=null)
	{
		varArray=document.location.href.split('#')[1].split('&');			
		for(var x=0;x<varArray.length;x++)
		{
			var tmp=varArray[x].split('=');				
			eval(unescape(tmp[0])+'="'+unescape(tmp[1])+'"');				
			if(tmp[0]=="vlat")
			{
				vlat=parseFloat(tmp[1]);
			};				
			if(tmp[0]=="vlng")
			{	vlng=parseFloat(tmp[1]);
			};				
			if(tmp[0]=="vzoom")
			{ vzoom=parseFloat(tmp[1]);
			};
			if(tmp[0]=="vt")
			{ mt=tmp[1];
			};
			if(tmp[0]=="iclass")
			{ document.forms[0].typeMenu.selectedIndex=parseInt(tmp[1]);
			};
		}
	}
	switch(mt){
		case "s":maptype=G_SATELLITE_MAP;
		break;
		case "h":maptype=G_HYBRID_MAP;
		break;
		case "t":maptype=G_PHYSICAL_MAP;
		break;
		default:maptype=G_NORMAL_MAP;
	}
	var center = new GLatLng(vlat, vlng);
	map.setCenter(center, vzoom, maptype);

	GEvent.addListener(map,"maptypechanged",function (){
		reloaddata();
	});
	GEvent.addListener(map,"dragend",function (){
		reloaddata();
	});
	GEvent.addListener(map,"zoomend",function (){
		reloaddata();
	});
	GEvent.addListener(map,"zoomend",function (){
		reloaddata();
	});
	GEvent.addListener(map,"infowindowclose",function (){
		infoopen=false;
	});
	GEvent.addListener(map,'click',function (overlay,point){
		if(overlay){
			if(typeof(overlay.htmls)!='undefined'){
				popupclose();
				overlay.openInfoWindowHtml(overlay.htmls);
				infoopen=true;
				infoid=overlay.id;
			}
		}else if(point){
			popupclose();
		}
	});
	reloaddata();
	firstload=1;
}

function bounds(){
	bound=map.getBounds();
	var lngdiff=0.000877*(Math.pow(2,17-map.getZoom()));
	var latdiff=0.000454*(Math.pow(2,17-map.getZoom()));
	boundexpand.minx=bound.getSouthWest().lat()-latdiff;
	boundexpand.maxx=bound.getNorthEast().lat()+latdiff;
	boundexpand.miny=bound.getSouthWest().lng()-lngdiff;
	boundexpand.maxy=bound.getNorthEast().lng()+lngdiff;
};

function showorder(sOrder){
	iorder=sOrder;
	reloaddata();
}

function setNewClass(){
	map.clearOverlays();
	markers=[];
	reloaddata();
}

function reloaddata(){
	var c = map.getCenter();
	var z = map.getZoom();
	var t = "h";
	var iclass = document.forms[0].typeMenu;
	if(map.getCurrentMapType().getName()=='地图') t="n";
	document.location.href = document.location.href.split('#')[0]+"#vlat="+c.lat()+"&vlng="+c.lng()+"&vzoom="+z+"&vt="+t+"&iclass="+iclass.selectedIndex;		

	if(infoopen==true) return;

	document.getElementById("loading2").style.visibility="visible";
	document.getElementById("loading2").style.cursor="wait";
	
	bounds();
	var icval = iclass.options[iclass.selectedIndex].value.split('|');
//	icon.image="getclassicon.asp?id="+iclassid+"&upkey="+syskey+"&field=cstop";
//	icon.shadow="getclassicon.asp?id="+iclassid+"&upkey="+syskey+"&field=csbak";
	icon.image="smallimg/"+icval[1];
	icon.shadow="smallimg/"+icval[2];

	loadtext='<div id="loadbar">'+'下载数据中...请等待'+'</div>';
	document.getElementById("loading2").innerHTML=loadtext;

	var xml = GXmlHttp.create();
	var err;
	var dom = "<root><cmd>querypos</cmd><upkey>"+syskey+"</upkey><minx>"+boundexpand.minx+
				"</minx><maxx>"+boundexpand.maxx+"</maxx><miny>"+boundexpand.miny+
				"</miny><maxy>"+boundexpand.maxy+"</maxy><ipage>"+curPage+"</ipage><iclass>"+icval[0]+
				"</iclass><mtype>"+t+"</mtype><iorder>"+iorder+"</iorder></root>";
	try{
		xml.open("POST","querypos.asp",true);
		xml.onreadystatechange = function(){
		  if(xml.readyState==4){
		    if (xml.status==200){
				var ret=xml.responseXML;
				if(ret.documentElement.getElementsByTagName("result")[0].getAttribute("ret") == "ok"){
					curCount = parseInt(ret.documentElement.getElementsByTagName("result")[0].getAttribute("iCount"));
					curTotel = parseInt(ret.documentElement.getElementsByTagName("result")[0].getAttribute("iTotel"));
					curPage =  parseInt(ret.documentElement.getElementsByTagName("result")[0].getAttribute("iPage"));
					gSize = parseInt(ret.documentElement.getElementsByTagName("result")[0].getAttribute("iSize"));
					nPlace = ret.getElementsByTagName("row");
					loadtext='<div id="loadbar">'+'显示数据中...请等待'+'</div>';
					document.getElementById("loading2").innerHTML=loadtext;
					htmlDiv="";
					r=0;
					loaddataprogress();
				}else{
					err = ret.getElementsByTagName("result")[0].getAttribute("descr");
					loadtext='<div id="loadbar">'+'下载出错:'+err+'</div>';
					document.getElementById("loading2").innerHTML=loadtext;
				}
		    }else{
		      	err = "status:"+xml.status+"\n"+xml.statusText;
					loadtext='<div id="loadbar">'+'下载出错:'+err+'</div>';
					document.getElementById("loading2").innerHTML=loadtext;
			}}
		};
	    xml.send(dom);
	}catch(e){}
}

function loaddataprogress(){
	var ff;
	var more;
	if(r<nPlace.length){
		if(nPlace[r].getAttribute("cstop").length>0){
			icon.image="smallimg/"+nPlace[r].getAttribute("cstop");
			icon.shadow="smallimg/"+nPlace[r].getAttribute("csbak");
		}
		if(markers[r]==undefined){
			markers[r]=new GMarker(new GLatLng(parseFloat(nPlace[r].getAttribute("sclat")),parseFloat(nPlace[r].getAttribute("sclon"))),icon);
			map.addOverlay(markers[r]);
			GEvent.addListener(markers[r],"mouseover",function (){
				showTooltip(this.idx);
			});
			GEvent.addListener(markers[r],"mouseout",function (){
				tooltip.style.visibility="hidden";
			});
			
		}else {
			if(markers[r].idx==-1) map.addOverlay(markers[r]);
			markers[r].setPoint(new GLatLng(parseFloat(nPlace[r].getAttribute("sclat")),parseFloat(nPlace[r].getAttribute("sclon"))));
			if(nPlace[r].getAttribute("cstop").length>0)
				markers[r].setImage("smallimg/"+nPlace[r].getAttribute("cstop"));
		}
		ff = "getscene.asp?upkey="+syskey+"&id="+nPlace[r].getAttribute("sceneid")+"&itype=mid";
		more='<iframe src="'+ff+'" width=100% height="180" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0"></iframe>';
		markers[r].htmls = '<table width="320" border="0" cellpadding="0" cellspacing="0" style="margin:5px 0 0 0;"><tr><td class="forumTitle" align="left" valign="middle" height="22"><img src="images/icon2.gif" border="0">&nbsp;<b>'+nPlace[r].getAttribute("scname")+
			'</b>&nbsp;</td><td align="right" valign="bottom"><div id="optionbar"><a href="javascript:showinfo(0);">文字简介</a></div></td></tr><tr><td align="left" valign="top" class="forumData" colspan="2">'+
			'<div id="infotxt" style="display:none;">'+URLDecode(nPlace[r].getAttribute("scmemo"))+'</div><div id="infobox">'+more+'</div>'+
			'</td></tr><tr><td align="center" class="forumbar" colspan="2"><a href="javascript:popup('+r+');">由此进入全屏观看<img src="images/goscreen.gif" border="0"></a></td></tr></table>';
//		markers[r].htmls='<table width=340 height=300 style="border:1px black solid"><tr><td>blah bleh blah ...</td></tr></table>';
		markers[r].idx = r;
		htmlDiv+='<li><a href="javascript:markerclick('+r+');" onMouseOver="showTooltip('+r+');" onMouseOut="hideTooltip();">'+nPlace[r].getAttribute("scname")+'</a></li>';
		r++;
		window.setTimeout(loaddataprogress,100);
	}else{
	
		var divTable = document.getElementById("postable");
		var divBar = document.getElementById("flashsize");
		var sbar;
		if(curPage>1)
			sbar='<a href="javascript:jumppage(1)">首页</a>&nbsp;<a href="javascript:jumppage('+(curPage-1)+');">上一页</a>&nbsp;';
		else
			sbar='首页&nbsp;上一页&nbsp;';
		if(curPage<curTotel)
			sbar+='<a href="javascript:jumppage('+(curPage+1)+');">下一页</a>&nbsp;<a href="javascript:jumppage('+curTotel+');">尾页</a>&nbsp;';
		else
			sbar+='下一页&nbsp;尾页&nbsp;'	;
		sbar+='&nbsp;<strong><font color="#FF0000">'+curPage+'</font></strong>/<strong><font color="#000066">'+curTotel+'</font></strong>';
		divTable.innerHTML="<lu><li class=h>"+sbar+"</li>"+htmlDiv+"</lu>";

		sbar='此区域共<strong><font color="#FF9900">'+curCount+'</font></strong>个点&nbsp;每页可显示<strong><font color="#FF9900">'+gSize+'</font></strong>个点'
		divBar.innerHTML=sbar;
		
		for(var i=r;i<markers.length;i++){
			markers[i].idx=-1;
			map.removeOverlay(markers[i]);
		}
		
		document.getElementById("loading2").style.visibility="hidden";
	}
}

function showinfo(op){
	var a = document.getElementById("optionbar");
	if(op==0){
		a.innerHTML='<a href="javascript:showinfo(1);">图像浏览</a>';
		document.getElementById("infobox").style.display="none";
		document.getElementById("infotxt").style.display="block";
	}else{
		a.innerHTML='<a href="javascript:showinfo(0);">文字简介</a>';
		document.getElementById("infotxt").style.display="none";
		document.getElementById("infobox").style.display="block";
	}
}

function jumppage(ipage){
	curPage=ipage;
	reloaddata();
}

function markerclick(idx){
	if(markers[idx]&&markers[idx].openInfoWindowHtml){
		if(typeof(markers[idx].htmls)!='undefined'){
			popupclose();
			markers[idx].openInfoWindowHtml(markers[idx].htmls);
			infoopen=true;
			infoid=idx;
		}
	}
}

function showTooltip(idx){
	tooltip.innerHTML="<img src=\"getscene.asp?upkey="+syskey+"&id="+nPlace[idx].getAttribute("sceneid")+"&itype=min\" width=\"150\" height=\"85\"/><br />"+nPlace[idx].getAttribute("scname")+"";
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(markers[idx].getPoint(),map.getZoom());
	var anchor=markers[idx].getIcon().iconAnchor;
	var width=markers[idx].getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(offset.x-point.x-anchor.x+width,offset.y-point.y-anchor.y-height));
	pos.apply(tooltip);
	wwx=offset.x-point.x-anchor.x;
	wwy=offset.y-point.y-anchor.y;
	tooltip.style.visibility="visible";
	
};

function hideTooltip(){
	tooltip.style.visibility="hidden";
};

function popup(id){
	map.closeInfoWindow();
	var styleObj=document.getElementById("content").style;
	styleObj.display="none";
	var styleObj=document.getElementById("more").style;
	styleObj.display="block";
	var height2=(getWindowHeight())-175+'px';
	
	var ff = "getscene.asp?upkey="+syskey+"&id="+nPlace[id].getAttribute("sceneid")+"&itype=max";
	var more='<iframe src="'+ff+'" width=100% height="'+height2+'" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0"></iframe>';
	
	moreinner='<div id="mainframe"><div id="back"><input type="submit" class="fh" name="Submit"  onclick="javascript:popupclose();" value=" 返回地图 " /></div>'+
				'<div id="titletitle"> ◆ '+nPlace[id].getAttribute("scname")+' ◆</div><div>'+more+'</div></div>';
	document.getElementById("more").innerHTML=moreinner;
	
};

function popupclose(){
	var styleObj=document.getElementById("more").style;
	if(styleObj.display!="none"){
		var d=document.getElementById('more');
		var olddiv=document.getElementById('mainframe');
		d.removeChild(olddiv);
		
	}styleObj.display="none";
	var styleObj=document.getElementById("content").style;
	styleObj.display="";
	map.closeInfoWindow();
	map.checkResize();
	infoopen=false;
};

function resizeMap()
{
	var nHeight=getWindowHeight()-185;	
	document.getElementById("map-wrap").style.height=nHeight+'px';	
	document.getElementById("mapview").style.height=nHeight+'px';	
	document.getElementById("wrapside").style.height=nHeight-112+'px';	
	document.getElementById("loading2").style.top=(getWindowHeight()/2)-120+'px';	
	document.getElementById("loading2").style.left=(getWindowWidth()/2)-260+'px';	
	document.getElementById("loading2").style.visibility="hidden";	
	map.checkResize();	
	if(firstload==1){
		reloaddata();
	}
};

function TextualloadControl(){
};
TextualloadControl.prototype=new GControl();
TextualloadControl.prototype.initialize=function (map){
	var container=document.createElement("div");
	var loadDiv=document.createElement("div");
	loadDiv.className='loading2';
	loadDiv.id='loading2';
	container.appendChild(loadDiv);
	loadDiv.appendChild(document.createTextNode("装载中...请等待"));
	map.getContainer().appendChild(container);
	return container;
	
};

function getWindowHeight(){
	if(typeof(window.innerHeight)=='number'){
		nHeight=window.innerHeight;
		
	}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
		nHeight=document.documentElement.clientHeight;
		
	}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
		nHeight=document.body.clientHeight;
		
	}return nHeight;
	
};
function getWindowWidth(){
	var nWidth;
	if(typeof(window.innerWidth)=='number'){
		nWidth=window.innerWidth;
		
	}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
		nWidth=document.documentElement.clientWidth;
		
	}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
		nWidth=document.body.clientWidth;
		
	}return nWidth;
	
};

String.prototype.trim=function(){
        return this.replace(/(^\s*)|(\s*$)/g, "");
}

function URLDecode(psEncodeString) 
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}
