// 该文件已废弃(没有废弃，购物车依然在使用，加载代码写到vm模板里了)
// 类目 | 适合二层级类目	
// ## required AE.widget.overShow

AE.namespace('AE.app');
AE.app.categories = function(config){
	this.config = {
		rootTarget : "categories",		// 根类目容器
		subTarget : "categories-subs",	// 子类目容器
		afterLoadClass : "after-load-subs",	// 子类目加载完全后根类目容器增加class
		itemCurrentClass : "current",	// 子类目容器显示后，当前选择的根类目className
		afterShowSubClass : "after-show-sub",	// 子类目容器显示后，例如隐藏其它子类目全部箭头className
		afterShowNextItemClass : "active-border-color",	// 子类目容器显示后下一个元素样式 注意 必须配置afterShowSubClass
		showDelayTime : 50,	// 子类目显示延时
		hiddenDelayTime : 40,	// 子类目隐藏延时
		excursion : [172,0],	// 子类目容器坐标
		searchBar : window.searchBar,	// 依赖searchBar配置
		
		forceRun	: false,	// 是否忽略判断折叠强制运行	折叠方式但是不启用AJAX获取数据
		getCategoriesAPI : "/api/get_categories.htm",	// 如果是折叠状态 获取类目API
		iframeMask : AE.bom.isIE ? true : false,	// 是否使用iframe挡板
		
		getThrough  : true,	// 是否需要打通
		throughMaskExcursion : [172,1],	// 打通元素坐标
		maskWidth : "1px",
		maskBackground : "#fff",
		
		maskHeightDiff	:	-2,		// 打通元素高度矫正值
		
		// 折叠状态部署
		isCollapse : YUD.getElementsByClassName("categories-collapse").length > 0 ? true : false,
		handleItem : get("categories-title"),	// 折叠控制按钮
		handleItemLink : get("categories-title").getElementsByTagName("a")[0],
		// view-more 相关
		viewMoreClass : "view-more",
		
		viewMoreText : "View More",
		viewMoreTextClass : "view-more",
		viewLessText : "View Less",
		viewLessTextClass : "view-less",
		
		isShadow : true,	// 是否有阴影
		shadowClass : "shadow-line",
		
		shadow4RootExcursion : [0,0], //root阴影坐标矫正值
		shadow4SubExcursion : [0,0] //sub阴影坐标矫正值
	};
	
	this.temp = {
		rootItems : [],		// 根类目容器元素缓存
		subItems : {},		// 子类目容器元素缓存
		collapseTimeoutId : "", // collapse 延时触发
		expandTimeoutId :"", //rootExpand  延时触发
		viewMoreBox : "",	// view more box
		viewMoreHandle : "",
		whiteMask : "",		// root&sub 补白
		shadow4Root : "",	// 根类目阴影
		shadow4Sub : "",	// 子类目阴影
		iframeMask : null	// 挡板
	};
	// 返回对象
	return this;
}




