﻿/**
 * @author liuwei
 */
$Dom().nameSplace('SentenceMode');
SentenceMode.funs = (function(){

	// --------------------------------【页面元素】-------------------------
	var sentences = {
		sentence_box : $Dom(document).getElementsByClassName('sentence_box')[0],
		layer : $('sentence_layer'),
		showArea : $('layer_cont'),
		reqText : '',
		reqHref : ''
	};

	// --------------------------------【设置浮动层的位置】-------------------------
	var _sentence_layer_position = function(_this, layer, hOffset){
		var	x = $Dom(_this).getXY().x,
			y = $Dom(_this).getXY().y;

		hOffset = hOffset || 0;

		$Dom(layer).setStyle('left', x+'px');
		$Dom(layer).setStyle('top', y-hOffset+'px');
	};

	// --------------------------------【绑定鼠标事件】-------------------------
	var _bindEvent = function(domain){
		var	sentence_box = sentences.sentence_box,
			reqContents = $Dom(sentence_box).getElementsByClassName('s_word'),
			layer = sentences.layer,
			outTime = null,
			reqContents_text = '';

		if(!reqContents) return;
		for(var i = 0, len = reqContents.length; i < len; i++){
			var link = reqContents[i].getElementsByTagName('a')[0];

			$EVENT(link).addEvent('mouseover', function(evt){
				var _this = this;
				sentences.reqText = this.innerHTML;
				sentences.reqHref = domain+'/search/zh/'+this.innerHTML;
				clearTimeout(outTime);
				setTimeout(function(){
					_sentenceData(_this, layer, domain);	// 数据请求
				}, 100);
				$EVENT().stopEvent(evt);
			});

			$EVENT(link).addEvent('mouseout', function(evt){
				clearTimeout(outTime);
				outTime = setTimeout(function(){
					$Dom(layer).setStyle('left', '-99999px');
					$Dom(layer).setStyle('top', '-99999px');
				}, 0);
				$EVENT().stopEvent(evt);
			});
		};

		$EVENT(layer).addEvent('mouseover', function(evt){
			clearTimeout(outTime);
			$EVENT().stopEvent(evt);
		});

		$EVENT(layer).addEvent('mouseup', function(evt){
			$EVENT().stopEvent(evt);
		});

		$EVENT(layer).addEvent('mouseout', function(evt){
			clearTimeout(outTime);
			outTime = setTimeout(function(){
				$Dom(layer).setStyle('left', '-99999px');
				$Dom(layer).setStyle('top', '-99999px');
			}, 0);
			$EVENT().stopEvent(evt);
		});

		$EVENT(document).addEvent('mouseup', function(evt){
			$Dom(layer).setStyle('left', '-99999px');
			$Dom(layer).setStyle('top', '-99999px');
		});
	};

	// --------------------------------【数据请求】-------------------------
	var _sentenceData = function(_this, layer, domain){
		var	ajax = new AJAX(),
			url = domain+'/tooltip/sentenceMode?query='+encodeURIComponent(sentences.reqText),
//			url = domain+'/html/xml_format_modify.xml?'+Math.random(),
			callBack = {
				success : function(resCont){
					if(typeof resCont === 'string'){
						//alert('返回数据类型错误！');
						return;
					}
					var	root = resCont.documentElement,
						titles = root.getElementsByTagName('title'),
						items = root.getElementsByTagName('item'),
						show = sentences.showArea;

					show.innerHTML = '';
					if(titles.length === 0) return;	// 无返回数据
					for(var i = 0, titlesLen = titles.length; i < titlesLen; i++){
						try{
							var	config = _configData(titles[i]),
								lists = items[i] ? items[i].getElementsByTagName('list') : null;

							// 创建title部分
							config.titleLink.innerHTML = titles[i].childNodes[0].nodeValue;
							config.titleLink.href = config.href;
							config.pinyinTag.innerHTML = '  [ '+config.pinyin+' ]  ';

							config.titleTag.appendChild(config.titleLink);
							config.titleTag.appendChild(config.pinyinTag);

							if(config.tts)	config.titleTag.innerHTML+= config.tts;

							// 显示【title】部分
							show.appendChild(config.titleTag);

							// 创建列表部分
							if(lists){
								for(var k = 0, listsLen = lists.length; k < listsLen; k++){
									if(lists[k].childNodes[0]){
										var listItem = $('<li>');
										listItem.innerHTML = lists[k].childNodes[0].nodeValue;
										config.lists.appendChild(listItem);
									}
								}

								// 显示整个列表
								show.appendChild(config.lists);
							}

							if(config.more){
								config.moreTag.innerHTML = config.more;
								$Dom(config.moreTag).addClass('more_c');
								config.moreTag.href = config.href;
								show.appendChild(config.moreTag);
							}
						}catch(e){ }
					}

					// 创建推荐部分
					config.recommend_link.innerHTML = sentences.reqText;
					config.recommend_link.href = sentences.reqHref;
					config.recommend.appendChild(config.recommend_link);
					show.appendChild(config.recommend);

					// 显示layer位置
					var	hLayer = layer.offsetHeight;
					_sentence_layer_position(_this, layer, hLayer);
				},
				failure : function(resCont){
					//alert(resCont);
				}
			};
		ajax.request('GET', url, callBack);
		ajax.abortXHR('5000');	// 5秒后停止请求
	};

	// --------------------------------【取出返回值】-------------------------
	var _configData = function(title){
		return{
			// 取出相应信息
			href : title.getAttribute('href'),
			pinyin : title.getAttribute('pinyin'),
			tts : title.getAttribute('tts'),
			more : title.getAttribute('more'),

			// 创建显示内容
			titleTag : $('<h4>'),
			titleLink : $('<a>'),
			pinyinTag : $('<span>'),
			lists : $('<ul>'),
			moreTag : $('<a>'),
			recommend : $('<p>'),
			recommend_link : $('<a>')
		}
	};

	return{
		sentence : function(domain){
			_bindEvent(domain);
		}
	}
})();
