String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function showOp() {
	var wrapper = document.getElementById('signWrap');
	var signs = wrapper.getElementsByTagName('div');
	for (var i = 0; i < signs.length; i++) {
		var sign = signs[i];
		if (sign.id.indexOf('sign_') < 0)
			continue;
		var oldclass = sign.className;
		sign.onmouseover = function() {
			var id = this.id.split('_')[1];
			this.className += " sign_list_hover";
			var signop = document.getElementById('signOp_' + id);
			signop.style.display = "block";
		}
		sign.onmouseout = function() {
			var id = this.id.split('_')[1];
			this.className = oldclass;
			var signop = document.getElementById('signOp_' + id);
			signop.style.display = "none";
		}
	}
}

if (document.all) {
	window.attachEvent('onload', showOp);
} else {
	window.addEventListener('load', showOp, false);
}

function submitSign() {
	var content = document.getElementById('newSign').value.trim();
	if (!content) {
		alert('请先输入您要提交的签名内容！');
		return;
	}
	if (content.length > 138) {
		alert('签名内容不能多于138个字符！');
		return;
	}
	content = content.replace(/\</ig, '&lt;').replace(/\>/ig, '&gt;');
	if (talkbar && talkbar.setImpresa) {
		talkbar.setImpresa(content);
	}
	content = encodeURIComponent(content);
	new Ajax.Request('../log.do?method=sendFeedAction&content=' + content, {
				method : 'get',
				parameters : {},
				onComplete : function(response) {
				}
			});
	new Ajax.Request('../log.do?method=submitToCommIndex&content=' + content, {
				method : 'get',
				parameters : {},
				onComplete : function(response) {
				}
			});
	document.forms['new_sign_form'].submit();
}

function delLog(logId) {
	if (window.confirm('你确定要删除该条签名吗？')) {
		var param = {};
		param['logId'] = logId;
		new Ajax.Request('../log.do?method=delLog&logId=' + logId, {
					method : 'get',
					parameters : param,
					onComplete : function(response) {
						var delSign = document.getElementById('sign_' + logId);
						var ne = getNextElement(delSign.nextSibling);
						if (ne && ne.id == 'commentDiv')
							ne.style.display = 'none';
						delSign.style.display = 'none';
					}
				});
	}
}

function addCollectionEx(logId, pid) {
	if (!pid)
		window.location.href = 'http://miniblog.xiaoi.com';
	else
		addCollection(logId);
}

function addCollection(logId) {
	var param = {};
	new Ajax.Request('../log.do?method=addCollection&logId=' + logId, {
				method : 'get',
				parameters : param,
				onComplete : function(response) {
					var resTxt = response.responseText;
					alert(resTxt);
				}
			});
}

function delCollection(logId) {
	if (window.confirm('你确定要删除该条收藏吗？')) {
		var param = {};
		new Ajax.Request('../log.do?method=removeCollection&logId=' + logId, {
					method : 'get',
					parameters : param,
					onComplete : function(response) {
						var delSign = document.getElementById('sign_' + logId);
						delSign.style.display = 'none';
					}
				});
	}
}

function modifyBlogUrl() {
	var mDiv = document.getElementById('modify_blog_div');
	mDiv.style.display = 'block';
	document.getElementById("cover").style.width = document
			.getElementById("doc_body").scrollWidth
			+ "px";
	document.getElementById("cover").style.height = document
			.getElementById("doc_body").scrollWidth
			+ "px";
	document.getElementById('cover').style.display = 'block';
}

function cancelModifyUrl() {
	var mDiv = document.getElementById('modify_blog_div');
	mDiv.style.display = 'none';
	document.getElementById('new_blog_title').value = '';
	document.getElementById('modify_blog_hint').innerHTML = '';
	document.getElementById('cover').style.display = 'none';
}

function submitModifyUrl(btn) {
	btn.disabled = 'disabled';
	var hint = document.getElementById('modify_blog_hint');
	var newUrl = document.getElementById('new_blog_title').value.trim();
	if (!newUrl) {
		hint.style.color = '#f00';
		hint.innerHTML = '请先输入你想要的新地址！';
		btn.disabled = '';
		return;
	}
	if (!isValidUrl(newUrl)) {
		hint.style.color = '#f00';
		hint.innerHTML = '地址必须为数字和字母组合，不能为纯数字！';
		btn.disabled = '';
		return;
	}
	var param = {};
	new Ajax.Request('../blog.do?method=modifyUrl&newUrl=' + newUrl, {
		method : 'get',
		parameters : param,
		onComplete : function(response) {
			var rt = response.responseText;
			if (rt == 'success') {
				hint.style.color = 'blue';
				hint.innerHTML = '修改成功！';
				setTimeout(cancelModifyUrl, 1500);
				document.getElementById('blog_modify_link').style.display = 'none';
				document.getElementById('blog_url').innerHTML = 'http://miniblog.xiaoi.com/'
						+ newUrl;
			} else {
				hint.style.color = '#f00';
				hint.innerHTML = '您填写的地址已被别人使用！';
			}
			btn.disabled = '';
		}
	});
}

