﻿// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
    if(document.getElementById('divOutline') == null || document.getElementById('imgZoom') == null || document.getElementById('divThumb') == null)
        return;
    
var tempX = 0;
	var tempY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		tempX = e.pageX;
		tempY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		tempX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		tempY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

  tempX -= getLeft(document.getElementById('divThumb'));
  if(tempX<0)
	tempX = 0;
   else if (tempX>document.getElementById('divThumb').clientWidth)
		tempX = document.getElementById('divThumb').clientWidth;
		
	tempY -= getBottom(document.getElementById('divThumb'));
	if(tempY<0)
		tempY = 0;
	else if (tempY>document.getElementById('divThumb').clientHeight)
		tempY = document.getElementById('divThumb').clientHeight;

  docx = document.getElementById('divOutline').clientWidth/2;
  docy = document.getElementById('divOutline').clientHeight/2;
  rx = document.getElementById('imgZoom').clientWidth/ document.getElementById('divThumb').clientWidth;
  ry = document.getElementById('imgZoom').clientHeight/ document.getElementById('divThumb').clientHeight;

  rpx = tempX / document.getElementById('divThumb').clientWidth;

	if(tempX>= docx*(document.getElementById('divThumb').clientWidth/document.getElementById('imgZoom').clientWidth) && tempX<= document.getElementById('divThumb').clientWidth-docx*(document.getElementById('divThumb').clientWidth/document.getElementById('imgZoom').clientWidth))
	{
		document.getElementById('divOutline').style.left = String(tempX-docx) + 'px';
		document.getElementById('imgZoom').style.marginLeft = '-' + String(Math.round(
		rpx*document.getElementById('divThumb').clientWidth*rx)-docx) + 'px';
	}

	
	if(tempY>=docy*(document.getElementById('divThumb').clientHeight/document.getElementById('imgZoom').clientHeight) && tempY<=document.getElementById('divThumb').clientHeight-docy*(document.getElementById('divThumb').clientHeight/document.getElementById('imgZoom').clientHeight))
	{
		document.getElementById('divOutline').style.top = String(tempY-docy-document.getElementById('divThumb').clientHeight) + 'px';

		document.getElementById('imgZoom').style.marginTop = '-' + String(Math.round(
		(tempY / document.getElementById('divThumb').clientHeight)
		*document.getElementById('divThumb').clientHeight*ry)-docy) + 'px';
	}

  return true
}

function getLeft(obj)
{
	left = 0;
	if(obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
		left -= 7;
	}
	else
	{
	    //Safari
	    left = obj.offsetLeft * .95;
	    left += 10;
	}
	return left;
}

function getBottom(obj)
{
	bottom = 0;
	if(obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			bottom += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		bottom += obj.y;
	return bottom;
}