
//界面控制对象
var ui={
	init:function(){//初始化
		this.allShow=true;
		this.rightShow=true;
		this.top=$('topDiv');
		this.left=$('leftDiv');
		this.right=$('rightDiv');
		this.rightIfr=$('rightIfr');
		this.rightBtn=$('rightBtnDiv');
		this.rightContent=$('rightContentDiv');
		this.main=$('mainDiv');
	},
	changePanel:function(panel,url){//切换面板
		url=url||pageConfig.getUrl('panel'+panel);
		this.rightIfr.src=url;

		for(var i=0;i<5;i++) $('panelBtn'+i).className='';
		$('panelBtn'+panel).className='current';
	},
	showRight:function(){//显示和隐藏右侧面板
		if(this.rightShow){
			this.rightContent.style.display='none';
			this.left.style.marginRight='10px';
			this.right.style.width='10px';
			this.rightShow=false;
		}
		else{
			this.rightContent.style.display='';
			this.left.style.marginRight='373px';
			this.right.style.width='373px';
			this.rightShow=true;
		}
	},
	fullScreen:function(o){//全屏切换
		var h=this.bodyHeight;
		if(this.allShow){
			this.top.style.display='none';
			this.left.style.marginRight='0px';
			this.right.style.display='none';
			this.allShow=false;
			o.setState(true);
			o.setName('返回');
		}
		else{
			this.top.style.display='';
			this.right.style.display='';
			if(this.rightShow)this.left.style.marginRight='373px';
			else this.left.style.marginRight='10px';
			h-=88;
			this.allShow=true;
			o.setState(false);
			o.setName('全屏');
		}
		this.ajustHeight(h);
	},
	ajustHeight:function(botH){
		this.left.style.height=botH+'px';
		this.right.style.height=botH+'px';
		this.rightBtn.style.height=botH+'px';
		this.rightContent.style.height=botH+'px';
		this.rightIfr.style.height=(botH-16)+'px';
		this.main.style.height=botH+'px';
		this.setMapHeight(botH);
	},
	ajustNav:function(){
		var w=navUtils.getWidth();
		var h=navUtils.getHeight();
		var mapW=this.left.offsetWidth;
		var mapH=this.left.offsetHeight;
		$('divMapNavContent').style.left=Math.floor((mapW-w)/2)+'px';
		$('divMapNavContent').style.top=Math.floor((mapH-h)/2)+'px';
		if($('divMapNav').style.display!='none'){
			$('divMapNav').style.width=mapW+'px';
			$('divMapNav').style.height=mapH+'px';
		}
	},
	setMapHeight:function(h){
		try{
			popomap.setHeight(h);
		}
		catch(e){}
	},
	resize:function(){//resize界面
		this.bodyWidth=document.documentElement.clientWidth;
		this.bodyHeight=document.documentElement.clientHeight;
		var topH=88;
		var botH=this.bodyHeight-topH;
		this.ajustHeight(botH);
		this.ajustNav();
	}
};


//首页的地图工具条
var database=[];
var idexMenu={
	getMain:function(){
		var database2=[];
		database2.push({id:10001,englishName:'BusStation',name:'公交',option:{clickFun:loadSign}});
		//database2.push({id:10002,englishName:'SubwayStation',name:'地铁'});
		database2.push({id:10008,englishName:'gySign',name:'标签',option:{clickFun:loadSign,state:true}});
		database2.push({id:10009,englishName:'areaSign',name:'名称',option:{clickFun:loadSign,state:true}});
		//database2.push({id:10003,name:'menu6_2',parent:10006});
		//database2.push({id:10004,name:'menu6_1_1',parent:10005});
		//database2.push({id:10005,name:'menu6_1',parent:10006});
		//database2.push({id:10006,name:'menu6'});
		//database2.push({id:10007,englishName:'testSign',name:'testSign'});
		database=database2.concat(database);
		/*for(var i=0;i<database.length;i++){
			database[i].option={clickFun:loadSign};
			if(database[i].display){
				database[i].option.state=true;
				database[i].option.disabled=true;
			}
		}*/

		var menuData=[];
		menuData.push({id:'sign',name:'图层'});
		menuData.push({});
		//menuData.push({id:'note',name:'便签',option:{clickFun:addNoteOpen}});
		menuData.push({id:'scale',name:'测距',option:{clickFun:doScale}});
		menuData.push({id:'mistake',name:'纠错',option:{clickFun:addErrOpen}});
		menuData.push({});
		menuData.push({id:'D3',name:'立体',option:{state:true,clickFun:changeMapType}});
		menuData.push({id:'D1',name:'卫星',option:{clickFun:changeMapType}});
		//menuData.push({id:'D2',name:'平面',option:{clickFun:changeMapType}});
		menuData.push({});
		menuData.push({id:'fullScreen',name:'全屏',option:{clickFun:fullScreen}});
		menuData[0]['menu']=menuUtil.getMenuDataFromDb(database);

		return menuUtil.getMenu(menuData);
	},
	regSigns:function(){
		var dataLoader=popomap.getInnerInstances().dataLoader;
		for(var i=0;i<database.length;i++){
			var sign=database[i];
			var signName=sign.englishName;
			if(sign.ico)sign.ico='http://'+domain_config.image+sign.ico;
			if(signName&&!dataLoader.getSignHandler(signName)){
				dataLoader.regSign(signName,null,{isLoad:sign.display,z:6});
				dataLoader.getSignHandler(signName).setSign(signName,'layer',sign.ico||'yp_1.gif');
			}
		}
	}
};

