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

function galGetPos(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);
}

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

var galhttp = galCreateRequestObject();

function galShow(userid, objectid, posid) {
	galShowInit();
	if(galShowRan[objectid]) {
		if(galShowTotalImages[objectid]) {
			galShowDisplay(objectid);
		}
		return;
	}

	galShowRan[objectid] = true;
	galhttp.open('GET', '/forum/galshow/index.php?userid='+userid+'&objectid='+objectid+'&posid='+posid);
	galhttp.onreadystatechange = galShowHandleResponse;
	galhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	galhttp.send(null);
}

var galShowIsInitialized = false;

function galShowInit() {
	if(galShowIsInitialized) {
		return;
	}
	galShowTotalImages = [];
	galShowCurIndex = [];
	galShowImageList = [];
	galShowUser = [];
	galShowPosId = [];
	galShowTimerId = [];
	galShowRan = [];
	galShowIsInitialized = true;
}

function galShowHandleResponse() {
	if(galhttp.readyState == 4) {
		var response = galhttp.responseText;
		if(response.length && response != '0') {
			// eval the json
			var data = JSON.parse(response);
			galShowTotalImages[data.objectid] = data.images.length;
			galShowCurIndex[data.objectid] = 0;
			galShowImageList[data.objectid] = data.images.slice();
			galShowUser[data.objectid] = data.user;
			galShowPosId[data.objectid] = data.posid;
			galShowInitObject(data.objectid);
		}
	}
}

function galShowInitObject(objectid) {
	var galobj = document.getElementById(objectid);
	galobj.innerHTML = '\
	<div class="title">'+galShowUser[objectid]+'</div>\
	<div class="imagecont">\
		<div class="prev" onclick="galShowPrev(\''+objectid+'\')"><<</div>\
		<div class="image">\
			<div class="imagetitle">'+galShowImageList[objectid][galShowCurIndex[objectid]].title+'</div>\
			<a target="_blank" href="/gallery/showphoto.php/photo/'+galShowImageList[objectid][galShowCurIndex[objectid]].id+'/cat/'+galShowImageList[objectid][galShowCurIndex[objectid]].cat+'"><img src="/gallery/data/'+galShowImageList[objectid][galShowCurIndex[objectid]].cat+'/thumbs/'+galShowImageList[objectid][galShowCurIndex[objectid]].image+'" alt="" /></a>\
			<div class="state">'+(galShowCurIndex[objectid]+1)+'/'+galShowTotalImages[objectid]+'</div>\
		</div>\
		<div class="next" onclick="galShowNext(\''+objectid+'\')">>></div>\
	</div>\
';

	var posobj = document.getElementById(galShowPosId[objectid]);
	var pos = galGetPos(posobj);
	var width = posobj.offsetWidth;
	var height = posobj.offsetHeight;
	galobj.style.left = pos[0]+(width/2)+'px';
	galobj.style.top = pos[1]+(height/2)+'px';

	galShowTimerId[objectid] = 0;
	galobj.onmouseout = function(){galShowStartTimer(objectid);};
	galobj.onmouseover = function(){galShowKillTimer(objectid);};

	galShowDisplay(objectid);
}

function galShowUpdateObject(objectid) {
	var galobj = document.getElementById(objectid);
	var elm = galGetElementsByClass('image', galobj, 'div');
	elm[0].innerHTML = '\
			<div class="imagetitle">'+galShowImageList[objectid][galShowCurIndex[objectid]].title+'</div>\
			<a target="_blank" href="/gallery/showphoto.php/photo/'+galShowImageList[objectid][galShowCurIndex[objectid]].id+'/cat/'+galShowImageList[objectid][galShowCurIndex[objectid]].cat+'"><img src="/gallery/data/'+galShowImageList[objectid][galShowCurIndex[objectid]].cat+'/thumbs/'+galShowImageList[objectid][galShowCurIndex[objectid]].image+'" alt="" /></a>\
			<div class="state">'+(galShowCurIndex[objectid]+1)+'/'+galShowTotalImages[objectid]+'</div>\
';
}

function galShowStartTimer(objectid) {
	if(!galShowTimerId[objectid]) {
		galShowTimerId[objectid] = setTimeout("galShowHide('"+objectid+"')", 2000);
	}
}

function galShowKillTimer(objectid) {
	if(galShowTimerId[objectid]) {
		clearTimeout(galShowTimerId[objectid]);
		galShowTimerId[objectid] = 0;
	}
}

function galShowDisplay(objectid) {
	document.getElementById(objectid).style.display = 'block';
	// set the timer off the bat since the mouse won't initially be over the object
	galShowStartTimer(objectid);
}

function galShowHide(objectid) {
	document.getElementById(objectid).style.display = 'none';
	galShowKillTimer(objectid);
}

function galShowPrev(objectid) {
	galShowCurIndex[objectid]--;
	if(galShowCurIndex[objectid] < 0) {
		galShowCurIndex[objectid] = galShowTotalImages[objectid] - 1;
	}
	galShowUpdateObject(objectid);
}

function galShowNext(objectid) {
	galShowCurIndex[objectid]++;
	if(galShowCurIndex[objectid] >=  galShowTotalImages[objectid]) {
		galShowCurIndex[objectid] = 0;
	}
	galShowUpdateObject(objectid);
}

function galGetElementsByClass(searchClass, node, tag) {
	var classElements = [];
	if(!node) { node = document; }
	if(!tag) { tag = '*';}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for(var i = 0, j = 0; i < elsLen; i++) {
		if(pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}