﻿/*
 * Copyright (c) 2009 Fullhouse
 * Author: James Newell
 * Date Created: 08/12/09
 * Version: 1.0
 *
 * Allows for a basic image preview tooltip when hovering over a linked image 
 * thumbnail.
 *
 *
 * Usage:
 * 		<a href="pathToImagePreview.jpg" class="thumbnail"><img src="pathToImageThumbnail.jpg" alt="Image Description" /></a>
 *		<script type="text/javascript">
*			$(".thumbnail").preview();
 *		</script> 
 * Available Settings:
 *	    - previewContainerID (string, default = 'Preview'): 
 *          The CSS id for the HTML container the tooltip will appear in.
 *	    - allowClickThrough (boolean, default = false): 
 *          If true the user is allowed to follow the link to the large image in the <a> wrapper tag.
 *
 */

/// <reference path="jquery-1.2.6-vsdoc.js"/>
(function($) {
	$.fn.preview = function(settings) {
		var xOffset = 30;
		var yOffset = 10;
		
		// Define default values for the settings if not provided
		settings = $.extend({
		    previewEvent: "hover",
			previewContainerID: "Preview",
			allowClickThrough: false
		}, settings);
		
		// Standard plugin hook to bind all capabilities to selected elements
		return this.each(function () {
			$(this)[settings.previewEvent](
				function(e) {
					var caption = "";
					if (settings.includeCaption) {
						var c = (this.t != "") ? "<br/>" + this.t : "";
					}
					$("body").append("<div id='" + settings.previewContainerID + "'><img src='" + this.href + "' alt='" + this.title + "' /></div>");								 
					$("#" + settings.previewContainerID)
					    .hide()
						.css("position", "absolute")
						.css("top", (e.pageY - yOffset) + "px")
						.css("left",(e.pageX + xOffset) + "px")
						.fadeIn("normal");
					$(this).children("img").attr("alt", "");						
			    },
				function(){
					$(this).children("img").attr("alt", $("#" + settings.previewContainerID + " img").attr("alt"));	
					$("#" + settings.previewContainerID).remove();
			    }
			);
			$(this).mousemove(function(e) {
				$("#" + settings.previewContainerID)
					.css("top", (e.pageY - yOffset) + "px")
					.css("left",(e.pageX + xOffset) + "px");
			});
			$(this).click(function(e) {
				return false || this.settings.allowClickThrough;
			});			
		});
	};
})(jQuery);