/* * imgPreview jQuery plugin * Copyright (c) 2009 James Padolsey * j@qd9.co.uk | http://james.padolsey.com * Dual licensed under MIT and GPL. * Updated: 09/02/09 * @author James Padolsey * @edit Mpro * @version 0.22 */(function($){        $.expr[':'].linkingToImage = function(elem, index, match){        // This will return true if the specified attribute contains a valid link to an image:        return !! ($(elem).attr(match[3]) && $(elem).attr(match[3]).match(/\.(gif|jpe?g|png|bmp)$/i));    };        $.fn.imgPreview = function(userDefinedSettings){                var s = $.extend({                        /* DEFAULTS */                        // CSS to be applied to image:            imgCSS: {},            // Position X            positionX: 'right', // left or right			// Position Y            positionY: 'bottom', // top or middle            // Distance between cursor and preview:            distanceFromCursor: {x:10, y:10},            // Boolean, whether or not to preload images:            preloadImages: true,            // Callback: run when link is hovered: container is shown:            onShow: function(){},            // Callback: container is hidden:            onHide: function(){},            // Callback: Run when image within container has loaded:            onLoad: function(){},            // ID to give to container (for CSS styling):            containerID: 'imgPreviewContainer',            // Class to be given to container while image is loading:            containerLoadingClass: 'loading',            // Prefix (if using thumbnails), e.g. 'thumb_'            thumbPrefix: '',            // Where to retrieve the image from:            srcAttr: 'href'                    }, userDefinedSettings),                $container = $('<div/>').attr('id', s.containerID)                        .append('<img/>').hide()                        .css('position','absolute')                        .appendTo('body'),                    $img = $('img', $container).css(s.imgCSS),                // Get all valid elements (linking to images / ATTR with image link):        $collection = this.filter(':linkingToImage(' + s.srcAttr + ')');				// Check types		if($.inArray(s.positionX, new Array('left', 'right')) == -1)			s.positionX = 'right';		if($.inArray(s.positionY, new Array('top', 'bottom')) == -1)			s.positionY = 'bottom';                // Re-usable means to add prefix (from setting):        function addPrefix(src) {            return src.replace(/(\/?)([^\/]+)$/,'$1' + s.thumbPrefix + '$2');        }                if (s.preloadImages) {            (function(i){				if(typeof($collection[i]) === 'object'){					var tempIMG = new Image(),						callee = arguments.callee;					tempIMG.src = addPrefix($($collection[i]).attr(s.srcAttr));					tempIMG.onload = function(){						$collection[i + 1] && callee(i + 1);					};				}            })(0);        }        		var mousemove_e;		var position_refresh = function(e){						if(!e && !mousemove_e)				return false;			else if(!e)				e = mousemove_e;			else				mousemove_e = e;						var top = 0, left = 0;						if(s.positionX == "left")				left = e.pageX - $container.width() - s.distanceFromCursor.x			else				left = e.pageX + s.distanceFromCursor.x							if(s.positionY == "top")				top = e.pageY - $container.height() - s.distanceFromCursor.y			else				top = e.pageY + s.distanceFromCursor.y						$container.css({				left: left + 'px',				top: top + 'px'			});					}        $collection            .mousemove(position_refresh)            .hover(function(){                                var link = this;                $container                    .addClass(s.containerLoadingClass)                    .css({						left: -1000 + 'px',						top: -1000 + 'px'					})                    .show();					position_refresh();                $img                    .load(function(){                        $container.removeClass(s.containerLoadingClass);                        $img.show();						position_refresh();                        s.onLoad.call($img[0], link);                    })                    .attr( 'src' , addPrefix($(link).attr(s.srcAttr)) );                s.onShow.call($container[0], link);                            }, function(){                                $container.hide();                $img.unbind('load').attr('src','').hide();                s.onHide.call($container[0], this);                            });                // Return full selection, not $collection!        return this;            };    })(jQuery);
