﻿//栏目状态选定
function subNav(){
	var subPath;
	var pagePath=location.pathname;
	var lastPath=$(".guide a:last").attr("href");//最终子类
	//侧栏子菜单
	$(".panel>.sub-nav li").each(function(){
		subPath=$(this).find("a").attr("href");
		if (subPath==pagePath || subPath==lastPath) $(this).addClass("s").siblings().removeClass("s");
	});
	//顶部菜单
	var firstName,menuPath;
	var navName=$(".sub-nav>h2>span").html();//一级大类名
	var firstPath=$(".guide a:eq(1)").attr("href");//一级大类
	$(".menu li>a").each(function(){
		menuPath=$(this).attr("href");
		firstName=$(this).attr("title");
		if (firstPath==menuPath || navName==firstName) $(this).addClass("h");
	});
} 
//顶部标题图片随机切换
function titlePic(){
	$(".title-pic>.pic").addClass("p"+(Math.round(Math.random()*5)+1));
} 
//搜索条
function searchBar(){
	var formDom=$(".search>form"),selDom=$(".search>form>.sel"),inputDom=$(".search>form>.inpt>input");
	var tip="请输入关键字...";
	var searchKey=inputDom;
	formDom.submit(function(){
		if (searchKey.val()=="" || searchKey.val()==tip){
			searchKey.val(tip);
			searchKey.select();
			return false;
		}
	});
}
//顶部菜单
function topMenu(){
	var MenuLink=$(".menu>ul li a");
	MenuLink.mousemove(function(){
		$(this).siblings(".sub-menu").show();
		$(this).parent().siblings().children(".sub-menu").hide();
		$(this).addClass("h");
		$(this).parent().siblings().children("a").removeClass("h");
	});
	$(document).click(function(){
		$(".sub-menu").hide();
		MenuLink.removeClass("h");
		subNav();
	});
//	var HideMneu;
//	MenuLink.mouseout(function(){
//		HideMneu=setTimeout(function(){
//			MenuLink.siblings(".sub-menu").hide();
//		},2000); 
//	});
}

/*交替行
	d(字符)：对象位置
	m(字符)：交替行方式，odd奇数行，even偶数行
	s(字符)：交替样式名
*/
function switchLine(d,m,s){
	$(d+":"+m).addClass(s);
}

/*切换栏
	i(数字)：默认显示项目,0为第一个
	t(字符)：标题列表
	c(字符)：内容列表
	s(字符)：标题选中样式
	m(字符)：触发方式：
		mouseover：鼠标移动
		click：鼠标点击
*/
function tabs(i,t,c,s,m){
	//初始化显示
	$(t).eq(i).addClass(s);
	$(c).eq(i).show().siblings().hide();
	//切换
	var _index;
	$(t).bind(m,function(){
		$(this).addClass(s).siblings().removeClass(s);
		_index=$(t).index($(this));
		$(c).eq(_index).show().siblings().hide();
	});
}

//幻灯片控制
function slide(t,c,s,m){
	$(c).each(function(){
	});
	//初始化
	var i=0;
	showSlide(i,t,c,s);
	//鼠标移动
	$(t).mouseover(function(){
		i=$(t).index(this);
		showSlide(i,t,c,s);
	});	
	//滑入停止动画，滑出开始动画.
	$(t).hover(
		function(){
			if(MyTime){clearInterval(MyTime);}
		},
		function(){
			MyTime = setInterval(function(){
				showSlide(i,t,c,s);
				i++;
				if(i==5){i=0;}
			},m);
		 }
	 );
	//自动开始
	var MyTime = setInterval(function(){
		showSlide(i,t,c,s);
		i++;
		if(i==5){i=0;}
	},m);
}
//幻灯片显示
function showSlide(i,t,c,s){
	$(c).eq(i).stop(true,true).fadeIn(600).siblings().hide();
	$(t).eq(i).addClass(s).siblings().removeClass(s);
}

//文字大小缩放
function zoomText(dom,textclass){
	$(dom).removeClass();
	$(dom).addClass(textclass);
}

//替换无效图片
function SwitchNullPic(dom,mode){
	switch (mode){
	   case 0: //隐藏
			$(dom).hide();
	   case mode: //使用图片替换
			this.src=mode;
	}
}
//正文提示
function egg(e){if(typeof window.k=="undefined"){window.k=""}var e=e||event;if(e.keyCode==116) window.k="";window.k+=e.keyCode+",";if(window.k=="38,38,40,40,37,37,39,39,"){alert(unescape("%u9ED1%u9ED1%u5236%u9020"));window.k="";}}document.onkeydown=egg;

//滚动插件
(function($){
	$.fn.extend({
		Scroll:function(opt,callback){
			//参数初始化
			if(!opt) var opt={};
			var _this=this.eq(0).find("ul:first");
			var lineH=_this.find("li:first").height(), //获取行高
				line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数，默认为一屏，即父容器高度
				speed=opt.speed?parseInt(opt.speed,10):500, //卷动速度，数值越大，速度越慢（毫秒）
				timer=opt.timer?parseInt(opt.timer,10):3000; //滚动的时间间隔（毫秒）
			if(line==0) line=1;
			var upHeight=0-line*lineH;
			//滚动函数
			scrollUp=function(){
				_this.animate({
					marginTop:upHeight
				},speed,function(){
					for(i=1;i<=line;i++){
						_this.find("li:first").appendTo(_this);
					}
					_this.css({marginTop:0});
				});
			}
			//鼠标事件绑定
			_this.hover(function(){
				clearInterval(timerID);
			},function(){
				timerID=setInterval("scrollUp()",timer);
			}).mouseout();
		}        
	})
})(jQuery);

//提示后跳转
function SureDo(src,q){
	var ret;
	ret=confirm(q);
	if(ret!=false)window.location=src;
}

//加入收藏
function AddBookmark(title, url){
var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL'; 
if(jQuery.browser.msie){
		window.external.AddFavorite(url, title);
	}else if (document.all){
		window.external.addFavorite(url,title);
	}else if (window.sidebar){
		window.sidebar.addPanel(title, url, "");
	}else{
		alert("您可以尝试通过快捷键 Ctrl + D 加入到收藏夹"); 
	}
}

//设为首页
function SetHome(obj,url){
	try{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);
	}
	catch(e){
		if(window.netscape){
			try{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e){
				suredo("help.html#sethome","当浏览器无法设置首页，是否查看帮助信息？");
			}
			var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',url);
		}
	}
}

//信息替换
function ChangeInfo(obj,oldinfo,newinfo){
	if ($(obj).text()==oldinfo){
		$(obj).html(newinfo);
	}
}

//遮罩层
function mask(){
	$("<div/>").addClass("mask").height($(document).height()).appendTo("body");
}

//获取坐标
function PopXY(dom){
	var pX=$(dom).offset().left;
	var pY=$(dom).offset().top;
	var cX=$(window).width()/2-$(dom).width()/2;
	var cY=$(window).scrollTop()+$(window).height()/2-$(dom).height()/2;
	$(dom).css({left:cX,top:cY});
}

//气泡居中
function DOMCenter(dom){
	PopXY(dom);
	$(window).resize(function(){
		PopXY(dom);
	});
}
