var SiteUrl='http://www.zhidaode.com/';
var arrTab= new Array(0,0,0,0,0,0,0,0,0);

var Yep={version:'1.0.0',encoding:'utf-8',author:'Yep-Yep',cookie_prefix:'zdc_'};
Yep.Base={
	setHome:function(obj,vrl){try{obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);}catch(e){if(window.netscape) {try {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  }catch (e){ alert("抱歉！您的浏览器不支持直接设为首页。请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为“true”，点击“加入收藏”后忽略安全提示，即可设置成功。");  }var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);prefs.setCharPref('browser.startup.homepage',vrl);}}},
	addFav:function(obj,vrl){try{window.external.AddFavorite(favUrl,favName);}catch(e){try{window.sidebar.addPanel(favName,favUrl,"");}catch(e){alert("使用快捷键 Ctrl+D 或 Cmd+D 来收藏此页！");}}},
	getCookieVal:function(offset) {var endstr = document.cookie.indexOf (";", offset);if (endstr == -1) {endstr = document.cookie.length;}return unescape(document.cookie.substring(offset, endstr));},
	getCookie:function(name) {if(name==''){return '';}var arg = name + "=";var alen = arg.length;var clen = document.cookie.length;var i = 0;var searchof=document.cookie.indexOf(arg);if(searchof>-1){return this.getCookieVal(searchof+alen);}else{return "";}},
	switchTab:function(tab,index,showclass,hideclass){var t,c;t=$('#tab'+tab+arrTab[tab]);c=$('#tab_c'+tab+arrTab[tab]);if(arrTab[tab]==index){t.attr('class',showclass);c.show();return true;} else {t.attr('class',hideclass);c.hide();var tt,cc;tt = $('#tab'+tab+index);cc = $('#tab_c'+tab+index);t.attr('class',showclass);cc.show();arrTab[tab]=index;}},
	domRefresh:function(){window.location.reload();}
};

