_package("bjmap.main");

bjmap.main.pop=function(){
	var $this=this;
	var query=new DCI.Service.POI();
	var isLoaded=false;
	//存储目录的引用
	var rootCatalog=void(0);
	var first_catalogs=void(0);
	
	//元素获取
	var body_pop=$("#body_pop");
	var body_type=$("#body_type");
	var pop_type=$("#pop_type");
	
	/**
	 * @public
	 * @see 显示热点分类
	 */
	this.show=function(){
		if(!isLoaded){
			loadPop();
		}
		body_pop.css("display","block");
	}
	/**
	 * @public
	 * @see 隐藏热点分类
	 */
	this.hide=function(){
		body_pop.css("display","none");	
	}
	
	
	
	
	/**
	 * @private 
	 * @see 加载目录分类。目录一共分为三级,不包括根。如果包括根（全部）,则一共是4级。
	 */
	function loadPop(){
		//获取数据,并附以回调函数，处理返回的结果
		query.getDirectory(function(root,data){
			//分左右两列分布
			var pop_main_left=$("#pop_main_left");
			var pop_main_right=$("#pop_main_right");
			//根目录，跟下便是一级目录分类
			rootCatalog=root;
			first_catalogs=root.catalogs;
			
			
			//<!--遍历一级目录，即大目录
			$.each(first_catalogs,function(index,f_catalog){
				
				var j_div=$("<div class='travel'><h4 class='popmainh4'>"+f_catalog.name+"</h4><dl><dt></dt><dd></dd></dl></div>");
				var j_dt=j_div.find("dt");
				var j_dd=j_div.find("dd");
				
				//<!--遍历2级目录分类
				$.each(f_catalog.catalogs,function(i,s_catalog){
					var s_name=s_catalog.name;
					var s_span=(i+1)==f_catalog.catalogs.length?"":"<span>/</span>";
					var s_link=$("<a href='#'>"+s_name+"</a>"+s_a);
					var s_a=$(s_link[0]).bind("click",function(){
						createType(s_catalog);
					});
					j_dt.append(s_a);
					//<!--遍历3级目录分类
					$.each(s_catalog.catalogs,function(j,t_catalog){
						var t_name=t_catalog.name;
						var a_type=$("<a href='#'>"+t_name+"</a>").bind("click",function(){		
							createType(t_catalog);						
						});
						j_dd.append(a_type);
					});
					//3级目录遍历结束-->
				});
				//2级目录遍历结束-->
				
				
				//给一级分类添加划过事件
				j_div.hover(
					function(){
						this.className='traffichover';
						this.firstChild.className="h4hover";
					},
					function(){
						this.className='traffic';
						this.firstChild.className="popmainh4";
					});
				//左右两个模块排列，平均分配
				if(index%2==0){
					pop_main_left.append(j_div);
				}else{
					pop_main_right.append(j_div);
				}		
			});
			//一级目录遍历结束-->
			isLoaded=true;
		});
	}
	
	
	/**
	 * @private
	 * @see 创建热门分类查询时，地图与模块控制之间的类型选择。
	 * @param flag [?] {number} 标示为，1代表全部目录，2代表二级目录，3代表三级目录,4一级目录
	 */
	function createType(currentCatalog){
		//被点击的目录类型
		var currentLink=void(0);
		
		//清空类型
		var dt=pop_type.find("dt");dt.empty();
		var dd=pop_type.find("dd");dd.empty();
		
		//显示类型
		$this.hide();
		body_type.css("display","block");
		
		
		dt.append($("<span style='font-size:13px;'>分类：</span>"));
		dt.append($("<a href='#' class='secondChildA'>全部</a>").bind("click",function(){
			createType();
		}));
		
		//全部
		if(!currentCatalog){
			$.each(first_catalogs,function(i,f_catalog){
				dd.append($("<a href='#'>"+f_catalog.name+"</a>").bind("click",function(){
					createType(f_catalog);
				}));
			});
			return;
		}
		
		
		//一级目录
		if(currentCatalog.depth==1){
			//添加一级目录
			dt.append($("<img src='images/main/popright.png'/><a href='#' class='secondChildA'>"+currentCatalog.name+"</a><img src='images/main/popright.png'/>"));		
			$.each(currentCatalog.catalogs,function(i,s_catalog){				
				//添加二级目录
				dt.append($("<a href='#'>"+s_catalog.name+"</a>").bind("click",function(){createType(s_catalog);}));
				//添加三级目录
				$.each(s_catalog.catalogs,function(i,t_catalog){
					dd.append($("<a href='#'>"+t_catalog.name+"</a>").bind("click",function(){createType(t_catalog);}));
				});
			});
			return;
		}
		
		//二级目录
		if(currentCatalog.depth==2){
			
		}
		
		//三级目录
		if(currentCatalog.depth==2){
			
		}
		

//		$.each(current_first.catalogs,function(i,s_catalog){				
//			//二级
//			var s_a=$("<a href='#' class=''>"+s_catalog.name+"</a>").bind("click",function(){
//				createType(2,s_catalog);
//			});
//			dt.append(s_a);
//			
//			if(s_catalog=currentCatalog){
//				$.each(s_catalog.catalogs,function(i,t_catalog){
//					//三级
//					var t_a=$("<a href='#' class=''>"+t_catalog.name+"</a>").bind("click",function(){
//						createType(4,t_catalog);
//					});
//					dd.append(t_a);
//				});
//			}
//		});
	}
};
