var ie = document.all ? 1 : 0;
var w3c = document.getElementById ? 1 : 0;
var ns4 = document.layers ? 1 : 0;



var clientWitdh=874;
var maxwidth=512; // the value of the "maxwidth" statement in the URL
var maxheight=384; // the value of the "maxheight" statement in the URL


var imageBoxOffset;
var imagePosLeft=0; // Optional offset to move the image within the ImageBox
var imagePosTop=0;  // Optional offset to move the image within the ImageBox

var imagePosX=0; // x-Position der Raumszene inkl. Verschiebung, Rahmen, Ausmittelung
var imagePosY=0; // y-Position der Raumszene inkl. Verschiebung, Rahmen


var imageBorder=11;
var zoomFactor=0;
var aspectRatio=0;


var lensWidth=120;
var lensHeight=120;
var lensCursor='crosshair';


var imageBox, image, lens;
var X=0;
var Y=0;
var imageBoxX=0;
var imageBoxY=0;
var mouseImgX=0;
var mouseImgY=0;
var leftClip, topClip, rightClip, bottomClip;
var LENS_INACTIVE=0;
var LENS_REQUESTED=1;
var LENS_ACTIVE=2;
var lensMode=LENS_INACTIVE;
var currentZoom=1;
var newImageLoaded;
var save_onmousemove;




var imgmapSourceWidth;
var imgmapSourceHeight;

function setNewImageLoaded() {
	newImageLoaded=true;
}

function initializeZoom() {
	initialize();
}

function initialize() {

	if((window.opera) && !(navigator.userAgent.indexOf("7")!=-1)) {
		alert("Lens requires Opera Version 7 or higher");
		return;
	}
	
	if (newImageLoaded) {
		isBound=true;
		newImageLoaded=false;
		image = $('#ImageBox a>img');





		imageHeight=image.height();
		zoomFactor=imgmapSourceHeight/imageHeight;


		aspectRatio=imgmapSourceWidth/imgmapSourceHeight
		imageWidth=imageHeight*aspectRatio;

		imageBox = $('#ImageBox');
		imageBox.css('z-index','1');
		imageBox.css('clip.width', imageWidth+'px');
		imageBox.css('clip.height', imageHeight+'px');

		url=image.attr('src');




		url=url.replace(new RegExp("maxwidth="+maxwidth+"&maxheight="+maxheight), "scale=100");
		image.attr('src', url);
		$('#ImageBox a').attr('href', 'url');
		
		minimize();
	}
	
	if(ns4) {document.captureEvents(Event.MOUSEMOVE);}
}


function startLens() {
	if (lensMode==LENS_INACTIVE)
	{
        lensMode=LENS_REQUESTED;
	    initialize();
	    minimize();


        imageBox.append('<div id="LensBox" style="position:absolute; left:auto; top:auto; cursor:'+lensCursor+';clip:rect(0 0 0 0); z-index:3;"><img src="'+image.attr("src")+'" width="'+(imageWidth*zoomFactor)+'" height="'+(imageHeight*zoomFactor)+'" alt="'+image.attr("alt")+'" title="'+image.attr("title")+'"></div>');
	    lens = $('#LensBox');
	    lens.click(function() {maxFocus();});
	    intervalID = setInterval("cut()", 10);
	    save_onmousemove = document.onmousemove;
	    document.onmousemove = pos;
	}
}


function stopLens() {
	if (lensMode==LENS_ACTIVE) {
		lens.css('visibility', 'hidden');    // Grosse Grafik unsichtbar machen
	    lens.remove();
		clearInterval(intervalID);
        document.onmousemove = save_onmousemove;
		lensMode=LENS_INACTIVE;
	}
}


function maximize() {
	initialize();
    imageBox.css('overflow', 'scroll');
    image.width(imageWidth * zoomFactor);
    image.height(imageHeight * zoomFactor);
	currentZoom = zoomFactor;
	scaleImageMap(calculateImageMapScaleFromZoom());
}


function minimize() {

	if(typeof image=="undefined") return;
	
	image.width(imageWidth);
	image.height(imageHeight);
	imageBox.css('overflow', 'hidden');
	imageBox.scrollLeft(0).scrollTop(0);
	currentZoom = 1;
	scaleImageMap(calculateImageMapScaleFromZoom());
}


function zoomIn() {
	initialize();
	imageBox.css('overflow', 'scroll');
	if(currentZoom < zoomFactor) {
		currentZoom = currentZoom + 0.1;
		image.width(Math.round(imageWidth * currentZoom));
		image.height(Math.round(imageHeight * currentZoom));
		scaleImageMap(calculateImageMapScaleFromZoom());
	} else {
		maximize();
	}
}


function zoomOut() {
	initialize();
	if(currentZoom > 1) {
		currentZoom = currentZoom - 0.1;
		image.width(Math.round(imageWidth * currentZoom));
		image.height(Math.round(imageHeight * currentZoom));
		scaleImageMap(calculateImageMapScaleFromZoom());
	} else {
		minimize();
	}
}


