


var toolmode,
	initDone, 
	curimg, 
	curdiv, 
	moveStartPosX, moveStartPosY, 
	moveImgPosX, moveImgPosY, 
	curZoomedWidth,curZoomedHeight,
	curZoomlevel,
	imgOrigWidth,imgOrigHeight,
	divHeight,
	divWidth,
	shiftzoom2 = {
	add : function(img) {
		if ( initDone == "done" )
			return false;
			
		initDone="done";

		curdiv = img.parentNode;
		curimg = img;
		
		
		
		// events
		curimg.onmousedown=shiftzoom2._startMove
		document.onmouseup=shiftzoom2._stopMove;
		document.onmousemove=shiftzoom2._whileMove; 
		
		var spanTag = document.createElement("span");
		spanTag.id = "imgviewer_tool";
		spanTag.className ="imgviewer_tool";
		spanTag.innerHTML = '<a href="' + curimg.src + '" target="_new"><img src="images/zusatz/expandView.png" /></a>' +  
			'<a href="javascript:shiftzoom2.zoompage();"><img src="images/zusatz/zoompage.gif" /></a>' + 
			'<a href="javascript:shiftzoom2.zoomone();"><img src="images/zusatz/zoom100.gif" /></a>' + 
			'<span class="percindicator" id="percindicator">100%</span>' ;
		
		curdiv.insertBefore ( spanTag, curimg );
		
		var divSpaceX;
		if ( curdiv.style.width == "" )
			divSpaceX = 150;
		else
			divSpaceX = parseInt(curdiv.style.width);
			
		var divSpaceY;
		if ( curdiv.style.height == "" )
			divSpaceY = 250;
		else
			divSpaceY = parseInt(curdiv.style.height);
		
		try
		{
			curdiv.style.width = ( parseInt (window.innerWidth) - divSpaceX ) + "px";
		} catch ( e )
		{
			// IE
			curdiv.style.width = ( parseInt (document.body.clientWidth) - divSpaceX ) + "px";
		}
		try
		{
			curdiv.style.height = ( parseInt(window.innerHeight) - divSpaceY ) + "px";
		} catch ( e )
		{
			// IE
			try
			{
				curdiv.style.height = ( parseInt (document.body.clientHeight) - divSpaceY ) + "px";
			} catch ( e )
			{
				curdiv.style.height = ( parseInt (document.documentElement.clientHeight) - divSpaceY ) + "px";
			}
		}
		
		spanTag.style.left = (parseInt(divSpaceX / 2)) + "px";
		//spanTag.style.top = spanTag.offsetTop + "px";
		
		//curdiv.style.overflow= "hidden";
		curdiv.unselectable = "on";
		curdiv.className = "imgviewer";
		
		//properties
		curimg.className = "imgviewerimg";
		curimg.style.position='relative';
		curimg.style.top = "0px";
		curimg.style.left = "0px";
		curimg.unselectable = "on";
		toolmode = "";
		
		divHeight = parseInt(curdiv.style.height);
		divWidth = parseInt(curdiv.style.width);
		imgOrigWidth = parseInt(curimg.offsetWidth);
		imgOrigHeight = parseInt(curimg.offsetHeight);
		
		shiftzoom2.zoompage();
		
		/** DOMMouseScroll is for mozilla. */
		if (window.addEventListener)
			window.addEventListener('DOMMouseScroll', shiftzoom2.wheel, false);
		/** IE/Opera. */
		window.onmousewheel = document.onmousewheel = shiftzoom2.wheel;
		
	},
	
	/** This is high-level function.
	 * It must react to delta being more/less than zero.
	 */
	handle: function (delta) {
	
		var step ;
		
		step = curZoomlevel / 15;
	
		if (delta < 0)
			curZoomlevel += step;
		else
			curZoomlevel -= step;
			
		curZoomlevel = Math.max ( curZoomlevel, 0.01);
			
		shiftzoom2.zoom();
	},

	/** Event handler for mouse wheel event.
	 */
	wheel: function (event){
		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;
		} 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;
		}
		/** If delta is nonzero, handle it.
		 * Basically, delta is now positive if wheel was scrolled up,
		 * and negative, if wheel was scrolled down.
		 */
		if (delta)
			shiftzoom2.handle(delta);
		/** Prevent default actions caused by mouse wheel.
		 * That might be ugly, but we handle scrolls somehow
		 * anyway, so don't bother here..
		 */
		if (event.preventDefault)
			event.preventDefault();
		event.returnValue = false;
	},
	
	zoomone: function () {
		curimg.style.top = "0px";
		curimg.style.left = "0px";
		curZoomlevel = 1;
		shiftzoom2.zoom();
	},
	
	zoompage: function () {
		curimg.style.top = "0px";
		curimg.style.left = "0px";
		curZoomlevel = Math.min ( (divWidth/imgOrigWidth), (divHeight/imgOrigHeight));
		shiftzoom2.zoom();
	},
		
	zoom: function () {
		var oldZoomedWidth, oldZoomedHeight;
		
		oldZoomedWidth = curZoomedWidth;
		oldZoomedHeight = curZoomedHeight;
	
		curZoomedWidth = Math.max(1, parseInt ( curZoomlevel * imgOrigWidth));
		curZoomedHeight = Math.max(1, parseInt ( curZoomlevel * imgOrigHeight));
		curimg.style.width = curZoomedWidth + "px";
		curimg.style.height = curZoomedHeight + "px";
		
		var percind = document.getElementById ( "percindicator" );
		percind.innerHTML = parseInt ( curZoomlevel * 100 ) + "%";
		
		var cury = Math.min(0,Math.max(parseInt(curimg.style.top) - ( ( curZoomedHeight - oldZoomedHeight ) / 2) , divHeight - curZoomedHeight));
		var curx = Math.min(0,Math.max(parseInt(curimg.style.left) - ( ( curZoomedWidth - oldZoomedWidth ) / 2) , divWidth - curZoomedWidth));
		
		try
		{
			curimg.style.top = cury + "px";
		} catch(e) {} 
		try
		{
			curimg.style.left = curx + "px";
		} catch(e) {} 
	},
	
	_startMove : function(e) {
		function falsefunc() { return false; } // used to block cascading events
		
		e=e?e:window.event;
		
		if ( toolmode != "" )
		{
			toolmode = "";
			curimg.style.cursor="default";
		}
		else
		{
			toolmode = "a";
			moveImgPosY = parseInt(curimg.style.top);
			moveImgPosX = parseInt(curimg.style.left);
			moveStartPosY = parseInt(e.clientY);
			moveStartPosX = parseInt(e.clientX);
			curimg.style.cursor="move";
			// cancel out any text selections 
			document.body.focus(); 
			
			try{
				// prevent text selection in IE 
				curimg.onselectstart = function () { return false; }; 
			} catch(e) {} 
			try{
				// prevent IE from trying to drag an image 
				curimg.ondragstart = function() { return false; }; 
			} catch(e) {} 
		}
		
		return false; 
	},
	
	_whileMove : function(e) {
		if ( toolmode == "a" )
		{
			e=e?e:window.event;
			var cury = Math.min(0,Math.max((( e.clientY - moveStartPosY) + moveImgPosY), divHeight - curZoomedHeight));
			var curx = Math.min(0,Math.max((( e.clientX - moveStartPosX) + moveImgPosX), divWidth - curZoomedWidth));
			curimg.style.top = cury + "px";
			curimg.style.left = curx + "px";
		}
		return false; 
	},
	
	_stopMove : function() {
		//document.onmousemove=null; 
		toolmode = "";
		curimg.style.cursor="default";
		//document.onmouseup=null; 
		return false; 
		
	}
}

