function ctrlSubmit(submitId, event)
{
	if (event.ctrlKey && (event.keyCode == 13)) { document.getElementById(submitId).click(); }
}

function setTab(/*string*/name,/*int*/ itemCnt,/*int*/ curItem, /**/classHide, /**/classShow)
{
	for(i=1;i<=itemCnt;i++)
	{
		eval("document.getElementById('tab_" + name + "_" + i + "').className='" + classHide + "'");
	}	
	eval("document.getElementById('tab_" + name + "_" + curItem + "').className='" + classShow + "'");
	
	for(i=1;i<=itemCnt;i++)
	{
		eval("ele_hide = document.getElementById('con_" + name + "_" + i +"')");
		if(ele_hide) ele_hide.style.display = "none";
	}
	eval("ele_play = document.getElementById('con_" + name + "_" + curItem + "')");
	if(ele_play) ele_play.style.display = "block";
}

function trim(content)
{
	content = content.replace(/(^\s*)|(\s*$)/g, "");
	return content;
}

function isEmpty(s)
{
	return (s==null)||(s.length==0);
}

function searchAnswer(questionTitle)
{		
	var title = trim(questionTitle.value);
	return !isEmpty(title);
}

var search_url = '';
var ask_url = '';
var cateId = '';
var questionOperateDialog = null;
function checkKey(questionTitle, ev)
{
	if(ev.keyCode==13)
	{
		if(searchAnswer(questionTitle))
			window.location.href = search_url + encodeURIComponent(questionTitle.value);
		else window.location.reload();
		return false;
	}
	return true;
}
function redirectSearch4Form(f,chParam,hasCh){
	
	var key = trim(f.w.value);
	if(key=='') {
		window.location.reload();
		return false;
	}
	else{
		// generate the url
		var action = search_solved_url.replace(/\?.*/gi,'');
		var url = action+'?sp=S'+encodeURIComponent(key);
		if(hasCh && chParam!='')
			url+='&ch='+chParam;
		if(Zhishi.Browser.isIE || Zhishi.Browser.isOpera){
			var a = document.createElement('a');
			a.href = url;
			document.body.appendChild(a);
			a.click();
		}
		else{
			document.location = url;
		}
		return false;
	}
}

// Description: onClick event handler for search condition radio buttons in SearchBarNew2 component.
// Author: simonlin 
function radSearch_Click(f)
{
	if(f.rdFlag.value == 1)
	{
		redirectSearch4Form(f);
	}
}

function redirectSearch(questionTitle, isSrch)
{
	if(isSrch)
	{
		if(searchAnswer(questionTitle))
			window.location.href = search_url + encodeURIComponent(questionTitle.value) + '&ch=k2';
		else window.location.reload();
	}
	else window.location.href = ask_url + encodeURIComponent(questionTitle.value) + '&ch=k1';
}
function redirectForAsk(questionTitle, first, sourceId, chParam)
{
	if(sourceId==null) sourceId=0;
	if(chParam==null) chParam=pageId;
	var title = trim(questionTitle.value);
	var tempUrl = '';
	tempUrl = '/z/Ask.e?sp=S'+ encodeURIComponent(title) + "&sp=" + sourceId;
	if(title == ''){
		tempUrl += '&ch=w.ask.empty';
	}
	else{
		tempUrl += '&ch=w.ask.word';
	}
	withRefererRedirect(tempUrl);
}
function redirectAskWithParam(questionTitle,ch)
{
	var title = trim(questionTitle.value);
	var tempUrl = '';
	if(ch==null) ch="";
	
	if (isEmpty(title))
	{
		var url = ask_for_title_url + "&ch="+ch;
		tempUrl = url;
	}
	else 
	{
		var url = ask_url + encodeURIComponent(questionTitle.value) + "&ch=" + ch;		
		tempUrl = url;
	}
	if(Zhishi.Browser.isIE || Zhishi.Browser.isOpera){
		var a = document.createElement('a');
		a.href = tempUrl;
		document.body.appendChild(a);
		a.click();
	}
	else{
		document.location = tempUrl;
	}
}

