// ** AJAX PRODUCTS LIST *darkSnow
// Base On YUI  | with : ae + connection + history
// 2009-2-2 14:43:42 
AE.namespace('AE.app.ajaxList');
AE.app.ajaxList = function(config){
	//** 静默配置定义 **
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	this.config = {
		target : get("product-list"),	//指定要填充的容器ID  容器中的innerHTML将作为模板进行循环  且其中的星雪花标签和数据集中的索引名相同
		linkMenu : get("category-list"),	//指定与之关联操作的菜单 
																		//关联菜单EXP <li><a source="freesample" limitNum="22">category-1</a></li>  其中source为数据集文件名 limitNum是取前n笔
		fileName : "freesample",				//文件索引(名称)
		showLimit : -1,						//列表显示数据笔数  -1是无穷 有多少显示多少  从第一个取值到指定笔数位置结束  如果数据笔数<指定限制 则全部显示
		cache : false,						//AJAX是否缓存
		autoHistoryRun : true,		//默认使用锚点记录载入列表
		alwaysRun : false,				//无论加载数据岛是否成功 都显示内容
		dataType : "XML",					//数据岛类型
		useProxy : false,					//强制启用代理	针对于Firefox3.5必定返回200状态开启
		
		//AJAX History配置文件
		historyIframeUrl : "blank.htm",	//锚点控制

		//如果是XMLData
		xmlPathPrefix : globalImgServer+"/images/cms/upload/xml/free_sample/",
		xmlPathSuffix : ".xml"


	};
	this.config = YL.merge(this.config,config||{});
	
	this.defineEvents();
	this.defaultRuns();
};