function body_onload(){
	ui.init();
	ui.resize();
	mapLoader();
}
window.onload=window.onresize=function(){
	ui.resize();
}

function showRight(){
	ui.showRight();
}
function fullScreen(o){
	ui.fullScreen(o);
}
//----------------------------------------------
//ui action
//加载和移除图层
function loadSign(o){
	var id=o.data.englishName;
	
	if(o.isCheck) popomap.loadSign(id);
	else popomap.removeSign(id);
}
function doScale(o){//测距
	popomap.selectMode(1);
}
function changeMapType(o){//切换三维、平面、卫星图
	switch(o.id){
		case 'D1':
			MainMenu.getMenuItemById('D1').setState(true);
			//MainMenu.getMenuItemById('D2').setState(false);
			MainMenu.getMenuItemById('D3').setState(false);
			popomap.changeMapType(1);
			break;
		case 'D2':
			MainMenu.getMenuItemById('D1').setState(false);
			//MainMenu.getMenuItemById('D2').setState(true);
			MainMenu.getMenuItemById('D3').setState(false);
			popomap.changeMapType(2);
			break;
		case 'D3':
			MainMenu.getMenuItemById('D1').setState(false);
			//MainMenu.getMenuItemById('D2').setState(false);
			MainMenu.getMenuItemById('D3').setState(true);
			popomap.changeMapType(3);
			break;
	}
}

//----------------------------------------------
//search
function setMapValue(form){
	var flag=true;
	if(form&&form.locale){
		if(typeof(o_locale)=='string')form.locale.value=o_locale;
	}
	if(form&&form.map){
		if(typeof(map)=='number'){
			form.map.value=map;
		}
		else{
			alert('没有找到地图');
			flag=false;
		}
	}
	//if(flag)switchNavView(false);
	return flag;
}