function isValidUrl(s) {
	var flag = true;
	var patrn = /^[0-9a-zA-Z]{6,14}$/;
	if (!patrn.exec(s))
		flag = false;
	if (flag) {
		patrn = /^[0-9]+$/;
		if (patrn.exec(s))
			flag = false;
	}
	return flag;
}

function getNextElement(node) {
	if (!node)
		return null;
	if (node.nodeType == 1) {
		return node;
	}
	if (node.nextSibling) {
		return getNextElement(node.nextSibling);
	}
	return null;
}

function useAsPersonalMessage(logId) {
	var msgr;
	try {
		msgr = new ActiveXObject("MSNMessengerLib.MSNMessenger");
	} catch (e) {
		alert("友情提示！该功能只有安装了MSNPlus才能使用！");
		return;
	}
	var s = document.getElementById('logContent_' + logId).innerHTML;
	if (s.length <= 96) {
		s = s + "[玩转你的MSN签名，就在miniblog.xiaoi.com！]";
	}
	msgr.MyPersonalMessage = s;
	alert("签名更换成功！");
}

function addTitleBackground(titleSpan) {
	titleSpan.style.border = '1px solid #fc0';
	titleSpan.style.backgroundColor = '#ffffb9';
}

function removeTitleBackground(titleSpan) {
	titleSpan.style.border = '1px solid #fff';
	titleSpan.style.backgroundColor = '#fff';
}

function showTitleModifyInput(titleSpan) {
	titleSpan.style.display = 'none';
	var input = document.getElementById('modify_input');
	input.style.display = 'block';
	input.focus();
	// input.value = input.value;
	input.select();
}

var enterPress = false;

function updateTitle(titleInput) {
	var titleSpan = document.getElementById('title_span');
	var oldVal = titleSpan.innerHTML.trim();
	var newVal = titleInput.value.trim();
	if (oldVal != newVal) {
		if (newVal.replace(/\s*/gm, '').length <= 0) {
			titleInput.value = oldVal;
			titleInput.style.display = 'none';
			titleSpan.style.display = 'block';
			alert("修改的博客名不能为空");
			return;
		}
		newVal = newVal.replace(/\<|>/ig, '');
		new Ajax.Request('../blog.do?method=modifyTitle&title='
						+ encodeURIComponent(newVal), {
					method : 'get',
					parameters : {},
					onComplete : function(response) {
						var rt = response.responseText;
						if (rt == 'success') {
							titleSpan.innerHTML = newVal;
						} else {
							alert('修改博客名称失败，请稍候再试！');
							titleInput.value = oldVal;
						}
						titleInput.style.display = 'none';
						titleSpan.style.display = 'block';
					}
				});
	}
	titleInput.style.display = 'none';
	titleSpan.style.display = 'block';
}

function updateTitleEnter(e, titleInput) {
	e = e || window.event;
	if (e.keyCode == 13)
		updateTitle(titleInput);
	enterPress = false;
}

function showComment(pid, logPid, logId) {
	var commentDiv = document.getElementById('commentDiv');
	if (commentDiv)
		commentDiv.parentNode.removeChild(commentDiv);
	commentDiv = document.createElement('div');
	commentDiv.id = 'commentDiv';
	commentDiv.style.padding = '0 0 0 26px';
	var toLog = document.getElementById('sign_' + logId);
	var ne = getNextElement(toLog.nextSibling);
	if (!document.all) {
		var clearBoth = document.createElement('div');
		clearBoth.style.clear = 'both';
		if (!ne)
			toLog.parentNode.appendChild(clearBoth);
		else
			ne.parentNode.insertBefore(clearBoth, ne);

	}
	if (!ne)
		toLog.parentNode.appendChild(commentDiv);
	else
		ne.parentNode.insertBefore(commentDiv, ne);
	var logContent = document.getElementById('logContent_' + logId).innerHTML;
	var cw = new CommentWidget(pid, 33, logPid, logId, logContent, "");
	cw.drawCommentDiv();
	cw.showCommentList();
	var countEle = document.getElementById('log_count_' + logId);
	CommentWidget.prototype.afterCommentAdded = function(data) {
		var count = parseInt(countEle.innerHTML) + 1;
		countEle.innerHTML = count;
		var rid = data.comment['receiverpId'];
		var cid = data.comment['pid'];
		var cname = encodeURIComponent(data.comment['author']);
		var content = encodeURIComponent(data.comment['content']);
		var param = 'rid=' + rid + '&cid=' + cid + '&cname=' + cname
				+ '&content=' + content;
		new Ajax.Request('../log.do?method=sendCommentMsg&' + param, {
					method : 'get',
					parameters : param,
					onComplete : function(response) {
					}
				});
	};
	CommentWidget.prototype.afterCommentDeleted = function(data) {
		var count = parseInt(countEle.innerHTML) - 1;
		if (count < 0)
			count = 0;
		countEle.innerHTML = count;
	}
}
