﻿function zoomer(oImg_non, strSmallUrl, strLargeUrl, intMinWidth, intMinHeight, intMaxWidth, intMaxHeight, intSteps, intInterval) {

// =========================================================================================================================
//
// Version 0.0.4 Updated 14 May 2007 By Mark Oakley
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Updated to allow images to zoom up and right rather than down and right when images are at the bottom of the page
// This Recifies and closes an error regarding images at the bottom of the page enlarging off the screen and lenghthening
// the page
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Version 0.0.3 Updated 23 November 2006 By Mark Oakley
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Updated to set display: block or display: none;
//
// The image source is now changed only when the initial call to the resize object is called (not for every to minimum, or
// toMaximum)
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Version 0.0.2 written 01 October 2006 By Mark Oakley
//
// -------------------------------------------------------------------------------------------------------------------------
//
// NOW WORKS PROPERTY IN FIREFOX AGAIN!!!
//
// Addedd 'px' to all dimention and location settings to fix firefox issue
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Version 0.0.1 written 01 October 2006 By Mark Oakley
//
// -------------------------------------------------------------------------------------------------------------------------
//
// Now caches images
//
// Image jumping at bottom bug has been fixed by saving the non-integer values and adding the increments to them
// rather than adding the math.floor integers to the math.floor current height (usually results in a 10-20 pixel
// jump at the bottom when the image reaches the correct width).
//
// Now uses math.ceiling and math.floor to figure out the correct increments to alter the widths and heights by
//
// turned into closure function
//
// -------------------------------------------------------------------------------------------------------------------------


// the interval object
var oInterval = null;


// the rollover image object (oImg_Non is the original image that doesn't move)
var oImg = document.getElementById(oImg_non.id+'_rollover');


// the the new caching system
var oSmallImage = new Image(); oSmallImage.src = strSmallUrl;
var oLargeImage = new Image(); oLargeImage.src = strLargeUrl;

// figure out the correct increments.
var numWidthIncrement = (intMaxWidth - intMinWidth) / intSteps
var numHeightIncrement = (intMaxHeight - intMinHeight) / intSteps

var intCurrWidth = intMinWidth;
var intCurrHeight = intMinHeight;

var mouseIsOver = false;
var bEnlargeDown = true;



	function toMaximum() {

		var intNewWidth = Math.ceil(intCurrWidth + numWidthIncrement);
		var intNewHeight = Math.ceil(intCurrHeight + numHeightIncrement);

		if(bEnlargeDown) {

			if(intNewWidth <= intMaxWidth) {
				oImg.style.visibility = 'visible';
				intCurrWidth = intCurrWidth + numWidthIncrement;
				intCurrHeight += numHeightIncrement;
				oImg.style.width = intNewWidth+'px';
				oImg.style.height = intNewHeight+'px';
			} else {
				clearInterval(oInterval);
				oImg.src = oLargeImage.src;
				oImg.style.width = intMaxWidth+'px';
				oImg.style.height = intMaxHeight+'px';
			}

		} else {

			if(intNewWidth <= intMaxWidth) {
				oImg.style.visibility = 'visible';
				intCurrWidth = intCurrWidth + numWidthIncrement;
				intCurrHeight += numHeightIncrement;
				oImg.style.top = eval(oImg_non.offsetTop-(intNewHeight-oImg_non.offsetHeight+2)) + 'px';
				oImg.style.height = intNewHeight+'px';
				oImg.style.width = intNewWidth+'px';

			} else {
				clearInterval(oInterval);
				oImg.src = oLargeImage.src;
				oImg.style.width = intMaxWidth+'px';
				oImg.style.height = intMaxHeight+'px';
			}

		}

	}

	function toMinimum() {

		var intNewWidth = Math.floor(intCurrWidth - numWidthIncrement);
		var intNewHeight = Math.floor(intCurrHeight - numHeightIncrement);

		if(bEnlargeDown) {

			if(intNewWidth >= intMinWidth) {
				intCurrWidth -= numWidthIncrement;
				intCurrHeight -= numHeightIncrement;
				oImg.style.width = intNewWidth+'px';
				oImg.style.height = intNewHeight+'px';
			} else {
				clearInterval(oInterval);
				oImg.style.width = intMinWidth+'px';
				oImg.style.height = intMinHeight+'px';
				oImg.style.visibility = 'hidden';
			}

		} else {

			if(intNewWidth >= intMinWidth) {
				intCurrWidth -= numWidthIncrement;
				intCurrHeight -= numHeightIncrement;
				oImg.style.top = eval(oImg_non.offsetTop-(intNewHeight-oImg_non.offsetHeight+2)) + 'px';
				oImg.style.height = intNewHeight+'px';
				oImg.style.width = intNewWidth+'px';
			} else {
				clearInterval(oInterval);
				oImg.style.width = intMinWidth+'px';
				oImg.style.height = intMinHeight+'px';
				oImg.style.visibility = 'hidden';
			}

		}

	}

	function mouseover() {
		clearInterval(oInterval);
		oImg.src = oSmallImage.src;
		toMaximum();
		oInterval = setInterval(toMaximum, intInterval);
	}

	function largemouseover() {
		mouseIsOver = true
	}

	function mouseout() {
		clearInterval(oInterval);
		oImg.src = oSmallImage.src;
		toMinimum();
		oInterval = setInterval(toMinimum, intInterval);
		mouseIsOver = false
	}

	if (oImg && oImg_non) {
		oImg_non.style.position = 'relative';
		oImg.style.visibility = 'hidden';
		oImg.style.display='block';
		oImg.style.position = 'absolute';
		oImg.style.top = oImg_non.offsetTop+'px';
		oImg.style.left = oImg_non.offsetLeft+'px';

		// determine the direction that the image should enlarge.. (up and right or down and right)
		bEnlargeDown = !eval(oImg_non.offsetTop+intMaxHeight > document.body.offsetHeight)

		var strMsg = 'Original Image Location:\n'+oImg_non.id+'\nTop: '+oImg_non.offsetTop+'\nLeft: '+oImg_non.offsetLeft
		strMsg += '\n\nRollover Image Location:\n'+oImg.id+'\nTop: '+oImg.offsetTop+'\nLeft: '+oImg.offsetLeft
		//alert(strMsg);



		//alert(oImg_non.offsetTop == oImg.offsetTop)

		if (oImg_non.offsetTop == oImg.offsetTop) {
			oImg_non.onmouseover = mouseover;
			oImg.onmouseout = mouseout;
			oImg.onmouseover = largemouseover;
		} else {
			oImg_non.onclick = function() { window.open(strLargeUrl, 'image', 'width=330,height=220'); }
		}
	}

}