﻿function attachIconEvent(){
		var obj = $("icon");
		for(var i=0;i<obj.childNodes.length;i++){
			(function(){
				var tempI = i;
				obj.childNodes[tempI]["onclick"] = function(evt){
					evt = evt||window.event;
					var tagElement = evt.srcElement?evt.srcElement:evt.target;
					if(tagElement.parentNode.className == "icon_2")return;
					var aElement = obj.childNodes[tempI].getElementsByTagName("div")[2].firstChild;					
					var target = aElement.target;
					if(target == "" || target == null){
						location.href=aElement.href;
					}else{
						self.open(aElement.href);
					}
				};
				obj.childNodes[tempI]["onmouseover"] = function(){
					obj.childNodes[tempI].getElementsByTagName("div")[0].style.top = "-3px";
				};
				obj.childNodes[tempI]["onmouseout"] = function(){
					obj.childNodes[tempI].getElementsByTagName("div")[0].style.top = "0px";
				};
			})();
		}
	}
	function attachBusBtnEvent(){
		var obj = $("bussearch").getElementsByTagName("div");
		for(var i=2;i<5;i++){
			(function(){
				var tempI = i;
				obj[tempI]["onclick"] = function(){busBtnClick(tempI);};
			})();
		}
	}
	var nowBusBtnI = 3;
	function busBtnClick(btnI){
		var obj = $("bussearch").getElementsByTagName("div");
		var conObj = $("search_bus");
		if(nowBusBtnI != btnI){
			obj[nowBusBtnI].className = "bus_blur";
			conObj.childNodes[nowBusBtnI-2].style.display = "none";
		}else{
			return;
		}
		nowBusBtnI = btnI;
		obj[btnI].className = "bus_focus";
		conObj.childNodes[btnI-2].style.display = "";
	}
	var nowBtnI = 0;
	function attachSearchBtnEvent(){
		var obj = $("btn");
		for(var i=0;i<obj.childNodes.length;i++){
			(function(){
				var tempI = i;
				obj.childNodes[tempI]["onclick"] = function(){searchBtnClick(tempI);};
				obj.childNodes[tempI]["onmouseover"] = function(){searchBtnOver(tempI);};
				obj.childNodes[tempI]["onmouseout"] = function(){searchBtnOut(tempI);};
			})();
		}
	}
	function searchBtnClick(btnI){
		var obj = $("btn");
		var conObj = $("container");
		if(nowBtnI != btnI){
			obj.childNodes[nowBtnI].className = "btn_normal";
			conObj.childNodes[nowBtnI+2].style.display = "none";
			if(nowBtnI == 1){
				$("bussearch").style.display = "none";
			}
		}else{
			return;
		}
		nowBtnI = btnI;
		if(nowBtnI == 1){
			$("bussearch").style.display = "";
		}
		obj.childNodes[btnI].className = "btn_focus";
		conObj.childNodes[btnI+2].style.display = "";
	}
	function searchBtnOver(btnI){
		var obj = $("btn");
		if(obj.childNodes[btnI].className == "btn_focus")return;
		obj.childNodes[btnI].className = "btn_hover";
	};
	function searchBtnOut(btnI){
		var obj = $("btn");
		if(obj.childNodes[btnI].className == "btn_focus")return;
		obj.childNodes[btnI].className = "btn_normal";
		
	};
	var inputTextHashTable = [];
	var inputTextNameTable = [];
	function attachTextInputEvent(){
		var objs = document.getElementsByTagName("input");
		for(var i=0;i<objs.length;i++){
			if(objs[i].className == "textinput"){
				//alert(objs[i].value);
				inputTextHashTable[i] = objs[i].value;
				inputTextNameTable[objs[i].id] = i;				
				(function(){
					var tempI = i;
					objs[tempI]["onfocus"] = function(){
						if(objs[tempI].value==inputTextHashTable[tempI]){
							objs[tempI].value = "";
						}else{
							objs[tempI].select();
						}
					};
					objs[tempI]["onblur"] = function(){
						if(objs[tempI].value == "")
						objs[tempI].value = inputTextHashTable[tempI];
					};
					objs[tempI]["onkeydown"] = function(evt){
						evt = evt||window.event;
						if(evt.keyCode == 13){
							var k=objs[tempI].nextSibling;
							while(k.className != "search_btn"){
								k = k.nextSibling;
							};
							if(document.all){
								k.fireEvent("onclick");
							}else{
								eval(k.getAttribute("onclick"));
							}
						}
					};
				})();
			}
		}
	}
	function attachCityListEvent(){
		var objs = $("cityChild").childNodes;
		for(var i=0;i<objs.length;i++){
			(function(){
				var tempI = i;
				//if(objs[tempI].value){
					objs[tempI]["onclick"] = function(){cityListClick(tempI);};
					objs[tempI]["onmouseover"] = function(){cityListOver(tempI);};
					objs[tempI]["onmouseout"] = function(){cityListOut(tempI);};
				//}
			})();
		}
	}
	function cityListClick(objI){
		var objs = $("cityChild").childNodes;
		citycode_load = objs[objI].getAttribute("code");
		location.href = "/index.jsp?citycode=" + citycode_load;
		//$("cityname").value = objs[objI].innerText;
		//$("cityChild").style.display = "none";
		return true;
	}
	function cityListOver(objI){
	   var objs = $("cityChild").childNodes;
	   objs[objI].style.backgroundColor = "#ffffff";
	}
	function cityListOut(objI){
		var objs = $("cityChild").childNodes;
		objs[objI].style.backgroundColor = "#D2E5D8";
	}
	function startSearch(act){
		var obj1 = obj2 = null;
		switch(act){
			case 0:
				obj1 = $("keyword");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入关键字");obj1.focus();return;};
				self.open(encodeURI("/bmap.jsp?act=local&key=" + obj1.value + "&citycode=" + citycode_load));
				return true;
			break;
			case 1:
				obj1 = $("linename");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入线路名称");obj1.focus();return;};
				self.open(encodeURI("/bmap.jsp?act=line&key=" + obj1.value + "&citycode=" + citycode_load));
				return true;
			break;
			case 2:
				obj1 = $("busstart");
				obj2 = $("busend");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入换乘起点");obj1.focus();return;};
				if(obj2.value == "" || obj2.value == inputTextHashTable[inputTextNameTable[obj2.id]]){alert("请输入换乘终点");obj2.focus();return;};
				self.open(encodeURI("/bmap.jsp?act=bus&begin=" + obj1.value + "&end="+obj2.value+"&citycode=" + citycode_load));
				return true;
			break;
			case 3:
				obj1 = $("stopname");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入站点名称");obj1.focus();return;};
				self.open(encodeURI("/bmap.jsp?act=stop&key=" + obj1.value + "&citycode=" + citycode_load));
				return true;
			break;
			case 4:
				obj1 = $("driverstart");
				obj2 = $("driverend");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入自驾起点");obj1.focus();return;};
				if(obj2.value == "" || obj2.value == inputTextHashTable[inputTextNameTable[obj2.id]]){alert("请输入自驾终点");obj2.focus();return;};				
				self.open(encodeURI("/bmap.jsp?act=driver&begin=" + obj1.value + "&end="+obj2.value+"&citycode=" + citycode_load));
				return true;
			break;
			case 5:
				obj1 = $("proname");
				if(obj1.value == "" || obj1.value == inputTextHashTable[inputTextNameTable[obj1.id]]){alert("请输入产品名称");obj1.focus();return;};
				self.open(encodeURI("/prosearch.jsp?key=" + obj1.value + "&citycode=" + citycode_load + "&model=Y"));
				return true;
			break;
		}
	};
	//pop
	var imgPosition = [		
		[[-212,-216],[-212,-220],[-160,-218],[-105,-218],[-51,-218],[0,-218]],
		[[-212,-339],[-212,-351],[-160,-339],[-105,-339],[-51,-339],[0,-339]],
		[[-212,9],[-212,7],[-155,5],[-103,5],[-51,5],[-5,5]],		
		[[-212,-297],[-212,-302],[-160,-297],[-105,-297],[-51,-297],[0,-297]],
		//[[-212,-32],[-212,-35],[-155,-34],[-100,-34],[-51,34],[0,-32]],		
		[[-212,-68],[-212,-70],[-155,-72],[-100,-72],[-51,-72],[0,-70]],
		[[-212,-103],[-212,-105],[-160,-107],[-105,-107],[-51,-107],[0,-107]],			
		[[-212,-141],[-212,-143],[-160,-144],[-105,-144],[-51,-144],[0,-144]],	
		[[-212,-177],[-212,-180],[-160,-181],[-105,-181],[-51,-181],[0,-181]]//,
		//[[-212,-177],[-212,-180],[-160,-181],[-105,-181],[-51,-181],[0,-181]]	
	];
	var motionInterval = [];
	var motionDoI = [0,0,0,0,0,0,0,0,0];
	var motionAct = [];
	function startMotion(type){
		if(motionInterval[type] != null){return};
		motionAct[type] = "in";
		motionInterval[type] = setInterval(function(){showMotion(type);},50);
	}
	function showMotion(type){
		if(motionDoI[type] < 0){
			motionDoI[type] = 0;
			endMotion(type);
			return;
		}else if(motionDoI[type] > 4){
			if(motionAct[type] == "out"){
				motionAct[type] = "outgo";
				motionDoI[type] = 4;
			}else{
				motionDoI[type] = 5;
			}
		}
		var objs = $("box").getElementsByTagName("div");
		objs[type].firstChild.style.left = imgPosition[type][motionDoI[type]][0];
		objs[type].firstChild.style.top = imgPosition[type][motionDoI[type]][1];
		if(motionAct[type] == "outgo"){
			motionDoI[type]--;
		}else{
			motionDoI[type]++;
		}
	}
	function endMotion(type){
		if(motionInterval[type] != null){
			clearInterval(motionInterval[type]);
			motionInterval[type] = null;
		}
	}
	function attachMotionEvent(){
		var objs = $("box").getElementsByTagName("div");
		var links = $("tex").getElementsByTagName("a");
		for(var i = objs.length -1;i>-1;i--){
			objs[i].firstChild.style.left = imgPosition[i][0][0];
			objs[i].firstChild.style.top = imgPosition[i][0][1];
			(function(){
				var temp = i;
				objs[temp]["onmouseover"] = links[temp]["onmouseover"] = function(){					
					startMotion(temp);
				};
				objs[temp]["onmouseout"] = links[temp]["onmouseout"] = function(){
					motionAct[temp] = "out";
				};
				objs[temp]["onclick"] = function (){
					//var element = links[temp].firstChild;
					window.open(links[temp]);
				};
			})();
		}
	}
	
	//错误反馈
	var module = "主站";
	var city = "福建省";
	var keyword = "无";
	var _userName ="";
	 function showErrorAdvice(cityname,wrongmodule,wrongkey,wrongcontent){
		if(cityname == null)cityname = city;
		if(wrongmodule == null)wrongmodule = module;
		if(wrongkey == null)wrongkey = keyword;
		if(wrongcontent == null)wrongcontent = "";	    
        window.open(encodeURI("/errAdvice.jsp?city="+cityname+"&operation="+wrongmodule+"&username="+_userName+"&keyword="+wrongkey+"&appendinfo="+wrongcontent+"&req=" + ((new Date()).valueOf())));
	}
	function $(objid){return document.getElementById(objid);};
	function showCityList(){
		var obj = $('cityChild');
		obj.style.display = '';
		obj.focus();
		if(!document.all){
			obj.style.left = -151;
			obj.style.top = -247;
		}
		//obj.style.filter = "gray(enabled=false)";		
		//obj.style.left = "0px";
		//obj.style.top = "0px";
	};
	function initPage(){
		attachSearchBtnEvent();
		attachBusBtnEvent();
		attachTextInputEvent();
		//attachIconEvent();
		attachMotionEvent();
		attachCityListEvent();
		//enableFilter();		
		
	};	
	function enableFilter(){
		var obj = $("logoImg");
		obj.style.filter = "gray(enabled=false)";
		//alert(obj.style.filter)
	};
	document.onmousedown = function(evt){
		evt = evt||window.event;
		var tagElement = evt.srcElement?evt.srcElement:evt.target;
		if(tagElement.parentNode.id == "cityChild")return;
		$("cityChild").style.display = "none";
		//window.status = $("cityChild").style.display;
	};	