﻿
 //Custom Setting
    var centreLat=35.9;  
    var centreLon=104.25; 
    var initialZoom=0;  
    var imageWraps=false; 
    var map; 
    
     function CustomProjection(a,b){
	 this.imageDimension=65536;
	 this.pixelsPerLonDegree=[];
	 this.pixelOrigin=[];
	 this.tileBounds=[];
	 this.tileSize=256;
         this.isWrapped=b;
	 var b=this.tileSize;
	 var c=1;
	 for(var d=0;d<a;d++){
           var e=b/2;
           this.pixelsPerLonDegree.push(b/360);
           this.pixelOrigin.push(new GPoint(e,e));
           this.tileBounds.push(c);
           b*=2;
           c*=2
         }
     }

     CustomProjection.prototype=new GProjection();
    
    
     CustomProjection.prototype.fromLatLngToPixel=function(latlng,zoom)
     {
         if(typeof(latlng) == "undefined")
            return  new GPoint(0,0);
           
         return (G_NORMAL_MAP.getProjection()).fromLatLngToPixel(latlng,zoom);       

     };

     CustomProjection.prototype.fromPixelToLatLng=function(pixel,zoom,unbounded)
     {
         if(typeof(pixel) == "undefined")
             return  new GLatLng(0,0,unbounded)
             
         if(typeof(unbounded)== "undefined")
             unbounded=false;
         return  (G_NORMAL_MAP.getProjection()).fromPixelToLatLng(pixel,zoom,unbounded);
     };

     CustomProjection.prototype.tileCheckRange=function(tile,zoom,tilesize){
         var tileBounds=this.tileBounds[zoom];
	 if (tile.y<0 || tile.y >= tileBounds) {return false;}
         if (this.isWrapped) {
	     if (tile.x<0 || tile.x>=tileBounds) { 
	         tile.x = tile.x%tileBounds; 
	         if (tile.x < 0) {tile.x+=tileBounds} 
	     }
	 }
	 else { 
             if (tile.x<0 || tile.x>=tileBounds) {return false;}
	 }  
  	 return true;
     }
       
     CustomProjection.prototype.getWrapWidth=function(zoom) {
         return this.tileBounds[zoom]*this.tileSize;
     }
     
     function customGetTileURL(a,b) {
       var c=Math.pow(2,b);
         var d=a.x;
         var e=a.y;
         
         if(MapMode==1)
			return myurl+"/../ChinaMaps/ChinaStreet/G"+b+"/my_new_task/gm_"+d+"_"+e+"_"+b+".png"
		 else if(MapMode==2)
			return myurl+"/../ChinaMaps/ChinaStatllite/G"+b+"/my_new_task/gs_"+d+"_"+e+"_"+b+".png"		 
         else if(MapMode==3)
			return myurl+"/../ChinaMaps/ChinaTerrain/G"+b+"/my_new_task/gt_"+d+"_"+e+"_"+b+".png"
		else
			return myurl+"/../ChinaMaps/ChinaStreet/G"+b+"/my_new_task/gm_"+d+"_"+e+"_"+b+".png"
		
    }
    
     function getWindowHeight() {
		if(CustomMapHeight==0)
		{			
			 if (window.self&&self.innerHeight) {
				return self.innerHeight;
			}
			 if (document.documentElement&&document.documentElement.clientHeight) {
				return document.documentElement.clientHeight;
			 }
			// debugger;
			if(document.documentElement&&document.documentElement.scrollHeight>0)
				return document.documentElement.scrollHeight-10;
         }
		else
		  return CustomMapHeight;
     }

     function resizeMapDiv() {
         //Resize the height of the div containing the map.
         //Do not call any map methods here as the resize is called before the map is created.
         var d=document.getElementById("map");
        // debugger;
         var offsetTop=0;
         for (var elem=d; elem!=null; elem=elem.offsetParent) {
             offsetTop+=elem.offsetTop;
         }
         var height=getWindowHeight()-offsetTop-16;
         if (height>=0) {
            d.style.height=height+"px";
            d.style.height="360px";
         }
     }

        function addSite(map, siteCode, siteDesc, lat, lng, address, type) {
	        var icon=new GIcon();
	      // debugger;
	        if(type<2007)
				icon.image=IconUrl;
			else
				icon.image=IconUrl2;
	        
	        if(InitZoomLevel!=null && InitZoomLevel>3)
			{
				icon.iconSize = new GSize(12,12);
				icon.iconAnchor = new GPoint(6,6);
				icon.infoWindowAnchor = new GPoint(6, 6);
			}
			else
			{
				icon.iconSize = new GSize(8,8);
				icon.iconAnchor = new GPoint(4,4);
				icon.infoWindowAnchor = new GPoint(4, 4);
			}
	        var mark = new GMarker(
		        new GLatLng(lat,lng), 
		        {icon:icon, title:siteDesc}
		        );
        		
	        map.addOverlay(mark);
	        GEvent.addListener(mark, "click", function() {
	  		ShowCityInfo(siteCode, siteDesc,1);
			//mark.openInfoWindowHtml(address);
	        }); 
	        GEvent.addListener(mark, "mouseover", function() {
		        mark.setImage(MouseOverIconUrl); 
				ShowCityInfo(siteCode, siteDesc,0);
		        }); 
	        GEvent.addListener(mark, "mouseout", function() {
	        	if(type<2007)
					mark.setImage(IconUrl);
				else
					mark.setImage(IconUrl2);
				
		       // SelectCity("中国",0);
		        }); 
        }
		
