var IE = document.all?true:false;
if (!IE) 
{
	document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getMouseXY;

var fullProductImageWidth = 538;
var fullProductImageHeight = 492;

var largeProductImageWidth = 316;
var largeProductImageHeight = 289;

var smallWindowWidth = 145;
var smallWindowHeight = 145;

var largeWindowWidth = 247;
var largeWindowHeight = 247;

var zoomActive = false;

function getMouseXY(e)
{
	if (zoomActive == true)
	{
		var mouseX = 0;
		var mouseY = 0;
		if (IE) 
		{
			mouseX = event.clientX + document.getElementById('html_element').scrollLeft;
			mouseY = event.clientY + document.getElementById('html_element').scrollTop;
		}
		else 
		{
			mouseX = e.pageX;
			mouseY = e.pageY;
		}  
		
		if (mouseX < 0){mouseX = 0};
		if (mouseY < 0){mouseY = 0};
		
		mouseInsideX = mouseX - largeProductImageLeft;
		mouseInsideY = mouseY - largeProductImageTop;
		
		newSmallWindowLeft = mouseInsideX - (smallWindowWidth/2);
		newSmallWindowTop = mouseInsideY - (smallWindowHeight/2);
		
		if (newSmallWindowLeft > (largeProductImageWidth - smallWindowWidth)) newSmallWindowLeft = largeProductImageWidth - smallWindowWidth;
		if (newSmallWindowLeft < 0) newSmallWindowLeft = 0;
		if (newSmallWindowTop > (largeProductImageHeight - smallWindowHeight)) newSmallWindowTop = largeProductImageHeight - smallWindowHeight;
		if (newSmallWindowTop < 0) newSmallWindowTop = 0;
		
		smallWindow.style.left = newSmallWindowLeft+'px';
		smallWindow.style.top = newSmallWindowTop+'px';
		
		newFullProductImageScrollLeft = parseInt(newSmallWindowLeft*(fullProductImageWidth/largeProductImageWidth));
		newFullProductImageScrollTop = parseInt(newSmallWindowTop*(fullProductImageHeight/largeProductImageHeight));
		
		fullProductImage.scrollLeft = newFullProductImageScrollLeft;
		fullProductImage.scrollTop = newFullProductImageScrollTop;
	}
	
}
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		//~ alert(obj.tagName+'#'+obj.id+' left:'+obj.offsetLeft+' sum:'+curleft);
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			//~ alert(obj.tagName+'#'+obj.id+' left:'+obj.offsetLeft+' sum:'+curleft);
		}
	}
	return [curleft,curtop];
}


function startZoom()
{
	zoomActive = true;
	if (document.getElementById('small_window'))
	{
		smallWindow = document.getElementById('small_window');
	}
	if (document.getElementById('product_description'))
	{
		productDescription = document.getElementById('product_description');
	}
	if (document.getElementById('full_product_image'))
	{
		fullProductImage = document.getElementById('full_product_image');
	}
	if (document.getElementById('large_product_image'))
	{
		largeProductImage = document.getElementById('large_product_image');
	}
	if (fullProductImage && smallWindow && largeProductImage)
	{
		productDescription.style.visibility = 'hidden';
		
		positionsArray = findPos(largeProductImage);
		largeProductImageLeft = positionsArray[0];
		largeProductImageTop = positionsArray[1];
		
		smallWindow.style.display = 'block';
		fullProductImage.style.display = 'block';
	}
}
function stopZoom()
{
	zoomActive = false;
	if (document.getElementById('small_window'))
	{
		smallWindow = document.getElementById('small_window');
	}
	if (document.getElementById('full_product_image'))
	{
		fullProductImage = document.getElementById('full_product_image');
	}
	if (document.getElementById('product_description'))
	{
		productDescription = document.getElementById('product_description');
	}
	if (fullProductImage && smallWindow)
	{
		productDescription.style.visibility = 'visible';
		smallWindow.style.display = 'none';
		fullProductImage.style.display = 'none';
	}
}

