


function img_zoom_in(obj_orig, bigImage) {
	
	if( $('imgzoom_flag') && $('imgzoom_flag').checked != true )	return false;
	
	if(bigImage != true)	bigImage = false;
	if( ! IMG_ZOOM_TXT || IMG_ZOOM_TXT == '' || IMG_ZOOM_TXT == 'null') {
		IMG_ZOOM_TXT  = 'click for detail view'
	}
	imageZoom.prototype.doZoom(obj_orig, bigImage, IMG_ZOOM_TXT);
}





imageZoom = Class.create();

imageZoom.prototype = {

   imgZoomSize: 400,
   imgZoomInDuration: 10,
   imgZoomInSteps: 5,
   imgZoomOutDuration: 16,
   imgZoomOutSteps: 8,
   
	
   zoomed: false,
   sourceImg: false,
   zoomImg: false,
   zoomInProgress: false,
   tempZoomImg: false,
   imgZoomTxt: 'click for detail view',
   
   
   doZoom: function( sourceImg, bigImage, imgZoomTxt ) {
   	
   		if( ! this.checkForValidSourceImg( sourceImg )) {
   			return false;
   		}
   		
   		if( ! $(sourceImg).imageZoom) {
   			return new imageZoom( sourceImg, bigImage );
   		} else {
   			$(sourceImg).imageZoom.zoomIn();
   		}
   		
   		if( imgZoomTxt && imgZoomTxt != '' && imgZoomTxt != 'null') {
   			this.imgZoomTxt = imgZoomTxt;
   		}
   		
   },
   
   
   initialize: function(sourceImg, bigImage) {

   		if(bigImage == true) {
   			this.imgZoomSize = 600;
   		}
   	
   		if( ! this.checkForValidSourceImg( sourceImg )) {
   			return false;
   		}
   		
		if( ! $(sourceImg).imageZoom) {
			
			this.sourceImg = sourceImg;
			this.sourceImg.imageZoom = this;
			
			this.createZoomImg();
		}
		
		this.zoomIn();
   },
   
   
   checkForValidSourceImg: function( sourceImg ) {
		if(sourceImg.tagName.toLowerCase() != "img") {
   			return false;
   		}
   		
   		return true;
   },
   
   
   getSourceImgLeft: function() {
   		if( ! this.checkForValidSourceImg( this.sourceImg )) {
   			return false;
   		}
   		
   		// get source img position
		return Position.cumulativeOffset( this.sourceImg )[0];
   },
   
   
   getSourceImgTop: function() {
   		if( ! this.checkForValidSourceImg( this.sourceImg )) {
   			return false;
   		}
   		
   		// get source img position
		return Position.cumulativeOffset( this.sourceImg )[1];
   },
   
   
   
   
   
   loadZoomImg: function( newSrc ) {
		this.tempZoomImg = new Image();
		this.tempZoomImg.src = newSrc;
		
		this.loadTimer = setTimeout( this.loadZoomImgFinal.bind(this), 1 );
   },
   
   loadZoomImgFinal: function(){  
   		if(this.loadTimer) {
   			clearTimeout( this.loadTimer );
   		}
   		
   		if( this.tempZoomImg.complete ) {
   			this.zoomImg.src = this.tempZoomImg.src;
   			return;
   		}
   		this.loadTimer = setTimeout( this.loadZoomImgFinal.bind(this), 0.05 );
   },
   
   getNewZoomImageSrc: function( thumbImg ) {
   	
   		var replaceUrl = "";
   		if(this.imgZoomSize == 600) {
   			replaceUrl = "http://www.westend61.de/image/gr/";
   		} else {
   			replaceUrl = "http://img.westend61.de/preview/";
   		}
   	
   		return thumbImg.replace(/http:\/\/img.westend61.de\/(\w.+)\//, replaceUrl);
   },
   
   
   createZoomImg: function() {
		
   		// Check if the source image is set and valid
   		if( this.sourceImg == false || ! this.checkForValidSourceImg( this.sourceImg )) {
   			return false;
   		}
		
		// clone source img
		this.zoomImg = this.sourceImg.cloneNode(true);
		this.zoomImg.imageZoom = this;
		this.zoomImg.style.position	= "absolute"; 
		this.zoomImg.onmouseover = null;
		this.zoomImg.className = "zoomImgCssImg";
		this.zoomImg.title = this.imgZoomTxt;
		
		this.loadZoomImg( this.getNewZoomImageSrc( this.zoomImg.src ) );
		//this.zoomImg.src = this.getNewZoomImageSrc( this.zoomImg.src );
		
		
		
		
		var linkTarget = this.getLinkTarget( this.sourceImg );
		if( ! linkTarget || linkTarget == "undefined") {
			linkTarget = "#";
		}
		var imgWithLink = document.createElement("a");
		imgWithLink.href = linkTarget;
		imgWithLink.title = this.imgZoomTxt;
		
		imgWithLink.appendChild( this.zoomImg );
		
		
		
		// append cloned zoom image to body
		var obj_body = document.getElementsByTagName("body")[0];
		obj_body.appendChild( imgWithLink );
		
		
		// set position of new zoom div
		this.zoomImg.style.left		= this.getSourceImgLeft() +"px"; 
		this.zoomImg.style.top		= this.getSourceImgTop() +"px"; 
		
	},
 
	
	getLinkTarget: function( obj ) {
		var linkTarget = false;
		
		while( obj.tagName.toUpperCase() != "BODY" ) {
			if( obj.tagName.toUpperCase() == "A") {
				linkTarget = obj.href;
				break;
			}
			obj = obj.parentNode;
		}
		
		return linkTarget;
	},
	
	
	startCheckingForMouseOut: function() {
		this.checkForMouseOut = this.checkForMouseOut.bind(this)
		Event.observe( document.getElementsByTagName("body")[0], 'mousemove', this.checkForMouseOut, false);
	},
	
	
	checkForMouseOut: function( event ) {
		var y = Event.pointerY(event);
		var x = Event.pointerX(event);
		
		var imgSize = Element.getDimensions(this.sourceImg);
		var imgPos = Position.cumulativeOffset(this.sourceImg);
		
		if( y < imgPos[1] || y > Number(imgPos[1]) + Number(imgSize.height) || x < imgPos[0] || x > Number(imgPos[0]) + Number(imgSize.width)) {
			this.stopCheckingForMouseOut();
			this.zoomOut();
		}
	},
	
	
	stopCheckingForMouseOut: function() {
		Event.stopObserving( document.getElementsByTagName("body")[0], 'mousemove', this.checkForMouseOut);
	},
	
	
	
	zoomOut: function() {
		if( this.zoomed == false || this.zoomInProgress == true ) {
			return false;
		}
		
		if( this.detailTimer ) {
			clearTimeout( this.detailTimer );
		}

		this.zoomInProgress = true;
		this.zoomImg.style.zIndex = "99";
		
		var imgSize	= Element.getDimensions(this.sourceImg);
		var imgPos	= Position.cumulativeOffset(this.sourceImg);
		
		this.startZoom( imgSize.width, imgSize.height, this.getSourceImgLeft(), this.getSourceImgTop(), this.imgZoomOutSteps, this.imgZoomOutDuration, {complete: this.zoomOutAfter } );
		
	},
	
	
	zoomOutAfter: function(obj) {
		//obj.zoomImg.style.display = "none";
		obj.zoomImg.style.visibility = "hidden";
		
		obj.zoomInProgress = false;
		obj.zoomed = false;
		
		//obj.sourceImg.imageZoom = obj;
	},
	
	
	
	
	
	
	getScreenLeft: function() {
		return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0 ;
	},
	
	getScreenTop: function() {
		return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ;
	},
	
	getScreenWidth: function() // based on a script by projectseven.com (PVII)
	{
		var w = 0;
		
		if (window.innerWidth) { w = window.innerWidth; } // ns4
		else if(document.body)
		{
			w = document.body.clientWidth;
			if (document.body.offsetWidth == w && document.documentElement && document.documentElement.clientWidth)
			{
				w = document.documentElement.clientWidth;
			}
		}
		return w;
	},
	
	getScreenHeight: function() // based on a script by projectseven.com (PVII)
	{
		var h = 0;
		
		if (window.innerHeight) { h = window.innerHeight; } // ns4
		else if(document.body)
		{
			h = document.body.clientHeight;
			if (document.body.offsetHeight == h && document.documentElement && document.documentElement.clientHeight)
			{
				h = document.documentElement.clientHeight;
			}
		}
		return h;
	},
	
	
	zoomIn: function() {
		
		if( this.zoomed == true || this.zoomInProgress == true ) {
			return false;
		}
		this.zoomed = true;
		this.zoomInProgress = true;
		
		
		var imgSizeSrc = Element.getDimensions( this.sourceImg );
		
		var new_size = this.getNewImgSize( imgSizeSrc.width, imgSizeSrc.height, this.imgZoomSize);
		
		//if(this.zoomImg.style.display == "none")	this.zoomImg.style.display	= "inline";
		
		this.zoomImg.style.visibility	= "visible"
		
		this.zoomImg.style.zIndex	= "100";
		
		this.zoomImg.style.left		= this.getSourceImgLeft() +"px";
		this.zoomImg.style.top		= this.getSourceImgTop() +"px";
		
		var new_x = this.getSourceImgLeft() - Math.round( (new_size[0] - this.sourceImg.width) / 2);
		var new_y = this.getSourceImgTop() - Math.round( (new_size[1] - this.sourceImg.height) / 2);
		var new_w = new_size[0];
		var new_h = new_size[1];
		
		var deltaX =  this.getScreenLeft() -19;
   		var deltaY =  this.getScreenTop() -2;
   		
		var windowWidth = this.getScreenWidth();
		var windowHeight = this.getScreenHeight();
		
		//alert(deltaX +"x"+ deltaY +" | "+ windowWidth +"x"+ windowHeight);
		
		
        if(new_x < deltaX)	new_x = deltaX;
		if(new_y < deltaY)	new_y = deltaY;
                
        if(new_x+new_w > deltaX+windowWidth)	new_x = deltaX+windowWidth-new_w;
		if(new_y+new_h > deltaY+windowHeight)	new_y = deltaY+windowHeight-new_h;
		
		this.startZoom( new_w, new_h, new_x, new_y, this.imgZoomInSteps, this.imgZoomInDuration, {complete: this.zoomInAfter.bind(this) } );
	},
	
	
	zoomInAfter: function() {
		this.zoomInProgress = false;
		
		this.startCheckingForMouseOut();
	},
	
	
	getNewImgSize: function(width, height, max_size) {
		if(height > width) {
			var new_height = max_size;
			var factor = width / height;
			var new_width = Math.round(new_height * factor);
		}
		else {
			var new_width = max_size;
			var factor = height / width;
			var new_height = Math.round(new_width * factor);
		}
		
		return [new_width,new_height];
	},
	
	
	
	
	
	
	
	startZoom: function(w, h, x, y, duration, steps, options) {
		this.options  = options || {};
		
		this.steps = steps;
		this.stepDuration	= duration / steps;
		
		this.currentX = this.zoomImg.offsetLeft;
		this.currentY = this.zoomImg.offsetTop;
		this.currentW = this.zoomImg.offsetWidth;
		this.currentH = this.zoomImg.offsetHeight;
		
		this.wChange = (w - this.currentW) / steps;
		this.hChange = (h - this.currentH) / steps;
		this.xChange = (x - this.currentX) / steps;
		this.yChange = (y - this.currentY) / steps;
		
		this.sizeAndPosition();
	},
	
	
	

   sizeAndPosition: function() {
   	
      if ( this.steps <= 0 ) {
         if(this.options.complete) {
         	this.options.complete(this);
         }
         return;
      }

      if (this.timer) {
         clearTimeout(this.timer);
      }

      // Resize and reposition
      this.currentW += this.wChange;
      this.currentH += this.hChange;
      this.currentX += this.xChange;
      this.currentY += this.yChange;
      
      this.zoomImg.style.left	= this.currentX + "px";
      this.zoomImg.style.top	= this.currentY + "px";
      this.zoomImg.style.width	= this.currentW + "px";
      this.zoomImg.style.height	= this.currentH + "px";

      this.steps--;

      this.timer = setTimeout( this.sizeAndPosition.bind(this), this.stepDuration );
   }
   
   
}