//		function ShowCityInfo(arrayID,CityName,OperType)
//		{
//		   document.getElementById('Path_Link').innerHTML="<a href='javascript:SelectCity(\"中国\",0)'><span >中国</span> </a> <span > &gt; </span><a href='javascript:SelectCity(\""+PosArr[arrayID][4]+"\",1)'><span >"+PosArr[arrayID][4]+"</span> </a><span> &gt; </span><a href='javascript:SelectCity(\""+PosArr[arrayID][3]+"\",2)'><span >"+PosArr[arrayID][3]+"</span> </a>";
//				
//			document.getElementById('City_Link').innerHTML="";
//			
//			if(OperType==1)
//			{
//				if(PosArr[arrayID][5]<2007)
//					FillQueryInfo(CityName,2);
//				else
//					document.getElementById('Product_List').innerHTML="<p><span>计划实施过程中，暂无数据</span></p>";
//			}
//		   
//		}
//		
//		function FillQueryInfo(cityName,Level)
//		{
//			var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
//　　　		oHttpReq.open("POST", "/Component/WebQueryProduction.aspx?cityName="+escape(cityName)+"&Level="+Level, false);
//			//　　　 alert(state);
//　　　		oHttpReq.send(null);
//　　　		result = oHttpReq.responseText;
//			//if(result!="")
//			{
//				document.getElementById('Product_List').innerHTML=result;
//		/*		var List = result.split(',');
//				for(var i=0;i<List.length;i++) 
//				{
//        　　		var Item = List[i].split('<-->');
//　    　			var newOption = document.createElement("OPTION");
//　    　			newOption.text = Item[1];
//　　				newOption.value = Item[0];
//　    　			drp2.options.add(newOption);
//				}   */
//			}
//		}

//        function SelectCity(Name,Level)
//        {
//			var strHtml="";
//			
//			if(Level==0)
//			{
//				document.getElementById('Path_Link').innerHTML="<a href='javascript:SelectCity(\"中国\",0)'><span >中国</span> </a> ";
//				for (var i = 0; i < PosArr.length; i++) 
//				{
//					if(strHtml.indexOf(PosArr[i][4])<0)
//						strHtml=strHtml+"<a href='javascript:SelectCity(\""+PosArr[i][4]+"\",1)'><span >"+PosArr[i][4]+"</span> </a>&nbsp;&nbsp;";
//				}
//				document.getElementById('City_Link').innerHTML=strHtml;
//				FillQueryInfo("中国",0);
//			}
//			else if(Level==1)
//			{
//				document.getElementById('Path_Link').innerHTML="<a href='javascript:SelectCity(\"中国\",0)'><span >中国</span> </a><span > &gt; </span> <a href='javascript:SelectCity(\""+Name+"\",1)'><span >"+Name+"</span> </a>";
//				for (var i = 0; i < PosArr.length; i++) 
//				{
//					if(PosArr[i][4]==Name && strHtml.indexOf(PosArr[i][3])<0)
//						strHtml=strHtml+"<a href='javascript:ShowCityInfo("+i+",\""+PosArr[i][3]+"\",1)'><span >"+PosArr[i][3]+"</span> </a>&nbsp;&nbsp;";
//				}
//				document.getElementById('City_Link').innerHTML=strHtml;
//				FillQueryInfo(Name,1);

