﻿function Mapper(_1,_2){
	cO(this,_1);
	if(this.parent){
		this.parent=$(this.parent);
	}
	else {
		return ;
	}
	var _;
	this.mouseX=this.mouseY=0;
	this.editable=this.editable||false;
	if(this.editable){
		this.zoomable=this.toggle=false;
		this.show=this.mouse=true;
		
	}else {
		this.zoomable=(this.zoomable==null?true:this.zoomable);
		this.toggle=(this.toggle==null?true:this.toggle);
		this.show=(this.show==null?true:this.show);
		this.mouse=(this.mouse==null?false:this.mouse);
		
	}
	this.zoom=(this.zoom==null?0:this.zoom);
	this.zone=(this.zone==null?0:this.zone);
	this.locale=(this.locale==null?"zhcn":this.locale);
	this.pins=[];
	this.Areaname;
	this.mapnum=0;
	this.parent.className="mapper";
	this.parent.appendChild(this.span=ce("span"));
	_=this.span.style;
	_.display="block";
	_.position="relative";
	ns(this.span);
	if(this.editable){
		this.span.onmouseup=this.addPin.bind(this);
		_=g_createGlow("点击地图加载/移除坐标点");
		_.style.position="absolute";
		_.style.bottom=_.style.right="0";
		ns(_);
		this.parent.appendChild(_);
		
	}
	if(this.zoomable){
		this.span.ondblclick=this.toggleZoom.bind(this);
		_=g_createGlow("双击以放大或缩小地图");
		_.style.position="absolute";
		_.style.bottom=_.style.right="0";
		ns(_);
		this.span.appendChild(_);
	}
	//
	if(this.show){
		var _area=ce("div");
		_area.innerHTML="";
		_area.style.position="absolute";
		_area.style.top="2px";
		_area.style.left="2px";
		_area.id="o1xasarea";
		this.span.appendChild(_area);
		_area=ce("div");
		_area.innerHTML="";
		_area.style.position="absolute";
		_area.style.top="18px";
		_area.style.left="2px";
		_area.id="o1xaspins";
		this.span.appendChild(_area);
	}
	//
	if(this.toggle){
		this.sToggle=_=g_createGlow("显示/隐藏坐标点");
		_.style.position="absolute";
		_.style.top=_.style.right="2px";
		_.onclick=this.toggleShow.bind(this);
		ns(_);
		this.parent.appendChild(_);
	}
	if(this.mouse){
		this.parent.onmouseout=(function (){
			this.timeout=setTimeout((function (){
				this.sMouse.style.display="none";
				
			}).bind(this),1);
			
		}).bind(this);
		this.parent.onmouseover=(function (){
			clearTimeout(this.timeout);
			this.sMouse.style.display="";
			
		}).bind(this);
		this.span.onmousemove=this.span.onmousedown=this.getMousePos.bind(this);
		this.sMouse=_=g_createGlow("(0.0, 0.0)");
		_.style.display="none";
		_.style.position="absolute";
		_.style.bottom=_.style.left="0";
		_.onmouseup=sp;
		ns(_);
		this.span.appendChild(_);
		
	}
	this.updateMap(_2);
	if(_1.coords!=null){
		this.setCoords(_1.coords);
		
	}else {
		if(_1.link!=null){
			this.setLink(_1.link);
			
		}
	}
}
Mapper.sizes=[[488,325,"normal"],[700,466,"zoom"]];
Mapper.prototype={
	update:function (_4,_5){
		if(_4.zoom!=null){
			this.zoom=_4.zoom;
		}
		if(_4.zone!=null){
			this.zone=_4.zone;
		}
		if(_4.locale!=null){
			this.locale=_4.locale;
		}
		if(_4.show!=null){
			this.show=_4.show;			
		}
		if(_4.areaname!=null){
			ge("o1xasarea").innerHTML="地图: "+_4.areaname+"";
		}
		if(_4.mapnum!=null){
			this.mapnum=_4.mapnum;
		}
		this.updateMap(_5);
		if(_4.coords!=null){
			this.setCoords(_4.coords);
		}else {
			if(_4.link!=null){
				this.setLink(_4.link);
			}
		}
	},getZone:function (){
		return this.zone;
	},setZone:function (_6,_7){
		this.zone=_6;
		this.updateMap(_7);
		return true;
	},getLocale:function (){
		return this.locale;
	},setLocale:function (_8,_9){
		this.locale=_8;
		this.updateMap(_9);
	},getZoom:function (){
		return this.zoom;
	},setZoom:function (_a,_b){
		this.zoom=_a;
		this.updateMap(_b);
	},toggleZoom:function (e){
		this.zoom=1-this.zoom;
		this.updateMap();
		this.getMousePos(e);
		
	},getShow:function (){
		return this.show;
		
	},setShow:function (_d){
		this.show=_d;
		var d=this.show?"":"none";
		for(var i=0,len=this.pins.length;i<len;++i){
			if(!this.pins[i].free){
				this.pins[i].style.display=d;
			}
		}
	},toggleShow:function (){
		this.setShow(!this.show);
		
	},getCoords:function (){
		var a=[];
		for(var i in this.pins){
			if(!this.pins[i].free){
				a.push([this.pins[i].x,this.pins[i].y]);
				
			}
		}return a;
		
	},setCoords:function (_12){
		var _;
		for(var i in this.pins){
			this.pins[i].style.display="none";
			this.pins[i].free=true;
			
		}for(var i in _12){
			var _16=_12[i];
			_=this.getPin();
			_.x=_12[i][0];
			_.y=_12[i][1];
			_.style.left=_.x+"%";
			_.style.top=_.y+"%";
			if(this.editable){
				_.a.onmouseup=this.delPin.bind(this,_);
				
			}else {
				if(_12[i][2]){
					_.a.href=_12[i][2];
					_.a.style.cursor="pointer";
					
				}
			}if(_12[i][3]){
				_.a.tt=_12[i][3];
				
			}else {
				_.a.tt="$";
				
			}_.a.tt=str_replace(_.a.tt,"$",_.x.toFixed(1)+", "+_.y.toFixed(1));
			
		}this.onPinUpdate&&this.onPinUpdate(this);
		
	},getLink:function (){
		var s="";
		for(var i in this.pins){
			if(!this.pins[i].free){
				s+=(this.pins[i].x<10?"0":"")+(this.pins[i].x*10).toFixed(0)+(this.pins[i].y<10?"0":"")+(this.pins[i].y*10).toFixed(0);
				
			}
		}return (this.zone?this.zone:"")+(s?":"+s:"");
		
	},setLink:function (_19){
		var a=[];
		_19=_19.split(":");
		if(!this.setZone(_19[0])){
			return false;
			
		}if(_19.length==2){
			for(var i=0;i<_19[1].length;i+=6){
				var x=_19[1].substr(i,3)/10;
				var y=_19[1].substr(i+3,3)/10;
				if(isNaN(x)||isNaN(y)){
					break;
					
				}a.push([x,y]);
				
			}
		}this.setCoords(a);
		return true;
		
	},updateMap:function (_1e){
		this.parent.style.width=this.span.style.width=Mapper.sizes[this.zoom][0]+"px";
		this.parent.style.height=this.span.style.height=Mapper.sizes[this.zoom][1]+"px";
		if(!this.editable){
			this.parent.style.cssFloat=this.parent.style.styleFloat="left";
		}
		if(this.zone==0){
			this.span.style.background="black";
		}
		else {
		    var d = this.mapnum;
		    if (this.zone == 4395 && d == 1) {
                d = 0;
            }
			this.span.style.background="url(/Maps/"+this.locale+"/"+Mapper.sizes[this.zoom][2]+"/"+this.zone + (d ? "_" + d: "") +".jpg)";
		}
		if(!_1e){
			g_scrollTo(this.parent,3);			
		}
		//
		
		//
		this.onMapUpdate&&this.onMapUpdate(this);		
	},cleanPin:function (i){
		var _=this.pins[i];
		_.style.display=this.show?"":"none";
		_.free=false;
		_.a.onmouseup=rf;
		_.a.href="javascript:;";
		_.a.style.cursor="default";
		return _;
		
	},getPin:function (){
		for(var i=0;i<this.pins.length;++i){
			if(this.pins[i].free){
				return this.cleanPin(i);
				
			}
		}
		var _=ce("div"),a=ce("a");
		_.className="pin";
		_.appendChild(a);
		_.a=a;
		a.onmouseover=this.pinOver;
		a.onmouseout=Tooltip.hide;
		a.onmousedown=rf;
		a.onclick=sp;
		if (!Browser.ie){
		a.innerHTML = "<img src='/images/map_party.png' />"
		}
		this.pins.push(_);
		this.cleanPin(this.pins.length-1);
		this.span.appendChild(_);
		return _;
		
	},addPin:function (e){
		e=$E(e);
		if(e._button>=2){
			return ;
			
		}this.getMousePos(e);
		var p=this.getPin();
		p.x=this.mouseX;
		p.y=this.mouseY;
		p.style.left=p.x.toFixed(1)+"%";
		p.style.top=p.y.toFixed(1)+"%";
		p.a.onmouseup=this.delPin.bind(this,p);
		p.a.tt=p.x.toFixed(1)+", "+p.y.toFixed(1);
		this.onPinUpdate&&this.onPinUpdate(this);
		return false;
		
	},delPin:function (pin,e){
		e=$E(e);
		pin.style.display="none";
		pin.free=true;
		sp(e);
		this.onPinUpdate&&this.onPinUpdate(this);
		return ;
		
	},pinOver:function (){
		Tooltip.show(this,this.tt,4,0);
		//
		ge("o1xaspins").innerHTML="坐标: "+this.tt+"";
		//
	},getMousePos:function (e){
		e=$E(e);
		var c=ac(this.parent);
		var _29=g_getScroll();
		this.mouseX=Math.floor((e.clientX+_29[0]-c[0]-3)/Mapper.sizes[this.zoom][0]*1000)/10;
		this.mouseY=Math.floor((e.clientY+_29[1]-c[1]-3)/Mapper.sizes[this.zoom][1]*1000)/10;
		if(this.mouseX<0){
			this.mouseX=0;
			
		}else {
			if(this.mouseX>100){
				this.mouseX=100;
				
			}
		}if(this.mouseY<0){
			this.mouseY=0;
			
		}else {
			if(this.mouseY>100){
				this.mouseY=100;
				
			}
		}if(this.mouse){
			g_setTextNodes(this.sMouse,"("+this.mouseX.toFixed(1)+", "+this.mouseY.toFixed(1)+")");
			
		}
	}
};

