// 切换【语言】栏
if(typeof nhn == "undefined") nhn = {};
nhn.setLanguage = (function(){
var oSelect = document.getElementById('lang'),
	oList = document.getElementById('lang_list'),
	oBtn = document.getElementById('select_open');

// 初始化语言站点信息与相应的提示文字
var getLink = function(off){
	var options_Len = oSelect.options.length,
		wLocation = window.location.href,
		reg = /^http:\/\/[^\/]*\/[^\/]*/,
		reg_domain = /^http:\/\/[^\/]*/,
		reg_serch = /^http:\/\/[^\/]*\/search/,
		reg_tool = /^http:\/\/[^\/]*\/tool/,
		reg_theme = /^http:\/\/[^\/]*\/theme/,
		reg_terms = /^http:\/\/[^\/]*\/terms/,
		reg_qna_noIndex = /^http:\/\/[^\/]*\/qna\?/,
		reg_qna = /^http:\/\/[^\/]*\/qna/,
		qnaToIndex = true;	// 在qna模块下，切换站点时跳转到qna的首页；false为跳转到qna相应的页面

	if(typeof(ismynciku) != 'undefined' && ismynciku){	// 我的n词酷中列表中显示的内容
		setTimeout(function(){
			for(var i = 0; i < options_Len; i++){
				var oLi = document.createElement('p');
				oLi.style.cursor = 'pointer';
				oLi.innerHTML = oSelect.options[i].text;
				oLi.setAttribute('href',oSelect.options[i].value);
				oLi.onclick = function(){
					var link = document.createElement('a');
					if(!link.click){
						window.location.href = this.getAttribute('href');
						return;
					}
					link.setAttribute('href',this.getAttribute('href'));
					link.style.display = 'none';
					oList.appendChild(link);
					link.click();
				}
				if(i === 0){
					oLi.title = SPS.text('MY_NCIKU_EN_ZH');
				}else if(i === options_Len-1){
					oLi.title = SPS.text('MY_NCIKU_TW');
				}
				oList.appendChild(oLi);
			}
		},1);
	}else{
		setTimeout(function(){
			for(var i = 0; i < options_Len; i++){
				var newHref = wLocation.replace(reg_domain,oSelect.options[i].value);
				var oLi = document.createElement('p');
				oLi.style.cursor = 'pointer';
				if(off == 'close'){
					if(!reg_serch.test(newHref) && reg.test(newHref)){
						if(reg_theme.test(newHref) && i !== options_Len-1){	// 酷词公园：除台湾外要将地址栏中所有内容带到其他站点
							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',newHref);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
						}else if(reg_tool.test(newHref) && i === options_Len-1){ // 工具：台湾显示到其首页
							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',oSelect.options[i].value);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
							oLi.title = SPS.text('TOOLS_TW');
						}else if(reg_terms.test(newHref)){
							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',newHref);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
						}else if(!qnaToIndex && reg_qna_noIndex.test(newHref)){
							var qna_index = newHref.indexOf('?'),
								qna_subHref_first = decodeURIComponent(newHref.substring(0,qna_index)),
								qna_subHref_last = decodeURIComponent(newHref.substring(qna_index)),
								qna_replaced_href = /http:\/\/[^\/]*/,
								qna_subHttp = qna_subHref_last.match(qna_replaced_href)[0],
								qna_subHttp_first = qna_subHttp.substring(0,qna_subHttp.lastIndexOf('.'));

							if(i === 0){
								var qna_href = qna_subHref_first + qna_subHref_last.replace(qna_replaced_href,qna_subHttp_first+'.com');
								oLi.innerHTML = oSelect.options[i].text;
								oLi.setAttribute('href',qna_href);
								oLi.onclick = function(){
									var link = document.createElement('a');
									if(!link.click){
										window.location.href = this.getAttribute('href');
										return;
									}
									link.setAttribute('href',this.getAttribute('href'));
									link.style.display = 'none';
									oList.appendChild(link);
									link.click();
								}
							}else if(i === options_Len-1){
								var qna_href = qna_subHref_first.replace(/^http:\/\/[^\/]*/,'http://www.nciku.com.tw');;
								oLi.innerHTML = oSelect.options[i].text;
								oLi.setAttribute('href',qna_href);
								oLi.onclick = function(){
									var link = document.createElement('a');
									if(!link.click){
										window.location.href = this.getAttribute('href');
										return;
									}
									link.setAttribute('href',this.getAttribute('href'));
									link.style.display = 'none';
									oList.appendChild(link);
									link.click();
								}
							}
						}else if(qnaToIndex && reg_qna.test(newHref)){
							if(i === 0){
								var qna_href = newHref.match(reg_qna)[0];
								oLi.title = oLi.title = SPS.text('QA_EN_ZH');
								oLi.innerHTML = oSelect.options[i].text;
								oLi.setAttribute('href',qna_href);
								oLi.onclick = function(){
									var link = document.createElement('a');
									if(!link.click){
										window.location.href = this.getAttribute('href');
										return;
									}
									link.setAttribute('href',this.getAttribute('href'));
									link.style.display = 'none';
									oList.appendChild(link);
									link.click();
								}
							}else if(i === options_Len-1){
								var qna_href = newHref.replace(newHref,'http://www.nciku.com.tw/qna');
								oLi.title = oLi.title = SPS.text('QA_TW');
								oLi.innerHTML = oSelect.options[i].text;
								oLi.setAttribute('href',qna_href);
								oLi.onclick = function(){
									var link = document.createElement('a');
									if(!link.click){
										window.location.href = this.getAttribute('href');
										return;
									}
									link.setAttribute('href',this.getAttribute('href'));
									link.style.display = 'none';
									oList.appendChild(link);
									link.click();
								}
							}
						}else{	// 除酷词公园和工具外，其他模块的显示
							var currPosition_href = newHref.match(reg)[0],
								currPosition_keyword_index = currPosition_href.lastIndexOf('/'),
								currPosition = currPosition_href.substring(currPosition_keyword_index+1);

							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',currPosition_href);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
							switch(currPosition){
								case 'conversation':
									i !== options_Len-1 ? oLi.title = SPS.text('CONVERSATION_EN_ZH') : oLi.title = SPS.text('CONVERSATION_TW');
									break;
								case 'qna':
									i !== options_Len-1 ? oLi.title = SPS.text('QA_EN_ZH') : oLi.title = SPS.text('QA_TW');
									break;
								case 'theme':
									i !== options_Len-1 ? null : oLi.title = SPS.text('THEME_WORDS_TW');
									break;
								case 'tool':
									oLi.title = SPS.text('TOOLS_EN_ZH');
									break;
							}
						}
					}else{	// 有搜索内容时，各下拉列表所显示的内容
						if(i === options_Len-1){
							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',oSelect.options[i].value);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
							oLi.title = SPS.text('SEARCH_RESULTS_TW');
						}else{
							oLi.innerHTML = oSelect.options[i].text;
							oLi.setAttribute('href',newHref);
							oLi.onclick = function(){
								var link = document.createElement('a');
								if(!link.click){
									window.location.href = this.getAttribute('href');
									return;
								}
								link.setAttribute('href',this.getAttribute('href'));
								link.style.display = 'none';
								oList.appendChild(link);
								link.click();
							}
						}
					}
				}else if(off == 'open'){
					if(!reg_serch.test(newHref) && reg.test(newHref)){
						oLi.innerHTML = oSelect.options[i].text;
						oLi.setAttribute('href',newHref.match(reg)[0]);
						oLi.onclick = function(){
							var link = document.createElement('a');
							if(!link.click){
								window.location.href = this.getAttribute('href');
								return;
							}
							link.setAttribute('href',this.getAttribute('href'));
							link.style.display = 'none';
							oList.appendChild(link);
							link.click();
						}
					}else{
						oLi.innerHTML = oSelect.options[i].text;
						oLi.setAttribute('href',newHref);
						oLi.onclick = function(){
							var link = document.createElement('a');
							if(!link.click){
								window.location.href = this.getAttribute('href');
								return;
							}
							link.setAttribute('href',this.getAttribute('href'));
							link.style.display = 'none';
							oList.appendChild(link);
							link.click();
						}
					}
				}
				oList.appendChild(oLi);
			}
		},1);
	}
};

	// 初始化打开与关闭语言列表
	var setList = function(evt){
		var elem = evt.srcElement || evt.target,
				oArrow = $('select_open');

		if((elem.tagName.toLowerCase() == 'span' || elem.tagName.toLowerCase() == 'img') && $Element(elem).hasClass('select_open')){
			if(oList.style.display == 'block'){
				oList.style.display = 'none';
				oList.parentNode.getElementsByTagName('h3')[0].className = 'langOpen';
				oList.parentNode.className = 'lang_sel';
				oArrow.src = oArrow.src.replace(/\w+\_\w+\.\w+/i,'btn_select_down.gif');
			}else{
				oList.style.display = 'block';
				oList.parentNode.className = 'lang_selected';
				oArrow.src = oArrow.src.replace(/\w+\_\w+\.\w+/i,'btn_select_up.gif');
			}
		}else if(oList.style.display == 'block'){//显示的时候再执行隐藏，IE6下的BUG会导致arrow消失
			oList.style.display = 'none';
			oList.parentNode.getElementsByTagName('h3')[0].className = 'langOpen';
			oList.parentNode.className = 'lang_sel';
			oArrow.src = oArrow.src.replace(/\w+\_\w+\.\w+/i,'btn_select_down.gif');
		}
	};

// 初始化鼠标放到语言栏上的背景颜色
var setMouseStyleForLi = function(){
	var oLis = oList.getElementsByTagName('p');
	for(var i = 0, len = oLis.length; i < len; i++){
		oLis[i].onmouseover = function(){
			this.style.backgroundColor = '#E5E5E5';
		}
		oLis[i].onmouseout = function(){
			this.style.backgroundColor = '';
		}
	}
};

return{
	setLink: function(off){
		getLink(off);
		setTimeout(setMouseStyleForLi,100);
		window.addEventListener ? document.addEventListener('click',setList,false) : document.attachEvent('onclick',setList);
	}
}
})();
nhn.setLanguage.setLink('close');