function createMap(id,center,zoom) 
{
	if(GBrowserIsCompatible()) 
	{
		var newGmap = new GMap2(top.document.getElementById(id));
		newGmap.addControl(new GSmallZoomControl());
		//newGmap.enableScrollWheelZoom();
		if(typeof(center)=="undefined") center = new GLatLng(39.98774,116.33376);
		if(typeof(zoom)=="undefined") zoom = 12;
		newGmap.setCenter(center,zoom);
		return newGmap;
	}
	else
	{
		alert("GBrowserIsCompatible is not TRUE");	
	}
}

GMarker.prototype.setZIndex=function(zindex)
{
	for(var m in this)
	{
		if(typeof(this[m])=="object")
		{
			try
			{
				if(typeof(this[m][0].src) != "undefined") this[m][0].style.zIndex=zindex;	
			}
			catch(e){}
		}	
	}	
};

function ICON(){}
ICON.prototype = new GIcon();
ICON.prototype.shadow = "http://ditu.google.com/mapfiles/shadow50.png";
ICON.prototype.iconSize = new GSize(20,34);
ICON.prototype.shadowSize = new GSize(37,34);
ICON.prototype.iconAnchor = new GPoint(9,34);
ICON.prototype.infoWindowAnchor = new GPoint(9,2);
ICON.prototype.infoShadowAnchor = new GPoint(18,25);
ICON.prototype.image="http://ditu.google.com/mapfiles/markerA.png";

GMap2.prototype.pixel = function(point)
{
	var pos = new Object();
	var bounds = this.getBounds();
	var DownLeft = this.getCurrentMapType().getProjection().fromLatLngToPixel(bounds.getSouthWest(),this.getZoom());
	var UpRight = this.getCurrentMapType().getProjection().fromLatLngToPixel(bounds.getNorthEast(),this.getZoom());
	var here = this.getCurrentMapType().getProjection().fromLatLngToPixel(point,this.getZoom());
	pos.x=here.x-DownLeft.x;
	pos.y=here.y-UpRight.y;
	return pos;
};

GMap2.prototype.fitShow = function(points)
{
	var Bound = new GLatLngBounds();
	for(i=0;i<points.length;i++)
	{
		Bound.extend(points[i]);
	}
	var newCenter = Bound.getCenter();
	var newZoom = this.getBoundsZoomLevel(Bound); 
	if(newZoom>=15) newZoom=15;
	this.setCenter(newCenter,newZoom);

};

function NTU2Degree(ntu)
{
return (ntu*1.0)/100000;	
}


//=================================================================================================
//Serach and Show in map
function Ye_showInfo()
{
	this.setZIndex(10000);
	var markerinfo=top.document.getElementById("markerinfo");
	if(markerinfo) markerinfo.parentNode.removeChild(markerinfo);
	this.outImage=this.getIcon().image;
	this.setImage("http://www.google.com/intl/zh-CN_ALL/mapfiles/markerie.gif");
	var infodiv=top.document.createElement("div");
	infodiv.style.border="solid 1px skyblue";
	infodiv.style.position="absolute";
	var pos=this.super_.pixel(this.getPoint());
	infodiv.style.padding="8px";
	infodiv.style.background="#fff";
	infodiv.style.left=pos.x+"px";
	infodiv.style.top=(pos.y)+"px";
	infodiv.setAttribute("id","markerinfo");
	infodiv.innerHTML="<div><img style='float:left' src='"+this.outImage+"'></div><div>"+this.name+"</div>";
	this.super_.getContainer().appendChild(infodiv);
}
function Ye_hideInfo()
{
	this.setZIndex(0);
	this.setImage(this.outImage);
	var markerinfo=top.document.getElementById("markerinfo");
	if(markerinfo) markerinfo.parentNode.removeChild(markerinfo);
}
function Ye_setLatLng()
{
	//var YES=confirm("You have select the \n\n "+this.name);	
	//if(YES) this.super_.doafter({point:this.getPoint(),name:this.name});
	var lat = top.document.createElement("input");
	lat.type="hidden";
	lat.name="lat";
	lat.id="lat";
	lat.value=this.point.lat();
	top.document.getElementById("form1").appendChild(lat);
	var lng = top.document.createElement("input");
	lng.type="hidden";
	lng.name="lng";
	lng.value=this.point.lng();
	top.document.getElementById("form1").appendChild(lng);
	this.super_.doafter({point:this.getPoint(),name:this.name});
}

function zIndexProcess(marker,b)
{
	return Math.round(marker.getPoint().lat()*-1000000)	
}