AE.app.categories.prototype = {
	
	// 子类目显示时
	afterShow : function(ev,s,args){	//args.rootItem : 根类目 , args.subItem : 子类目
		var _self = this;var config = this.config;var temp = this.temp;
		
		
		
		// 自动计算Y坐标 适应屏幕显示
		var xz = (YUD.getY(args.subItem) + args.subItem.offsetHeight) - (document.documentElement.clientHeight + YUD.getDocumentScrollTop());
		if(xz>0){
			YUD.setY( args.subItem,( YUD.getY(args.subItem) - xz ) )
		}else{
			YUD.setY( args.subItem,( YUD.getY(args.rootItem) ) )
		}
				
		// 移动挡板
		if(config.iframeMask){
			YUD.setStyle( temp.iframeMask , "display" ,"block" );
			YUD.setStyle( temp.iframeMask , "width" , args.subItem.offsetWidth + "px" );
			YUD.setStyle( temp.iframeMask , "height" , args.subItem.offsetHeight + "px" );
			YUD.setX( temp.iframeMask,YUD.getX(args.subItem) );
			YUD.setY( temp.iframeMask,YUD.getY(args.subItem) );
		}

		//alert(args.rootItem.offsetHeight)
		if(config.afterShowSubClass){
			YUD.addClass(config.rootTarget,config.afterShowSubClass);
		}

		// 如果有searchBar则隐藏其智能联想
		if(config.searchBar){
			searchBar.intelligentObj.UIController.hideList();
		}
		
		YUD.addClass(args.nextItem,config.afterShowNextItemClass);
		YUD.addClass(args.rootItem.parentNode,config.itemCurrentClass);
		
		// 如果父子菜单打通
		if(config.getThrough){
			
			if(!_self.temp.whiteMask){
				var whiteMask = _self.temp.whiteMask = document.createElement("div");
				whiteMask.style.width = config.maskWidth;
				whiteMask.style.background = config.maskBackground;
				config.subTarget.appendChild(whiteMask);
			}else{
				var whiteMask = _self.temp.whiteMask;
			}
			whiteMask.style.height = (args.rootItem.offsetHeight+config.maskHeightDiff)+"px";
			YUD.setXY(whiteMask,[YUD.getX(args.rootItem)+config.throughMaskExcursion[0],YUD.getY(args.rootItem)+config.throughMaskExcursion[1]]);
		}
		
		// 如果有阴影
		if(config.isShadow){
			if(!_self.temp.shadow4Root){
				var shadow4Root = _self.temp.shadow4Root = document.createElement("div");
				YUD.addClass(shadow4Root,config.shadowClass); 
				config.rootTarget.appendChild(shadow4Root);
			}else{
				var shadow4Root = _self.temp.shadow4Root;
			}
			
			if(args.nextItem){
				shadow4Root.style.display = "";
				shadow4Root.style.width = args.rootItem.offsetWidth+"px";
				YUD.setXY(shadow4Root,[YUD.getX(args.rootItem)+config.shadow4RootExcursion[0],YUD.getY(args.rootItem)+args.rootItem.offsetHeight+config.shadow4RootExcursion[1]]);
			}
			
			if(!_self.temp.shadow4Sub){
				var shadow4Sub = _self.temp.shadow4Sub = document.createElement("div");
				YUD.addClass(shadow4Sub,config.shadowClass); 
				config.rootTarget.appendChild(shadow4Sub);
			}else{
				var shadow4Sub = _self.temp.shadow4Sub;
			}
			shadow4Sub.style.display = "";
			shadow4Sub.style.width = args.subItem.offsetWidth+"px";
			YUD.setXY(shadow4Sub,[YUD.getX(args.subItem)+config.shadow4SubExcursion[0],YUD.getY(args.subItem)+args.subItem.offsetHeight+config.shadow4SubExcursion[1]]);
		}
		
		// 业务相关 未抽离为customEvent
		// 如果有page 强制提升page层级遮挡头部等区域
		if(get("page")){
			get("page").bak_zIndex = YUD.getStyle("page","z-index");
			YUD.setStyle("page","z-index",100000);
		}
		
		
	},
	
	// 子类目隐藏时
	afterHidden : function(ev,s,args){	//args.rootItem : 根类目 , args.subItem : 子类目 , args.nextItem 下一个元素
		var _self = this;var config = this.config;var temp = this.temp;
		
		// 隐藏挡板
		if(config.iframeMask){
			YUD.setStyle( temp.iframeMask , "display" ,"none" );
		}
		
		// 隐藏打通挡板
		YUD.setXY(temp.whiteMask,[-10000,YUD.getY(args.rootItem)+config.throughMaskExcursion[1]]);
		
		if(config.afterShowSubClass){
			YUD.removeClass(config.rootTarget,config.afterShowSubClass);
		}
		
		YUD.removeClass(args.nextItem,config.afterShowNextItemClass);
		YUD.removeClass(args.rootItem.parentNode,config.itemCurrentClass);
		
		if(config.isShadow && _self.temp.shadow4Root && args.subItem.style.display == ""){
			_self.temp.shadow4Root.style.display = "none";
			_self.temp.shadow4Sub.style.display = "none";
		}
		
		// 业务相关 未抽离为customEvent
		// 如果有page 强制提升page层级遮挡头部等区域
		if(get("page") && get("page").bak_zIndex != undefined){
			YUD.setStyle("page","z-index",get("page").bak_zIndex);
		}
		
	},
	
	// 展开根类目容器
	rootExpand : function(){
		var _self = this;var config = this.config;
		config.rootTarget.style.visibility = "visible";
		if(config.isCollapse){
			YUD.addClass(config.handleItemLink,"expanded");
			YUD.addClass(config.handleItemLink.parentNode.parentNode,"handle-expanded");
		}
	},

	rootCollapse : function(){
		var _self = this;var config = this.config;
		config.rootTarget.style.visibility = "hidden";
		if(config.isCollapse){
			YUD.removeClass(config.handleItemLink,"expanded");
			YUD.removeClass(config.handleItemLink.parentNode.parentNode,"handle-expanded");
		}
	},

	// 专用 toggle 方法 (改变高度)
	toggle : function(ev,args){
		var _self = this;var config = this.config;
		var handle = args.handle;
		var collapseClass = args.collapseClass;
		var target = args.target;
		var toggleHeight = args.toggleHeight;

		// 动画中无视执行
		if(handle.locked == true)return;
		
		// 如果处于收起状态
		if(YUD.hasClass(handle,collapseClass)){
			if(args.collapseForce == true)return;
			
			if(args.beforeExpandCall){
				args.beforeExpandCall.call(_self, args);
			}
			
			target.style.height = "auto";
			var toHeight = target.offsetHeight;
			target.style.height = toggleHeight + "px";
			var attributes = {
				height: { to: toHeight }
			};
			var anim = new YAHOO.util.Anim(target, attributes, args.expandTime, YAHOO.util.Easing.easeOut); 
			anim.onComplete.subscribe(function(s, o) {
				handle.locked = false;
				target.style.height = "auto";
				YUD.removeClass(args.handle,args.collapseClass);
				if(args.afterExpandCall){
					args.afterExpandCall.call(_self, args);
				}
			});
			
			handle.locked = true;
			anim.animate();
			
		}else{
			if(args.expandForce == true)return;
			
			if(args.beforeCollapseCall){
				args.beforeCollapseCall.call(_self, args);
			}
			
			var attributes = {   
				height: { to: toggleHeight }
			};
			var anim = new YAHOO.util.Anim(target, attributes, args.collapseTime, YAHOO.util.Easing.easeOut); 
			anim.onComplete.subscribe(function(s, o) {
				handle.locked = false;
				YUD.addClass(args.handle,args.collapseClass);
				if(args.afterCollapseCall){
					args.afterCollapseCall.call(_self, args);
				}
			});
			
			handle.locked = true;
			anim.animate();
		}
	},
	
	// refineItem-viewMore收起后执行
	viewMoreAfterCollapse : function(args){
		var _self = this;var config = this.config;
		args.handleText.innerHTML = config.viewMoreText;
		YUD.replaceClass(args.handleText,config.viewLessTextClass,config.viewMoreTextClass);
		if(AE.bom.isIE){ CollectGarbage(); }
	},
	
	// refineItem-viewMore展开后执行
	viewMoreAfterExpand : function(args){
		var _self = this;var config = this.config;
		args.handleText.innerHTML = config.viewLessText;
		YUD.replaceClass(args.handleText,config.viewMoreTextClass,config.viewLessTextClass);
		if(AE.bom.isIE){ CollectGarbage(); }
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 执行
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	run : function(){
		
			var _self = this;var config = this.config;var temp = this.temp;
			
			config.rootTarget = get(config.rootTarget);
			config.subTarget = get(config.subTarget);
			
			// 遍历子类目
			var subItems = config.subTarget.getElementsByTagName("div");
			
			for(var i=0; i<subItems.length; i++){
				var subItem = subItems[i];
				// 生成并链接与子类目dom元素rel对应的键值
				_self.temp.subItems[trim(subItem.getAttribute("rel")).toUpperCase()] = subItem;
			}

			// 遍历根类目
			var rootItemsA = config.rootTarget.getElementsByTagName("a");
			var rootItemsSpan = config.rootTarget.getElementsByTagName("span");
			
			// 收集元素 . 由于getElementByTagName返回非数组
			var rootItems = [];
			for(var i=0; i<rootItemsA.length; i++){
				rootItems.push(rootItemsA[i]);
			}
			for(var i=0; i<rootItemsSpan.length; i++){
				rootItems.push(rootItemsSpan[i]);
			}
			
			for(var i=0; i<rootItems.length; i++){
				var rootItem = rootItems[i];
				// 取得key
				var key = trim((rootItem.innerText?rootItem.innerText:rootItem.textContent)).toUpperCase();
				// 如果存在子类目dom元素
				if(_self.temp.subItems[key]){
					var overShow = rootItem.overShow = new AE.widget.overShow();
					var subItem = rootItem.subItem = this.temp.subItems[key];subItem.rootItem = rootItem;
					var nextItem = rootItems[i+1]?rootItems[i+1]:null;
					
					// 注册自定义事件
					overShow.afterShow.subscribe(_self.afterShow,{rootItem:rootItem, subItem:subItem, nextItem:nextItem },_self);
					overShow.afterHidden.subscribe(_self.afterHidden,{rootItem:rootItem, subItem:subItem, nextItem:nextItem },_self);
					
					var excursion = config.excursion;
										
					overShow.init({
						targetId:rootItem,
						contentId:subItem,
						showDelayTime:config.showDelayTime,
						hiddenDelayTime:config.hiddenDelayTime,
						excursion:excursion
					});
				}

				_self.temp.rootItems.push(rootItem);
			}


			// 创建挡板
			if(config.iframeMask){
				temp.iframeMask = document.createElement("iframe");
				YUD.setStyle(temp.iframeMask,"position","absolute");
				YUD.setStyle(temp.iframeMask,"display","none");
				YUD.setStyle(temp.iframeMask,"opacity","0");
				YUD.setStyle(temp.iframeMask,"z-index",YUD.getStyle(config.subTarget,"z-index") - 1);
				config.rootTarget.appendChild(temp.iframeMask);
			}
			
			// if has view-more
			var viewMoreHandle = this.temp.viewMoreHandle = YUD.getElementsByClassName(config.viewMoreClass,"a",config.rootTarget)[0];
			if(viewMoreHandle){
				var viewMoreBox = this.temp.viewMoreBox = YUD.getNextSibling(viewMoreHandle);
				var toggleConfig = {
							handle:viewMoreHandle,
							handleText:viewMoreHandle,
							collapseClass:config.viewMoreClass,
							target:viewMoreBox,
							toggleHeight:0,
							afterCollapseCall : _self.viewMoreAfterCollapse,
							afterExpandCall : _self.viewMoreAfterExpand,
							expandTime : 0.25,
							collapseTime : 0.25
				};
				
				YUE.on(viewMoreHandle,"click",_self.toggle,toggleConfig,_self);
			}
			
			

			// 如果是折叠状态
			if(config.isCollapse){
				
				
				YUE.on([config.handleItem,config.rootTarget,config.subTarget], "mouseover", function(){
					clearTimeout(_self.temp.collapseTimeoutId);
					_self.temp.expandTimeoutId = setTimeout(function(){_self.rootExpand();},200)
				})
				
				YUE.on([config.handleItem,config.rootTarget,config.subTarget], "mouseout", function(){
					clearTimeout(_self.temp.expandTimeoutId);
					_self.temp.collapseTimeoutId = setTimeout(function(){_self.rootCollapse();},100)
				})
				
				YUE.on(config.handleItem, "mouseover", function(){
					// 如果有view more 折叠起来
					if(_self.temp.viewMoreBox){
						_self.temp.viewMoreBox.style.height = "0";
						_self.temp.viewMoreHandle.innerHTML = "View More";
						YUD.replaceClass(_self.temp.viewMoreHandle,"view-less","view-more");
					}
				})
				
			}

			// 初始化后向根类目容器增加CSS样式	可使用箭头等
			YUD.addClass(config.rootTarget,config.afterLoadClass);
			
			// 初始化链接
			if(AE.tool){
				AE.tool.defineEmptyLinks(config.rootTarget);
				AE.tool.defineEmptyLinks(config.subTarget);
			}
			if(typeof AE.app.cateClick != 'undefined'){
				var cateclick = new AE.app.cateClick();
				cateclick.init();
			}
	},
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 初始化
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	init : function(customConfig){
		
			// 合并配置
			this.config = YL.merge(this.config,customConfig||{});
			// 定义_self
			var _self = this;var config = this.config;var temp = this.temp;

			if(!config.forceRun && config.isCollapse){
					// 远程获取数据填充
					var callback =
					{
					  success:function(o){
					  	var categoriesBody = document.createElement("div");
					  	categoriesBody.id = "categories-body";
							categoriesBody.innerHTML = o.responseText;
							config.handleItem.parentNode.parentNode.appendChild(categoriesBody);
							// 填充完毕开始真正的初始化
							_self.run();
					  },
					  failure:function(){
					  	// 尝试使用AJAX代理访问
					  	try{
								var ap = new AE.run.ajaxProxy();
								ap.asyncRequest("POST", config.getCategoriesAPI, callback ,"",{proxy:"http://www.aliexpress.com/api/ajax_proxy.htm"});
								ap = null;
							}catch(e){}
					  }
					};
					try{
						var request = YAHOO.util.Connect.asyncRequest('GET', config.getCategoriesAPI , callback);
					}catch(e){
						// 失败尝试运行
						//_self.run();
						var ap = new AE.run.ajaxProxy();
						ap.asyncRequest("POST", config.getCategoriesAPI, callback ,"",{proxy:"http://www.aliexpress.com/api/ajax_proxy.htm"});
						ap = null;
					}
			}else{
				// 正常执行
				_self.run();
			}
			
			
			// 回收内存
			if(AE.bom.isIE){ CollectGarbage(); }
			
			// 返回对象
			return _self;
	}
}