//----------------------------------------------
//utils
function $(id){
	return document.getElementById(id);
}
function isEmpty(fieldValue)
{
	var regEx = /\S+/i;
	if(regEx.test(fieldValue))
		return false;
	else
		return true;
}
function LinkHandler(){
	function fixLink(){
		var linkList=document.links;
		for(var i=0;i<linkList.length;i++){
			linkList[i].onfocus=function(){this.blur()};
		}
	}
	if(document.all)window.attachEvent('onload',fixLink);
	else window.addEventListener('load',fixLink,false);
}
function writeFlash(flashURL,flashWIDTH,flashHEIGHT){
	document.writeln ('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" WIDTH='+flashWIDTH+' HEIGHT='+flashHEIGHT+'>'); 
	document.writeln ('<PARAM NAME=movie VALUE="'+flashURL+'">'); 
	document.writeln ('<PARAM NAME=wmode VALUE=transparent>'); 
	document.writeln ('<PARAM NAME=loop VALUE=true>'); 
	document.writeln ('<PARAM NAME=quality VALUE=high>'); 
	document.writeln ('<PARAM NAME=menu VALUE=false>'); 
	document.writeln ('<EMBED src="'+flashURL+'" wmode="transparent" loop=true wmode=opaque quality=high swLiveConnect=FALSE WIDTH='+flashWIDTH+' HEIGHT='+flashHEIGHT+' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>'); 
	document.writeln ('</OBJECT>');
}
LinkHandler();
var utils={};
utils.html={
	data:{},
	changeCls:function(obj,cls){
		if(obj.className!=''&&!obj._className){
			obj._className=obj.className;
		}
		if(cls) obj.className=cls;
		else if(obj._className) obj.className=obj._className;
	},
	isInputText:function(obj){
		var name='value';
		var name2='_value';
		if(!obj[name2]) return false;
		else if(this.trim(obj[name])==obj[name2]) return false;
		return true;
	},
	cleanText:function(obj,state){
		var name='value';
		var name2='_value';
		if(state==1){
			if(!obj[name2]) obj[name2]=obj[name];
			if(obj[name]==obj[name2]) obj[name]='';
		}
		else if(state==0){
			if(this.trim(obj[name])=='') obj[name]=obj[name2];
		}
	},
	isEmpty:function(str){
		var re=/\S+/;
		if(re.test(str)) return false;
		else return true;
	}
}
function textEvent(obj,type){
	switch(type){
		case 'focus':
			obj.isfocus=true;
			utils.html.cleanText(obj,1);
		case 'mouseover':
			utils.html.changeStyle(obj,'borderColor','#91cf0f');
			break;
		case 'blur':
			obj.isfocus=false;
			utils.html.cleanText(obj,0);
		case 'mouseout':
			if(!obj.isfocus) utils.html.changeStyle(obj,'borderColor','#b4b4b4');
			break;
	}
}
function dosearch(form,keyword,initKey)
{
	if(!keyword&&(isEmpty(form.keyword.value)||form.keyword.value==initKey)){
	  alert('请输入关键字');
	  form.keyword.focus();
	  return false;
	}
	ui.changePanel(0);
	//setPanel2Style();
	if(keyword){
		form.keyword.value=keyword;
		form.submit();
	}
	return true;
}
function dosearch1(keyword){
	if(!omapApi.getMapStatus()) {
		alert('请在加载完以后再搜索,谢谢!');
		return false;
	}
	var form=document.mapForm;
	if(form.screen){
		var d=getMapRang();
		form.lx.value=Math.floor(d.startX/2);
		form.ly.value=Math.floor(d.startY/2);
		form.ux.value=Math.floor(d.endX/2);
		form.uy.value=Math.floor(d.endY/2);
	}
	var initKey='楼房实体 / 道路 / 河流';
	return dosearch(form,keyword,initKey);
}

function doquery1(form)
{
	if(!isComplete) {
		alert('请在加载完以后再搜索,谢谢!');
		return false;
	}
    if (isEmpty(form.from.value)||form.from.value=='所在地')
	{
   	   alert('起始不能为空');
	   form.from.focus();
      return false;
     }
	 else if (isEmpty(form.to.value )||form.to.value=='目的地')
	 {
   	   alert('终点不能为空');
	   form.to.focus();
       return false;
     }else
	 {
	    //form.action = ui.getUrl('busline');
		form.target = 'rightIfr';
		ui.changePanel(0);
		//setPanel2Style();
		//menuSwitch('search');
	 }
	 return setMapValue(form);
 } 
function doquery2(form)
{  
	if(!isComplete) {
		alert('请在加载完以后再搜索,谢谢!');
		return false;
	}
     if (isEmpty(form.stationName.value )||form.stationName.value=='例如:体育中心')
	{
   	   alert('站点不能为空');
	   form.stationName.focus();
       return false;
     }else
	 {
	    //form.action = ui.getUrl('busline');
		form.target = 'rightIfr';
		ui.changePanel(0);
		//setPanel2Style();
		//menuSwitch('search');
	 }
	 return setMapValue(form);
 }
function doquery3(form)
{
	if(!isComplete) {
		alert('请在加载完以后再搜索,谢谢!');
		return false;
	}
     if (isEmpty(form.name.value )||form.name.value=='例如:178')
	 {
   	   alert('路线不能为空');
	   form.name.focus();
      return false;
     }else
	 {
	    //form.action = ui.getUrl('busline');
		form.target = 'rightIfr';
		ui.changePanel(0);
		//setPanel2Style();
		//menuSwitch('search');
	 }
	 return setMapValue(form);
 }