var baseURL = 'http://www.mixfog.com/blog/';

function getById(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers) {
		return document.layers[id];
	} else {
		return null;
	}
}
//*************************************************************AJAX 初始化一个xmlhttp对象
function InitAjax()
{
	var ajax=false;
	try {
		ajax = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			ajax = false;
		}
	}
	if (!ajax && typeof XMLHttpRequest!='undefined') {
		ajax = new XMLHttpRequest();
	}
	return ajax;
}
//**********************************************************读取评论
function loadComments(id)
{
	var url = baseURL+'include/comments.php?action=list&entryid='+id;
	var commLoading = getById('commloading');
	var comments = getById('comments');
	var ajax = InitAjax();
	ajax.open("GET", url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			comments.innerHTML = ajax.responseText;
			commLoading.style.display = 'none';

			var targetComment = getById(location.hash.slice(1));
			if (targetComment) {
				targetComment.style.backgroundColor = '#FFFFE7';
				window.scrollTo(0,targetComment.offsetTop);
			}
		}
	}
	ajax.send(null);
}
function getNewCode() {
	getById('seccode-img').src = baseURL+'include/validation_code.php?code='+Math.random();
}
//**********************************************************提交评论
function postComment() {
	var url = baseURL+'include/comments.php?action=post';
	var f = getById('postcommentform');
	
	var postinfo = getById('postinfo');
	if (!postinfo) {
		postinfo = document.createElement('p');
		postinfo.id = 'postinfo';
		f.parentNode.insertBefore(postinfo,f);
	}
	postinfo.innerHTML = '<img src='+baseURL+'images/ajax_loading.gif" alt="Loaging..." />';
	
	var siteid = f.siteid.value;
	var entryid = f.entryid.value;
	var name = f.name.value;
	var email = f.email.value;
	var uri = f.uri.value;
	var content = f.message.value;
	var seccode = f.seccode.value;
	
	if (name == '') {
		postinfo.innerHTML = '您没有填写姓名';
		return false;
	}
	if (name.length > '30') {
		postinfo.innerHTML = '姓名最好缩减一下';
		return false;
	}
	if (email == '') {
		postinfo.innerHTML = '您没有填写邮箱，您的邮箱地址并不会显示给其他浏览者';
		return false;
	}
	var	patnEmail = /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/;
	if (!patnEmail.test(email)) {
		postinfo.innerHTML = '邮箱格式错误';
		return false;
	}
	if (content == '') {
		postinfo.innerHTML = '您没有填写评论内容';
		return false;
	}
	if (seccode == '') {
		postinfo.innerHTML = '您没有填写验证码';
		return false;
	}
	if (seccode.length != '4') {
		postinfo.innerHTML = '验证码错误';
		return false;
	}
	if (content.length > '2000') {
		postinfo.innerHTML = '评论内容最好缩减一下';
		return false;
	}
	var patnUrl = /http:\/\/[A-Za-z0-9\.-]{2,}\.[A-Za-z]{2}/;
	if (uri != '' && !patnUrl.test(uri)) {
		postinfo.innerHTML = '网址填写错误';
		return false;
	}
	
	var postStr = 'siteid='+ siteid +'&entryid='+ entryid +'&name='+ name +'&email='+ email +'&uri='+ uri +'&content='+ content +'&seccode='+ seccode;
	var ajax = InitAjax();
	ajax.open("POST", url, true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	ajax.send(postStr);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			postinfo.innerHTML = ajax.responseText;
			if (ajax.responseText == '您的评论已经提交成功，稍候会显示在该页面中') {
				f.getElementsByTagName('button')[0].onclick = function() { alert('请勿重复提交'); }
			}
		}
	}
	return false;
}
//**********************************************************读取评论数目
function readNum (entries) {
	var entries_array = entries.split(',');
	var nums = new Array;
	var url = baseURL+'include/comments.php?action=readnum&entries='+entries;
	var ajax = InitAjax();
	ajax.open("GET", url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			nums = ajax.responseText.split(',');
			for (i=0; i<entries_array.length-1; i++) {
				getById('commnum-'+entries_array[i]).innerHTML = nums[i*2];
				//getById('tbnum-'+entries_array[i]).innerHTML = nums[i*2+1];
			}
		}
	}
	ajax.send(null);
}
//**********************************************************Add To
function trackback(entryid) {
	prompt('本页Trackback地址',baseURL+'include/trackback.php?id='+entryid)
}
//**********************************************************最新文章
function recentEntries(num,siteid) {
	var url = baseURL+'include/entries.php?action=recent&num='+num+'&siteid='+siteid;
	var recentEntriesList = getById('recententries').getElementsByTagName('ul')[0];
	var ajax = InitAjax();
	ajax.open("GET", url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			recentEntriesList.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}
//**********************************************************最新评论
function recentComments(num,siteid) {
	var url = baseURL+'include/comments.php?action=recent&num='+num+'&siteid='+siteid;
	var recentEntriesList = getById('recentcomments').getElementsByTagName('ul')[0];
	var ajax = InitAjax();
	ajax.open("GET", url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			recentEntriesList.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}

//**********************************************************页面名称
function getPageTitle() {
	var pathName = location.pathname;
	if (pathName.indexOf('/labels/')!=-1) {
		var fileName = pathName.split('/labels/');
		var tagName = fileName[1].split('.');
		document.write ('<h1>TAG: '+ tagName[0] +'</h1>');
	} else if (pathName.indexOf('/archives/')!=-1) {
		var pageTitle = document.title.split(':');
		document.write ('<h1>存档: '+ pageTitle[1] +'</h1>');
	}
}
//**********************************************************存档
function scrollToggle(id,maxHeight){
	var obj=document.getElementById(id);
	var h = parseInt(obj.offsetHeight);
	var act;
	if ( maxHeight > 0 && (maxHeight - h) > 2 ) {
		var addspeed = Math.floor((maxHeight-h)/3);
		obj.style.height = (h + addspeed)+"px";
		clearTimeout(act);
		act = setTimeout( function(){ scrollToggle(id,maxHeight)}, 1 );
	} else if ( maxHeight == 0 && h > 0 ) {
		if ( h <= 2 ) {
			clearTimeout(act);
			obj.style.height = "0px";
		} else { 
			var addspeed = Math.floor(h/3);
			obj.style.height = (h - addspeed)+"px";
			clearTimeout(act);
			act = setTimeout( function(){ scrollToggle(id,0)}, 1);
		}
	}
}
function showArchive() {
	if ( document.getElementById('archivelink') ) {
		var archiveLink = document.getElementById('archivelink');
		var maxHeight = document.getElementById('archivelist').clientHeight;
		if ( archiveLink.style.height == '' || archiveLink.style.height == '0px' ) {
			scrollToggle('archivelink',maxHeight);
		} else {
			scrollToggle('archivelink',0);
		}
	} else {
		var archiveLink = document.createElement('div');
		archiveLink.id = 'archivelink';
		archiveLink.className = 'clearfix';
		archiveLink.appendChild(document.getElementById('archivelist'));
		document.getElementById('wrap').insertBefore(archiveLink, document.getElementById('content'));
		showArchive();
	}
}