var topTab = $Class({
    _classOptions : {
        tabClass : "on",
        menuClass : "on",
        langClass : "change"
    },
    $init : function(obj)
    {
        this.tabList = obj.tabList;
        this.elLang = obj.elLang;
        this.elSignIn = obj.elSignIn;
        this._bindEvents();
        
    },
    _bindEvents : function()
    {
        var owner = this;
        // Bind Event for Top Tab
        $A(this.tabList).forEach(function(v, i){
            if($$( "> div", v).length>0)
            {
            	//var tabALink = $$("> a",v)[0];
                $Fn($Fn(owner._handlers._tabOver, owner).bind(i), owner).attach(v, "mouseover");
                $Fn($Fn(owner._handlers._tabOut, owner).bind(i), owner).attach(v, "mouseout");
                
                //$Fn($Fn(owner._handlers._tabOver, owner).bind(i), owner).attach(tabALink, "mouseover");
                //$Fn($Fn(owner._handlers._tabOut, owner).bind(i), owner).attach(tabALink, "mouseout");
                var layer = $$( "> div", v)[0];
                $A($$("ul li",layer)).forEach(function(el){
                        $Fn($Fn(owner._handlers._listOver, owner).bind(el), owner).attach(el, "mouseover");
                        $Fn($Fn(owner._handlers._listOut, owner).bind(el), owner).attach(el, "mouseout");
                    }
                );
            }
        });
        
        // Bind Event for language area
        $Fn($Fn(owner._handlers._langOver, owner).bind(), owner).attach(this.elLang, "mouseover");
        $Fn($Fn(owner._handlers._langOut, owner).bind(), owner).attach(this.elLang, "mouseout");
        var layer = $$( "> div", this.elLang)[0];
        $A($$("a",layer)).forEach(function(el){
                $Fn($Fn(owner._handlers._langClick, owner).bind(el), owner).attach(el, "click");
                owner.addTitle(el.getAttribute("href", 2), el);
            }
        );
        $A($$("p",layer)).forEach(function(el){
	            el.onmouseover = function(){this.style.background = '#e5e5e5';};
	            el.onmouseout = function(){this.style.background = '';};
	        }
	    );
        // Bind Event for sign in area
        if(this.elSignIn)
        {
            var v = this.elSignIn;
            if($$( "> div", v).length>0)
            {
                $Fn($Fn(owner._handlers._nameOver, owner).bind(v), owner).attach(v, "mouseover");
                $Fn($Fn(owner._handlers._nameOut, owner).bind(v), owner).attach(v, "mouseout");
                var layer = $$( "> div", v)[0];
                $A($$("ul li",layer)).forEach(function(el){
                        $Fn($Fn(owner._handlers._listOver, owner).bind(el), owner).attach(el, "mouseover");
                        $Fn($Fn(owner._handlers._listOut, owner).bind(el), owner).attach(el, "mouseout");
                    }
                );
            }
        }
    },
    _handlers:{
        _tabOver:function(i, e)
        {
            $Element(this.tabList[i]).addClass(this._classOptions.tabClass);
            e.stop();
        },
        _tabOut:function(i, e)
        {
            $Element(this.tabList[i]).removeClass(this._classOptions.tabClass);
            e.stop();
        },
        _listOver:function(el, e)
        {
            $Element($$("a",el)[0]).addClass(this._classOptions.menuClass);
        },
        _listOut:function(el, e)
        {
            $Element($$("a",el)[0]).removeClass(this._classOptions.menuClass);
        },
        _langClick:function(el, e)
        {
            this.changeLanguage(el.getAttribute("href", 2));
            e.stop();
        },
        _langOver:function(e)
        {
            $Element(this.elLang).addClass(this._classOptions.langClass);
            e.stop();
        },
        _langOut:function(e)
        {
            $Element(this.elLang).removeClass(this._classOptions.langClass);
            e.stop();
        },
        _nameOver:function(el, e)
        {
            $Element(el).addClass(this._classOptions.tabClass);
        },
        _nameOut:function(el, e)
        {
            $Element(el).removeClass(this._classOptions.tabClass);
        }
    },
    
    changeLanguage : function(url)
    {
    	var 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_eZine = /^http:\/\/[^\/]*\/Account/,
    		reg_qna_noIndex = /^http:\/\/nboard[^\/]*\/qna\?/,
    		reg_qna = /^http:\/\/nboard[^\/]*/,
    		qnaToIndex = true;	// 在qna模块下，切换站点时跳转到qna的首页；false为跳转到qna相应的页面
    
        //首页
        if(wLocation.replace(reg_domain, '').length <= 1 || reg_eZine.test(wLocation))
        {
            window.location.href = url;
            return;
        }
        
		if(typeof(ismynciku) != 'undefined' && ismynciku){	// 我的n词酷中列表中显示的内容
			setTimeout(function(){
				window.location.href = url;
			},1);
		}
		else
		{
			setTimeout(function()
			{
				var newHref = wLocation.replace(reg_domain, url);
				
			    if(!reg_serch.test(newHref) && reg.test(newHref))
				{
					if(reg_theme.test(newHref) && newHref.indexOf("tw") == -1)
					{	// 酷词公园：除台湾外要将地址栏中所有内容带到其他站点
						window.location.href = newHref;
					}
					else 
					    if(reg_tool.test(newHref) && newHref.indexOf("tw") > -1)
					    { // 工具：台湾显示到其首页
						    window.location.href = url;
						}
						else 
						    if(reg_terms.test(newHref))
						    {
						        window.location.href = newHref;
						    }
						    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(newHref.indexOf("cn") > -1)
								    {
								        var qna_href = qna_subHref_first + qna_subHref_last.replace(qna_replaced_href,qna_subHttp_first+'.com');
								        window.location.href = qna_href;
							        }
							        else 
							            if(newHref.indexOf("tw") > -1)
							            {
							                var qna_href = qna_subHref_first.replace(/^http:\/\/[^\/]*/,'http://www.nciku.com.tw');;
							                window.location.href = qna_href;
							            }
							    }
							    else 
							        if(qnaToIndex && reg_qna.test(newHref))
							        {
							            if(newHref.indexOf("cn") > -1)
							            {
    										var qna_href = newHref.match(reg_qna)[0];
    										window.location.href = qna_href;
    								    }
    								    else if(newHref.indexOf("tw") > -1)
    								    {
    								        var qna_href = newHref.replace(newHref,'http://www.nciku.com.tw/qna');
    								        
    										window.location.href = qna_href;
    								    }
    								}
    								else
    								{	// 除酷词公园和工具外，其他模块的显示
    									var currPosition_href = newHref.match(reg)[0],
    										currPosition_keyword_index = currPosition_href.lastIndexOf('/'),
    										currPosition = currPosition_href.substring(currPosition_keyword_index+1);
    									var reg_all_vocab = /^http:\/\/[^\/]*\/vocab/;
    									var reg_qna_allnotes = /^http:\/\/[^\/]*\/qna\/allnote/;
    									var reg_qna_entrynote = /^http:\/\/[^\/]*\/qna\/entrynote/;
										if(reg_qna_allnotes.test(newHref))
										{
											window.location.href = newHref;
											return;
										}
										if(reg_qna_entrynote.test(newHref))
										{
											window.location.href = url + "/qna/allnote" ;
											return;
										}
										
    									if(reg_all_vocab.test(currPosition_href))
    									{
    										window.location.href = url;
    									}
    									else
    									{
    										window.location.href = currPosition_href;
    									}
    								}
    			}
    			else
    			{	// 有搜索内容时，各下拉列表所显示的内容
    				var reg_search_radical = /^http:\/\/[^\/]*\/search\/radical/;
    				var reg_search_stroke = /^http:\/\/[^\/]*\/search\/stroke/;
					if(newHref.indexOf("tw") > -1 || reg_search_radical.test(newHref) || reg_search_stroke.test(newHref))
					{
						window.location.href = url;
					}
					else
					{
						window.location.href = newHref;
					}
				}
				
			},1);
		}
    },
    addTitle : function(url,el)
    {
		var 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:\/\/nboard[^\/]*\/qna\?/,
			reg_qna = /^http:\/\/nboard[^\/]*/,
			qnaToIndex = true;	// 在qna模块下，切换站点时跳转到qna的首页；false为跳转到qna相应的页面

		//首页
        if(wLocation.replace(reg_domain, '').length <= 1 && !reg_qna.test(wLocation))
        {
            return;
        }
        
		if(typeof(ismynciku) != 'undefined' && ismynciku){	// 我的n词酷中列表中显示的内容
			setTimeout(function(){
					if(url.indexOf("cn") > -1){
						el.title = SPS.text('MY_NCIKU_EN_ZH');
					}else if(url.indexOf("tw") > -1){
						el.title = SPS.text('MY_NCIKU_TW');
					}
			},1);
		}
		else
		{
			setTimeout(function(){
				
					var newHref = wLocation.replace(reg_domain,url);
					
					
						if(!reg_serch.test(newHref) && reg.test(newHref))
						{
							if(reg_theme.test(newHref) && newHref.indexOf("tw") == -1)
							{	// 酷词公园：除台湾外要将地址栏中所有内容带到其他站点
								
							}
							else 
							    if(reg_tool.test(newHref) && newHref.indexOf("tw") > -1)
							    { // 工具：台湾显示到其首页
								    el.title = SPS.text('TOOLS_TW');
								}
								else 
								    if(reg_terms.test(newHref))
								    {
                                    }
                                    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(newHref.indexOf("cn") > -1)
        									{
        										
        									}
        									else 
        									    if(newHref.indexOf("tw") > -1)
        									    {
        										}
							            }
							            else 
							                if(qnaToIndex && reg_qna.test(newHref))
							                {
							                    if(newHref.indexOf("cn") > -1)
							                    {
            										el.title = SPS.text('QA_EN_ZH');
            									}
            									else 
            									    if(newHref.indexOf("tw") > -1)
            									    {
            										    el.title = SPS.text('QA_TW');
            										}
							                }
							                else
							                {	// 除酷词公园和工具外，其他模块的显示
							                    var currPosition_href = newHref.match(reg)[0],
            										currPosition_keyword_index = currPosition_href.lastIndexOf('/'),
            										currPosition = currPosition_href.substring(currPosition_keyword_index+1);
							                    
							                    var reg_qna_allnotes = /^http:\/\/[^\/]*\/qna\/allnote/;
							                    var reg_qna_entrynote = /^http:\/\/[^\/]*\/qna\/entrynote/;
							                    if(reg_qna_allnotes.test(newHref))
												{
													newHref.indexOf("tw") == -1 ? el.title = SPS.text('NCIKU_NOTES_ZH') : el.title = SPS.text('NCIKU_NOTES_TW');
													return;
												}
							                    if(reg_qna_entrynote.test(newHref))
												{
													newHref.indexOf("tw") == -1 ? el.title = SPS.text('NCIKU_NOTES_ZH') : el.title = SPS.text('NCIKU_NOTES_TW');
													return;
												}

							                    switch(currPosition)
            									{
            										case 'conversation':
            											currPosition_href.indexOf("tw") == -1 ? el.title = SPS.text('CONVERSATION_EN_ZH') : el.title = SPS.text('CONVERSATION_TW');
            											break;
            										case 'qna':
            											currPosition_href.indexOf("tw") == -1 ? el.title = SPS.text('QA_EN_ZH') : el.title = SPS.text('QA_TW');
            											break;
            										case 'theme':
            											currPosition_href.indexOf("tw") == -1 ? null : el.title = SPS.text('THEME_WORDS_TW');
            											break;
            										case 'tool':
            											el.title = SPS.text('TOOLS_EN_ZH');
            											break;
            									}
            								}
						}
						else
						{	// 有搜索内容时，各下拉列表所显示的内容
							if(newHref.indexOf("tw") > -1)
							{
								el.title = SPS.text('SEARCH_RESULTS_TW');
							}
							else
							{
							}
						}
			},1);
		}
    }
    
});
SPS.DOMLoad.attach(function(){
    var tt = new topTab({
		tabList : $$("div.top_k ul.top_k_tab > li", $("top")),
		elLang : $$("div.top_k ul.top_k_list > li:last-child", $("top"))[0],
		elSignIn : $$("div.top_k ul.top_k_list > li:first-child", $("top"))[0]
	});
});
