var clock, objRef, currentx, currenty, dx, dy;
var cs, sn, realComp, imagComp, stepCnt, l, tmp, frameDuration;

function initZoom(imgname, total, steps, maxx, maxy) {
	// Rotations are quick and easy if we precalculate (cosT, i.sinT) where T is angular increment in Argand plane
		cs = 0.99904822;
		sn = 0.04361938;
	// Initialise timer
		clock = new Array(); // Array of clocks for animations
		frameDuration = total / steps;
	// Declare arrays
		stepCnt = new Array(); // Number of increments from default size
		objRef = new Array(); // Convenient array of handles for images
		// Point to be rotated in Argand plane
			realComp = new Array(); // Real component - used as current size increment for image
			imagComp = new Array(); // Imaginary component
		// Current size
			currentx = new Array();
			currenty = new Array();
	for(l = 0; l < 2; l++) { // Initialise arrays
		stepCnt[l] = 0;
		objRef[l] = eval("document.getElementById('" + imgname + l + "')");
		// Point to be rotated (anti-clockwise) in Argand plane
			realComp[l] = 0; // Initial speed is zero
			imagComp[l] = -0.5; // Eventual speed is 0.5. Initialise -ve so realComp increases with initial rotation
		// Current size
			currentx[l] = objRef[l].width;
			currenty[l] = objRef[l].height;
	}
	// Increment sizes we would need for linear scaling - still useful multipliers for non-linear scaling as they give the correct aspect ratio
		stepX = (maxx - currentx[0]) / steps;
		stepY = (maxy - currenty[0]) / steps;
}

function zoomImg(i) {
	clearInterval(clock[i]); // Halt any current animation of this image
	realComp[i] = -realComp[i]; // A sudden bounce - same speed, opposite direction
	// Animate image
		functionRef = "biggerImg(" + stepX + ", " + stepY + ", " + i + "); biggerImg(" + stepX + ", " + stepY + ", " + i + ")";
		clock[i] = setInterval(functionRef, frameDuration); // Start animation
}

function shrinkImg(i) {
	clearInterval(clock[i]); // Halt any current animation of this image
	realComp[i] = -realComp[i]; // A sudden bounce - same speed, opposite direction
	// Animate image
		functionRef = "smallerImg(" + stepX + ", " + stepY + ", " + i + ")";
		clock[i] = setInterval(functionRef, frameDuration); // Start animation
}
	

function biggerImg(dx, dy, i) {
//	objRef[i].width = currentx[i];
	if (stepCnt[i] < 28) {
		// Rotate point in Argand plane by (a + i.b)(cosT + i.sinT)
			tmp = realComp[i] * cs - imagComp[i] * sn;
			imagComp[i] = realComp[i] * sn + imagComp[i] * cs;
			realComp[i] = tmp;
		// Increment size
			currentx[i] += realComp[i] * dx;
			currenty[i] += realComp[i] * dy;
			// Actual object dimensions
				objRef[i].width = currentx[i];
				objRef[i].height = currenty[i];
		stepCnt[i]++; // Track number of increments away from default size
	} else clearInterval(clock[i]);
}

function smallerImg(dx, dy, i) {
	if (stepCnt[i] > 0) {
		// Decrement size
			currentx[i] += realComp[i] * dx;
			currenty[i] += realComp[i] * dy;
			// Actual object dimensions
				objRef[i].width = currentx[i];
				objRef[i].height = currenty[i];
		// Rotate point in Argand plane by (a + i.b)(cosT + i.sinT)
			tmp = realComp[i] * cs - imagComp[i] * sn;
			imagComp[i] = realComp[i] * sn + imagComp[i] * cs;
			realComp[i] = tmp;
		stepCnt[i]--; // Track number of increments away from default size
	} else clearInterval(clock[i]);
}

