function changeHeaderTab() {
	var navis = document.getElementById('navis').getElementsByTagName('span');
	for (var i = 0; i < navis.length; i ++) {
		if (navis[i].className == 'btns') {
			if (navis[i].innerHTML == "") {
				continue;
			}
			navis[i].onmouseover = function() {
				this.className = 'hover';
			}
			navis[i].onmouseout = function() {
				this.className = 'btns';
			}
		}
	}
}

function showLogin() {
	$.colorbox({href:"/ajax/?m=user_action&ac=show_login", width:450, height:320});
}

function ajaxLogin() {
	var f = document.klogin;
	if (f.username.value == "") {
		alert("请输入您的用户名");
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert("请输入您的密码");
		f.password.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
	}
	$.post("/ajax/?m=user_action&ac=do_login&SessionId=" + Math.random(), { username: f.username.value, password: f.password.value, remember: f.remember.value }, function(data) {
		if (data == "1") {
			alert("登陆成功");
			window.location.reload();
		} else {
			alert(data);
			f.button.disabled = "";
		}
	});
	return false;
}

function getToday() {
	getTodayWeather();
}
function getTodayDate() {
	$.get("/ajax/?m=common_action&ac=get_date&SessionId=" + Math.random(), function(data) {
		$("#header_date").html(data);
	});
}
function getTodayWeather() {
	$.get("/ajax/?m=common_action&ac=get_weather&SessionId=" + Math.random(), function(data) {
		$("#header_weather").html(data);
	});
}
function getUserStatus() {
	$.get("/ajax/?m=user_action&ac=get_user_status&SessionId=" + Math.random(), function(data) {
		$("#header_user").html(data);
	});
}
function getAlmanac() {
	if ($("#header_almanac").css("visibility") == "visible") {
		$("#header_almanac").css("visibility", "hidden");
	} else {
		$.get("/ajax/?m=common_action&ac=get_almanac&SessionId=" + Math.random(), function(data) {
			$("#header_almanac").html(data);
		});
		$("#header_almanac").css("visibility", "visible");
	}
}
function addToFavorite(index, id) {
	$.get("/ajax/?m=common_action&ac=add_to_favorite&index=" + index + "&subject_id=" + id + "&SessionId=" + Math.random(), function(data) {
		//alert(data);
		if (data == "need_login") {
			$.colorbox({href:"/ajax/?m=user_action&ac=show_login", width:450, height:320});
		} else {
			alert(data);
		}
	});
}
function removeFavorite(id) {
	if (confirm("您确定要从收藏夹中移除么")) {
		$.get("/ajax/?m=common_action&ac=remove_favorite&id=" + id + "&SessionId=" + Math.random(), function(data) {
			if (data == "1") {
				alert("移除成功");
				window.location.reload();
			} else {
				alert(data);
			}
		});
	} else {
	
	}
}

function addSmiley(smiley, f, textarea) {
	var input = f[textarea];
	input.value += "[s_" + smiley + "]";
	input.focus();
}

function refreshAuthCode() {
	document.getElementById('captcha').src='/class/captcha/?w=82&h=20&c=4&' + Math.random();
}

function loadComments(index, id) {
	$.get("/ajax/?m=comment_action&ac=load_comment&index=" + index + "&id=" + id + "&SessionId=" + Math.random(), function(data) {
		$("#ajax_comment").html(data);
	});
}

function postComment(index, id, rel) {
	var f = document.kform;
	if (f.content.value == "") {
		alert("请输入您的评论内容");
		f.content.focus();
		return false;
	} else if (f.authcode.value == "") {
		alert("请填写您看到的图形验证码");
		f.authcode.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		f.button.value = "评论提交中，请稍候";
	}
	$.post("/ajax/?m=comment_action&ac=post_comment&index=" + index + "&id=" + id + "&SessionId=" + Math.random(), { authcode: f.authcode.value, content: f.content.value }, function(data) {
		if (data == "1") {
			alert("您的评论已成功提交，请等待本站工作人员审核后显示");
			if (rel == 1) {
				window.location.reload();
			} else {
				f.button.disabled = "";
				f.button.value = "发表评论";
				f.authcode.value = "";
				f.content.value = "";
				refreshAuthCode();
				loadComments(index, id);
			}
		} else {
			alert(data);
			f.button.disabled = "";
			f.button.value = "发表评论";
		}
	});
	return false;
}

