/*
 *公交换乘查询
*/
var stationPlace=null,inintSelectIndex=null,inintBusAjaxSelect=null,isInintOne=false,isOpenTip=false,isTipClick=false;
var oldAjaxKey="",keyupTimer=null;
function pageX(elem)
{
	return elem.offsetParent?elem.offsetLeft+pageX(elem.offsetParent):elem.offsetLeft;
}
function pageY(elem){
	return elem.offsetParent?elem.offsetTop+pageY(elem.offsetParent):elem.offsetTop;
}
function getStyle(elem,name){
		if(elem.style[name])
		{
			return elem.style[name];
		}
		else if(elem.currentStyle)//尝试使用IE的方法
		{
			return elem.currentStyle[name];
		}
		else if(document.defaultView && document.defaultView.getComputedStyle){
			name=name.replace(/([A-Z])/g,"-$1");
			anme=name.toLowerCase();
			//获取样式对象并获取属性值
			var s=document.defaultView.getComputedStyle(elem,"");
			return s&& s.getPropertyValue(name);
		}else
			return null;
}
function fullWidth(elem)
	{
		if(getStyle(elem,"display")!="none")
			return elem.offsetWidth||getWidth(elem);
		var old=resetCSS(elem,{display:'',visibility:'hidden',position:'absolute'});
		var w=elem.clientWidth||getWidth(elem);
		restoreCSS(elem,old);
		return w;
	}
function resetCSS(elem,prop)
	{
		var old={};
		for(var i in prop){
			old[i]=elem.style[i];
			elem.style[i]=prop[i];
		}
		return old;
	}
function restoreCSS(elem,prop)
	{
		for(var i in prop)
		{
			elem.style[i]=prop[i];
		}
	}
