/*
 * 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
 * @version 0.22
 */

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function findCoordX(obj)
{
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function findCoordY(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}

(function(c)
{
	c.expr[':'].linkingToImage=function(a,g,e) {
		return!!(c(a).attr(e[3])&&c(a).attr(e[3]).match(/\.(gif|jpe?g|png|bmp)$/i))
	};
	c.fn.imgPreview=function(j){
		var b=c.extend({
				imgCSS:{},
				distanceFromCursor:{top:10,left:10},
				preloadImages:true,
				onShow:function(){},
				onHide:function(){},
				onLoad:function(){},
				containerID:'imgPreviewContainer',
				containerLoadingClass:'loading',
				thumbPrefix:'',srcAttr:'href'
			},j),
			d=c('<div/>').attr('id',b.containerID).append('<img/>').hide().css('position','absolute').appendTo('body'),
			f=c('img',d).css(b.imgCSS),
			h=this.filter(':linkingToImage('+b.srcAttr+')');
		function i(a){
			return a.replace(/(\/?)([^\/]+)$/,'$1'+b.thumbPrefix+'$2')
		}
		if(b.preloadImages){
			(function(a){
				var g=new Image(),e=arguments.callee;
				g.src=i(c(h[a]).attr(b.srcAttr));
				g.onload=function(){h[a+1]&&e(a+1)}
			})(0)
		}
		h.mousemove(
			function(a){
				var imgWidth = f.width();
				var imgHeight = f.height();
				if (imgHeight == 0)
					imgHeight = 400;
				if (imgWidth == 0)
					imgWidth = 400;
				coordY = a.pageY;
					
				if (b.distanceFromCursor.top > 0)
					coordY += b.distanceFromCursor.top;
				else 
					coordY -= imgHeight - b.distanceFromCursor.top;

				coordX = a.pageX;
                if (b.distanceFromCursor.left > 0)
                    coordX += b.distanceFromCursor.left;
                else
                    coordX -= imgWidth - b.distanceFromCursor.left;
				d.css(
					{top:coordY+'px',
					left:coordX+'px'}
				)
			}).hover(function(){
				var a=this;
				d.addClass(b.containerLoadingClass).show();
				f.load(function(){
					d.removeClass(b.containerLoadingClass);
					f.show();
					b.onLoad.call(f[0],a)
				}).attr('src',i(c(a).attr(b.srcAttr)));
				coordX = findCoordX(a) + a.width / 2;
				coordY = findCoordY(a) + a.height / 2;
				if (typeof (document.body.scrollTop) != "undefined")
					scrollY = document.body.scrollTop;
				else 
					scrollY = window.pageYOffset;
				if (typeof(document.body.scrollLeft) != "undefined")
					scrollX = document.body.scrollLeft;
				else
					scrollX = window.pageXOffset;

				scrollCoords = getScrollXY();

                if (typeof(window.innerHeight) != "undefined"){
                    windowWidth = window.innerWidth;
                    windowHeight = window.innerHeight;
                } else if (typeof(document.documentElement.clientHeight) != "undefined" && document.documentElement.clientHeight > 0){
                    windowWidth = document.documentElement.clientWidth;
                    windowHeight = document.documentElement.clientHeight;
                } else {
                    windowWidth = document.body.clientWidth;
                    windowHeight = document.body.clientHeight;
                }

				if (coordX - scrollCoords[0] > windowWidth / 2)
					b.distanceFromCursor.left = -Math.abs(b.distanceFromCursor.left);
				else 
					b.distanceFromCursor.left = Math.abs(b.distanceFromCursor.left);
                if (coordY - scrollCoords[1] > windowHeight / 2)
                    b.distanceFromCursor.top = -Math.abs(b.distanceFromCursor.top);
                else
                    b.distanceFromCursor.top = Math.abs(b.distanceFromCursor.top);
				b.onShow.call(d[0],a);
			},
			function(){
				d.hide();
				f.unbind('load').attr('src','').hide();
				b.onHide.call(d[0],this)
			});
			return this
		}
})(jQuery);

