//document.observe('dom:loaded',init);
var _RATE;
var _INVERSE_RATE;
function calculateImageOffset(image,container,rate) {
	// find current dimensions
	var dCurrent = image.getDimensions();

	// new dimensions
	var dNew = { width: eval(dCurrent.width * (rate/100)), height: eval(dCurrent.height * (rate/100)) };

	// find the current offset
	var oCurrent = { 
		left: pxToInt(image.getStyle('left')), 
		top: pxToInt(image.getStyle('top'))
	};

	// find the current center
	var cCurrent = {
		x: -oCurrent.left + eval(container.getDimensions().width / 2),
		y: -oCurrent.top + eval(container.getDimensions().height / 2)
	};

	// find the % change
	var pChange = {
		x: cCurrent.x / dCurrent.width,
		y: cCurrent.y / dCurrent.height
	}
	
	// find the new center
	var cNew = {
		x: dNew.width * pChange.x,
		y: dNew.height * pChange.y
	};

	// find the new offset
	var oNew = {
		left: -1 * Math.ceil(cNew.x - eval(container.getDimensions().width / 2)),
		top: -1 * Math.ceil(cNew.y - eval(container.getDimensions().height / 2))
	}

	// error checking to ensure still in bounds
	if (eval((-1 * oNew.top)+container.getDimensions().height) > dNew.height) {
		oNew.top = -1 * (dNew.height - container.getDimensions().height);
	}
	if (eval((-1 * oNew.left)+container.getDimensions().width) > dNew.width) {
		oNew.left = -1 * (dNew.width - container.getDimensions().width);
	}
	if (oNew.top > 0) {
		oNew.top = 0;
	}
	if (oNew.left > 0) {
		oNew.left = 0;
	}
	return { top: oNew.top, left: oNew.left };
}
function photoInit() {

	_RATE = 200;
	_INVERSE_RATE = 50;

	_PHOTO_DRAG = new Draggable(_ID_PHOTO, { 
		starteffect: null, 
		endeffect: null,
		snap: function(x, y, draggable) {
			function constrain(n, lower, upper) {
				if (n > upper) return upper;
				else if (n < lower) return lower;
				else return n;
				
			}
			var element = draggable.element.getDimensions();
			var parent = {
				width: eval((_PHOTO_SIZE.width * 2) - document.viewport.getWidth()),
				height:eval((_PHOTO_SIZE.height * 2) - document.viewport.getHeight())
			}
			// if the image is smaller
			if (_PHOTO_SIZE.width <= document.viewport.getWidth()) {
				cxlow = eval(document.viewport.getWidth() / 2) - (_PHOTO_SIZE.width / 2) - eval(pxToInt($(_ID_PHOTO).getStyle('margin-left')));
				cxhigh = eval(document.viewport.getWidth() / 2) - (_PHOTO_SIZE.width / 2) - eval(pxToInt($(_ID_PHOTO).getStyle('margin-left'))); //0 //element.width + eval(pxToInt($(_ID_PHOTO).getStyle('margin-left')))
			}
			else {
				cxlow = element.width - parent.width - eval(pxToInt($(_ID_PHOTO).getStyle('margin-left')));
				cxhigh = -eval(pxToInt($(_ID_PHOTO).getStyle('margin-left')));
			}

			if (_PHOTO_SIZE.height <= document.viewport.getHeight()) {
//				db('here (if1)');
				cylow = eval(document.viewport.getHeight() / 2) - (_PHOTO_SIZE.height / 2) - parseInt(pxToInt($(_ID_PHOTO).getStyle('margin-top')))-25;
				cyhigh = eval(document.viewport.getHeight() / 2) - (_PHOTO_SIZE.height / 2) - parseInt(pxToInt($(_ID_PHOTO).getStyle('margin-top')))-25;
			}
			else {
//				db('here (if2)');
				cylow = element.height - parent.height - parseInt(pxToInt($(_ID_PHOTO).getStyle('margin-top'))) - 50;
				cyhigh = 0 - parseInt(eval(pxToInt($(_ID_PHOTO).getStyle('margin-top'))));
			}
//			db('x: '+x+', '+cxlow+', '+cxhigh);
//			db('y: '+y+', '+cylow);//+', '+cyhigh);
			return [
				constrain(x, cxlow, cxhigh),
				constrain(y, cylow, cyhigh)
			];
		},
		onEnd: function() {
			$(_ID_PHOTO).blur();
		}
	});

	Event.observe(_ID_PHOTO,'dblclick',zoomIn);
}
/*
function zoomOut() {
	_ZOOMED = false;
	var o = calculateImageOffset($(_ID_PHOTO),$(_ID_PHOTO_CONTAINER),_INVERSE_RATE);
	var s = new Effect.Scale(_ID_PHOTO, _INVERSE_RATE, { queue: 'parallel' });
	var m = new Effect.Morph(_ID_PHOTO, { style: 'top: '+o.top+'px; left: '+o.left+'px', queue: 'parallel' });
}
function zoomIn() {
	_ZOOMED = true;
	var o = calculateImageOffset($(_ID_PHOTO),$(_ID_PHOTO_CONTAINER),_RATE);
	var s = new Effect.Scale(_ID_PHOTO, _RATE, { queue: 'parallel' });
	var m = new Effect.Morph(_ID_PHOTO, { style: 'top: '+o.top+'px; left: '+o.left+'px', queue: 'parallel' });
}
*/