function redirectSearch4Form(f,chParam){
	var key = f.w.value.trim();
	if(key=='') {
		window.location.reload();
		return false;
	}
	else{
		// generate the url
		var chParam = "w.search.sb";
		var action = '';
		try{if(search_solved_url) action = search_solved_url.replace(/\?.*/gi,'');}
		catch(e){action = '/z/Search.e';}
		if(Zhishi.Browser.isIE || Zhishi.Browser.isOpera){
			var a = document.createElement('a');
			a.href = action+'?sp=S'+encodeURIComponent(key)+'&ch='+chParam;
			var input = get('sb');
			input.parentNode.appendChild(a);
			a.click();
			return false;
		}
		else{
			f.action = action;
			f.sp.value = 'S' + key;
			f.ch.value = chParam;
			return true;
		}
	}
}

function mark(node, tagValue, tagFlag)
{
    var childs = node.childNodes;
    var childTagValue = null;
    if(!childs || !childs.length) return;

    for(var i=0; i<childs.length;i++)
    {
        childTagValue = null;
        if(childs[i].tagName=='A')
        {
            if(tagValue && childs[i].setAttribute)
            {
				var href = childs[i].href;
                var x = href.indexOf('?')<0 ? '?' : '&';
				var prefix ='';
				if(href.indexOf('#')>-1){
					prefix = href.substring(href.indexOf('#'),href.length);
					href = href.substring(0,href.indexOf('#'));
				}
                var linkText = childs[i].innerHTML;
                if(tagFlag == "pid")
                        childs[i].href = href + x + 'pid=' + tagValue + prefix;
                if(tagFlag == "ch")
                        childs[i].href = href + x + 'ch=' + tagValue + prefix;
				if(tagFlag == "cid")
                        childs[i].href = href + x + 'cid=' + tagValue + prefix;
                childs[i].innerHTML = linkText;
            }
        }
        else
        {
            if(typeof childs[i] != 'null')
            {
                if(document.all && childs[i].ch)
                {
                        childTagValue = childs[i].ch;
                        tagFlag = "ch";
                }
                else if(document.all && childs[i].pid)
                {
                        childTagValue = childs[i].pid;
                        tagFlag = "pid";
                }
				else if(document.all && childs[i].cid)
                {
                        childTagValue = childs[i].cid;
                        tagFlag = "cid";
                }
                else if(navigator.userAgent.toLowerCase().search('msie')<0 && childs[i].getAttribute)
                {
                        if(childs[i].getAttribute('ch'))
                        {
                                childTagValue = childs[i].getAttribute('ch');
                                tagFlag = "ch";
                        }
                        if(childs[i].getAttribute('pid'))
                        {
                                childTagValue = childs[i].getAttribute('pid');
                                tagFlag = "pid";
                        }
						 if(childs[i].getAttribute('cid'))
                        {
                                childTagValue = childs[i].getAttribute('cid');
                                tagFlag = "cid";
                        }
                }

                if(childTagValue)
                     mark(childs[i], childTagValue, tagFlag);
                else
                     mark(childs[i], tagValue, tagFlag);
            }
        }
    }
}
function focus_input(input)
{
	if(!input || input.disabled) return true;
	else {
		input.select();
		return false;
	}
}

function aboutSpecialty()
{
	setTimeout(function(){
		var expertDialog = new Zhishi.Dialog('图标说明',380,240,true,'http://cache.soso.com/wenwen/help_sw_new3.htm');
		expertDialog.show();
	},0);
	
}

function questionOperate(url,title,width,height){
	questionOperateDialog = new Zhishi.Dialog(title,width,height,true,url);
	questionOperateDialog.afterClose = function(){ document.location.reload(); };
	questionOperateDialog.show();
}
function splitResNum( total )
{
        var ts = total.toString();
        var tl = ts.length;
        if(tl<4) 
        {
                document.write(ts);
                return;
        }
        var tmp = tl/3;
        var tlC = Math.floor(tmp);   
        var tlY = tl%3; res = "";
        if(tlY == 0) 
        {
                tlY = 3;
                tlC = tlC-1;
        }
        res += ts.substr(0,tlY)+"," + ts.substr(tlY,3);
        for(var i=1; i<tlC; i++)
        {
                res += ","+ts.substr(tlY+i*3,3);
        }
        document.write(res);
}
String.prototype.trim=function()
{
        return   this.replace(/(^\s*)|(\s*$)/g,'');
}
 
