
//注册响应远程方法的listener
function doAction(o){
	o=crossApi.cloneObject(o);
	var p=o.params;
	switch(o.action){
		case 'postInfo':
			postInfo(p.curtype);
			break;
		case 'showPop':
			showPop(p.x,p.y,p.content);
			break;
		case 'showInfo':
			showInfo(p.id,p.x,p.y,p.type,p.subtype);
			break;
		case 'changeIfr':
			changeIfr_info(p.id,p.url);
			break;
		case 'changeTarget':
			ui.changeTarget(p.panel);
			break;		
		case 'showResult':
			showResult(p.id,p.type);
			break;
		case 'showLoginPanel':
			showLoginPanel(p.returnPanel,p.returnSubPanel,p.returnUrl)
			break;
		case 'go2Building':
			omapUtils.go2Building(p.id,p.content);
			break;
		case 'openGame':
			navUtils.openGame();
			break;
		case 'showGameRoom':
			omapUtils.showGameRoom(p.id);
			break;
		case 'goHouse':
			omapUtils.goHouse(p.houseId);
			break;
		case 'buyHouse':
			omapUtils.buyHouse(p.houseId);
			break;
		case 'createPopT':
			mapLibC.createPopT(p.idStr,p.htmlStr,p.x0,p.y0,p.sx,p.sy,p.z);
			break;
		case 'createPop':
			mapLibC.createPop(p.idStr,p.htmlStr,p.x0,p.y0,p.sx,p.sy,p.z);
			break;
		case 'createIframePop':
			mapLibC.createIframePop(p.title,p.src,p.width,p.height,p.x,p.y);
			break;
		case 'createSimpleLabel':
			createSimpleLabel(p.id,p.x,p.y,p.content);
			break;
		case 'addSimpleSignData':
			mapLibC.addSimpleSignData(p.data);
			break;
		case 'clearSimpleSignData':
			mapLibC.clearSimpleSignData();
			break;
		case 'initConfig':
			mapLibC.initConfig(p.config);
			break;
		default:
			return false;
	}
	return true;
}


