/*
Zoomer.js
version 2
2008-01-23 07:20:15 EST
created by Jack Hodgson
http://www.jackhodgson.com
*/

var img_left = 0;
var img_top = 0;
var img_width = 0;
var img_height = 0;
document.onmousemove = dragmove;
var dragging = false;
var prevMousex = "";
var prevMousey = "";
//var percent = 1;

var divbox, zoomimage, fullwidth, minrate;

function initthings(w,h) {
	divbox = document.getElementById('zoomer');
	zoomimage = document.getElementById('zoom-img');
	
	fullwidth = w;
	fullheight = h;
	fullwh = fullwidth + "x" + fullheight;
	
	if(fullwidth > fullheight) {
		minrate = divbox.clientWidth / fullwidth;
		img_left = 0;
		img_top = (divbox.clientHeight-(fullheight*minrate))/2;
	} else {
		minrate = divbox.clientHeight / fullheight;
		img_left = (divbox.clientWidth-(fullwidth*minrate))/2;
		img_top = 0;
	}
	scaleimage(minrate);
	setimgloc(img_left,img_top);
	
	zoomimage.style.visibility="visible";
	setscalelabel();
	setzoomlabel();
	document.getElementById('loadingmsg').style.visibility="hidden";
	document.getElementById('loadingmsg').style.height="0";
}

function setupimage() {
	if(zoomimage.width > zoomimage.height) {
		minrate = divbox.clientWidth / zoomimage.width;
		alert(minrate);
	} else {
		minrate = divbox.clientHeight / zoomimage.height;
		alert(minrate);
	}
	scaleimage(minrate);
}

function scaleimage(percent) {
	var maxrate = 2;
	
	if(percent == 0) percent = minrate;
	if(percent > maxrate) percent = maxrate;
	if(percent < minrate) percent = minrate;
	
	//var orig_w = zoomimage.width;
	//var orig_h = zoomimage.height;
	
	img_width = zoomimage.width = fullwidth * percent;
	img_height = zoomimage.height = fullheight * percent;

	setscalelabel();
	setzoomlabel();
}

function zoomin() {
	var el = zoomimage;
	var rate = el.width / fullwidth;
	//var tempwidth = el.width;
	//var tempheight = el.height;
	
	var prevwidth = img_width;
	var prevheight = img_height;
	
	scaleimage(rate * 2);
	
	var dif_width = img_width - prevwidth;
	var dif_height = img_height - prevheight;
	
	img_left = img_left - (dif_width/2);
	img_top = img_top - (dif_height/2);
	setimgloc(img_left,img_top);
}

function zoomout() {
	var el = zoomimage;
	var rate = el.width / fullwidth;
	
	var prevwidth = img_width;
	var prevheight = img_height;
	
	scaleimage(rate / 2);
	
	var dif_width = img_width - prevwidth;
	var dif_height = img_height - prevheight;
	
	img_left = img_left + (dif_width*2);
	img_top = img_top + (dif_height*2);
	setimgloc(img_left,img_top);
}

function startdrag() {
	dragging = true;
}

function enddrag() {
	prevMousex = "";
	prevMousey = "";
	dragging = false;
}

function dragmove(ev) {
	if(dragging) {
		ev = ev || window.event;
		
		var mousePos = mouseCoords(ev);
		
		if(!prevMousex) {
			prevMousex = mousePos.x;
			prevMousey = mousePos.y;
		}
		if(prevMousex) {
			img_left += mousePos.x - prevMousex;
			
			img_top += mousePos.y - prevMousey;
			
			setimgloc(img_left,img_top);
		}
		
		prevMousex = mousePos.x;
		prevMousey = mousePos.y;
	}
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function setimgloc(x,y) {
	zx = zoomimage.width;
	dw = divbox.clientWidth;
	if(zx <= dw) {
		if(x < 0) x = 0;
		if(x > (dw - zx)) x = dw - zx;
	} else {
		if(x < dw - zx) x = dw - zx;
		if(x > 0) x = 0;
	}
	zh = zoomimage.height;
	dh = divbox.clientHeight;
	if(zh <= dh) {
		if(y < 0) y = 0;
		if(y > dh - zh) y = dh - zh;
	} else {
		if(y < dh - zh) y = dh - zh;
		if(y > 0) y = 0;
	}
	img_left = x;
	img_top = y;
	zoomimage.style.left = x + "px";
	zoomimage.style.top = y + "px";
	
	setscalelabel();
	setzoomlabel();
}

function setzoomlabel() {
	var img2 = zoomimage;
	var rate = img2.width / fullwidth;
	var num = new Number(rate);
	numstr = num.toFixed(2) * 100;
	//debug(img.width + ", " + img.width);
	var el = document.getElementById('zoomlabel');
	el.innerHTML = "&nbsp;"+numstr +"%&nbsp;";
	
}

function setscalelabel() {
	var img2 = zoomimage;
	var rate = img2.width / fullwidth;
	//debug(img.width + ", " + img.width);
	var el = document.getElementById('scalelabel');
	el.innerHTML = rate + ", " + img2.style.left + ", " + img2.style.top + ", " + fullwh + ", " + img_width + ", " + img_height;
	
}

function debug(aMsg) {
   setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}

function changeimage(url, caption, w, h) {
	document.getElementById("zoom-img").src = url;
	initthings(w, h);
}

