
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;
var ns4 = (document.layers) ? true : false;

var zi_mouseDownX = -1, zi_mouseUpX = -1, zi_mouseDownY = -1, zi_mouseUpY = -1;
var zi_handlerFunction = null;
var zi_started = false, zi_dragActive = false;
var zi_bgLyr;
var zi_lineWidth = 1, zi_lineColor = 'black';
var zi_ds, zi_lib, zi_SG = null;
var zi_xmax, zi_ymax, zi_mapLeft, zi_mapTop, zi_mapWidth, zi_mapHeight;
var zi_lines = new Array(4);
var offSetX, offSetY, SG_id;

function ZoomInTool (mapX_min, mapY_min, mapWidth, mapHeight) {
	var	zi_bgLyrContent = '<img id="zi_bgImg" name="zi_bgImg" border=0  src="' + toolImgURL + 'spacer.gif" vspace="0" hspace="0"' +
				' width=' + mapWidth + ' height=' + mapHeight + 
				' style="POSITION:ABSOLUTE; LEFT:0; TOP:0; cursor:crosshair' +
				';" usemap="">';
	zi_xmax = mapX_min + mapWidth;
	zi_ymax = mapY_min + mapHeight;
	zi_mapLeft = mapX_min;
	zi_mapTop = mapY_min;
	zi_mapWidth = mapWidth;
	zi_mapHeight = mapHeight;

	if (ie4 || ie5) {
	 	offSetX = Math.round(zi_mapWidth/2) + zi_mapLeft;
		offSetY = Math.round(zi_mapHeight/2) + zi_mapTop;
		SG_id = "zoomin_SG";
		
		createLayer('zi_bgLyr', null, mapX_min, mapY_min, mapWidth, mapHeight, zi_bgLyrContent, '', 'hidden');
		zi_addSGObj(false);
		zi_bgLyr = document.all['zi_bgLyr'];
		this.bgImg = document.all['zi_bgImg'];

	}
	
    zi_bgLyr.onmousedown = zoomintoolMouseDown;
    zi_bgLyr.onmouseup = zoomintoolMouseUp;
    zi_bgLyr.onmousemove = zoomintoolMouseMove;

	this.addListener = zoomintoolAddListener;
	this.start = zoomintoolStart;
	this.stop = zoomintoolStop;
	this.clearAllLines = zoomintoolClearAllLines;
	this.setLineColor = zoomintoolSetLineColor;
	this.setLineWidth = zoomintoolSetLineWidth;
	this.getMouseUpX = zoomintoolGetMouseUpX;
	this.getMouseUpY = zoomintoolGetMouseUpY;
	this.getMouseDownX = zoomintoolGetMouseDownX;
	this.getMouseDownY = zoomintoolGetMouseDownY;
	this.getStartPoint = zoomintoolGetStartPoint;
	this.getEndPoint = zoomintoolGetEndPoint;
	this.setUseMap = zoomintoolSetUseMap;
	
}

function zi_addSGObj (removeOld) {
	var objStr = '';
	if (removeOld) {
		destroyLayer("zi_lyr");
	}	
	createLayer('zi_lyr', null, zi_mapLeft, zi_mapTop, zi_mapWidth, zi_mapHeight, objStr, '');
	jg3 = new jsGraphics("zi_lyr");

	//zi_SG = document.all[SG_id];
	//zi_SG.Translate(-offSetX, -offSetY, -1);
	//zi_ds = document.all[SG_id].DrawSurface;
	//zi_lib = document.all[SG_id].Library;
}

function zoomintoolSetUseMap (imageMapID) {
	if (imageMapID.substring(0, 1) != "#") {
		imageMapID = "#" + imageMapID;
	}
	this.bgImg.useMap = imageMapID;
}
function zoomintoolAddListener (handlerFunction) {
	if (handlerFunction != null || handlerFunction != '') {
		zi_handlerFunction = handlerFunction;
		if (zi_handlerFunction.lastIndexOf(')') == -1) {
			zi_handlerFunction += '()';
		} 
	}
}
function zoomintoolStart () {
	jg3.setColor(zi_lineColor);
	
	zoomintoolShowHideBgLyr(true);
	zi_started = true;
}
function zoomintoolStop () {
	zoomintoolShowHideBgLyr(false);
	zi_started = false;
}
function zoomintoolShowHideBgLyr (show) {
	if (ie4 || ie5 || ns6) {
		zi_bgLyr.style.visibility = (show)? 'visible' : 'hidden';
	} else if (ns4) {
		zi_bgLyr.visibility = (show) ? 'show' : 'hide'; 
	} 
}

