//pass in an anchor tag with an href pointing to a photo you want to preview
//$("a.preview").filetip(); //pass true if you want preload

(function($) {
		  
	var get_filetype = function(filename) { //determine if this file is a photo
		//get filename extension
		var ext = filename.substr(filename.lastIndexOf('.')).toLowerCase();
		//create array of allowed extension types and check if the file is a photo
		var phototypes = [".jpeg", ".jpg", ".gif", ".png", ".bmp"];
		for (var i=0; i < 5; i++) {
			if (phototypes[i] == ext) {
				return true;
			}
		}
	};
	var tooltip_position = function(event) {
		var tpos_x = event.pageX - 5;
		var tpos_y = event.pageY + 20;
		$("div.tooltip").css({"top": tpos_y, "left": tpos_x});
	};
	var tooltip_show = function(event) {
		$("div.tooltip").remove();
		var filename = $(this).attr("href");
		var photo = get_filetype(filename);
		//if it is a photo, write the img tag
		if (photo) { //if it is a photo
			var preview = "<img src='" + filename + "'>";
		} else {//it is not a photo
			var preview = "Click to view File";
		}
		$("<div class='tooltip'>" + preview + "</div>").appendTo("body");
		tooltip_position(event);
	};
	var tooltip_hide = function() {
		$("div.tooltip").remove();
	};  
		  
	$.fn.filetip = function(preload) {
		//loop through the items
		this.each(function() {
			$this = $(this);
			if (preload) {//preload images	
				var filename = $(this).attr("href");
				var photo = get_filetype(filename);
				if (photo) {
					$("<img />").attr("src", filename);	
				}
			}
			
			$this.hover(tooltip_show, tooltip_hide).mousemove(tooltip_position);
		});
		// returns the jQuery object to allow for chainability.  
		return this;
	}

})(jQuery);