function Ye_showSearch(searchResult){
	var points=new Array();
	var point=new Object();
	var markers=new Array();
	var marker=new Object();
	var icon=new Object();
	var Letters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
	if(searchResult.searchPoints.length>0)
	{
		for(var m=0;m<searchResult.searchPoints.length;m++)
		{
			poi = searchResult.searchPoints[m];
			point=new GLatLng( NTU2Degree(poi.point[1]) , NTU2Degree(poi.point[0]) );
			points.push(point);
			icon = new ICON();
			icon.image="http://ditu.google.com/mapfiles/marker"+Letters[m]+".png";
			marker = new GMarker(point,{icon:icon});
			//marker = new GMarker(point,{icon:icon,zIndexProcess:zIndexProcess});
			marker.name = poi.name;
			marker.super_ = this ;
			marker.point = point;
			GEvent.addListener(marker,"mouseover",Ye_showInfo);
			GEvent.addListener(marker,"mouseout",Ye_hideInfo);
			GEvent.addListener(marker,"click",Ye_setLatLng);
			this.addOverlay(marker);
			markers.push(marker);				
		}
		this.fitShow(points);
	}
	else
	{
		this.doafter(null);
	}
}
GMap2.prototype.searchAvtive = function(city,address,doAfter)
{
	var thismap=this;
	thismap.doafter =doAfter;
	var search=new LTLocalSearch(Ye_showSearch.bind(thismap));
	search.setCity(city);
	search.search(address);
	var search=new LTLocalSearch();
};
GMap2.prototype.search = function(city,address)
{
	var thismap=this;
	var addr = address;
    var addr_length=addr.length;
	thismap.doafter=function(obj){
		showSelect();
		if(obj==null)
		{
			alert("There is no matched result!");
			if(top.document.getElementById("overlay")) top.document.body.removeChild(top.document.getElementById("overlay"));
			if(top.document.getElementById("map")) top.document.body.removeChild(top.document.getElementById("map"));
			return;
		};
		if(obj=="close")
		{
			//alert("There is no matched result!");
			if(top.document.getElementById("overlay")) top.document.body.removeChild(top.document.getElementById("overlay"));
			if(top.document.getElementById("map")) top.document.body.removeChild(top.document.getElementById("map"));
			return;
		};
		var point=obj.point;
		var name=obj.name;
		var lat = top.document.createElement("input");
		lat.type="hidden";
		lat.name="lat";
		lat.id="lat";
		lat.value=point.lat();
		top.document.getElementById("form1").appendChild(lat);
		var lng = top.document.createElement("input");
		lng.type="hidden";
		lng.name="lng";
		lng.value=point.lng();
		top.document.getElementById("form1").appendChild(lng);
		if(top.document.getElementById("overlay")) top.document.body.removeChild(top.document.getElementById("overlay"));
		if(top.document.getElementById("map")) top.document.body.removeChild(top.document.getElementById("map"));
		top.document.getElementById("cn_location").value=address;	
	};
	var search=new LTLocalSearch(function(searchResult){
		if(!searchResult.searchPoints[0]){
				addr_length--;
				search.search(addr.substring(0,addr_length))
	    }else{
			var points=new Array();
			var point=new Object();
			var markers=new Array();
			var marker=new Object();
			var icon=new Object();
			var Letters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
			if(searchResult.searchPoints.length>0)
			{
				for(var m=0;m<searchResult.searchPoints.length;m++)
				{
					poi = searchResult.searchPoints[m];
					//alert(NTU2Degree(poi.point[1])+":"+NTU2Degree(poi.point[0]));
					point=new GLatLng( NTU2Degree(poi.point[1]) , NTU2Degree(poi.point[0]) );
					points.push(point);
					icon = new ICON();
					icon.image="http://ditu.google.com/mapfiles/marker"+Letters[m]+".png";
					marker = new GMarker(point,{icon:icon});
					//marker = new GMarker(point,{icon:icon,zIndexProcess:zIndexProcess});
					marker.name = poi.name;
					marker.super_ = this ;
					marker.point = point;
					GEvent.addListener(marker,"mouseover",Ye_showInfo);
					GEvent.addListener(marker,"mouseout",Ye_hideInfo);
					GEvent.addListener(marker,"click",Ye_setLatLng);
					this.addOverlay(marker);
					markers.push(marker);				
				}
				this.fitShow(points);
			}
			else
			{
				this.doafter(null);
			}
		}
	}.bind(thismap));
	search.setCity(city);
	search.search(address);
};

GMarker.prototype.showInfo = function()
{
	this.setZIndex(1000);
	this.setImage("/images/map/on/"+(this.num_>0 ? this.num_ : 'star')+".png");
	if(!this.super_.getBounds().contains(this.getPoint())) return;
	var infodiv = top.document.createElement("div");
	infodiv.style.position="absolute";
	infodiv.innerHTML=this.info_;
	this.super_.getContainer().appendChild(infodiv);
	infodiv.style.left=this.super_.pixel(this.getPoint()).x-infodiv.offsetWidth-this.getIcon().iconAnchor.x+"px";
	infodiv.style.top=this.super_.pixel(this.getPoint()).y-infodiv.offsetHeight/2+10-this.getIcon().iconAnchor.y+"px";
	this.infodiv=infodiv;
};

GMarker.prototype.hideInfo = function()
{
	this.setZIndex(0);
	this.setImage("/images/map/out/"+(this.num_>0 ? this.num_ : 'star')+".png");
	if(this.infodiv) this.infodiv.parentNode.removeChild(this.infodiv);
};
//=================================================================================================


function makeCopyrightSmaller() 
{
	if (map.isLoaded()) 
	{
		for(var i = 0; i < map.getContainer().childNodes.length; ++i) {
			if(map.getContainer().childNodes[i].innerHTML.indexOf(String.fromCharCode(169)) !== -1){
      			map.getContainer().childNodes[i].style.display = 'none';
      			window.clearInterval(copyrightTimer);
      			break;
    		}
    	}
  	}
}
