/**
	引用： http://www.xiaoxiaozi.com/2009/06/25/962/
*/

function switchTab(showPanels, hidePanels, activeTab, activeClass, fadeTab, fadeClass) {
	
	//debugger;  //for firebug debug.
	//看其原來方法，無非就是給他換個類名
	//用jQuery的方法，先把原類名刪了，再增加一個新類名，搞定
	//$("#"+activeTab).removeClass().addClass(activeClass);
	//$("#"+fadeTab).removeClass().addClass(fadeClass);
	//一定要將 "$" 改為 jQuery，否則根本無法呼叫 jQuery 函式。
	jQuery("#"+activeTab).removeClass().addClass(activeClass);
	jQuery("#"+fadeTab).removeClass().addClass(fadeClass);

	var panel, panelList;
	panelList = showPanels.split(',');
	for (var i = 0; i < panelList.length; i++) {
		var panel = panelList[i];
		//我感覺這個顯示的效果會比display block更人性一些吧？
		jQuery("#"+panel).fadeIn(250);
	}
	panelList = hidePanels.split(',');
	for (var i = 0; i < panelList.length; i++) {
		panel = panelList[i];
		//同樣，隱藏的時候也用這個特效好了，我比較喜歡亂改
		jQuery("#"+panel).fadeOut(250);
	}
}

function reply(authorId, commentId, commentBox) {
	//通過元素id，來取這個作者名
	//在jQuery中html()就相當於innerHtml
	var author = jQuery('#'+authorId).html();
	//裡面內容不變，正則學的真好啊。。
	var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + ' </a> \n';

	appendReply(insertStr, commentBox);
}

function appendReply(insertStr, commentBox) {
	//判斷這個對象存在，並且它是textarea類型
	//就是下面的那個評論框
	if(jQuery("#"+commentBox) && jQuery("#"+commentBox).attr('type') == 'textarea') {
		//借助私有變量field
		field = jQuery("#"+commentBox);

	} else {
		//沒有評論框，彈窗，告訴你丫的這個評論框居然不存在
		//敢晃點我就彈窗，哼
		alert("The comment box does not exist!");
		return false;
	}

	if (field.val().indexOf(insertStr) > -1) {
		//判斷你要發的內容是否已經寫一遍了，即你點了一個人兩次回復
		alert("You've already appended this reply!");
		return false;
	}

	//還是正則，我不解釋，不懂
	if (field.val().replace(/\s|\t|\n/g, "") == '') {
		field.val(insertStr);
	} else {
		//同樣，是把內容換成什麼什麼亂七八糟的
		replay_val  = field.val().replace(/[\n]*$/g, "") + '\n\n' + insertStr;
		field.val(replay_val);
	}
	//這裡沒說的了，一定要使評論框獲得焦點，嗯。
	field.focus();
}

function quote(authorId, commentId, commentBodyId, commentBox) {
	//同樣，獲取其作者innerhtml內容
	var author = jQuery("#"+authorId).html();
	//同樣，獲取其innerhtml內容
	var comment = jQuery("#"+commentBodyId).html();

	//加上了些blockquote 標籤，給你引用的內容包起來
	var insertStr = '<blockquote cite="#' + commentBodyId + '">';
	insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
	insertStr += comment.replace(/\t/g, "");
	insertStr += '</blockquote>\n';

	insertQuote(insertStr, commentBox);
}

function insertQuote(insertStr, commentBox) {
	//同樣，得到評論框，和上面評論的代碼相同，不解釋
	if(jQuery("#"+commentBox) && jQuery("#"+commentBox).attr('type') == 'textarea') {
		field = jQuery("#"+commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if(document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = insertStr;
		field.focus();

	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var cursorPos = startPos;
		//這裡面仍然借助私有變量(臨時變量)replay_value
		replay_value = field.val().substring(0, startPos)
					+ insertStr
					+ field.val().substring(endPos, field.value.length);
		//因為在這裡我要給field也就是評論框賦值
		field.value(replay_value);
		cursorPos += insertStr.length;
		//獲取焦點
		field.focus();
		field.selectionStart = cursorPos;
		field.selectionEnd = cursorPos;

	} else {
		//還是這個臨時變量，重新賦值啊，哈
		replay_value = field.val();
		field.val(replay_value+insertStr);
		field.focus();
	}
}
