if(!this.JSON) {
	var JSON = {};
	JSON.parse = function(text) { return eval('('+text+')'); };
}

function subInfoCreateRequestObject() {
	return navigator.appName == "Microsoft Internet Explorer" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}

var subInfoReady = 1;
var subInfoHttp, subInfoMenu, subInfoData, subInfoButton;
var subInfoLastTime = 0;

function subInfoShow(e, obj) {
	subInfoButton = obj;

	// set returnvalue to false in case there's a link in the href (ie)
	e = e ? e : window.event;
	e.returnValue = false; 

	// prevent the click from bubbling up to document because that will immediately hide the menu
	e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; 
	
	if(subInfoMenu) {
		// exit if it returns false
		if(!subInfoMenu.onTriggerClick()) {
			return false;
		}
	}
	
	// don't query the server more than once every 3 seconds
	var curtime = new Date().getTime();
	if(typeof subInfoData != 'undefined' && (curtime - subInfoLastTime) < 3000) {
		subInfoProcess(subInfoData);
		return false;
	}

	if(!subInfoHttp) {
		subInfoHttp = subInfoCreateRequestObject();
	}

	if(subInfoReady) {
		subInfoReady = 0; // prevent overlapping queries
		subInfoHttp.open('GET', '/forum/subinfo/main.php');
		subInfoHttp.onreadystatechange = subInfoHandleResponse;
		subInfoHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		subInfoHttp.send(null);
	}

	return false;
}

function subInfoBuildPageDots(threadid, totalpages) {
	if(totalpages < 2) {
		return '';
	}

	var maxpages = 3;

	var ret = '(';
	for(var x = 1; x <= totalpages && x <= maxpages; x++) {
		if(x > 1) {
			ret += ' ';
		}
		ret += '<a href="/forum/showthread.php?t='+threadid+'&page='+x+'">'+x+'</a>';
	}
	if(totalpages > x || (x == maxpages+1 && totalpages == x)) {
		ret += ' ... <a href="/forum/showthread.php?t='+threadid+'&page='+totalpages+'">Last Page</a>';
	}
	ret += ') ';
	return ret;
}

function subInfoHandleResponse() {
	if(subInfoHttp.readyState == 4) {
		subInfoData = subInfoHttp.responseText; // cache the response
		if(subInfoData.length) {
			subInfoLastTime = new Date().getTime(); // store the last query timestamp
			subInfoProcess(subInfoData);
		}
		subInfoReady = 1;
	}
}

function subInfoProcess(ajaxdata) {
	if(!ajaxdata.length) {
		return;
	}

	if(!subInfoMenu) {
		subInfoMenu = new aMenuObj();
		subInfoMenu.Init('subinfotarget', 'subinfomenu');
	}
	subInfoMenu.Clear();
	
	// if there's something to show
	if(ajaxdata != 0) {
		var data = JSON.parse(ajaxdata);
		var threads = data.threads;
		var oItem;
		var bNew, pageDots;
		var firstclass;
		for(var x = 0; x < threads.length; x++) {
			if(threads[x].totalpages == "") {
				threads[x].totalpages = "1";
			}
			bNew = /new/.test(threads[x].statusicon);
			pageDots = subInfoBuildPageDots(threads[x].threadid, parseInt(threads[x].totalpages));

			// thread title & lastposter are already htmlified

			oItem = document.createElement("li");
			firstclass = (x == 0 ? ' amenuitemfirst' : '');
			oItem.className = "amenuitem"+firstclass;
			oItem.onmouseup = function(url) {
				return function(e) {
					var e = e ? e : window.event;
					if(!(e.shiftKey || e.ctrlKey || e.metaKey) && getMouseButton(e) == 'LEFT')
						window.location.href = url;
				};
			}('/forum/showpost.php?p='+threads[x].lastpostid);
			// first line
			oItem.innerHTML += '<a href="/forum/showpost.php?p='+threads[x].lastpostid+'"><img width="12" height="12" alt="" border="0" src="/forum/images/buttons/lastpost.gif" /></a> ';
			oItem.innerHTML += '<a class="subinfothreadtitle" href="/forum/showthread.php?t='+threads[x].threadid+'">'+(bNew ? '<b>' : '')+threads[x].title+(bNew ? '</b>' : '')+'</a>';
			oItem.innerHTML += '<p class="subinfospacer"></p>';
			// second line
			oItem.innerHTML += pageDots + '<span class="subinforeplies">' + threads[x].replies + ' ' + (threads[x].replies == '1' ? 'reply' : 'replies') + '</span>' + '<p class="subinfospacer"></p>';
			// third line
			oItem.innerHTML += threads[x].lastdate + ' ' + threads[x].lasttime + ' by ' + threads[x].lastposter;
			oItem.innerHTML += ' &nbsp;<a href="/forum/subscription.php?do=removesubscription&t='+threads[x].threadid+'">Unsubscribe</a>';
			subInfoMenu.Add(oItem);
		}
	}
	else {
		var oItem = document.createElement("li");
		oItem.innerHTML = 'You have no new thread subscriptions';
		subInfoMenu.Add(oItem);
	}

	// position it relative to the subinfotarget link
	var menuObj = subInfoMenu.getMenuObj();
	var targetPos = subInfoGetPos(subInfoButton);
	var targetWidth = subInfoButton.offsetWidth;
	var targetHeight = subInfoButton.offsetHeight;
	menuObj.style.left = targetPos[0]+0+'px';
	menuObj.style.top = targetPos[1]+(targetHeight+2)+'px';
	
	subInfoMenu.Show();
}

function subInfoGetPos(elm) {
	var _top = 0, _left = 0;
	if(elm.offsetParent) {
		_left = elm.offsetLeft;
		_top = elm.offsetTop;
		while(elm = elm.offsetParent) {
			_left += elm.offsetLeft;
			_top += elm.offsetTop;
		}
	}

	return Array(_left, _top);
}