_package("bjmap.index");

bjmap.index.module=function(){
	
	var $this=this;
	var isCurrentModule=false;	//当前模块
	var currentModule=void(0);
	/**
	 * 绑定示例
	 */
	var ms=DCI.get("mapsearch");		//地图搜索
	var ms_arr=ms.getElementsByTagName("a");
	DCI.each(ms_arr,function(a,i){
		DCI.event.bindDom(a,"click",function(){
			var value=a.firstChild.nodeValue;
			params.keyword(value);			
		});		
	});
	var nearsearch_example=DCI.get("nearsearch_example");		//周边搜索
	DCI.event.bindDom(nearsearch_example,"click",function(){
		var text=nearsearch_example.firstChild.nodeValue;
		var values=text.split(" ");
		params.near(values[0],values[2]);			
	});	
	var busexample=DCI.get("busexample");	//公交车
	DCI.event.bindDom(busexample,"click",function(){
		var value=busexample.firstChild.nodeValue;
		var id=this.currentBusModule.id;
		if(id=="buschange"){
			var values=value.split(" ");
			params.transfer(values[0],values[2]);
		}else if(id=="busname"){
			params.line(value);
		}else if(id=="busstation"){
			params.station(value);
		}
	},this);
	var navsearch_example=DCI.get("navsearch_example");		//导航查询
	DCI.event.bindDom(navsearch_example,"click",function(){
		var text=navsearch_example.firstChild.nodeValue;
		var values=text.split(" ");
		params.navigation(values[0],values[2]);			
	});	
	
	var image="images/index/toolbar.png",mdwidth=52,mdheight=37,step=5,spacing=20;
	var modules=[
			new m("地图搜索","搜索POI兴趣点","javascript:void(0);",0,"#54a70d","mapsearch"),
			new m("周边搜索","查询某点附近的兴趣点","javascript:void(0);",1,"#3b79e7","nearsearch"),
			new m("公交查询","北京公交换乘","javascript:void(0);",3,"#96cfec","bussearch"),
			new m("驾车导航","导航北京道路","javascript:void(0);",2,"#e8d40f","navsearch"),
			new m("开发API","二次开发知道","http://www.beijingmap.gov.cn/API/",4,"#ea504c","mapapi")
		];
	
	/**
	 * 通过模块对象组织HTML
	 */
	var gethtml=function(){
		var html='<table style="margin:0 auto;border-collapse:collapse;" border="0"><tr>';
		for(var i=0;i<modules.length;i++){
			var module=modules[i];
			var e=[],y=module.position*mdheight;
			html+='<td valign=bottom style="text-align:center;padding:0px '+spacing+'px;margin:0;cursor:pointer;cursor:hand"><a style="font-size:13px;color:#444;text-decoration:none;vertical-align:bottom" href="'+module.href+'"><div><div style="width:52px;height:37px;margin:.5em auto;cursor:pointer;cursor:hand;background:url('+image+') no-repeat 0 '+-y+'px"></div><span style="white-space:nowrap">'+module.name+"</span></div></a></td>"
		}
		html+="</tr></table>";
		return html;
	};
	
	/**
	 * 模块切换
	 */
	function switcher(id,a){
//		if(typeof(event)=="string"){
//			a=id;
//			id=event;			
//		}
		if($this.currentNode){
			$this.currentNode.style.display="none";
		}
		var div=DCI.get(id);
		div.style.display="block";
		div.style.zIndex=2;
		$this.currentNode=div;
		a.blur();
	};
	
	
	/**
	 * 公交查询类型切换
	 */
	var busSwitcher=function(event,radio){
		radio=radio||event;	
		if(this.currentBusModule){
			this.currentBusModule.style.display="none";	
		}
		var values=radio.value.split(";");
		var div=DCI.get(values[0]);
		var a=DCI.get("busexample");
		a.innerText=values[1];
		div.style.display="block";
		div.style.zIndex=2;
		radio.checked=true;
		this.currentBusModule=div;
	};
	
	/**
	 * 模块切换绑定事件
	 */
	var div=DCI.get('module');
	div.innerHTML=gethtml();
	div.style.textAlign="center";
	var alinks=div.getElementsByTagName("a");
	DCI.each(alinks,function(a,i){
		var module=modules[i];
		new flash(a,module);
		$(a).bind("click",function(){
			if(module.id=='mapapi'){
//					window.open("http://www.beijingmap.gov.cn/API/","_blank","");
					return true;
				}
			
			//激发模块切换和划入之前的模块
			if(currentModule&&currentModule!=a){	
				var preModule=currentModule;
				currentModule=a;
				$(preModule).trigger('mouseout');
			}
			switcher(module.id,a);	
			currentModule=a;	
		});
		//默认打开地图搜索，即第一个模块
		if(i==0){
			$(a).trigger("mouseover");
			$(a).trigger("click");
		}
	});
	
	/**
	 * 公交切换绑定事件
	 */
	var bustype=DCI.get("bustype");
	var radios=bustype.getElementsByTagName("input");
	for(var i=0;i<radios.length;i++){
		var radio=radios[i];
		DCI.event.bindDom(radio,"click",busSwitcher,this,[radio]);
	};
    busSwitcher.call(this,radios[0]);	
	
	/**
	 * 绑定提示
	 */
	bjmap.bindTxt();

	
	/**
	 * 模块类
	 */
	function m(name,info,href,position,color,id){
		this.name=name||"";
		this.info=info||"";
		this.href=href||"";
		this.position=position||"";
		this.color=color||"";
		this.id=id||"";
	};
	
	
	/**
	 * 动画类
	 */
	function flash(a,module){
		this.a=a;
		this.icon=a.firstChild.firstChild;
		this.module=module;
		
		this.offsetXY=[];
		for(var i=0;i<step;i++){
			var x=i*mdwidth;
			var y=this.module.position*mdheight;
			this.offsetXY.push({x:-x,y:-y});
		}
		
		function out(event){
			if(currentModule!=this){
				if($this.inwardTimeout){
					window.clearTimeout($this.inwardTimeout);
					$this.inwardTimeout=null;
					if(current<start){
						current=start;
					}
				}
		        style.color=$this.module.color;
		        style.textDecoration="underline"
		        style.cursor="pointer";
				if(current<stop){
					var offset=$this.offsetXY[current];
					currentOffset=offset;
					$this.icon.style.backgroundPosition=offset.x+"px "+offset.y+"px";
					current++;
					$this.outTimeout=window.setTimeout(out,interval);
				}else{
					window.clearTimeout($this.outTimeout);
					$this.outTimeout=null;
					current=stop-1;
					var offset=currentOffset;
					var y=offset.y+3;
					$this.icon.style.backgroundPosition=offset.x+"px "+y+"px";
				}
			}
		};
		
		function inward(event){
			if(currentModule!=this){
				if($this.outTimeout){
					window.clearTimeout($this.outTimeout);
					$this.outTimeout=null;
					if(current>=stop){
						current=stop-1;
					}
				}
		    	style.color="#444";
		      	style.textDecoration="none"
		      	style.cursor="pointer";
				if(current>=start){
					var offset=$this.offsetXY[current];
					currentOffset=offset;
					$this.icon.style.backgroundPosition=offset.x+"px "+offset.y+"px";
					current--;
					$this.inwardTimeout=window.setTimeout(inward,interval);
				}else{
					window.clearTimeout($this.inwardTimeout);
					$this.inwardTimeout=null;
					current=start;
				}
			}
		};			
		
		var interval=60;
		var isOutting=false;
		var isInwarding=false;
		var stop=step;
		var start=0;
		var current=start;
		var $this=this;
		var currentOffset=void(0);
		$(this.a).bind("mouseover",out);
		$(this.a).bind("mouseout",inward);
		
		var style=$this.a.style;
	};
};




