var flytOk = 0
var zoomOk = 0
var startX = 0
var startY = 0
var hjuldelta = 0
var cropHoejde = 0
var cropBredde = 0
var cropZoom = 0
var tasten = 0
document.onmousemove = mousemove;
document.onmousedown = mousedown;
document.defaultAction = false;
document.onmouseup = mouseup;
document.onmousewheel = mousewheel;
document.onkeypress = keypress;
document.onkeyup = keyup;
document.onkeydown = keydown;

if (window.addEventListener) {
	window.addEventListener('DOMMouseScroll', mousewheel, false);
}

function startFlyt() {
	flytOk = 1	
}

function mousedown(evt) {
	if (flytOk == 1) {
		if (!evt) {
			evt = window.event;
			evt.returnValue = false;
		} else {
			if (evt.preventDefault) {
				evt.preventDefault()
			}
		}
		var objDiv = window.document.getElementById('cropBillede')
		startX = evt.clientX - parseInt(objDiv.style.left)
		startY = evt.clientY - parseInt(objDiv.style.top)
	}
}

function mousemove(evt) {
	if (flytOk == 1) {
		if (!evt) {
			evt = window.event;
			evt.returnValue = false;
		} else {
			if (evt.preventDefault) {
				evt.preventDefault()
			}
		}
		var objDiv = window.document.getElementById('cropBillede')
		objDiv.style.position = 'absolute';
		objDiv.style.left = (evt.clientX - startX).toString()+'px'
		objDiv.style.top = (evt.clientY - startY).toString()+'px'
	}
}

function mouseup() {
	if (flytOk == 1) {
		flytOk = 0
	}
//alert('hej')
//return true
}


function nulstil() {
	objDiv = window.document.getElementById('cropBillede')
	objDiv.style.MozTransform = 'scale(2)'
}

function mousewheel(event) {
	if (window.document.getElementById('cropBillede')) {
		objDiv = window.document.getElementById('cropBillede')
		if (!window.event) {
			if (event.preventDefault) {
				event.preventDefault()
			}
		}
		var delta = 0;
		if (!event) /* For IE. */
			event = window.event;
		if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;
			/** In Opera 9, delta differs in sign as compared to IE.
			 */
			if (window.opera) {
				delta = -delta;
			}
			//objDiv.style.zoom = objDiv.style.zoom*1 + (delta/100)
			cropZoom = cropZoom + (delta/100)
			objDiv.style.width = cropBredde*cropZoom + 'px'
			objDiv.style.height = cropHoejde*cropZoom + 'px'

		} else if (event.detail) { /** Mozilla case. */
			/** In Mozilla, sign of delta is different than in IE.
			 * Also, delta is multiple of 3.
			 */
			delta = -event.detail/3;
			//var mozZoomValue = objDiv.style.MozTransform.replace(/scale\(/,'').replace(/\)/,'')*1
			//if (mozZoomValue==0) mozZoomValue = 1
			//objDiv.style.MozTransform = 'scale('+(mozZoomValue+(delta/100))+')'
			cropZoom = cropZoom + (delta/100)
			objDiv.style.width = cropBredde*cropZoom + 'px'
			objDiv.style.height = cropHoejde*cropZoom + 'px'


		}


//		if (event.preventDefault)
//			event.preventDefault();
		event.returnValue = false;
	}
}

function keydown(evt) {
	if (window.document.getElementById('cropBillede')) {
		if (!evt) {
			evt = window.event;
			evt.returnValue = false;
		} else {
			if (evt.preventDefault) {
				evt.preventDefault()
			}
		}
		tasten = evt.keyCode
		objDiv = window.document.getElementById('cropBillede')
		if (tasten == 100 || tasten == 37) {
			objDiv.style.left = (parseInt(objDiv.style.left)-1).toString()+'px'
		} else if (tasten == 104 || tasten == 38) {
			objDiv.style.top = (parseInt(objDiv.style.top)-1).toString()+'px'
		} else if (tasten == 102 || tasten == 39) {
			objDiv.style.left = (parseInt(objDiv.style.left)+1).toString()+'px'
		} else if (tasten == 98 || tasten == 40) {
			objDiv.style.top = (parseInt(objDiv.style.top)+1).toString()+'px'
		} else if (tasten == 107 || tasten == 187) {
			cropZoom = cropZoom + 0.01
			objDiv.style.width = cropBredde*cropZoom + 'px'
			objDiv.style.height = cropHoejde*cropZoom + 'px'
		} else if (tasten == 109 || tasten == 189) {
			cropZoom = cropZoom - 0.01
			objDiv.style.width = cropBredde*cropZoom + 'px'
			objDiv.style.height = cropHoejde*cropZoom + 'px'
		}
	}

}

function keypress() {
}

function keyup() {
	tasten = 0
}