$d = function(id){
        return  document.getElementById(id);
}
var st='';
function sm(event)
{
        clearTimeout(st);
        o=$('tm');
        tt = setTimeout(function(){o.style.display="block";document.onmousemove = cp;},500);
}
function cp(event)
{
                evt = window.event?window.event:event;
                var pointer=function(event) {
		            return {
		              x: event.pageX || (event.clientX +
		                (document.documentElement.scrollLeft || document.body.scrollLeft)),
		              y: event.pageY || (event.clientY +
		                (document.documentElement.scrollTop || document.body.scrollTop))
		            };
       			}(evt);
       			
                l=o.offsetLeft;t = o.offsetTop-20;
                r=l+o.offsetWidth;b=o.offsetTop+o.offsetHeight;
 
                py = pointer.y;px = pointer.x;
 
                if((py>t && py<b) && (px>l&&px<r)){
                        }
                else{ 
                        st = setTimeout(function(){o.style.display="none";},500);
                        document.onmousemove=function(){};
                }
}


function togDispMore(e){stopB(e);var elems=document.getElementsByName('tm');for(var i=0;i<elems.length;i++){var obj=elems[i],dp="";if(obj.style.display==""){dp="none";}obj.style.display=dp;}return false;}
function stopB(e){if(!e)e=window.event;e.cancelBubble=true;}
document.onmouseup=function(event){var elems=document.getElementsByName('tm');if(elems[0] && elems[0].style.display==""){togDispMore(event);}}

// add for dropdown menu of login component
function sm2(){
	var o=get('dm');
	Zhishi.sm2 = setTimeout(function(){
		var link = get('s_nick');
		o.style.left = 0+'px';
		o.style.right = 10+'px';
		var heightIncrement;
		if(Zhishi.Browser.isIE)
			heightIncrement = 20;
		else
			heightIncrement = 20;

		o.style.top = link.offsetTop+heightIncrement+'px';
		o.style.display="block";
		document.onmousemove = cp2;
		},500);
}
function cp2(event){
	var event = event ? event : window.event;
	var px = event.clientX;
    var py = event.clientY;
	var o=get('s_nick');	
	var p=get('dm');
	var l=o.offsetLeft;
	var t = o.offsetTop;
	while (o.offsetParent) {
		o = o.offsetParent;
		t += o.offsetTop;
		l += o.offsetLeft;
	}
	var w = p.offsetWidth + l;
	var h = p.offsetHeight + t;
	if(Zhishi.Browser.isIE){
		//l -= 100;
		//w -= 100;
		h += 20;
	}
	else{
		h += 20;
	}
	
	if(px < l || px>w || py<t || py>h){
		window.setTimeout(function(){p.style.display='none';},300);
		document.onmousemove=function(){};
	}
}
// end add

/*navigation*/

var nav = (function(){
	var topQuestionTimer = null,topCategoriesTimer = null,topCategories = null;
	
	function initTopCategories(){
		Zhishi.Event.attachEventListener('topQuestion','mouseover',topQuestionMouseOverEvent);
		Zhishi.Event.attachEventListener('topQuestion','mouseout',topQuestionMouseOutEvent);
		Zhishi.Event.attachEventListener('topCategories','mouseout',topCategoriesMouseOutEvent);
		Zhishi.Event.attachEventListener('topCategories','mouseover',topCategoriesMouseOverEvent);
	}

	function topQuestionMouseOverEvent(){
		if(topCategoriesTimer != null){
			clearTimeout(topCategoriesTimer);
		}
		topQuestionTimer = setTimeout(function(){
			if(!topCategories.visible()){
				topCategories.show();
				if(document.compatMode == "CSS1Compat"){
					topCategories.slideDown(145,0.8,null,Zhishi.Tween.Quart.easeOut);
				}
				else{
					topCategories.setStyle('height','145px');
				}
			}
		},200);
		
	}
	
	function topQuestionMouseOutEvent(){
		if(topQuestionTimer != null){
			clearTimeout(topQuestionTimer);
		}
		topCategoriesMouseOutEvent();
	}

	function topCategoriesMouseOutEvent(){
		topCategoriesTimer = setTimeout(function(){
			if(topCategories.visible()){
				if(document.compatMode == "CSS1Compat"){
					topCategories.slideUp(0.3,function(){topCategories.hide();});
				}
				else{
					topCategories.hide();
				}
			}
		},400);
	}

	function topCategoriesMouseOverEvent(){
		if(topCategoriesTimer != null){
			clearTimeout(topCategoriesTimer);
		}
	}
	
	function initElement(){
		topCategories = get("topCategories");
	}
	
	function initNavigation(){
		initTopCategories();
		initElement();
	}
	
	return{
		initNavigation : initNavigation	
	};
})();