//			}
//			else 
//			{
//			
//			}
//			
//        }
        
        function addSiteX(map, siteCode, siteDesc, lat, lng, address, type) {
	        var icon=new GIcon();
	        icon.image=IconUrl;
			
	        icon.iconSize = new GSize(16, 16);
	        icon.iconAnchor = new GPoint(8, 8);
	        opts = {
		        "icon": icon,
		        "clickable": false,
		        "draggable": false,
		        "labelText": siteDesc,
		        "labelOffset": new GSize(-8, -8)
	        };
	        var mark = new LabeledMarker(new GLatLng(lat, lng), opts); 
        		
	      //  arySite[siteCode] = mark;
	        map.addOverlay(mark);

	        mark.div.onclick = function() {alert(address);};
	        // 非GEvent事件
	       GEvent.addListener(mark, "click", function() {mark.openInfoWindowHtml(address);}); 
        }

        function LabeledMarker(latlng, options){
            this.latlng = latlng;
            this.labelText = options.labelText || "";
            this.labelClass = options.labelClass || "markerLabel";
            this.labelOffset = options.labelOffset || new GSize(0, 0);
            GMarker.apply(this, arguments);
        }

        LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));

        LabeledMarker.prototype.initialize = function(map) {
	        GMarker.prototype.initialize.call(this, map);
	        var div = document.createElement("div");
	        div.className = this.labelClass;
	        div.innerHTML = this.labelText;
	        div.style.position = "absolute";
	        div.style.background="red";
	        div.style.border="1px solid";
	        div.style.width="32px";
	        map.getPane(G_MAP_MARKER_PANE).appendChild(div);
        	
	        this.map = map;
	        this.div = div;
        }

        LabeledMarker.prototype.redraw = function(force) {
	        GMarker.prototype.redraw.call(this, map);
  	        if (!force) return;
	        var p = this.map.fromLatLngToDivPixel(this.latlng);
	        var z = GOverlay.getZIndex(this.latlng.lat());
	        this.div.style.left = (p.x + this.labelOffset.width) + "px";
	        this.div.style.top = (p.y + this.labelOffset.height) + "px";
	        this.div.style.zIndex = z + 1; 
        }

        LabeledMarker.prototype.remove = function() {
          this.div.parentNode.removeChild(this.div);
          this.div = null;
          GMarker.prototype.remove.call(this);
        }

     function load() {
       if (GBrowserIsCompatible()) {
         resizeMapDiv();
         var copyright = new GCopyright(1,
                               new GLatLngBounds(new GLatLng(18.05, 73.5),
                                                 new GLatLng(53.75, 135)),
                               0,
                               "");
         var copyrightCollection = new GCopyrightCollection("");
         copyrightCollection.addCopyright(copyright);

         //create a custom picture layer
         var pic_tileLayers = [ new GTileLayer(copyrightCollection , 0, 15)];
         pic_tileLayers[0].getTileUrl = customGetTileURL;
         pic_tileLayers[0].isPng = function() { return false; };
         pic_tileLayers[0].getOpacity = function() { return 1.0; };
         var proj=new CustomProjection(12,imageWraps);
         var pic_customMap = new GMapType(pic_tileLayers, proj, "Pic",
             {maxResolution:11, minResolution:3, errorMessage:""});

         map = new GMap2(document.getElementById("map"),{mapTypes:[pic_customMap]});
         
         if(ShowToolbar)
         {
         //  map.addControl(new GScaleControl());
           map.addControl(new GMapTypeControl());
	       map.addControl(new GOverviewMapControl());
           map.addControl(new GLargeMapControl());
		  }
         map.enableDoubleClickZoom();
	     map.enableContinuousZoom();
	     map.enableScrollWheelZoom();
         map.setCenter(new GLatLng(centreLat, centreLon), initialZoom, pic_customMap);

           var minPosX=0,minPosY=0,maxPosX=0,maxPosY=0;
         //  var icon,siteDesc;
         //  debugger;
         
           for (var i = 0; i < PosArr.length; i++) 
           {
            // var point = new GLatLng(PosArr[i][1],PosArr[i][0]);
                 var siteDesc= PosArr[i][3];
                 addSite(map,i,PosArr[i][3],PosArr[i][2],PosArr[i][0],"",PosArr[i][5]);
            /*                 
                 if (i < 1)
                 {
                     minPosX = maxPosX = PosArr[i][0];
                     minPosY = maxPosY = PosArr[i][2];
                 }
                 else
                 {
                     if (maxPosX < PosArr[i][0]) maxPosX = PosArr[i][0];
                     if (maxPosY < PosArr[i][2]) maxPosY = PosArr[i][2];
                     if (minPosX > PosArr[i][0]) minPosX = PosArr[i][0];
                     if (minPosY > PosArr[i][2]) minPosY = PosArr[i][2];
                 }  */
             }

          //设中心点和缩放级别
           if(InitZoomLevel!=null)
				map.setZoom(InitZoomLevel);
           else
				map.setZoom(3);
        /*    map.panTo(new GLatLng((minPosY + maxPosY) / 2,(minPosX + maxPosX) / 2));
            var y=(minPosY + maxPosY) / 2;
            var x=(minPosX + maxPosX) / 2; */
       }
     }
     try
     {
         load();
     }
     catch(err)
     { }    