$.tabs = function(containerId, start) {
    var ON_CLASS = 'on';
    var id = '#' + containerId;
    var i = (typeof start == "number") ? start - 1 : 0;
    $(id + '>div:eq(' + i + ')').css({display:"block"});
    $(id + '>ul>li:nth-child(' + (i+1) + ')').addClass(ON_CLASS);
    $(id + '>ul>li>a').click(function() {
        if (!$(this.parentNode).is('.' + ON_CLASS)) {
            var re = /([_\-\w]+$)/i;
            var target = $('#' + re.exec(this.href)[1]);
            if (target.size() > 0) {
                $(id + '>div:visible').css({display:"none"});
                target.css({display:"block"});
                $(id + '>ul>li').removeClass(ON_CLASS);
                $(this.parentNode).addClass(ON_CLASS);
            } else {
                alert('There is no such container.');
            }
        }
        return false;
    });
};
Yep.Search={
	Go:function(qid,qtitle){
		$.get(SiteUrl+'default/question/click/qid/'+qid, function(data) {return;});
		window.open('http://www.baidu.com/s?wd='+qtitle);
	}	
};
Yep.AdList={
	GG:function(id){
		var adstr = '';
		var bdM=0;
		switch(id){
			case "s001":adstr = 'google_ad_slot="9043156648";google_ad_width=600;google_ad_height=90;';break;//问题
			case "s002":adstr = 'google_ad_slot="0440693582";google_ad_width=468;google_ad_height=15;';break;//最佳答案 参考
			case "s003":adstr = 'google_ad_slot="4415620180";google_ad_width=200;google_ad_height=200;';break;//右侧图片
			//case "s003": adstr ='<iframe id="baiduSpFrame" border="0" vspace="0" hspace="0" marginWidth="0" marginHeight="0" frameSpacing="0" frameBorder="0" scrolling="no" width="245" height="250" src="http:\/\/spcode.baidu.com\/spcode\/spstyle\/style3936.jsp?tn=uotee_sp&ctn=0&styleid=3936"></iframe>';bdM=1;break;
			//case "s003":adstr='<a href="http://s.click.taobao.com/t_8?e=7HZ5x%2BOzfPC3LXWClPEo%2FPsQJw%3D%3D&p=mm_25421431_0_0"><img src="http://www.zhidaode.com/images/220_220.jpg"></a>';bdM=1;break;;
			case "s004":adstr = '';break;//adstr = 'google_ad_slot="5865423657";google_ad_width=468;google_ad_height=60;';break;//最佳答案下文字
			case "s005":adstr = '';break;//adstr = '<a href="http://www.99cpa.com/135451.html"><font color="red">回家诱惑国语全集在线观看</font></a>';bdM=1;break;//'google_ad_slot="0230025822";google_ad_width=600;google_ad_height=90;';break;//更多参考
			case "s008":adstr = '';break;//adstr='google_ad_slot="9853720986";google_ad_width=600;google_ad_height=90;';break;//最佳答案
			case "s009":adstr = 'google_ad_slot="4104988462";google_ad_width=250;google_ad_height=250;';break;
			default:adstr ='';break;
		}
		if(adstr && !bdM){adstr='<script type="text/javascript">google_ad_client="pub-6795435718088812";'+adstr+'</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';}
		document.writeln(adstr);
	},
	BD:function(id){
		var bdM=0;
		switch(id){
			case "BD001":adstr ="var cpro_id='u394605';";break;
			default:adstr ='';break;
		}
		if(adstr && !bdM){
			adstr='<script type="text/javascript">'+adstr+'</script><script src="http://cpro.baidu.com/cpro/ui/c.js" type="text/javascript"></script>';
		}
		document.writeln(adstr);
	},
	HTML:function(id){
		var adstr = '';
		switch(id){
			case "s005":adstr = '<a href="http:\/\/s.click.taobao.com\/t_1?i=qXnw%2BBqc%2BytUJlQ%3D&p=mm_25421431_0_0&n=11">宅男女神 张馨予代言减肥产品 看看她是怎么瘦的?<\/a>';break;//右上文字链接
			default:adstr ='';break;
		}
		document.writeln(adstr);
	}

};
Yep.ZhiDaoDe={
	checkMail:false,
	checkPass:false,
	currURL:String(document.location),
	checkEmail:function(){
		var email = $('#login_email').val();var filter= /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;this.checkMail = true;
		if(email == '' || !new RegExp(filter).test(email)){Yep.Box.Tip('login_email',Yep.Box.tipMsg.A);this.checkMail=false;}
	},
	checkPassword:function(){
		this.checkPass=true; var pass = $('#login_password').val();
		if(pass == '' || pass=='*****' || pass.length > 20){Yep.Box.Tip('login_password',Yep.Box.tipMsg.B);$('#login_password').focus();this.checkPass = false;}
	},
	sysLogout:function(v){
		$.getJSON(SiteUrl+"login.php?c=user&a=logout&callback=?", function(data){
			if(data['msg'] == 'error'){Yep.Box.Tip('login_email',Yep.Box.tipMsg.D);return;
			}else{
				var vid = v=='syscommonuser' ? 'syscommonuser' : 'sys_user_bar';
				Yep.Box.Tip(vid,Yep.Box.tipMsg.E);
				setTimeout('Yep.Base.domRefresh()', 500);}
		});
	},
	getUBar:function(){
		var n = Yep.Base.getCookie(Yep.cookie_prefix+'username');
		var defhtml = '<a href="javascript:void(0);" onclick="Yep.Box.Login();">登录</a><a href="/user/reg/" target="_blank">免费注册</a>'; 
		if(n){
			defhtml = '<a href="#" target="_blank">欢迎 '+decodeURIComponent(n)+'</a><a href="javascript:void(0);" onclick="Yep.ZhiDaoDe.sysLogout(\'syscommonuser\');">退出</a>';
		}
		defhtml +='<a href=\'javascript:void(0);\' onclick=\'Yep.Base.addFav("'+document.title+'","'+window.location.href+'");\'>收藏本页</a>';
		$('#syscommonuser').html(defhtml);
	},
	getHelpMenu:function(){
		var defhtml='';
        defhtml +='<li><a href="http://www.zhidaode.com/" class="noselect">知道网</a><ul class="subnav"><li><a href="http://www.zhidaode.com/top/">问答库</a></li></ul></li>';
		$('#syshelpmenu').html(defhtml);
	},
	moveHelpMenu:function(){
		$('<span></span>').appendTo($(".noselect"));
		$("ul.topnav li a").mouseover(function() { //When trigger is clicked...
			$(this).parent().find("ul.subnav").slideDown('fast').show();
			$(this).parent().hover(function() {
			}, function(){	
				$(this).parent().find("ul.subnav").slideUp('fast');
			});
			}).hover(function() { 
				$(this).addClass("topnavA");
			}, function(){
				$(this).removeClass("topnavA");
		});
	},
	submitTopBanner:function(){
		this.checkEmail(); if(!this.checkMail){return false;}
		this.checkPassword(); if(!this.checkPass){return false;}
		
		$.getJSON(SiteUrl+"login.php?c=user&a=login&email="+$('#login_email').val()+"&password="+$('#login_password').val()+"&callback=?", function(data){
			if(data['msg'] == 'error'){Yep.Box.Tip('login_email',Yep.Box.tipMsg.C);return;}
			if(Yep.ZhiDaoDe.currURL.indexOf("html")==-1){
				setTimeout('Yep.Base.domRefresh()', 500);
			}
			Yep.ZhiDaoDe.getUBar();$('#cl-login').remove();
		});
	},
	cateList:function(catid){//首页
		var catdiv = "tab_1"+catid;
		var subcatdiv = "tab_c1"+catid;
		var subcatobj = $('#'+subcatdiv);
		var sTop = document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		var divleft = (document.body.clientWidth-subcatobj.width())/2;
		var divtop = sTop + (document.documentElement.clientHeight-subcatobj.height())/2;
		//if(divtop > 100) divtop = divtop + 20;
		divtop = sTop+subcatobj.height();
		var s_top = $('#'+catdiv).offset().top;
		s_top -=28;
		divleft = divleft - 165;
		subcatobj.css({"top":s_top,"left":divleft});
		subcatobj.show();
	},
	cateClear:function(catid){$('tab_c1'+catid).hide();},
	setTureAsk:function(aid,qid){
		if(Yep.Base.getCookie(Yep.cookie_prefix+"username")=='') {document.location.href=SiteUrl+'/user/login/';}
		$.get(SiteUrl+"default/question/trueask/aid/"+aid+'/qid/'+qid,function(result){
				var myObject = eval('(' + result + ')');
				if(myObject.status){
					setTimeout('Yep.Base.domRefresh()', 500);
				}else{
					alert('参数错误');return false;
				}
			});
	},
	getCateList:function(v){
		$.ajax({
			type:"GET",
			url:"http://www.zhidaode.com/index.php?app=default&c=question&a=catelist&catid="+v+"&_randoms="+Math.random(),
			success: function(data) {
				$('#catid').html(data);
			}
		});
	},
	cntView:function(qid){$.get(SiteUrl+'default/question/viewcnt/qid/'+qid, function(data) {return;});},
	redirectAnswer:function(){var word = $('#w').val();if(word=='' || word.length < 2){alert('搜索参数不合法');return false;}document.location.href=SiteUrl+'q/search/'+word;},
	redirectQuestion:function(){document.location.href=SiteUrl+'default/question/ask/';}
};