AE.app.ajaxList.prototype = {
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 动作捆绑相关
	defineEvents : function(){
		var _self = this;var config = this.config;
		//注册自定义函数集
		this.afterRun = new YAHOO.util.CustomEvent("afterRun",this);
		this.beforeRun = new YAHOO.util.CustomEvent("beforeRun",this);
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 默认运行组
	defaultRuns : function(){
		var _self = this;var config = this.config;
		// 初始化 history
		this.setHistory();
		// 批量装换目标内空链接
		this.defineEmptyLinks(config.linkMenu);
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	//** 定义方法组 **	
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 提取根据模板生成&填充内容
	tplValues : function(sendConfig){
			var _self = this;var config = this.config;
			var target = config.target;
			
			// 模板的存放
			if(!target.getAttribute("model")){
				target.setAttribute("model",target.innerHTML);
				target.innerHTML = "";
			}
			
			// 定位数据集
			var xmlDoc = this.xmlDoc;
			var root = this.selectSingleNode("//Freesample");
			var promotionId = this.selectSingleNode("//PROMOTION").text;
			var productItems = root.getElementsByTagName("PRODUCT");
			
			// 确定显示个数 sendConfig从sendRequest中来~
			var showLimit = sendConfig.showLimit?sendConfig.showLimit:config.showLimit;
			if(showLimit > productItems.length){showLimit = productItems.length;}
			if(showLimit == -1){showLimit = productItems.length;}
			
			// 初始话填充代码
			var listHtml = "";
			// 开始遍历
			for(x=0;x<showLimit;x++){
				var item = productItems[x];
				// 生成当前使用的临时模板
				var beReplaceHtml = target.getAttribute("model");
				var attributes = item.attributes;
				// 二次遍历 属性名对应模板里面的星雪花标签替换
				for(n=0;n<attributes.length;n++){
					var attr = attributes[n];
					beReplaceHtml = beReplaceHtml.replace(eval("/\\*"+attr.nodeName+"\\*/ig"),attr.nodeValue);
				}
				listHtml += beReplaceHtml;
				if((x+1)%4==0){
					listHtml += '<div style="clear:both;"></div>';
				}
			}
			
			//重新注入容器
			target.innerHTML = listHtml;
			
			//GA 释放内存
			listHtml = null;
			if(AE.bom.isIE){ CollectGarbage(); }
			
			//触发自定义函数集  - AftertplValues  因为需要productItems.length 所以放在此函数体内 暂不抽离
			this.afterRun.fire({
													filePath:config.sourceFile,
													count:productItems.length
												});
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// AJAX发送 如果跨域自动切换至代理模式
	sendRequest : function(sendConfig){
		var _self = this;var config = this.config;
		
		// 发送前执行
		this.beforeRun.fire({
												filePath:config.sourceFile
											});

		// 防止重复刷新
		if(_self.isLoading)return;
		
		// 确定读取文件名
		if(sendConfig.fileName){config.fileName	= sendConfig.fileName;}

		// 判断数据源格式 组合完整路径
		if(config.dataType == "XML"){
			config.sourceFile	= config.xmlPathPrefix + config.fileName + config.xmlPathSuffix;
		}
		
		var callback =
		{
			success:function(o){
					// 建立通用数据
					_self.buildCommonData(o);
					// 开始执行操作
					_self.tplValues(sendConfig);
					_self.isLoading = false;
				},
			failure:function(o){
								if(!_self.config.alwaysRun)return false;
								_self.isLoading = false;
								//触发自定义函数集  - AftertplValues
								_self.afterRun.fire({
																		filePath:config.sourceFile
																	});
				},
			cache:_self.config.cache,
			timeout:5000
		}
		
		// ajax 启用请求方式
		if(config.useProxy){
			// 代理模式
			var pAjax = new AE.run.ajaxProxy();
			pAjax.asyncRequest("GET", _self.config.sourceFile, callback,"");
			_self.isLoading = true;
		}else{// 默认模式
			try{
					YAHOO.util.Connect.asyncRequest('GET', _self.config.sourceFile, callback);
					_self.isLoading = true;
			}catch(e){
					var pAjax = new AE.run.ajaxProxy();
					pAjax.asyncRequest("GET", _self.config.sourceFile, callback,"");
					_self.isLoading = true;
			}
		};
		
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 制作通用数据  黑盒
	buildCommonData : function(request){
		switch(this.config.dataType.toUpperCase()){
				case "XML" :
					this.xmlDoc = request.responseXML.documentElement;
					break;
				case "JSON" :
					break;	
		}
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// History <- -> fav
	setHistory : function(){
		var _self = this;var config = this.config;
		//针对cms不让加iframe 动态增加
		var historyIframe = document.createElement("iframe");
		historyIframe.id = "yui-history-iframe";
		historyIframe.src = config.historyIframeUrl;
		historyIframe.style.display = "none";
		document.body.appendChild(historyIframe);
		
		var historyInput = document.createElement("input");
		historyInput.id = "yui-history-field";
		historyInput.style.display = "none";
		document.body.appendChild(historyInput);
				
		YUE.on(historyIframe,"load",function(){
			try {
		    var bookmarkedState = YAHOO.util.History.getBookmarkedState("source");
		    var bookmarkedState = bookmarkedState || "freesample";
		    //alert(YAHOO.util.History.getBookmarkedState("limitNum"));
		    YAHOO.util.History.register("source", bookmarkedState, function (state) {
		    	var source = YAHOO.util.History.getBookmarkedState("source");
		    	var limitNum = YAHOO.util.History.getBookmarkedState("limitNum");
		    	productsList.sendRequest({fileName:source,showLimit:limitNum});
		    });
		    YAHOO.util.History.onReady(function () {
		    	var source = YAHOO.util.History.getBookmarkedState("source");
		    	var limitNum = YAHOO.util.History.getBookmarkedState("limitNum");
		      productsList.sendRequest({fileName:source,showLimit:limitNum});
		    });
	      YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
	    } catch (e) {}
	  })
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 加入函数至自定义函数集 函数集将在ajax sending前触发
	pushBF : function(fn){
			if(this.beforeRun){
				this.beforeRun.subscribe(fn);
			}
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 加入函数至自定义函数集 函数集将在ajax sending后触发
	pushAF : function(fn){
			if(this.afterRun){
				this.afterRun.subscribe(fn);
			}
	},
	
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 其它支援工具函数 | -------------------------------------------------------这些很好用  有望提取为通用工具
	
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 兼容FF的 xPath Select
	selectSingleNode : function(elementPath){
			var xmlDoc = this.xmlDoc;
	    if(window.ActiveXObject)
	    {
	        return xmlDoc.selectSingleNode(elementPath);
	    }
	    else
	    {
	       var xpe = new XPathEvaluator();
	       var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
	       var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	       return results.singleNodeValue;
	    }
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 正则截取前后
	trim : function(str){
		return str.replace(/(^\s*)|(\s*$)/g,"");
	},
	//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
	// 批量装换目标内空链接href为javascript:void(0) 支持css:hover
	defineEmptyLinks : function(target){
		var links = target.getElementsByTagName("a");
		for(i=0;i<links.length;i++){
			var link = links[i];
			if(!link.href){
					link.href = "javascript:void(0);";
				}
		}
	}
}
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