function zoomintoolClearAllLines () {
	jg3.clear(); 
}

function zoomintoolSetLineWidth (newLineWidth) {
	zi_lineWidth = newLineWidth;
}
function zoomintoolSetLineColor (newColor) {
	zi_lineColor = newColor;
}

function zoomintoolGetMouseDownX () {
	return (zi_mouseDownX-zi_mapLeft);
}
function zoomintoolGetMouseUpX () {
	return (zi_mouseUpX-zi_mapLeft);
}
function zoomintoolGetMouseDownY () {
	return (zi_mouseDownY-zi_mapTop);
}
function zoomintoolGetMouseUpY () {
	return (zi_mouseUpY-zi_mapTop);
}
function zoomintoolGetStartPoint () {
	return new Arrary(zi_mouseDownX-zi_mapLeft, zi_mouseDownY-zi_mapTop);
}
function zoomintoolGetEndPoint () {
	return new Array(zi_mouseUpX-zi_mapLeft, zi_mouseUpY-zi_mapTop);
}

function zoomintoolDrawRect (x, y) {
	var w, h, wmax, hmax;

	if (ie4 || ie5) {
		//zi_ds.fillStyle(0);	
		//zi_ds.BorderColor(zi_lib[zi_lineColor]);
		//zi_ds.borderWidth(zi_lineWidth);
	
		w = x - zi_mouseDownX;
		h = y - zi_mouseDownY;			
		(w > 0)? (wmax = zi_xmax - zi_mouseDownX):(wmax = zi_mouseDownX - zi_mapLeft);
		(h > 0)? (hmax = zi_ymax - zi_mouseDownY):(hmax = zi_mouseDownY - zi_mapTop);  
	
		if (w > wmax) 	w = wmax;
		if (h > hmax) 	h = hmax;
		//zi_ds.Rect(zi_mouseDownX, zi_mouseDownY, w, h);
		//zi_SG.DrawSurface = zi_ds;
		//zi_ds.clear();
		
		zi_mouseDownY_new = zi_mouseDownY
		zi_mouseDownX_new = zi_mouseDownX
		if (w < 0 ) 
		{
			zi_mouseDownX_new = zi_mouseDownX + w
			w = w * -1	
		}
		if (h < 0)
		{
			zi_mouseDownY_new = zi_mouseDownY + h
			h = h * -1
		}
		
	 	zoomintoolClearAllLines()
		jg3.fillRect(zi_mouseDownX_new-zi_mapLeft, zi_mouseDownY_new-zi_mapTop, w, h); 
		jg3.paint();
	}
}

function zoomintoolShowHideDrawLyr (show) {
	if (show) {
		for (var i=0; i < zi_lines.length; i++) {
			zi_lines[i].show();
		}	
	} else {
		for (var i=0; i < zi_lines.length; i++) {
			zi_lines[i].hide();
		}
	}

}
function zoomintoolMouseDown (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x+document.body.scrollLeft;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;
		if (zi_started) {
			zi_mouseDownX = x;
			zi_mouseDownY = y;
			zi_dragActive = true;
			
		}
		return false;
	}
	return true;
}

function zoomintoolMouseUp (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x+document.body.scrollLeft;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;		
		if (zi_started) {
			zi_mouseUpX = x;
			zi_mouseUpY = y;
			zi_dragActive = false;
			// kaldes af handler funktionen og evt. slet linierne
			if (zi_handlerFunction != null) {
				eval(zi_handlerFunction);
			}
		}
		return false;
	}
	return true;
}

function zoomintoolMouseMove (e) {
	if ((ns4 && e.which == 1) || (ns6 && e.button == 0) || ((ie4 || ie5) && window.event.button == 1)) {
    	var x = (ns4 || ns6)? e.pageX : event.x+document.body.scrollLeft;
        var y = (ns4 || ns6)? e.pageY : event.y+document.body.scrollTop;	
		
		if (zi_started && zi_dragActive) {
			zoomintoolDrawRect(x,y);
		}
		return false;
	}
	return true;	
}