Yep.Box={
	tipMsg:{'A':'邮箱格式错误','B':'密码不能为空','C':'登录邮箱和密码不一致','D':'网络异常!,请重试',
		'E':'<img src="http://demo.zhidaode.com/zd/v2/loading.gif">'
	},
	show:function(options){
		var defaults = {
			sinno:'cl-sinno',
			clickid:'',
			boxClass:'',
			boxCode:1,//1为内置html代码 0外部引入内容
			boxHtml:'',//boxcode为0时 值为引入url
			boxHref:'',
			boxPos:'center',// center|current|right|   c居中|u当前点击右下|r右下角|
			boxOpa:0,//是否opacity
			boxCloseTime:0,//自动关闭时间 0为不关闭
			boxWH:[200,200],
			boxLeft:0,
			boxTop:0
		};
		var options = $.extend(defaults, options);
		var html='';var TopY=0;
		
		var ishave = $('#'+options.sinno).length;
		var styleclass = options.boxClass ? 'class="'+options.boxClass+'"':'';
		if(!ishave){
			var clbox=$('<div id="'+options.sinno+'" '+styleclass+'></div>');
			$("body").prepend(clbox)
			clbox.hide();
		}else{
			var clbox=$('#'+options.sinno);
		}
		if(options.boxOpa){html='<div id="graybg" class="graybg" ></div><iframe id="popiframe" class="popiframe" frameborder="0" ></iframe>';}
		if(!options.boxCode){//ajax得到网页内容
			if(!ishave){//login使用跨域
				if(options.sinno=='cl-login'){
					$.getJSON(options.boxHref+"?callback=?", function(data){
						//alert(data);
						$('#'+options.sinno).html(html+data.html);
					});
				}else{
					$.ajax({ 
						type:"GET",
						url: options.boxHref,
						success: function(data){
							$('#'+options.sinno).html(html+data);
						}
					});
				}
			}
		}else{
			html+=options.boxHtml
			clbox.html(html);
		}
		
		//init
		if(options.clickid && options.boxPos=='current'){
			var offtop=$('#'+options.clickid).offset().top+$('#'+options.clickid).height();
			var offleft=$('#'+options.clickid).offset().left;
		}
		var vleft=$(window).width()-options.boxWH[0];
		var vtop=$(window).height()-options.boxWH[1];
			
		switch(options.boxPos){
			case 'center':
				vleft=vleft/2;
				vtop=vtop/2;
			break;
			case 'right':
				vleft-=Math.abs(options.boxWH[0]/2)-10;
				vtop-=Math.abs(options.boxWH[1]/4)-10;
			break;
			case 'current':
				vtop=offtop;
				vleft=offleft;
			break;
		}
		vleft = vleft < 0 ? 0 : vleft;
		vtop = vtop < 0 ? 50 : vtop;//,zIndex:1000
		clbox.css({'top':vtop+'px','left':vleft+'px','width':options.boxWH[0]+'px','height':options.boxWH[1]+'px',position:"absolute"}).show();

		if(options.boxCloseTime){
			clbox.css({zIndex:1000});
			clbox.fadeOut(options.boxCloseTime,function(){})
		}
		if(options.boxPos=='right'){
			$(window).scroll(function() {
				clbox.css("top", $(window).scrollTop() + $(window).height() - clbox.height()-10);//当滚动条滚动的时候始终在屏幕的右下角
				TopY=clbox.offset().top;//当滚动条滚动的时候随时设置元素距父原素距离
			});
		}
		if(options.clickid){
			$('#cldel-'+options.clickid).click(function(){
				$('#'+options.sinno).hide();
			});
		}
	},
	closeBox:function(sinno){$('#'+sinno).hide();},
	Login:function(){
		this.show({sinno:'cl-login',boxWH:[446,350],boxCode: 0,clickid:'loginclose',boxOpa:1,boxHref:SiteUrl+'login.php'});
	},
	centerWin:function(w,h,url,ops){
		w = w? w:446;
		h = h? h:350;
		ops=ops ? '-'+ops:'';
		url=url?url:'winbox/login';
		this.show({sinno:'cl-center'+ops,boxWH:[w,h],boxCode: 0,boxOpa:1,boxHref:SiteUrl+url,boxClass:'cl-box'});
	},
	Tip:function(clickid,html,times){
		times = times? times:4000;
		this.show({sinno:'cl-common-tip',boxWH:[160,40],clickid:clickid,boxCloseTime:times,boxPos:'current',boxClass:'cl-tip-box',boxHtml:'<p>'+html+'</p>'});
	},
	rightTip:function(w,h){
		this.show({sinno:'cl-right-tip',boxWH:[w,h],boxCode: 0,boxPos:'right',boxHref:'b.html'});
	},
	delTip:function(itemid){//itemid写法: message-del-pmid-itemid
		var xhtml = '<div class="cl-tip-del"><span id="cldel-'+itemid+'">X</span></div><div class="cl-tip-con"><p>确定删除？</p><div class="cl-tip-button"><a href="javascript:void(0);" onclick="Yep.Call.delRecord(\''+itemid+'\')">确定</a></div></div>';
		this.show({sinno:'cl-'+itemid,boxWH:[210,125],clickid:itemid,boxPos:'current',boxClass:'cl-tip-box',boxHtml:xhtml});
	}
};
Yep.Call={
	delRecord:function(itemid){
		var params = itemid.replace(/\-/g,'/');
		$.ajax({type:"GET",url: CL.Curl.t+params,
			success: function(data){
				data=parseInt(data);
				$('#cl-'+itemid).remove();
				if(!data){
					CL.Box.Tip(itemid,'出错了');
				}else{
					$('#list-'+itemid).remove();
				}
			}
		});
	}
};


