$( function() {
	$("#select_index").click( function() {
		$("#select_list").toggle();
		return false;
	});
	$("#select_list li").hover( function() {
		$("#select_list li.current").removeClass();
		$(this).attr("class", "current");
	});
	$("#select_list li").click( function() {
		$("#select_index").text($(this).text());
		$("#select_list").hide();
	});
	$("body").click( function() {
		$("#select_list").hide();
	});

	function news() {
		var newsText = $("#news_text");
		newsText.stop(true, true).animate( {
			"top" : "-15px"
		}, 400, function() {
			newsText.append(newsText.find("p:eq(0)"));
			newsText.css("top", "0px");
			t = setTimeout(news, 5000);
		});
	}

	/* 最新发表 滚动*/
	var t = null;
	$("#news_text p").hover( function() {
		clearTimeout(t);
		$("#news_text").stop(true, true);
	}, function() {
		t = setTimeout(news, 2000);
	});

	news();
});

function getNewSubmitForm(formAction, method) {
	var submitForm = document.createElement("form");
	document.body.appendChild(submitForm);
	submitForm.method = method;
	submitForm.action = formAction;
	return submitForm;
}

function createNewFormElement(inputForm, elementName, elementValue) {
	var newElement = document.createElement("input");
	newElement.name = elementName;
	newElement.type = "hidden";
	newElement.value = elementValue;
	inputForm.appendChild(newElement);
	return newElement;
}

function usernameAccess(username, channel, orderby) {
	var submitForm = getNewSubmitForm("/space.jsp", "get");
	if (username) {
		createNewFormElement(submitForm, "u", username);
	}
	if (channel) {
		createNewFormElement(submitForm, "c", channel);
	}
	if (orderby) {
		createNewFormElement(submitForm, "o", orderby);
	}
	submitForm.submit();
}

Cms = {};
/**
 * 首页统计
 */
Cms.indexCount = function(base, siteId, checkCount, nocheckCount, todayCount,
		authorCount) {
	checkCount = checkCount || "checkCount";
	nocheckCount = nocheckCount || "nocheckCount";
	todayCount = todayCount || "todayCount";
	authorCount = authorCount || "authorCount";
	$.getJSON(base + "/index_count.jsp", {
		siteId : siteId
	}, function(data) {
		if (data.length > 0) {
			$("#" + checkCount).text(data[0]);
			$("#" + nocheckCount).text(data[1]);
			$("#" + todayCount).text(data[2]);
			$("#" + authorCount).text(data[3]);
		}
	});
}
/**
 * 浏览次数
 */
Cms.viewCount = function(base, contentId, viewId, commentId, downloadId, upId,
		downId) {
	viewId = viewId || "views";
	commentId = commentId || "comments";
	downloadId = downloadId || "downloads";
	upId = upId || "ups";
	downId = downId || "downs";
	$.getJSON(base + "/content_view.jsp", {
		contentId : contentId
	}, function(data) {
		if (data.length > 0) {
			$("#" + viewId).text(data[0]);
			$("#" + commentId).text(data[1]);
			$("#" + downloadId).text(data[2]);
			$("#" + upId).text(data[3]);
			$("#" + downId).text(data[4]);
		}
	});
}
/**
 * 成功返回true，失败返回false。
 */
Cms.up = function(base, contentId, origValue, upId) {
	upId = upId || "ups";
	var updown = $.cookie("_cms_updown_" + contentId);
	if (updown) {
		return false;
	}
	$.cookie("_cms_updown_" + contentId, "1");
	$.get(base + "/content_up.jsp", {
		"contentId" : contentId
	}, function(data) {
		$("#" + upId).text(origValue + 1);
	});
	return true;
}
/**
 * 成功返回true，失败返回false。
 */
Cms.down = function(base, contentId, origValue, downId) {
	downId = downId || "downs";
	var updown = $.cookie("_cms_updown_" + contentId);
	if (updown) {
		return false;
	}
	$.cookie("_cms_updown_" + contentId, "1");
	$.get(base + "/content_down.jsp", {
		contentId : contentId
	}, function(data) {
		$("#" + downId).text(origValue + 1);
	});
	return true;
}
/**
 * 获取附件地址
 */
Cms.attachment = function(base, contentId, n, prefix) {
	$.get(base + "/attachment_url.jsp", {
		"cid" : contentId,
		"n" : n
	}, function(data) {
		var url;
		for ( var i = 0; i < n; i++) {
			url = base + "/attachment.jsp?cid=" + contentId + "&i=" + i
					+ data[i];
			$("#" + prefix + i).attr("href", url);
		}
	}, "json");
}
/**
 * 提交评论
 */
Cms.comment = function(callback, form) {
	form = form || "commentForm";
	$("#" + form).validate( {
		submitHandler : function(form) {
			$(form).ajaxSubmit( {
				"success" : callback,
				"dataType" : "json"
			});
		}
	});
}
/**
 * 获取评论列表
 * 
 * @param siteId
 * @param contentId
 * @param greatTo
 * @param recommend
 * @param orderBy
 * @param count
 */
Cms.commentList = function(base, c, options) {
	c = c || "commentListDiv";
	$("#" + c).load(base + "/comment_list.jsp", options);
}
/**
 * 客户端包含登录
 */
Cms.loginCsi = function(base, c, options) {
	c = c || "loginCsiDiv";
	$("#" + c).load(base + "/login_csi.jsp", options);
}
