var current=false;

var cheezburger =
{
	cx:0,
	cy:0,

	mouseX:function(evt) {
		if (evt.pageX) {
			return evt.pageX;
		} else if (evt.clientX) {
			return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft :document.body.scrollLeft);
		} else return null;
	},

	mouseY:function(evt) {
		if (evt.pageY) {
			return evt.pageY;
		} else if (evt.clientY) {
			return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop :  document.body.scrollTop);
		} else return null;
	},

	onMouseMove:function(evt) {
		this.cx = this.mouseX(evt);
		this.cy = this.mouseY(evt);

		if (current) {
			var top = this.cy + 15;
			$('preview_window').style.top = top + "px";
		}
	},

	hideLolCat:function(id, fullsize) {
		if (current == fullsize) {
			$('preview_img').src = "/images/spaceball.gif";
			$('preview_window').style.display = "none";
			current = false;
		}
	},

	previewLolCat:function(id, fullsize) {
		if (current == fullsize) {
			// already loading..
			return;
		}

		var offsets = Position.positionedOffset($(id));
		var left = offsets[0];
		var top = offsets[1]; 	
		top = this.cy + 15;
	
		$('preview_img').style.display = "none";
		$('preview_loader').style.display = "block";
	
		$('preview_window').style.top = top + "px";
		$('preview_window').style.left = left + "px";
		$('preview_window').style.display = "block";
       	
		var image = new Image();
		image.onload = function()
		{
			if (current == this.src)
			{
				// make sure we need to show this image after all (user might move the cursor over several thumbs at once
				var w = this.width;
				var h = this.height;
	       	
				var offsets = Position.positionedOffset($(id));
				var content = Position.cumulativeOffset($('content'));
				var margin = offsets[0] - content[0];
				if (margin + w > 780)
				{
					// we won't overlap the right edge of $('content')
					var left = content[0] + 780 - w;
					$('preview_window').style.left = left + "px";
				}
	       	
				$('preview_loader').style.display = "none";
				$('preview_img').src = this.src;
				$('preview_img').style.display = "block";
			}
		}      	

		current = fullsize;
		image.src = fullsize;
	},

	load:function() {
		if (/MSIE/.test(navigator.userAgent)) {
			// Internet Explorer, u haz mai attenshun
			Event.observe(document, 'mousemove', this.onMouseMove.bindAsEventListener(this));
		} else {
			Event.observe(window, 'mousemove', this.onMouseMove.bindAsEventListener(this));
		}
	}
};

cheezburger.load();