function maxFocus() {
	stopLens();
	maximize();
	$('#ImageBox a').css("position", 'absolute');
	$('#ImageBox a').css("left",  0);
	
	imageBox.scrollLeft(mouseImgX*(zoomFactor-1)).scrollTop(mouseImgY*(zoomFactor-1));
}


function imageMapDefined(imgWidth, imgHeight){
  imgmapSourceWidth=imgWidth;
  imgmapSourceHeight=imgHeight;
}

function calculateImageMapScale(maxwidth, maxheight) {
  if (typeof maxwidth == "undefined") {
    maxwidth = -1;
  }
  if (typeof maxheight == "undefined") {
    maxheight = -1;
  }
  
  var scaleX=maxwidth>0 ? maxwidth/imgmapSourceWidth : 1.0;
  var scaleY=maxheight>0 ? maxheight/imgmapSourceHeight : 1.0;
  
  var scale=1.0;
  if(scaleX<=scaleY && scaleX<1) scale=scaleX;
  else if(scaleY<scaleX && scaleY<1) scale=scaleY;
  
  return scale;
}

function calculateImageMapScaleFromZoom() {





  return currentZoom/zoomFactor;
}

function calculateImagePosition(scale, imgmapSourceWidth, imgMaxWidth){
	if (isCentered()) {
        var imgWidth = scale* imgmapSourceWidth;
        var imageBoxWidth = $('#ImageBox').width();
        var calcWidth = (imageBoxWidth < imgMaxWidth)?imgMaxWidth:imageBoxWidth;
        imagePosX =((calcWidth-imgWidth-(2*imageBorder))/2)+imagePosLeft+imageBorder;
	}
	else {
        imagePosX = imagePosLeft+imageBorder;
	}
    imagePosY = imagePosTop + imageBorder;
}

var currentImageMapScale=1.0;

function scaleImageMap(scale) {
  currentImageMapScale=scale;
  calculateImagePosition(scale, imgmapSourceWidth, maxwidth);
  var targetMap=$("map[name='imgmap']");
  targetMap.empty();
  
  $("map[name='imgmapSource']").children('area').each(function() {
    var clone=$(this).clone(true);
    var coords=clone.attr('coords').split(',');
    for(var i=0; i<coords.length; i++) {
      coords[i]=Math.round(parseInt(coords[i])*scale);
    }
    clone.attr('coords', coords.join(','));
    targetMap.append(clone);
  });
}

function getImageMapScale() {
	return currentImageMapScale;
}

/*

 */
function pos(e) {
    if (lensMode>LENS_INACTIVE) {
    	
        if(!e) e = window.event; // Event-Definition fuer IE


        imageBoxOffset = $('#ImageBox').offset();


        var doc = document, body = doc.body, docElem = doc.documentElement,
        top  = (self.pageYOffset || jQuery.boxModel && docElem.scrollTop  || body.scrollTop ),
        left = (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft);
        imageBoxX = ((ns4)?e.pageX:e.clientX) + left - imageBoxOffset.left;
        imageBoxY = ((ns4)?e.pageY:e.clientY) + top - imageBoxOffset.top;
        


        mouseImgX = imageBoxX - getImagePosX();
        mouseImgY = imageBoxY - getImagePosY();
        



        X = Math.round(mouseImgX * zoomFactor);
        Y = Math.round(mouseImgY * zoomFactor);

        leftClip = X - lensWidth/2;  
        topClip = Y - lensHeight/2;  
        rightClip = X + lensWidth/2;
        bottomClip = Y + lensHeight/2;
    }
}


function cut() { // Clipping und Verschieben der grossen Grafik
    if (lensMode>LENS_INACTIVE) {
	    if(mouseImgX > 0 && mouseImgX < imageWidth && mouseImgY > 0 && mouseImgY < imageHeight) {
			if(ie||w3c) { // Fuer IE 5+, Mozilla, Netscape 6+ und Opera 7 // Clipping
                lens.css('clip', 'rect(' + topClip + 'px ' + rightClip +'px '+ bottomClip +'px '+ leftClip +'px)');
			}
			else if(ns4) { // Fuer NS 4
				lens.css('clip.left', leftClip);
				lens.css('clip.top', topClip);
				lens.css('clip.right', rightClip);
				lens.css('clip.bottom', bottomClip);
			}

            lens.css('left', getImagePosX() - (mouseImgX*(zoomFactor-1)) + 'px');
            lens.css('top', getImagePosY() - (mouseImgY*(zoomFactor-1)) + 'px');
            lens.css('visibility', 'visible');
            
			lensMode=LENS_ACTIVE;
	    }
	    else
	    {
	        stopLens();
	    }
   	}
}

/**

 */
function getImagePosX() {
	return imagePosX;
}

function getImagePosY() {
    return imagePosY;
}

function setImagePosX(posX) {
    return imagePosX=posX;
}

function setImagePosY(posY) {
    return imagePosY=posY;
}

