/*  main variable and function used to select&replace avatarImg */
var avatarData = "[avatar:75]";
var cmtList = document.getElementById("comments");
var cmtForm = document.forms[0];
var cmtTextarea = cmtForm.content;

if(cmtList) {
	cmtList.innerHTML = cmtList.innerHTML.replace(/\[avatar:(\d{2})\]/g,'<img class="userAvatar" src="http://files.blogbus.com/suga.blogbus.com/files/120625085$1.jpg">');
}
cmtForm.onsubmit = replaced;

document.writeln(" <div id=\"avatarSelection\">");
document.writeln(" 	<select name=\"selectYourAvatar\" onchange=\"changeAvatar(this.options[this.options.selectedIndex].value)\"> ");
document.writeln(" 		<option value=\"75\">头像1</option>");
document.writeln(" 		<option value=\"77\">头像2</option>");
document.writeln(" 		<option value=\"79\">头像3</option>");
document.writeln(" 		<option value=\"71\">头像4</option>");
document.writeln(" 		<option value=\"73\">头像5</option>");
document.writeln(" 		<option value=\"74\">头像6</option>");
document.writeln(" 		<option value=\"78\">头像7</option>");
document.writeln(" 		<option value=\"70\">头像8</option>");
document.writeln(" 		<option value=\"72\">头像9</option>");
document.writeln(" 		<option value=\"76\">头像10</option>");
document.writeln(" 	</select>");
document.writeln(" 	<img id=\"avatar\" src=\"http://files.blogbus.com/suga.blogbus.com/files/12062508575.jpg\" />");
document.writeln(" </div>");

function changeAvatar(value) {
	var avatarImg = document.getElementById("avatar");
	avatarImg.src = 'http://files.blogbus.com/suga.blogbus.com/files/120625085'+value+'.jpg';
	avatarData = '[avatar:'+value+']';
}
function replaced() {
	if(check_cmt(cmtForm))  { 
		cmtTextarea.value = avatarData+cmtTextarea.value; 
	}
   else { return false; }
}

/*insert avatarSelectDiv above h2 */
var commentForm = document.getElementById("commentForm");
var cmtH2 = commentForm.getElementsByTagName("h2")[0];

var avatarDiv = document.createElement("div");
avatarDiv.setAttribute("id","avatarSelectDiv");

cmtH2.appendChild(avatarDiv);
document.getElementById("avatarSelectDiv").innerHTML = document.getElementById("avatarSelection").innerHTML;

/*  try to replace sidebar innerHTML */
var cmtList = document.getElementById("nComments");
if(cmtList) {
	cmtList.innerHTML = cmtList.innerHTML.replace(/\[avatar:\d{2}\]/g,"");
}

/* try to remember which avatar you selected */
	var avatarImgSelection = document.getElementsByName("selectYourAvatar");
	var avatarImgSelectionOptions = avatarImgSelection[0].options;
	var new_date = new Date(); 
	var avatar_expire_date = new Date(); 
	avatar_expire_date.setTime(parseInt(new_date.getTime()) + 1000 * 86400 * 365);
	
	avatarImgSelection[0].onchange = selectedIndexSave;

	function selectedIndexSave() {
		document.cookie = "avatarImgSelectionIndex="+avatarImgSelectionOptions.selectedIndex+"; expires="+avatar_expire_date.toGMTString()+";path=/";
	//alert(document.cookie);
	changeAvatar(avatarImgSelectionOptions[avatarImgSelectionOptions.selectedIndex].value)
	}

window.onload = function() {
	cookieArray = document.cookie.split(";");
	//alert(cookieArray.length);
	for (var i=0;i<cookieArray.length;i++) {
		if(cookieArray[i].indexOf("avatarImgSelectionIndex")!=-1) {
			avartarImgIndex = cookieArray[i].substring(cookieArray[i].indexOf("=")+1,cookieArray[i].length);
			avatarImgSelectionOptions.selectedIndex = parseInt(avartarImgIndex);
changeAvatar(avatarImgSelectionOptions[avatarImgSelectionOptions.selectedIndex].value)
			//alert(avartarImgIndex);
		}
	}
}