function windowSize(){
	var B=0,A=0;
	if(typeof (window.innerWidth)=="number"){
		B=window.innerWidth;
		A=window.innerHeight
	}else{
		if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
			B=document.documentElement.clientWidth;
			A=document.documentElement.clientHeight
		}else{
			if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
				B=document.body.clientWidth;
				A=document.body.clientHeight
			}
		}
	}
	return{height:A,width:B}
}

function openZoomViewer(A){
	GK_ZoomViewer.open(A)
}

function MM_findObj(E,D){
	var C,B,A;if(!D){D=document}
	if((C=E.indexOf("?"))>0&&parent.frames.length){
		D=parent.frames[E.substring(C+1)].document;
		E=E.substring(0,C)
	}
	if(!(A=D[E])&&D.all){
		A=D.all[E]
	}
	for(B=0;!A&&B<D.forms.length;B++){
		A=D.forms[B][E]
	}
	for(B=0;!A&&D.layers&&B<D.layers.length;B++){
		A=MM_findObj(E,D.layers[B].document)
	}
	if(!A&&D.getElementById){
		A=D.getElementById(E)
	}
	return A
}

function swapImage(){
	var D,C=0,A,B=swapImage.arguments;
	document.MM_sr=new Array;
	for(D=0;D<(B.length-2);D+=3){
		if((A=MM_findObj(B[D]))!=null){
			document.MM_sr[C++]=A;
			if(!A.oSrc){
				A.oSrc=A.src
			}
			A.src=B[D+2];
			A.alt=B[D+2]
		}
	}
};

