/**
 * 页面右上角切换中英文站点的工具
 * @author CHX
 * @namespace nhn.syncDomain
 * @version 090710
 */

nhn.SyncDomain = jindo.$Class({
	$init: function(){
		this.sDomain = /\.com$/i.test(window.location.hostname)?"com":"cn";
	
		this.$langSel = jindo.$Element(this._getByClassName("lang_sel", document.body)[0]);
		this.$langSelList = jindo.$Element(this._getByClassName("langlist", this.$langSel.$value())[0]);
		this.$langPane = jindo.$Element(this._getByClassName("langOpen", this.$langSel.$value())[0]);
		
		$Fn(this.showList, this).attach(this.$langPane, "click");
		$Fn(this.hideList, this).attach(document, "click");
		
		var _this = this;
		$Fn(function(e){
			var _tar = e.element;
			if(_tar.tagName == "A" && /site_(com|cn)/i.test(_tar.className)){
				this.jump(RegExp.$1);
			}
			e.stop();
		}, this).attach(this.$langSelList.$value(), "click");
		
		if(this.$langSelList.visible()) this.hideList();

	},
	
	_getByClassName: function(sClassName, oParent) {

		return jindo.cssquery("." + sClassName, oParent);
	},
	
	showList: function(e){
		e.stop();
		this.$langSelList.show();
	},
	hideList: function(){
		this.$langSelList.hide();
	},
	
	jump: function(site){
		
		if (site == this.sDomain) {
			this.hideList();
			return;
		}
		window.location.href = window.location.href.replace(new RegExp("\.("+this.sDomain+")(\:\d+)?\/", "i"), function($$, $1, $2){
			return "." + site + ($2||"") + "/";
		});
	}
});