function showSearchTab(index) {
	var tabs = document.getElementById('search_tabs').getElementsByTagName('span');
	for (var i = 0; i < tabs.length; i ++) {
		tabs[i].className = '';
	}
	document.getElementById('search_tab_' + index).className = 'current';
	var forms = document.getElementById('search_forms').getElementsByTagName('div');
	for (var i = 0; i < forms.length; i ++) {
		forms[i].style.display = 'none';
	}
	document.getElementById('search_form_' + index).style.display = '';
}

function checkRegister() {
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	if (getStringLength(f.username.value) > 32 || getStringLength(f.username.value) < 4) {
		alert("用户名长度应为4-32个字符或2-10个汉字");
		f.username.focus();
		return false;
	} else if (!isNaN(f.username.value)) {
		alert("用户名不能为纯数字组合");
		f.username.focus();
		return false;
	} else if (f.password.value.length > 16 || f.password.value.length < 6) {
		alert("密码长度应为6-16个字符");
		f.password.focus();
		return false;
	} else if (f.repassword.value == "" || f.repassword.value != f.password.value) {
		alert("请重复输入密码并确保两次输入的密码相符");
		f.repassword.focus();
		return false;
	} else if (emailstr.test(f.email.value) == false) {
		alert("请正确输入您的电子邮件地址");
		f.email.focus();
		return false;
	} else if (f.usercheck.value == 0) {
		alert("请检查您的用户名并确保用户名通过检测");
		f.username.focus();
		return false;
	} else if (f.authcodecheck.value == 0) {
		alert("请填写您看到的图形验证码并确认验证码正确");
		f.authcode.focus();
		return false;
	} else if (f.termofuse.checked == false) {
		alert("请阅读并同意本站服务条款");
		f.termofuse.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkRegisterClick(obj) {
	var text;
	switch (obj.name) {
		case "username":
		text = "4-16个字符或2-8个汉字(数字、字母和下划线)。";
		break;
		case "password":
		text = "6-16个字符(数字、大小写字母和符号)。";
		break;
		case "repassword":
		text = "请重复输入上面您输入的密码。";
		break;
		case "email":
		text = "请输入有效的电子邮箱，可用于在您忘记密码的时候寻回密码。";
		break;
		case "authcode":
		text = "请按照上图中显示字母输入验证码。";
		break;
		default:
		text = "";
	}
	try {
		document.getElementById("d_" + obj.name).className = 'hint hint2';
		document.getElementById("d_" + obj.name).innerHTML = text;
	} catch(e) {
		
	}
}

function checkRegisterBlur(obj) {
	var o = document.getElementById("d_" + obj.name);
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	switch (obj.name) {
		case "username":
		o.innerHTML = "用户名检测中……";
		o.className = "hint hint1";
		$.post("/ajax/?m=user_action&ac=check_username&SessionId=" + Math.random(), { username: f.username.value }, function(data) {
			if (data == "available") {
				f.usercheck.value = '1';
				o.innerHTML = "&nbsp;";
				o.className = "hint hint3";
			} else if (data == "error_length") {
				f.usercheck.value = '0';
				o.innerHTML = "用户名长度应为4-16个字符或2-8个汉字";
				o.className = "hint hint4";
			} else if (data == "error_numeric") {
				f.usercheck.value = '0';
				o.innerHTML = "用户名不能为纯数字组合";
				o.className = "hint hint4";
			} else {
				f.usercheck.value = '0';
				o.innerHTML = "您选择的用户名已被占用，请另外选择用户名";
				o.className = "hint hint4";
			}
		});
		break;
		case "password":
		if (f.password.value.length > 16 || f.password.value.length < 6) {
			o.innerHTML = "请填写密码，密码长度应为6-16个字符";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		return false;
		break;
		case "repassword":
		if (f.repassword.value == "" || f.repassword.value != f.password.value) {
			o.innerHTML = "请重复输入密码并确保两次输入的密码相符";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		break;
		case "email":
		if (emailstr.test(f.email.value) == false) {
			o.innerHTML = "请正确输入您的电子邮件地址";
			o.className = "hint hint4";
		} else {
				o.innerHTML = "&nbsp;";
			o.className = "hint hint3";
		}
		break;
		case "authcode":
		o.innerHTML = "验证码检测中……";
		o.className = "hint hint1";
		$.post("/ajax/?m=user_action&ac=check_authcode&SessionId=" + Math.random(), { authcode: f.authcode.value }, function(data) {
			if (data == "S") {
				f.authcodecheck.value = '1';
				o.innerHTML = "&nbsp;";
				o.className = "hint hint3";
			} else {
				f.authcodecheck.value = '0';
				o.innerHTML = "验证码不正确";
				o.className = "hint hint4";
			}
		});
		break;
		default:
		return;
	}
}

function checkUserName() {
	var f = document.kform;
	$.post("/ajax/?m=user_action&ac=check_username&SessionId=" + Math.random(), { username: f.username.value }, function(data) {
		if (data == "available") {
			f.usercheck.value = '1';
			$("#ajax_user_check").html("<font color=green>用户名可用</font>");
		} else if (data == "error_length") {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>用户名长度应为4-16个字符或2-5个汉字</font>");
		} else if (data == "error_numeric") {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>用户名不能为纯数字组合</font>");
		} else {
			f.usercheck.value = '0';
			$("#ajax_user_check").html("<font color=red>您选择的用户名已被占用，请另外选择用户名</font>");
		}
	});
}

function checkProfile() {
	var f = document.kform;
	var emailstr = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/; 
	if (emailstr.test(f.email.value) == false) {
		alert("请正确输入您的电子邮件地址");
		f.email.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function checkLogIn() {
	var f = document.klogin;
	if (f.username.value == "") {
		alert("请输入您的用户名");
		f.username.focus();
		return false;
	} else if (f.password.value == "") {
		alert("请输入您的密码");
		f.password.focus();
		return false;
	} else {
		f.button.disabled = "disabled";
		return true;
	}
	return false;
}

function confirmInfo(info, url) {
	if (confirm(info)) {
		window.location = url;
	} else {
		
	}
}

function getStringLength(str) {
	var length = 0;
	for (var i = 0; i < str.length; i ++) {
		if (str.charCodeAt(i) > 255) {
			length += 3;
		} else {
			length ++;
		}
	}
	return length;
}

function showSpaceTwitImage(image, id) {
	$.colorbox({href: "/" + image});
}

// Home Focus
(function($){
	$.fn.ADRoll = function(settings) {
		settings = jQuery.extend({
        	speed : "normal",
			num : 4,
			timer : 1000,
			direction : "top",
			imgHeight : "240"
    	}, settings);
		return this.each(function() {
			$.fn.ADRoll.scllor( $(this), settings );
    	});
	};
	$.fn.ADRoll.scllor = function($this,settings) {
		var index = 0;
		var li = $(".thumbs li");
		var showBox = $(".floater")
		li.hover(function() {
			if (intervalTime) {
				clearInterval(intervalTime);
			}
			index = li.index(this);
			intervalTime = setTimeout(function() {
				ShowAD(index);
			},100);
		},function() {
			clearInterval(intervalTime);
			intervalTime = setInterval(function() {
				ShowAD(index);
				index ++;
				if(index == settings.num){
					index = 0;
				}
			},settings.timer)
		});
		showBox.hover(function() {
			if (intervalTime) {
				clearInterval(intervalTime);
			}
		},function() {
			clearInterval(intervalTime);
			intervalTime = setInterval(function() {
				ShowAD(index);
				index++;
				if (index == settings.num) {
					index = 0;
				}
			}, settings.timer);
		});
		var intervalTime = setInterval(function(){
			ShowAD(index);
			index ++;
			if(index == settings.num){
				index = 0;
			}
		},settings.timer);
		var ShowAD = function(i) {
			showBox.animate({"top":-i*settings.imgHeight},settings.speed);
			$(".thumbs li").eq(i).addClass("highlight").siblings().removeClass("highlight");
		};
	};
})(jQuery);

/* COMMON */
$(function() {
	
});

/* BAIDU */
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F59155b0d0528be3813cb1f511c334a8d' type='text/javascript'%3E%3C/script%3E"));