GK_ImageZoomer={
	minWidth:50,
	maxWidth:2400,
	zoomIn:function(E,A){
			if($(E).getWidth()>=this.maxWidth){
				return 
			}
			if(!A){
				A={
					x:($(E).up().getWidth())/2,
					y:($(E).up().getHeight())/2
				}
			}
			var F={
				height:Math.round($(E).getHeight()),
				width:Math.round($(E).getWidth()),
				x:Position.positionedOffset($(E))[0],
				y:Position.positionedOffset($(E))[1]};
			var G={
				x:(F.width*1.1)-F.width,
				y:(F.height*1.1)-F.height};
				var C={x:(A.x-F.x)/F.width,
				y:(A.y-F.y)/F.height};
			var B=F.y-(G.y*C.y);
			var D=F.x-(G.x*C.x);
			$(E).setStyle({
				width:(F.width*1.1)+"px",
				top:B+"px",left:D+"px"})
				
				var leftPixel = (GK_InnerZoom.targetWidth/2)-(Element.getWidth(E)/2);
				var topPixel = (GK_InnerZoom.targetHeight/2)-(Element.getHeight(E)/2);
				//Position the image in the center of the div
				$(E).setStyle({
					top:topPixel+"px",					
					left:leftPixel+"px"
				});					
			},
			zoomOut:function(E,A){
				if($(E).getWidth()<=GK_InnerZoom.imageWidth){
					return 
				}
				if(!A){
					A={
						x:($(E).up().getWidth())/2,
						y:($(E).up().getHeight())/2
					}
				}
				var F={
					height:Math.round($(E).getHeight()),
					width:Math.round($(E).getWidth()),
					x:Position.positionedOffset($(E))[0],
					y:Position.positionedOffset($(E))[1]
				};
				var G={
					x:F.width-(F.width*0.85),
					y:F.height-(F.height*0.85)
				};
				var C={
					x:(A.x-F.x)/F.width,
					y:(A.y-F.y)/F.height
				};
				var B=F.y+(G.y*C.y);
				var D=F.x+(G.x*C.x);
				$(E).setStyle({
					width:(F.width*0.85)+"px"});
				var leftPixel = (GK_InnerZoom.targetWidth/2)-(Element.getWidth(E)/2);
				var topPixel = (GK_InnerZoom.targetHeight/2)-(Element.getHeight(E)/2);
				//Position the image in the center of the div
				$(E).setStyle({
					top:topPixel+"px",					
					left:leftPixel+"px"
				});						
			},
			moveUp:function(B){
				var A=Position.positionedOffset($(B))[1]-60;$(B).setStyle({top:A+"px"})
			},
			moveDown:function(B){
				var A=Position.positionedOffset($(B))[1]+60;$(B).setStyle({top:A+"px"})
			},
			moveLeft:function(B){
				var A=Position.positionedOffset($(B))[0]-60;$(B).setStyle({left:A+"px"})
			},
			moveRight:function(B){
				var A=Position.positionedOffset($(B))[0]+60;$(B).setStyle({left:A+"px"})
			},
			moveCenter:function(A){}
	};
	GK_InnerZoom={
		container:"innerZoom",
		standardWidth:250,
		standardHeight:200,
		standardTop:10,
		standardLeft:10,
		width:200,
		height:300,
		top:10,
		left:10,
		targetWidth:700,	
		targetHeight:300,
		targetTop:10,
		targetLeft:10,
		large:false,
		inserted:false,
		running:false,
		processId:null,
		imageWidth:200,
		swapImage:function(B,A){
			$(B).src=$(A).alt;
			$(B).alt=$(A).parentNode.href
		},
		open:function(fullScreen){
			if(window.addEventListener){
				window.addEventListener("DOMMouseScroll",GK_InnerZoom.wheel,false)
			}
			window.onmousewheel=document.onmousewheel=GK_InnerZoom.wheel;

			var top = document.getElementById('imageViewer_offset_top');
			var bottom = document.getElementById('imageViewer_offset_bottom');

			if( fullScreen ) {
				this.targetWidth=windowSize().width-50;
				this.targetTop=15;
				this.targetLeft=15;
				this.targetHeight=windowSize().height-50;			
				$("innerZoomImage").src=$("mainImage").alt;
				this.showLocalZoom("mainImage", true);
				$("zoomFullscreen").hide();
				$("zoomNoFullscreen").show();
			} else if(top==null || bottom==null){
				this.targetWidth=windowSize().width-50;
				this.targetTop=15;
				this.targetLeft=15;
				this.targetHeight=windowSize().height-50;			
				$("innerZoomImage").src=$("mainImage").alt;
				//$("innerZoomImage").onload=this.adjustImage;												
				this.showLocalZoom("mainImage", true);
				$("zoomNoFullscreen").hide();
				$("zoomFullscreen").show();
			}else{
				this.targetWidth=(Element.getWidth("imageViewer_offset_bottom")-5);
				this.targetTop=Position.cumulativeOffset($("imageViewer_offset_top"))[1];
				this.targetLeft=Position.cumulativeOffset($("imageViewer_offset_bottom"))[0];
				this.targetHeight=Position.cumulativeOffset($("imageViewer_offset_bottom"))[1]-((Position.cumulativeOffset($("imageViewer_offset_top"))[1])+5);						
				$("innerZoomImage").src=$("mainImage").alt;
				this.showLocalZoom("mainImage", true);
				$("zoomNoFullscreen").hide();
				$("zoomFullscreen").show();
			}			
			return false
		},
		init:function(){			
			if( $("enlargeImage") ) {
				($("enlargeImage")).onclick=function(){ 
					GK_InnerZoom.open();
					return false
				};	
			}
			if($("mainImage")){			
				/* Should have been $("mainImage").up() */
				($("mainImage")).onclick=function(){ 
					GK_InnerZoom.open();
					return false
				};					
				var C='<div id="innerZoom" style="z-index: 9996; overflow: hidden; border: 1px solid #000; position:absolute; background-color: #fff">';
				C+='	<a style="z-index: 1001" id="zoomClose" onClick="GK_InnerZoom.showLocalZoom();"><span>close</span></a>';
				C+='	<a style="z-index: 1002" id="zoomFullscreen" onClick="GK_InnerZoom.open(true);"><span>fullscreen</span></a>';				
				C+='	<a style="z-index: 1003" id="zoomNoFullscreen" onClick="GK_InnerZoom.open();"><span>nofullscreen</span></a>';				
				C+='	<a style="z-index: 1004" id="innerZoomIn" onClick="GK_ImageZoomer.zoomIn($(\'innerZoomImage\'))"><span>Zoom In</span></a>';
				C+='	<a style="z-index: 1005" id="innerZoomOut" onClick="GK_ImageZoomer.zoomOut($(\'innerZoomImage\'))"><span>Zoom Out</span></a>';
				C+='	<div id="zoomViewerImage"><img id="innerZoomImage" src="'+$("mainImage").alt+'"></div>';
				C+="</div>";
   		    	new Insertion.Top("page",C);
				var B=Position.cumulativeOffset($("mainImage"));			
				this.standardTop=B[1];
				this.standardLeft=B[0];
				this.width=this.standardWidth;
				this.height=this.standardHeight;
				this.top=this.standardTop;
				this.left=this.standardLeft;
				this.targetWidth=Element.getWidth("imageViewer_offset_bottom");
				this.targetTop=Position.cumulativeOffset($("imageViewer_offset_top"))[1];
				this.targetLeft=Position.cumulativeOffset($("imageViewer_offset_bottom"))[0];
				this.targetHeight=Position.cumulativeOffset($("imageViewer_offset_bottom"))[1]-((Position.cumulativeOffset($("imageViewer_offset_top"))[1])+5);						
				this.imageWidth = Element.getWidth("innerZoomImage");
				$("innerZoom").setStyle({
					width:this.standardWidth+"px",
					height:this.standardHeight+"px",
					top:this.standardTop+"px",
					left:this.standardLeft+"px"});
				$("innerZoom").hide();				
				var A=$("thumbViewer").getElementsBySelector("table.thumbnail");
				for(i=0;i<A.length;i++){
					var id = A[i].getElementsBySelector("img.thumb")[0].id;
					eval("A[i].onmouseover=function(){GK_InnerZoom.swapImage('mainImage','"+ id + "')};");
					A[i].onclick=function(){
						GK_InnerZoom.open();
						return false
					}
				}	
			}
		},
			showLocalZoom:function(A, resize){
				if(!this.large || resize ){
					this.running=true;
					this.large=true;			
					//$("innerZoomImage").src=$("mainImage").alt;
					$("innerZoom").setStyle({background:"#fff"});
					$("innerZoom").show();
					$("innerZoomImage").makePositioned();											
					$("innerZoomImage").show();
					this.processId=window.setInterval(
						function(){
							if(Math.abs(GK_InnerZoom.targetWidth-GK_InnerZoom.width)<1){
								GK_InnerZoom.stopAnimation();
								GK_InnerZoom.adjustImage();
								new Draggable("innerZoomImage",{
									starteffect:function(){},
									endeffect:function(){}
								})								
							}
							var C=GK_InnerZoom.getAnimatedDim()[0];
							var E=GK_InnerZoom.getAnimatedDim()[1];
							var D=GK_InnerZoom.getAnimatedDim()[2];
							var B=GK_InnerZoom.getAnimatedDim()[3];
						
							GK_InnerZoom.width=C;
							GK_InnerZoom.height=E;
							GK_InnerZoom.top=D;
							GK_InnerZoom.left=B;
							$("innerZoom").setStyle({
								width:C+"px",
								height:E+"px",
								top:D+"px",
								left:B+"px"})						
						},30)
				} else {
					if(!this.running){
						this.large=false;
						this.targetWidth=this.standardWidth;
						this.targetHeight=this.standardHeight;
						this.targetTop=this.standardTop;
						this.targetLeft=this.standardLeft;
						$("innerZoomImage").hide();
						$("innerZoom").setStyle({background:"transparent"});
						this.processId=window.setInterval(function(){
							if(Math.abs(GK_InnerZoom.targetWidth-GK_InnerZoom.width)<1){
								GK_InnerZoom.stopAnimation();
								$("innerZoom").hide();
								try {
									window.removeEventListener("DOMMouseScroll",GK_InnerZoom.wheel,false);
								} catch( e ) {}
								window.onmousewheel=document.onmousewheel=null
							}
							var C=GK_InnerZoom.getAnimatedDim()[0];
							var E=GK_InnerZoom.getAnimatedDim()[1];
							var D=GK_InnerZoom.getAnimatedDim()[2];
							var B=GK_InnerZoom.getAnimatedDim()[3];
							GK_InnerZoom.width=C;
							GK_InnerZoom.height=E;
							GK_InnerZoom.top=D;
							GK_InnerZoom.left=B;
							$(GK_InnerZoom.container).setStyle({
								width:C+"px",
								height:E+"px",
								top:D+"px",
								left:B+"px"})
						},30)
					}
				}
			},
			adjustImage:function() {

				var divWidth = Element.getWidth("innerZoom"); 
				var divHeight = Element.getHeight("innerZoom");
				this.imageWidth = Element.getWidth("innerZoomImage");
				var imageHeight = Element.getHeight("innerZoomImage");

				var newWidth = 0;
				
				var scaleImageX = false;
				var scaleImageY = false;

			
				if( (0.9*divWidth) < this.imageWidth ) {
					scaleImageX = true;
				}
				
				if( (0.9*divHeight) < imageHeight  ) {
					scaleImageY = true;
				}		
				
				if( scaleImageX && scaleImageY ) {
					//Scale on X first - if not enough then scale on Y
					this.imageWidth = (divWidth*0.9);
					$("innerZoomImage").setStyle({
						width:this.imageWidth+"px"
					});		
					
					//Recalculate image height
					imageHeight = Element.getHeight("innerZoomImage");						
					
					if( (scale*divHeight) < imageHeight ) {
						//Further scaling required
						//Set the height to 90% of the div height
						var newHeight = (divHeight*0.9);
						var scale = ((100/(imageHeight/newHeight))/100);		
						
						this.imageWidth = (this.imageWidth*scale);
						$("innerZoomImage").setStyle({
							width:this.imageWidth+"px"
						});							
					}
				} else if( !scaleImageX && scaleImageY ) {
					//Set the height to 90% of the div height
					var newHeight = (divHeight*0.9);
					var scale = ((100/(imageHeight/newHeight))/100);		
					
					this.imageWidth = (this.imageWidth*scale);
					$("innerZoomImage").setStyle({
						width:this.imageWidth+"px"
					});						
				} else if( scaleImageX && !scaleImageY ) {
					//Set the width of the image to be 90% of the div width
					this.imageWidth = (divWidth*0.9);
					$("innerZoomImage").setStyle({
						width:this.imageWidth+"px"
					});			
				}

				//Center the image...
				var leftPixel = (divWidth/2)-(Element.getWidth("innerZoomImage")/2);
				var topPixel = (divHeight/2)-(Element.getHeight("innerZoomImage")/2);
				//Position the image in the center of the div
				$("innerZoomImage").setStyle({
					top:topPixel+"px",					
					left:leftPixel+"px"
				});
			},
			stopAnimation:function(){
				if(this.processId){
					window.clearInterval(this.processId);
					this.processId=null;
					this.running=false
				}
			},
			getAnimatedDim:function(){
				var A=4;
				if(this.targetWidth>this.width){
					return[Math.ceil((this.targetWidth-this.width)/A)+this.width,Math.ceil((this.targetHeight-this.height)/A)+this.height,Math.ceil((this.targetTop-this.top)/A)+this.top,Math.ceil((this.targetLeft-this.left)/A)+this.left]
				}else{
					return[Math.floor((this.targetWidth-this.width)/A)+this.width,Math.floor((this.targetHeight-this.height)/A)+this.height,Math.floor((this.targetTop-this.top)/A)+this.top,Math.floor((this.targetLeft-this.left)/A)+this.left]
				}
			},
			getCenter:function(){
				return{
					x:((this.width)/2)+this.left,
					y:((this.height)/2)+this.top}
			},
			handle:function(B,A){
				if(B<0){
					GK_ImageZoomer.zoomOut("innerZoomImage")					
				}else{
					GK_ImageZoomer.zoomIn("innerZoomImage")
				}
			},
			wheel:function(A){
				var B=0;
				if(!A){
					A=window.event
				}
				if(A.wheelDelta){
					B=A.wheelDelta/120;
					if(window.opera){
						B=-B
					}
				}else{
					if(A.detail){
						B=-A.detail/3
					}
				}
				if(Event.element(A).id+""=="innerZoom"||Event.element(A).id+""=="innerZoomImage"){
					if(B){
						GK_InnerZoom.handle(B,A)
					}
					A.returnValue=false;
					A.preventDefault()
				}else{
					A.returnValue=true
				}
			}
		};
	Event.observe(window,"load",function(){GK_InnerZoom.init()});	