var $_msg={//类型 代码
	0:['网络红人小狐仙视频全集','http://www.uotee.com/mm/post/26'],
	1:["国外性感网络红人美女",'http://www.uotee.com/mm/post/25'],
	2:['苏紫紫？难道沾血的仅仅只是处女膜？','http://www.uotee.com/mm/post/23']
};
Yep.Message={
	msgtitle:'快乐知道',
	show:function(){
		var TopY=0;//初始化元素距父元素的距离
		var msg = '';
		for(var i=0;i<3;i++){
			msg += '<li><a target="_blank" href="'+$_msg[i][1]+'">'+$_msg[i][0]+'</a></li>';
		}
		var html = '<div id="task-guide-content"><div id="task-guide-hd"><div>'+this.msgtitle+'</div><span id="task-guide-close">x</span></div><div id="task-guide-body"><div class="task-tip"><div class="recommend-task"><ul>'+msg+'</ul></div></div></div></div>';
		$("#task-guide").html(html);
		$("#task-guide").slideDown(1000);//弹出
		$('#task-guide').show('slow');
		$("#task-guide-close").click(function() {//当点击关闭按钮的时候
			if(TopY==0){
				$("#task-guide").slideUp(1000);//这里之所以用slideUp是为了兼用Firefox浏览器
			}else{
				$("#task-guide").animate({top: TopY}, "slow", function() { $("#task-guide").hide(); });//当TopY不等于0时  ie下和Firefox效果一样
			}
		 });
		$(window).scroll(function() {
			$("#task-guide").css("top", $(window).scrollTop() + $(window).height() - $("#task-guide").height());//当滚动条滚动的时候始终在屏幕的右下角
			TopY=$("#task-guide").offset().top;//当滚动条滚动的时候随时设置元素距父原素距离
		});
	}
};


$(function(){
	//Yep.Message.show();
	Yep.ZhiDaoDe.moveHelpMenu();
});
