function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

function getOffsetPosition(obj) {
	var posX = 0;
	var posY = 0;
	while (obj.offsetParent!=null) {
		posX += obj.offsetLeft;
		posY += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return [posX, posY];
}

var thumbnailsOffsetSide = 5;
var thumbnailsOffsetTop = -20;
function prepareThumbnails(container, activator, thumbnail, oS, oT) {
	if (container==null || activator==null || thumbnail==null || container==undefined || activator==undefined || thumbnail==undefined) return false;
	if (oS!=null && oS!=undefined) thumbnailsOffsetSide = oS;
	if (oT!=null && oT!=undefined) thumbnailsOffsetTop = oT;
	var containers = cssQuery(container);
	for (var i=0; i<containers.length; i++) {
		var activatorObj = cssQuery(activator,containers[i]);
		if (activatorObj.length==0) continue;
		activatorObj = activatorObj[0];
		activatorObj.container = containers[i];
		activatorObj.thumbnail = cssQuery(thumbnail,containers[i]);
		if (activatorObj.thumbnail.length==0) continue;
		else activatorObj.thumbnail = activatorObj.thumbnail[0];
		activatorObj.thumbnail.container = containers[i];
		activatorObj.thumbnail.show = function() {
			var windowSize = getWindowSize();
			var scrolled = getScrollXY();
			this.style.left = (this.container.offsetWidth + thumbnailsOffsetSide) + "px";
			this.style.top = thumbnailsOffsetTop + "px";
			this.style.display = "block";
			var offsetPosition = getOffsetPosition(this);
			if (offsetPosition[0] + this.offsetWidth > windowSize[0] - 20 + scrolled[0]) this.style.left = -(this.offsetWidth + thumbnailsOffsetSide) + "px";
			if (offsetPosition[1] + this.offsetHeight > windowSize[1] - 20 + scrolled[1]) this.style.top = -(this.offsetHeight + offsetPosition[1] - windowSize[1] + 20 - scrolled[1]) + "px";
		}
		activatorObj.thumbnail.hide = function() {
			this.style.display = "none";
			this.container.style.zIndex = 1;
		}
		activatorObj.onmouseover = function() {
			clearTimeout(this.hideTimeout);
			this.thumbnail.show();
			this.container.style.zIndex = 2;
		}
		activatorObj.onmouseout = function() {
			var o = this;
			this.hideTimeout = setTimeout(function(){o.thumbnail.hide();},20);
		}
	}
}