function createTipBox(){
	var o=document.createElement("div");
	o.id="tipCon";
	o.className="tip_con";
	o.onmouseover=function(){
		isTipClick=true;
	}
	o.onmouseout=function(){
		isTipClick=false;
	}
	document.body.appendChild(o);
}
function createInput(id,value,con){
	var container=con.parentNode||document.body;
	var o=document.createElement("input");
	o.type="hidden";
	o.id=id;
	o.name=id;
	o.value=value;
	container.appendChild(o);
}
function openTip(obj){
	var tipCon=$("tipCon");
	tipCon.style.left=(pageX(obj))+"px";
	//tipCon.style.top=(pageY(obj)+obj.offsetHeight)+"px";
	var w=(fullWidth(obj)-2);
	if(obj.id=="buidingKeywords"||obj.id=="pageKeywords"){
		w=(fullWidth(obj)-7);
	}
	tipCon.style.width=w+"px";
	tipCon.style.display="block";
	isOpenTip=true;
}
function setInputBlur(obj){
	if(!$("tipCon")){
		createTipBox();
	}
		initInputValue(obj);
		if(isTipClick){
			setTimeout(function(){
				$("tipCon").style.display="none";
				isOpenTip=false;					
			},200);		
		}else{
			$("tipCon").style.display="none";
			isOpenTip=false;	
		}
}
function setInputFocus(obj){
	if(!$("tipCon")){
		createTipBox();
	}
	clearInputValue(obj);
	obj.setAttribute("autoComplete","off");
	$("tipCon").innerHTML="";
	if(obj.value==""){
		return;		
	}
	stationPlace=obj;
	var queryString="";
	if(obj.id=="stationKeywords"){
		queryString="/dis/tbsAction!find10BusStation.action?tbs.name="+encodeURIComponent(obj.value);
	}else if(obj.id=="startKeywords"||obj.id=="endKeywords"){
		queryString="/dis/buildingAction!listAll.action?building.name="+encodeURIComponent(obj.value)+"&pageNo=1&pageSize=10";
	}else if(obj.id=="buidingKeywords"){
		queryString="/dis/buildingAction!find10.action?name="+encodeURIComponent(obj.value);
	}else{
		queryString="/dis/companyAction!find10.action?name="+encodeURIComponent(obj.value);
	}
	ajax({url:queryString, type:"text", onSuccess:viewTipList});
	if(!isOpenTip)openTip(obj);
}
function getStationTip(obj,event){
	var e=event||window.event;
	var keycode=e.keyCode?e.keyCode:e.which;
	if(!isOpenTip)openTip(obj);
	if((keycode>=48&&keycode<=111)||(keycode>186&&keycode<222)||keycode==8||keycode==32||keycode==46){
		if(obj.value=="" ||obj.value==oldAjaxKey){
			if(obj.value==""){
				$("tipCon").innerHTML="";
			}
			return;		
		}
		$("tipCon").innerHTML="";
		var in_x=$(obj.id+"_x");
		var in_y=$(obj.id+"_y");
		var in_id=$(obj.id+"_id");
		if(in_x)in_x.value="";
		if(in_y)in_y.value="";
		if(in_id)in_id.value="";
		oldAjaxKey=obj.value;
		stationPlace=obj;
		var queryString="";
		if(obj.id=="stationKeywords"){
			queryString="/dis/tbsAction!find10BusStation.action?tbs.name="+encodeURIComponent(obj.value);
		}else if(obj.id=="startKeywords"||obj.id=="endKeywords"){
			queryString="/dis/buildingAction!listAll.action?building.name="+encodeURIComponent(obj.value)+"&pageNo=1&pageSize=10";
		}else if(obj.id=="buidingKeywords"){
			queryString="/dis/buildingAction!find10.action?name="+encodeURIComponent(obj.value);
		}else{
			queryString="/dis/companyAction!find10.action?name="+encodeURIComponent(obj.value);
		}
		if(keyupTimer){
				clearTimeout(keyupTimer);
				keyupTimer=null;
		}	
		keyupTimer=setTimeout(function(){
			ajax({url:queryString, type:"text", onSuccess:viewTipList});
		},300);
	}
}
function viewTipList(data){
	var list=setEval(data);
	if(list=="")return;
	var length=list.name.length;
	var html="";
	var count=0;
	for(var i=0;i<length;i++){
		var className="out";
		if(list.name[i]==stationPlace.value && count==0){
			setStationPoint(list.name[i],list.x[i],list.y[i],list.id[i]);
			inintSelectIndex=i;
			className="over";
			count++;
		}
		var type="";
		if(stationPlace.id=="startKeywords"){
			type="start";
		}else if(stationPlace.id=="endKeywords"){
			type="end";
		}else{
			type="";
		}
		html+='<dl><dt sid="'+list.id[i]+'" class=\''+className+'\' onmouseover="this.className=\'over\'" onmouseout="this.className=\'out\'" onclick="setStationPoint(\''+list.name[i]+'\','+list.x[i]+','+list.y[i]+',\''+list.id[i]+'\');createBusTipLabel('+list.x[i]+','+list.y[i]+',\''+type+'\');">'+list.name[i]+'</dt><dd>'+list.x[i]+'</dd><dd>'+list.y[i]+'</dd></dl>';
	}
	$("tipCon").innerHTML=html;
	inintBusAjaxSelect=null;
	isInintOne=false;
}
function setStationPoint(name,x,y,id){
	stationPlace.value=name;
	var x_id=stationPlace.id+"_x";
	var y_id=stationPlace.id+"_y"
	var id_id=stationPlace.id+"_id"
	$(x_id)?$(x_id).value=x:createInput(x_id,x,stationPlace);
	$(y_id)?$(y_id).value=y:createInput(y_id,y,stationPlace);
	$(id_id)?$(id_id).value=id:createInput(id_id,id,stationPlace);
}
function setKeyHander(event,nextObj,noTip){
	try{
		e=event||window.event;
		var keycode=e.keyCode?e.keyCode:e.which;
		if(nextObj && keycode==13){
			if(typeof nextObj=='string'){
				eval(nextObj);
				$("tipCon").style.display="none";
			}else{
				nextObj.focus();
			}
		}
		if(noTip)return;
		if(keycode==38||keycode==40){
			var elems=$("tipCon").childNodes;
			if(elems.length>0)
			{
				var startText="";
				var x,y,id;
				x=y=id="";
				if(inintSelectIndex!=null&& isInintOne==false)
				{
					inintBusAjaxSelect=elems[inintSelectIndex];
					isInintOne=true;
				}
				if(keycode==40)//下
				{
					if(inintBusAjaxSelect==null){
						inintBusAjaxSelect=elems[0];
						inintBusAjaxSelect.childNodes[0].className="over";
						startText=inintBusAjaxSelect.childNodes[0].innerText||inintBusAjaxSelect.childNodes[0].textContent;
						id=inintBusAjaxSelect.childNodes[0].getAttribute("sid");
						x=inintBusAjaxSelect.childNodes[1].innerText||inintBusAjaxSelect.childNodes[1].textContent;
						y=inintBusAjaxSelect.childNodes[2].innerText||inintBusAjaxSelect.childNodes[2].textContent;
					}else{
						inintBusAjaxSelect.childNodes[0].className="out";
						inintBusAjaxSelect=inintBusAjaxSelect.nextSibling||elems[0];
						inintBusAjaxSelect.childNodes[0].className="over";
						startText=inintBusAjaxSelect.childNodes[0].innerText||inintBusAjaxSelect.childNodes[0].textContent;
						id=inintBusAjaxSelect.childNodes[0].getAttribute("sid");
						x=inintBusAjaxSelect.childNodes[1].innerText||inintBusAjaxSelect.childNodes[1].textContent;
						y=inintBusAjaxSelect.childNodes[2].innerText||inintBusAjaxSelect.childNodes[2].textContent;
					}
					
				}else{//上
					if(inintBusAjaxSelect==null){
						inintBusAjaxSelect=elems[elems.length-1];
						inintBusAjaxSelect.childNodes[0].className="over";
						startText=inintBusAjaxSelect.childNodes[0].innerText||inintBusAjaxSelect.childNodes[0].textContent;
						id=inintBusAjaxSelect.childNodes[0].getAttribute("sid");
						x=inintBusAjaxSelect.childNodes[1].innerText||inintBusAjaxSelect.childNodes[1].textContent;
						y=inintBusAjaxSelect.childNodes[2].innerText||inintBusAjaxSelect.childNodes[2].textContent;
					}else{
						inintBusAjaxSelect.childNodes[0].className="out";
						inintBusAjaxSelect=inintBusAjaxSelect.previousSibling||elems[elems.length-1];
						inintBusAjaxSelect.childNodes[0].className="over";
						startText=inintBusAjaxSelect.childNodes[0].innerText||inintBusAjaxSelect.childNodes[0].textContent;
						id=inintBusAjaxSelect.childNodes[0].getAttribute("sid");
						x=inintBusAjaxSelect.childNodes[1].innerText||inintBusAjaxSelect.childNodes[1].textContent;
						y=inintBusAjaxSelect.childNodes[2].innerText||inintBusAjaxSelect.childNodes[2].textContent;
					}
				}
				setStationPoint(startText,x,y,id);
			}
		}
		}catch(e){}
	}