var mapLibC={
	getMapConfig:function(){
		var x=popomap.getCenterX();
		var y=popomap.getCenterY();
		var w=popomap.getMapWidth();
		var h=popomap.getMapHeight();
		var zoom=popomap.getZoom();

		var zz=Math.pow(2,zoom-1);
		var startX=x-Math.floor(w/2)*zz;
		var startY=y-Math.floor(h/2)*zz;
		var endX=startX+w*zz;
		var endY=startY+h*zz;

		return {centerX:x,centerY:y,width:w,height:h,zoom:zoom,startX:startX,startY:startY,endX:endX,endY:endY};
	},
	createPop:function(idStr,htmlStr,x0,y0,sx,sy,z){
		htmlStr=htmlStr.replace('!{mapApi.removePop}','mapApi.removePop(\''+idStr+'\');return false;');
		popomap.createPop(idStr,htmlStr,x0,y0,sx,sy,z);
	},
	createPopT:function(idStr,htmlStr,x0,y0,sx,sy,z){
		htmlStr=htmlStr.replace('!{mapApi.removePop}','mapApi.removePop(\''+idStr+'\');return false;');
		popomap.createPopT(idStr,htmlStr,x0,y0,sx,sy,z);
	},
	createIframePop:function(title,src,w,h,x,y){
		var sx=150*Math.pow(2,popomap.getZoom()-1);
		var htmlStr=popSet['iframePop'];
		if(!x)x=popSet['X'];
		if(!y)y=popSet['Y'];
		w=parseInt(w);
		h=parseInt(h);
		var id='iframePop';
		var params={x:x,y:y};
		htmlStr=htmlStr.replace('!{id}',id);
		htmlStr=htmlStr.replace('!{title}',title||'信息');
		htmlStr=htmlStr.replace('!{w1}',w+20);
		htmlStr=htmlStr.replace('!{h1}',h+50);
		htmlStr=htmlStr.replace('!{w2}',w);
		htmlStr=htmlStr.replace('!{h2}',h);
		htmlStr=htmlStr.replace('!{url}',src);
		popomap.removePop(id);
		popomap.go2xyS(parseInt(x)+sx,y,null,fun);
		function fun(){
			popomap.createPop(id,htmlStr,x,y,103,103,5);
		}
	},
	initConfig:function(config){//temp
		for(var item in config){
			switch(item){
				case 'infoWin_UrlCfg':
					this._setCfg_infoWin_UrlCfg(config.infoWin_UrlCfg);
					break;
				case 'map_dataUrl':
					this._setCfg_map_dataUrl(config.map_dataUrl);
					break;
				case 'map_isShowInfoWin':
					popomap.setValue('isShowAUInfo',config.map_isShowInfoWin);
					break;
				case 'map_events':
					var events=config.map_events;
					for(var i=0;i<events.length;i++){
						this.setEventOut(events[i].eventType,events[i].eventFun);
					}
					break;
				case 'map_simpleSign':
					var d=config.map_simpleSign;
					for(var item in d){
						this.addSimpleSign(item,d[item]);
					}
					break;
				default:
					break;
			}
		}
	},
	_setCfg_infoWin_UrlCfg:function(cfg){
		cfg=cfg||{};
		var url=cfg.url||'';
		var data=cfg.data;
		try{
			if(data.constructor!=Array.toString()){
				data=[];
			}
		}
		catch(e){
			data=[];
		}
		var ids={};
		for(var i=0;i<data.length;i++){
			ids[data[i]]=1;
		}
		popomap.getMap().Config.urls['entityMapInfo']=url.replace('!{param.id}','!{id}');
		popomap.getMap().Config.urlEntityIds=ids;
	},
	_setCfg_map_dataUrl:function(cfg){
		if(!cfg) return;
		popomap.getMap().Config.infoLoadType='proxy';
		popomap.getMap().Config.hsLoadType='proxy';
		popomap.getMap().Config.mapDataPage=cfg;
		popomap.getInnerInstances().dataLoader.busStationData={};
		//popomap.getMap().Static.dataLoader.reloadInfo();
	},
	addSimpleSign:function(type,sign){
		var simpleSign=popomap.getInnerInstances().simpleSign;
		simpleSign.addSign(type,sign.img,sign.clickFun,sign.sx,sign.sy);
		if(sign.subtype){
			for(var item in sign.subtype){
				simpleSign.addSignSubtype(item,sign.subtype[item],type);
			}
		}
	},
	addSimpleSignData:function(data){
		popomap.getMap().mapApi.addUserPop(data,'simpleSign');
	},
	clearSimpleSignData:function(){
		popomap.getMap().mapApi.addUserPop(null,'simpleSign');
	},
	setEventOut:function(eventType,eventFun){
		switch(eventType){
			case 'mapPosChange':
				mapInvokeLib.regEvent_MapPosChange(eventFun);
				break;
			case 'mapZoom':
				mapInvokeLib.regEvent_MapZoom(eventFun);
				break;
			default:
				break;
		}
	}
}
var mapInvokeLib={
	inovkes:{bs:'bstest'},
	outInvoke:function(funKey,args){
		var fun=this.inovkes[funKey];
		if(typeof fun=='function'){
			fun(args);
		}
		else{
			var d=null;
			d={args:args};
			d.action=funKey;
			crossApi.execute(d);
		}
	},
	regOutInvoke:function(type,fun){
		this.invokes[type]=fun;
	},
	invokeEvent_MapPosChange:function(){
		if(this.mapPosChangeTimeout){
			clearTimeout(this.mapPosChangeTimeout);
			this.mapPosChangeTimeout=null;
		}
		this.mapPosChangeTimeout=setTimeout(fireEvent_MapPosChange,2000);
	},
	fireEvent_MapPosChange:function(){
		this.mapPosChangeTimeout=null;
		var mapConfig=mapLibC.getMapConfig();
		var operation={action:this.mapPosChangeEvent,mapConfig:mapConfig};
		crossApi.execute(operation);
	},
	regEvent_MapPosChange:function(eventKey){
		this.mapPosChangeEvent=eventKey;
		window.fireEvent_MapPosChange=function(){
			mapInvokeLib.fireEvent_MapPosChange();
		};
		var fun=function(){
			mapInvokeLib.invokeEvent_MapPosChange();
		};
		popomap.setEvent('mapPosChange',fun);
	},
	fireEvent_MapZoom:function(zoom){
		var operation={action:this.mapZoomEvent,zoom:zoom};
		crossApi.execute(operation);
	},
	regEvent_MapZoom:function(eventKey){
		this.mapZoomEvent=eventKey;
		var fun=function(zoom){
			mapInvokeLib.fireEvent_MapZoom(zoom);
		};
		popomap.setEvent('mapZoom',fun);
	}
};
function outInvoke(type,args){
	mapInvokeLib.outInvoke(type,args);
}
function mapInvoke(arg){
	var operation=arg;
	operation.action=arg.clickFun;
	crossApi.execute(operation);
}
function createSimpleLabel(id,x,y,content){
	content=content||'';
	var re=/<\s*br\s*(\/)?\s*>/ig;
	content=content.replace(re,'\n');
	var rows=content.split(/\n/).length;
	content=content.replace(/\n/g,'<br />');
	var htmlStr='<table border="0" cellpadding="1" cellspacing="3" bgcolor="#59D2FF" style="border: 1px solid #209FE2;">    <tr>      <td bgcolor="#FFFFFF"><table border="0" cellpadding="1" cellspacing="1"><tr><td style="font-size:12px;line-height:16px;" nowrap>'+content+'</td><td valign="top" style="padding:2px"><a href="javascript:mapApi.removePop(\'simpleLabel'+id+'\')" style="text-decoration:none"><font color="#ff6600" face="Verdana" style="cursor:pointer;font-size:9px;border: 1px solid #ff6600; padding:0px 2px;"><strong>X</strong></font></a></td></tr></table></td>    </tr>  </table><img src="http://'+domain_config.api+'/images/point.gif" width="17px" height="17px">';
	var sy=26+rows*16;
	popomap.go2xy(x,y);
	popomap.removePop('simpleLabel'+id);
	popomap.createPop('simpleLabel'+id,htmlStr,x,y,0,sy,10);
}