function shiftx(e,xOffset)
{
  var r = false;
  var x =e.pageX + xOffset;
  var img = $("#zoomimg").get(0);
  if (img!=null)
     {
     var w =img.width;
     if ((x + w)>($(window).width() + $(window).scrollLeft())) { r = true;}
     }
  return r;
}

function calcx(e,xOffset)
{
  var x =e.pageX + xOffset;
  var img = $("#zoomimg").get(0);
  if (img!=null)
     {
     var w =img.width;
     if ((x + w)>($(window).width() + $(window).scrollLeft())) { x = $(window).width() + $(window).scrollLeft() -w - xOffset;}
     }
  return x;
}

function calcy(e,xOffset,yOffset)
{
var y =e.pageY + yOffset;
  var img = $("#zoomimg").get(0);
  if (img!=null)
     {
     var h =img.height + 48;
     if ((y + h)>($(window).height() + $(window).scrollTop())) 
        {
        if (shiftx(e,xOffset)==true) 
            { y = e.pageY - yOffset - h; }
           else 
            { y = $(window).height() + $(window).scrollTop() -h - yOffset;}
        }

     }
  return y;
}

this.zoomproc = function()
        {	
	xOffset = 10;
	yOffset = 10;
	$("img.zoomtag").hover(function(e)
                                    {
                                     var c = (this.alt != "") ? this.alt : "";
                                     var h = this.src;
                                     var p = h.lastIndexOf(".");
                                     h = h.substr(0,p) + "-zoom" + h.substr(p);
                                     var s = "<div id='zoomdiv' class='zoomdiv'><div class='zoomheader'>" + c + "</div><div class='zoombody'><img id='zoomimg' class='zoomimg' src='"+ h + "' alt='Zoom' /></div><div class='zoomfooter'></div</div>"; 
                                     $("body").append(s);								 
                                     $("#zoomdiv").css("top" ,calcy(e,xOffset,yOffset) + "px")
                                                  .css("left",calcx(e,xOffset) + "px")
                                                  .fadeIn("slow");						
                                    }
                                  ,
                                   function()
                                    {
                                    $("#zoomdiv").remove();
                                    }
                                  );	

	$("img.zoomtag").mousemove(function(e)
                                     {
                                     $("#zoomdiv")
                                              .css("top", calcy(e,xOffset,yOffset) + "px")
                                              .css("left",calcx(e,xOffset) + "px");
                                     }
                                   );			
        };


this.linkzoomproc = function()
                        {	
                         xOffset = 5;
                      	 yOffset = 5;
                         $("a.linkzoom").hover(function(e)
                                                   {
                                               		this.t = this.title;
                                               		this.title = "";
                                               		var s = "<div id='linkzoom' class='linkzoom'><img id='zoomimg' class='zoomimg' src='"+ this.rel + "' alt='Zoom' /></div>"; 
                                               		$("body").append(s);								 
                                             		$("#linkzoom")
                                                   			.css("top",  calcy(e,xOffset,yOffset) + "px")
                                                   			.css("left", calcx(e,xOffset) + "px") 
                                                   			.css("z-index", "99999") 
                                                   			.fadeIn("slow");
						
                                                   }
                                                   ,
                                                  function()
                                                  {
                                              		this.title = this.t;	
                                              		$("#linkzoom").remove();
                                                  }
                                                  );	
                                                  

  	$("a.linkzoom").mousemove(function(e)
                                     {
                                     $("#linkzoom")
                                              .css("top", calcy(e,xOffset,yOffset)  + "px")
                                              .css("left",calcx(e,xOffset)  + "px");
                                     }
                                   );			
                                };





$(document).ready(function()
                     {
                     zoomproc();
                     linkzoomproc()
                     }
                 );
