var SAXUE = {};
SAXUE.uid = 0;
SAXUE.popTip = function(d) {
	var a = '<div id="J_FIXED" class="m-tip" style="display:none;"><div id="J_TIPS" class="inner"></div></div>';
	if ($("#J_FIXED").length == 0) {
		$("body").prepend(a)
	}
	var c = d, b = $("#J_TIPS");
	b.empty().html(c);
	b.parent().fadeIn(300).delay(3000).fadeOut(300)
};
SAXUE.popTipClose = function(f) {
	var e = $("#J_FIXED_X");
	if (e.length == 0) {
		var a = '<div id="J_FIXED_X" class="m-tip" style="display:none;"><div id="J_TIPS_X" class="inner"><p></p></div></div>';
		e = $(a).appendTo($("body"))
	}
	var d = $("#J_TIPS_X p");
	d.empty().html(f);
	d.append('<a class="close" href="javascript:;"></a>');
	$(".close", e).click(function() {
		e.fadeOut(300)
	});
	$(".link", e).click(function() {
		e.fadeOut(300)
	});
	e.fadeIn(500)
};
SAXUE.menuHover = function(u, v) {
	var u = $(u), t = $(v), s = false;
	if (u.hasClass("name")) {
		var w = u.offset().left - $(".m-topbar .inner").offset().left - t.width() / 2;
		t.css("left", w + 40)
	}
	u.mouseenter(function() {
		s = true;
		t.toggle()
	});
	u.mouseleave(function() {
		s = false;
		var x = setTimeout(function() {
			if (!s) {
				if (t.css("display") !== "none") {
					t.toggle()
				}
			}
		}, 500)
	});
	t.mouseenter(function() {
		s = true
	});
	t.mouseleave(function() {
		s = false;
		t.toggle()
	})
};
SAXUE.setStyle = function (u) {
    var lk = document.createElement("LINK");
    lk.type = "text/css";
    lk.rel = "stylesheet";
    lk.href = u + "?v=" + (new Date().getTime());
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(lk);
};
SAXUE.headerInit = function () {
	$.getScript(saxue_url+"/api/uinfo.php", function() {
		if(user.id!=''){
			SAXUE.uid = user.id;
			$('#j-user .name span').html(user.name);
			$('#j-login').hide();
			$('#j-user').show();
			if (user.oauth!='') {
				$('#j-user .name').addClass(user.oauth)
			}
			SAXUE.menuHover("#j-user .name", ".submenu")
		}else{
			SAXUE.menuHover("#j-login .login", ".m-loginops")
		}
	});
};
SAXUE.Report = {
	Aid: 0,
	Url: "",
	isReport: false,
	showReport: function (aid,url) {
		if(typeof(aid)=="undefined" || typeof(url)=="undefined"){
			SAXUE.popTip('参数错误');
			return;
		}
		SAXUE.Report.Aid = aid;
		SAXUE.Report.Url = url;
		var a = '<div class="m-message-layer" id="ReportLayer" style="display:none"></div>';
		if ($("#ReportLayer").length == 0) {
			$("body").prepend(a);
			$("#ReportLayer").load("/article/report.php?action=ajaxhtml");
		}
		$("#ReportLayer").show()
	},
	doReport: function () {
		if (SAXUE.Report.isReport) {
			SAXUE.popTip('请不要重复举报');
			return;
		}
		if ($('#reporttype').val()=='' || $('#reporttype').val()==0) {
			SAXUE.popTip('请选择举报该文章理由');
			return;
		}
		if ($('#note').val()=='') {
			SAXUE.popTip('请列举相关证据或链接');
			return;
		}
		SAXUE.Report.isReport = true;
		$.post("/article/report.php", { "aid":SAXUE.Report.Aid, "url":SAXUE.Report.Url, "action":'report', "reporttype":$('#reporttype').val(), "note":$('#note').val() }, function(data){
			if(data.flag==0){
				SAXUE.Report.isReport = false;
			} else {
				$("#ReportLayer").hide();
			}
			SAXUE.popTip(data.msg);
		}, "json")
	}
};
SAXUE.Message = {
	Uid: 0,
	isSend: false,
	showMessage: function (uid) {
		if(SAXUE.uid <= 0){
			SAXUE.Login.showLogin();
			return;
		}
		if(typeof(uid)=="undefined"){
			SAXUE.popTip('参数错误');
			return;
		}
		if(SAXUE.uid == uid){
			SAXUE.popTip('不能给自己发短信');
			return;
		}
		SAXUE.Message.Uid = uid;
		var a = '<div class="m-message-layer" id="MessageLayer" style="display:none"></div>';
		if ($("#MessageLayer").length == 0) {
			SAXUE.setStyle(saxue_skin_url+'/css/msg.css');
			$("body").prepend(a);
			$("#MessageLayer").html(SAXUE.Message.messageForm);
			$(".j-close-message").unbind().click(function(e) {
				e.preventDefault();
				$("#MessageLayer").hide();
			})
		}
		$("#MessageLayer").show()
	},
	doSend: function () {
		if (SAXUE.Message.isSend) {
			SAXUE.popTip('请不要重复发送');
			return;
		}
		if ($('#content').val()=='') {
			SAXUE.popTip('短信内容不能为空');
			return;
		}
		SAXUE.Message.isSend = true;
		$.post("/api/userclick.php", { "toid":SAXUE.Message.Uid, "do":'message', "title":$('#title').val(), "content":$('#content').val() }, function(data){
			if(data.flag==0){
				SAXUE.Message.isSend = false;
			} else {
				$("#MessageLayer").hide();
			}
			SAXUE.popTip(data.msg);
		}, "json")
	},
	messageForm: function () {
		var text = '<div class="lytt yy">'
		+'	<a class="lyclose j-close-message" style="display:block;">关闭</a>'
		+'	<h4>站内短信</h4>'
		+'</div>'
		+'<div class="m-message">'
		+'	<div class="ipt-row">标题：<br><input tabindex="1" id="title" type="text" autocomplete="off"></div>'
		+'	<div class="ipt-row">内容：<br><textarea tabindex="2" id="content"></textarea></div>'
		+'	<div class="ipt-row"><button tabindex="4" onclick="javascript:SAXUE.Message.doSend();return false;"><span>发送</span></button></div>'
		+'</div>';
		return text
	}
};
SAXUE.Login = {
	isLogin: false,
	showLogin: function () {
		var a = '<div class="m-login-layer" id="LoginLayer" style="display:none"></div>';
		if ($("#LoginLayer").length == 0) {
			SAXUE.setStyle(saxue_skin_url+'/css/log.css');
			$("body").prepend(a);
			$("#LoginLayer").html(SAXUE.Login.loginForm);
			$(".j-close-login").unbind().click(function(e) {
				e.preventDefault();
				$("#LoginLayer").hide();
			}), $(".j-login-qq").bind('click', function(){SAXUE.oauthLogin("qq")}), $(".j-login-weibo").bind('click', function(){SAXUE.oauthLogin("weibo")})
		}
		$("#LoginLayer").show()
	},
	doLogin: function () {
		if (SAXUE.Login.isLogin) return;
		var er = $('#j-err');
		if ($('#u').val()=='') {
			er.empty().html('请输入用户名或邮箱').css("visibility","visible");
			return;
		}
		if ($('#p').val()=='') {
			er.empty().html('请输入密码').css("visibility","visible");
			return;
		}
		SAXUE.Login.isLogin = true;
		er.empty().html('正在登录...').css("visibility","visible");
		$.ajax({
			type: 'POST',
			url: saxue_url + '/member/login.php',
			data: {account: $('#u').val(), password: $('#p').val(), action: 'login', vcode: 'no', rnd: Math.random()},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "jsonpcallback",
			success: function(data){
				if(data.flag==0){
					er.empty().html(data.msg).css("visibility","visible");
					SAXUE.Login.isLogin = false;
				}else{
					document.location.reload();
				}
			}
		})
	},
	loginForm: function () {
		var text = '<div class="lytt yy">'
		+'	<a class="lyclose j-close-login" style="display:block;">关闭</a>'
		+'	<h4>会员登录</h4>'
		+'</div>'
		+'<div class="m-content">'
		+'	<div class="m-loginwrap">'
		+'		<div class="m-login">'
		+'			<p id="j-err">&nbsp;</p>'
		+'			<div class="row">'
		+'				<span class="ipt-wrap"><input tabindex="1" id="u" class="ipt" placeholder="会员帐号" type="text" autocomplete="off"></span>'
		+'			</div>'
		+'			<div class="row">'
		+'				<span class="ipt-wrap"><input tabindex="2" id="p" class="ipt" placeholder="登录密码" type="password"></span>'
		+'			</div>'
		+'			<div class="row row-1">'
		+'				<a tabindex="3" target="_blank" href="'+saxue_url+'/member/forget.php" style="float:right">忘记密码？</a>'
		+'			</div>'
		+'			<div class="row row-2">'
		+'				<button tabindex="4" onclick="javascript:SAXUE.Login.doLogin();return false;"><span>登录</span></button>'
		+'			</div>'
		+'			<div class="row">'
		+'				<a tabindex="5" target="_blank" class="wyt" href="'+saxue_url+'/member/register.php">没有帐号？免费注册</a>'
		+'			</div>'
		+'		</div>'
		+'		<div class="m-loginswitch">'
		+'			<h6>其他帐号登录：</h6>'
		+'			<ul>'
		+'				<li class="qq"><a class="j-login-qq"><span class="login-icon"></span><div>QQ登录</div></a></li>'
		+'				<li class="xl"><a class="j-login-weibo"><span class="login-icon"></span><div>新浪微博</div></a></li>'
		+'				<li class="tx"><a><span class="login-icon"></span><div>腾讯微博</div></a></li>'
		+'				<li class="wx"><a><span class="login-icon"></span><div>微信</div></a></li>'
		+'			</ul>'
		+'		</div>'
		+'	</div>'
		+'</div>';
		return text
	}
};
SAXUE.goTop = function() {
	if ($(".g-bd").length == 0) {
		return
	}
	var u = $(".g-bd").offset().left;
	var v = '<a class="m-gotop" title="回到顶部" href="javascript:;"></a>';
	var s = $(v).appendTo($(".g-doc")).css({
		right: (u - 80)
	});
	$(".m-gotop").click(function() {
		$("html, body").animate({
			scrollTop: 0
		}, 300)
	});
	$(window).resize(function() {
		u = $(".g-bd").offset().left;
		s.css({
			right: (u - 80)
		})
	});
	var t = function() {
		var w = $(document).scrollTop(), y = $(window).height(), x = $(window).width();
		(w > 0) ? $(".m-gotop").show() : $(".m-gotop").hide();
		if (!window.XMLHttpRequest) {
			s.css("top", w + y - 86)
		}
	};
	$(window).bind("scroll", t);
	$(function() {
		t()
	})
};
SAXUE.headerFix = function() {
	if ($("#J_HeaderFix").length == 0) {
		return
	}
	var s = $(window).height();
	$(window).scroll(function(u) {
		var t = $(window).scrollTop();
		if ( !! window.ActiveXObject && !window.XMLHttpRequest) {
			if (t > $("#J_Header").height() + 10) {
				$(".m-headerfix").show()
			} else {
				$(".m-headerfix").hide()
			}
		} else {
			if (t > $("#J_Header").height() + 10) {
				$(".m-headerfix").addClass("m-headerfix-scrolling")
			} else {
				$(".m-headerfix-scrolling .J_SearchOps").hide();
				$(".m-headerfix").removeClass("m-headerfix-scrolling")
			}
		}
	})
};
SAXUE.resizeImg = function(){
	if($.browser.msie && $.browser.version === '6.0') return;
	$('.j-img').each(function(){
		var imgW = $(this).width(),imgH = $(this).height();
		var imgR = imgW && (imgW/imgH), dWidth= $(this).attr('w'), dHeight = $(this).attr('h');
		var R = dWidth/dHeight;
		if(imgR > R){//原图比率更宽
			$(this).height(dHeight+'px');
			var distance = -($(this).width() - dWidth)/2;
			$(this).css({'margin-left':distance+'px'});
		}else if(imgR < R){//原图比率窄
			$(this).width(dWidth+'px');
			var distance = -($(this).height() - dHeight)/2;
			$(this).css({'margin-top':distance+'px'});
		}else{
			$(this).width(dWidth+'px');
		}
	})
};
SAXUE.drawImg = function(){
	$('.j-pic').each(function(){
		var maxWidth = $(this).attr('w'); // 图片最大宽度
		var maxHeight = $(this).attr('h');    // 图片最大高度
		var ratio = 0;  // 缩放比例
		var width = $(this).width();    // 图片实际宽度
		var height = $(this).height();  // 图片实际高度
   
		// 检查图片是否超宽
		if(width > maxWidth){
			ratio = maxWidth / width;   // 计算缩放比例
			height = height * ratio;    // 计算等比例缩放后的高度
			width = maxWidth;
		}
   
		// 检查图片是否超高
		if(height > maxHeight){
			ratio = maxHeight / height; // 计算缩放比例
			width = width * ratio;    // 计算等比例缩放后的宽度
			height = maxHeight;
		}
		$(this).css("width", width+'px'); // 设定实际显示宽度
		$(this).css("height", height+'px');  // 设定实际显示高度
	})
};
SAXUE.doFav = function(e,id,cid) {
	e && e.preventDefault();
	if(SAXUE.uid <= 0){
		SAXUE.Login.showLogin();
		return;
	}
	if(typeof(id)=="undefined" && typeof(aid)=="undefined"){
		SAXUE.popTip('参数错误');
		return;
	}
	if(typeof(id)=="undefined"){
		id = aid
	}
	if(typeof(cid)=="undefined"){
		if(typeof(chapterid)=="undefined"){
			cid = 0
		}else{
			cid = chapterid
		}
	}
	$.post("/api/click.php", { "aid":id, "do":'fav', "chapterid":cid }, function(data){
		SAXUE.popTip(data.msg);
	}, "json");
};
SAXUE.doTop = function(e,id) {
	e && e.preventDefault();
	if(typeof(id)=="undefined" && typeof(aid)=="undefined"){
		SAXUE.popTip('参数错误');
		return;
	}
	if(typeof(id)=="undefined"){
		id = aid
	}
	$.post("/api/click.php", { "aid":id, "do":'top' }, function(data){
		SAXUE.popTip(data.msg);
	}, "json");
};
SAXUE.doDown = function(e,id,type) {
	e && e.preventDefault();
	if(SAXUE.uid <= 0){
		SAXUE.Login.showLogin();
		return;
	}
	if(typeof(id)=="undefined" && typeof(aid)=="undefined"){
		SAXUE.popTip('参数错误');
		return;
	}
	if(typeof(id)=="undefined"){
		id = aid
	}
	if(typeof(type)=="undefined"){
		type = 'word'
	}
	var j = null;
	var i = function() {
		var n = document.createElement("iframe");
		n.style.position = "absolute";
		n.style.left = "-9999px";
		n.width = "0";
		n.height = "0";
		n.frameborder = "0";
		n.scrolling = "no";
		document.getElementsByTagName("body")[0].appendChild(n);
		return n
	};
	if (!j) {
		j = i()
	}
	j.src = "/api/click.php?do=down&aid="+id+"&type="+type;
	//window.location.href="/api/click.php?do=down&aid="+id;
};
SAXUE.doFollow = function(e,id) {
	e && e.preventDefault();
	if(SAXUE.uid <= 0){
		SAXUE.Login.showLogin();
		return;
	}
	if(typeof(id)=="undefined" && typeof(uid)=="undefined"){
		SAXUE.popTip('参数错误');
		return;
	}
	if(typeof(id)=="undefined"){
		id = uid
	}
	if(SAXUE.uid == id){
		SAXUE.popTip('不能关注自己');
		return;
	}
	$.post("/api/userclick.php", { "toid":id, "do":'follow' }, function(data){
		SAXUE.popTip(data.msg);
	}, "json");
};
SAXUE.doFollowed = function(e,id,callback) {
	e && e.preventDefault();
	if(SAXUE.uid <= 0){
		SAXUE.Login.showLogin();
		return;
	}
	if(typeof(id)=="undefined" && typeof(uid)=="undefined"){
		SAXUE.popTip('参数错误');
		return;
	}
	if(typeof(id)=="undefined"){
		id = uid
	}
	$.post("/api/userclick.php", { "toid":id, "do":'unfollow' }, function(data){
		SAXUE.popTip(data.msg);
		if (data.flag==1 && typeof(callback)==="function") {
			callback(id)
		}
	}, "json");
};
SAXUE.adFloat = function() {
	var f = $("#ad_float");
	if(f.length==0) return;
	var fTop = f.offset().top;
	if(fTop==0) return;
	window.onscroll = function(){
		var scroll = $(document).scrollTop();
		scroll > fTop ? f.css({ "position" : "fixed", "top" :"0" }) : f.css({ "position" : "static", "top" : "auto" })
	}
};
SAXUE.oauthLogin = function(site) {
	window.location.href="/member/oauthlogin.php?site=" + site
};
$(function(){
	SAXUE.headerInit(), SAXUE.goTop(), SAXUE.adFloat(), SAXUE.headerFix(), $(".j-login-site").bind('click', SAXUE.Login.showLogin), $(".j-login-qq").bind('click', function(){SAXUE.oauthLogin("qq")}), $(".j-login-weibo").bind('click', function(){SAXUE.oauthLogin("weibo")})
})