var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
var browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
	safari: /webkit/.test(userAgent),
	opera: /opera/.test(userAgent),
	msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),
	mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
};

var alreadyRunFlag=0; //flag to indicate whether target function has already been run
var onloadFnList = [];
var init = function (){
	alreadyRunFlag=1;
	var fn = onloadFnList;
	var bID = document.getElementsByTagName("body")[0].id;
	for (var i = 0, l = fn.length; i < l; i++)
		if ((bID != "" && fn[i].tag.indexOf(bID) != -1) || fn[i].tag == "*")
			fn[i].fn();
};

if (document.addEventListener){
	document.addEventListener("DOMContentLoaded", function(){
		init();
	}, false);
}else if (browser.safari){
	(function(){
		if (document.readyState == "loaded" || document.readyState == "complete")
			init();
		else
			setTimeout(arguments.callee, 50);
	})();
}else if (browser.msie){
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	var contentloadtag=document.getElementById("contentloadtag")
	contentloadtag.onreadystatechange=function(){
		if (this.readyState=="complete"){
			init();
		}
	}
}

window.onload=function(){
	setTimeout("if (!alreadyRunFlag){init();}", 0);
};var $ = function (sel){
	return document.getElementById(sel);
};

var ajax = function (method, url, data, cb){
	var xhr;
	if (window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else if (window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		return;
	}
	
	var qStr = data;
	if (typeof data == "object"){
		qStr = "";
		for (var x in data){
			qStr += encodeURIComponent(x) + "=" + encodeURIComponent(data[x]) + "&";
		}
		qStr = qStr.substr(0, qStr.length - 1);
	}
	
	xhr.onreadystatechange = function (){
		if (xhr.readyState==4){
			cb(xhr.responseText);
		}
	};
	
	xhr.open(method, url + "?" + qStr, true);
	xhr.send();
};onloadFnList.push({
	tag: "newPost replyPost",
	fn: function (){
		window.hkgImgCh = function (){
			ajax("GET", "index.php", {
				p: "gID"
			}, function (data){
				var nCls = (" " + $("uploading").className + " ").replace(" hide ", "");
				$("uploading").className = nCls.substr(1, nCls.length -1);
				eval("var a=" + data);
				$("transactionID").value = a[0];
				$("imgFm").submit();
			});
		};
		
		window.hkgUploaded = function (){
			if ($("transactionID").value != ""){
				var img = document.createElement("img");
				img.src="http://w3img.westkit.net/" + $("transactionID").value + ".img";
				img.onclick = function (){
					InsertText("[img]" + img.src + "[/img]", false);
				};
				
				$("gal").appendChild(img);
				$("imgFm").reset();
				$("uploading").className = $("uploading").className + " hide";
			}
		};
		
		window.post = function (){
			$("pFm").submit();
		};
	}
});

var hkgUploaded = function(){};function InsertText( text, splittable ) {
	var TextArea = document.getElementById("content");
	var l;
	if (TextArea) {
		TextArea.focus();
		if (splittable)
			l = text.split(/,/);
		else
			l = text
			
		if ((typeof TextArea.selectionStart) != 'undefined') {	// Mozilla
			var ti = TextArea.selectionEnd, ts = TextArea.selectionStart;	// Copied from the Glomerulus
			if (l instanceof Array) {
				if (ti != ts) {
					TextArea.value = TextArea.value.substring(0, ts) + l[0] + TextArea.value.substring(ts, ti) + l[2] + TextArea.value.substr(ti);
					TextArea.selectionStart = ts + l[0].length
					TextArea.selectionEnd = ti + l[2].length - 1;
				} else {
					TextArea.value = TextArea.value.substring(0, ts) + l[0] + l[1] + l[2] + TextArea.value.substr(ti);
					TextArea.selectionStart = ti + l[0].length;
					TextArea.selectionEnd = TextArea.selectionStart + l[1].length;
				}
			} else {
				TextArea.value = TextArea.value.substring(0, ts) + l + " " + TextArea.value.substr(ti);
				TextArea.selectionStart = TextArea.selectionEnd = ti + l.length + 1;
			}
		} else if (document.selection) {						// IE
			var r = document.selection.createRange();			// No Glomerulus here ;-(
			if (l instanceof Array) {
				if (r.text != "")
					r.text = l[0] + r.text + l[2];
				else
					r.text = l[0] + l[1] + l[2];
			} else
				r.text = l + " ";
			//r.select();										// Useless
		} else {												// Neither.
			TextArea.value += text + " ";
		}
		TextArea.focus();
	}
	//return false;
}

function InsertList() {
	var lll = "[list]\n[*]第一項[/*]\n[*]第二項[/*]\n[*]第三項[/*]\n[/list]";
	var TextArea = document.getElementById("content");
	if (TextArea) {
		TextArea.focus();
		if ((typeof TextArea.selectionStart) != 'undefined') {	// Mozilla
			var ti = TextArea.selectionEnd, ts = TextArea.selectionStart;
			if (ti != ts) {
				TextArea.value = TextArea.value.substring(0, ts) + "[*]" + TextArea.value.substring(ts, ti) + "[/*]" + TextArea.value.substr(ti);
				TextArea.selectionEnd = ti + 7;
			} else {
				TextArea.value = TextArea.value.substring(0, ts) + lll + TextArea.value.substr(ti);
				TextArea.selectionEnd = ti + lll.length;
			}
			TextArea.selectionStart = ts;
		} else if (document.selection) {						// IE
			var r = document.selection.createRange();
			if (r.text != "")
				r.text = "[*]" + r.text + "[/*]";
			else
				r.text = lll;
		} else {												// Neither.
			TextArea.value += lll + " ";
		}
		TextArea.focus();
	}
}
onloadFnList.push({
	tag: "viewPost",
	fn: function (){
		var links = document.getElementsByTagName("a");
		for (var i = 0, l = links.length; i < l; i++){
			var patt = new RegExp("^http://([a-z]*.|)youtube.com");
			if (patt.test(links[i].href)){
				var id = links[i].href.match(/(v=|v\/)([a-zA-Z0-9_-]+)&?/i);
				if (id != null){
					id = id[2];
					var newDiv = document.createElement("div");
					newDiv.innerHTML = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+id+"&hl=zh_TW&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+id+"&hl=zh_TW&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>";
					links[i].parentNode.insertBefore(newDiv, links[i]);
				}
			}
		}
	}
});