/***************************************************************
*  Copyright notice
*
*  (c) 2009 Stephan Lucas <sl@not-only-pixel.de>
*  All rights reserved
*
***************************************************************/

/**
 * Class for image preview on the cursor
 * 
 * @param {String} preview	Image that should shown on loading
 */
TrailImage = function(preview)
{
	this.previewIMG = preview;

	this.trail_element = document.createElement('img');
	this.trail_element.id = "trailimg";
	this.trail_element.style.position = "absolute";
	this.trail_element.style.zIndex = "1000";
	
	this.hide();

	var body = document.getElementsByTagName("body")[0];
	body.appendChild(this.trail_element);
}
/**
 * Sets the preview image
 * 
 * @param {String} image
 */
TrailImage.prototype.set_image = function (image)
{
	this.trail_element.src = image;
}
/**
 * Display the preview image
 * 
 * @param {Object} target	The tumbnail object
 * @param {String} image	The URL to the image that should displayed on thumbnail mouseover
 */
TrailImage.prototype.show = function (target, image)
{
	target.trail_element = this.trail_element;
	target.trailObject = this;
	
	this.trail_element.style.visibility = "visible";
	this.set_image(image);

	/*** On mouse move over the thumbnail we set the position for the preview image ***/
	target.onmousemove = function (evt)
	{
		if (!evt) {
			evt = window.event;
		}
		
		var mousex, mousey;
		var left, top;
		var trail_width =this.trail_element.width;
		var trail_height = this.trail_element.height;
		

		mousex = evt.clientX;
		mousey = evt.clientY;

		/*** Calculate ***/
		// if there is enougth space between the current position of the mouse and the bottom of the page..
		if (getInnerHeight() - mousey > trail_height) {
			 // align the picture under the cursor
			top = mousey + getScrollHeight() + 15;
		}
		else {
			// align the picture above the cursor
			top = mousey + getScrollHeight() - 5 - trail_height; 
		}
		// if there is enougth space between the current position of the mouse and the left side of the page..
		if (getInnerWidth() - mousex > trail_width) {
			// align the picture right of the cursor
			left = mousex + getScrollWidth() + 15;
		}
		else {
			// align the picture left of the cursor
			left = mousex + getScrollWidth() - 10 - trail_width; 
		}
		/*** Apply positioning ***/
		this.trail_element.style.top = top + "px";
		this.trail_element.style.left = left + "px";
	}
	/*** If the cursor goes out the thumbnail we hide the preview image ***/
	target.onmouseout = function (evt) {
		this.trailObject.hide();
		this.onmousemove = null;
	}
}
/**
 * Hide the preview image i.e. if the cursor moves out the thumbnail
 */
TrailImage.prototype.hide = function ()
{
	this.trail_element.style.visibility = "hidden";
	this.set_image(this.previewIMG); 
}



 /**
  * DOMLIB
  */
	/**
	 * returns how width you scolled right
	 */
	function getScrollWidth() {
		if (self.pageYOffset) // (JS 1.1) Netscape 3, Firefox, Safari, Konqueror 3.3, Opera 5.12
			return self.pageXOffset;
		else if (document.documentElement && document.documentElement.scrollTop) // IE 6 Strict
			return document.documentElement.scrollLeft;
		else if (document.body) // IE 5
			return document.body.scrollLeft;
	}
	/**
	 * returns how much you scolled down
	 */
	function getScrollHeight() {
		if (self.pageYOffset) // (JS 1.1) Netscape 3, Firefox, Safari, Konqueror 3.3, Opera 5.12
			return self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop) // IE 6 Strict
			return document.documentElement.scrollTop;
		else if (document.body) // IE 5
			return document.body.scrollTop;
	}
	/**
	 * returns the viewable window width.
	 */
	function getInnerWidth() {
		if (self.innerHeight) // (JS 1.2) Netscape 4, Firefox, Safari, Konqueror 3.3, Opera 5.12
			return self.innerWidth;
		else if (document.documentElement && document.documentElement.clientHeight) // IE 6 Strict Mode
			return document.documentElement.clientWidth;
		else if (document.body) // IE 5
			return document.body.clientWidth;
	}
	/**
	 * returns the viewable window height.
	 */
	function getInnerHeight() {
		if (self.innerHeight) // (JS 1.2) Netscape 4, Firefox, Safari, Konqueror 3.3, Opera 5.12
			return self.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) // IE 6 Strict Mode
			return document.documentElement.clientHeight;
		else if (document.body) // IE 5
			return document.body.clientHeight;
	}


