var reloadTimer = null;
var new_win;
var bImageReceived  = false;
var FULL_ZOOM_MINX = 2115724.228406568;
var FULL_ZOOM_MINY = 4669293.93797549;
var FULL_ZOOM_MAXX = 2762926.24989933;
var FULL_ZOOM_MAXY = 5155774.00008557;
//*********************************************************************************************************
function getMouseXY(slika, e){
	if (document.all){  // IE
		if ((e.offsetX != e.x) || (e.offsetY != e.y))
			return;
		mX = e.offsetX;
		mY = e.offsetY;
		return;
	}
	if (document.layers){  // NN 4.x
		mX = e.x;
		mY = e.y;
	}
	if ((!document.all)&&(!document.layers)){	//NN 6
		mX = e.layerX;
		mY = e.layerY;
	}	
	return;
}
//*********************************************************************************************************
function stakloOnMouseDown(slika, e){

	if (bImageReceived == false)
		return false;
		
	var downCursor = "";
	getMouseXY(slika, e);
	mX1 = mX;
	mY1 = mY;
	mX2 = mX1;
	mY2 = mY1;
	updateFrame = true;
						
	switch (currTool){
		case "zoomin":			
			drawFrame();
			break;
		case "zoomout":
			drawFrame();
			break;
		case "selectfeature":
			drawFrame();
			break;
		case "pan":
			sloj = getLayer("mapLayer");
			moveLayer(sloj);
			break;
	}	
	
	e.cancelBubble = true;
	e.returnValue = false;
			
	return false;
}
//*********************************************************************************************************
function moveLayer(sloj){

	if (!updateFrame) 
		return false;
		
	if (document.layers){
		sloj.moveTo(mX2-mX1, mY2-mY1);
	}
	if (document.getElementById){
		sloj.height = imageHeight - ((mY2-mY1)>0 ? (mY2 - mY1) : 0);
		sloj.width = imageWidth - ((mX2-mX1)>0 ? (mX2 - mX1) : 0);
		sloj.top = mY2 - mY1;
		sloj.left = mX2 - mX1;
	}
	return false;
}
//*********************************************************************************************************			
function drawFrame(){

	if (!updateFrame) 
		return false;

	var x1, x2, y1, y2;
	
	if (mX1>mX2){
		x1 = mX2;
		x2 = mX1;
	}
	else {
		x1 = mX1;
		x2 = mX2;
	}

	if (mY1>mY2){
		y1 = mY2;
		y2 = mY1;
	}
	else {
		y1 = mY1;
		y2 = mY2;
	}
		
	clipLayer(getLayer("frTop"), x1, x2, y1, y1+2);
	clipLayer(getLayer("frBottom"), x1, x2, y2-2, y2);
	clipLayer(getLayer("frLeft"), x1, x1+2, y1, y2);
	clipLayer(getLayer("frRight"), x2-2, x2, y1, y2);
			
	return false;
}
//*********************************************************************************************************		
function clipLayer(sloj, left, right, top, bottom){
	if (document.getElementById){ // zaobilazimo sloj i izravno pozivamo "ID"-ove
		sloj.clip = "rect(" + top + " " + right + " " + bottom + " " + left + ")";
		sloj.visibility = "visible";
	}
	
	if (document.layers){ // zaobilazimo sloj i izravno pozivamo "ID"-ove
		sloj.clip.top = top;
		sloj.clip.bottom = bottom;
		sloj.clip.left = left;
		sloj.clip.right = right;
		sloj.visibility = "visible";
	}
	
	return false;
}
//*********************************************************************************************************		
function stakloOnMouseMove(slika, e){
	
	if (bImageReceived == false)
		return false;
	
	var pixelWidth = (maxx-minx) / imageWidth;
	var pixelHeight = (maxy-miny) / imageHeight;
	var x = minx + mX * pixelWidth;
	var y = maxy - mY * pixelHeight;

	getMouseXY(slika, e);
	showPositon(e);
	mX2 = mX;
	mY2 = mY;
	
	switch (currTool){
		case "zoomin":			
			drawFrame();
			break;
		case "zoomout":			
			drawFrame();
			break;
		case "selectfeature":			
			drawFrame();
			break;
		case "pan":
			sloj = getLayer("mapLayer");
			moveLayer(sloj);
			break;
		case "measure":
			measureMouseMove();
			break;
		
	}			
	
	return false;
}	
//*********************************************************************************************************		
function HideZoomFrame(){
	getLayer("frTop").visibility = "hidden";
	getLayer("frBottom").visibility = "hidden";
	getLayer("frLeft").visibility = "hidden";
	getLayer("frRight").visibility = "hidden";
}
//*********************************************************************************************************
function stakloOnMouseUp(slika, e){	

	if (bImageReceived == false)
		return false;

	var cmd;			
	updateFrame = false;
			
			
	if(isNaN(mX1) || isNaN(mX2)){ //Nisu uhvacene pocetne koordinate
		return false;
	}			
			
	switch (currTool) {
		case "zoomin":
			ZoomIn(mX1,mX2,mY1,mY2); //ZoomIn.ascx
			HideZoomFrame();
			break;
		case "zoomout":
			ZoomOut(mX1,mX2,mY1,mY2);
			HideZoomFrame();
			break;
		case "pan":
			cmd = "cmd=160";
			cmd += GetPanExtent("mappan");
			cmd += "&layers=" + layers;
			command(cmd);
			if (!document.all){
				sloj = getLayer("mapLayer");
				sloj.top = 0;
				sloj.left = 0;
				sloj.height = imageHeight;
				sloj.width = imageWidth;
			}
			break;
		case "identify":
			cmd = "cmd=300";
			cmd += "&layers=" + layers;
			mX2 = mX1;
			mY2 = mY1;
			CalcSelectFrame();
			var layerName = document.getElementById("txtActiveName").value;					
			sPage = "Attributes.aspx?cmd=1&layerId="+ActiveLayerID+"&minx=" + minxPom +"&miny=" + minyPom + "&maxx=" + maxxPom + "&maxy=" + maxyPom+"&layerName="+layerName;
			openPopup(sPage, "Atributi", 460,500);
			break;
		case "selectfeature":			
			CalcSelectFrame();				
			var layerName = document.getElementById("txtActiveName").value;
			sPage = "Attributes.aspx?cmd=4&layerId="+ActiveLayerID+"&minx=" + minxPom +"&miny=" + minyPom + "&maxx=" + maxxPom + "&maxy=" + maxyPom +"&layerName="+layerName;
			openPopup(sPage, "Atributi", 460,500);
			HideZoomFrame();
			break;
		case "selectByLine":
			var coordPom = new Coord(mX1,mY1);
			StoreLineCoords(coordPom)
			break;
		case "measure":
			//CalcMeasureLength(mX1, mY1);
			measureMouseUp();
			break;
		case "coordinates":
			coordTool();
			break;
		}

		return false;
}
//*********************************************************************************************************
function stakloOnMouseDoubleClick(slika, e){
	
	switch (currTool){
		case "measure":
			vmlPolygon = new Polygon();
			tmpPolygon = new Polygon();
			drawMeasurePath2(vmlPolygon,tmpPolygon);
			break;
	}
	
}
//*********************************************************************************************************
function ZoomIn(mouseX1, mouseX2, mouseY1, mouseY2){
	var bPreZoom_ZI = false;
	if(bPreZoom_ZI){
		sloj = getLayer("mapLayer");
		zoomInMapLayer(mouseX1,mouseX2,mouseY1,mouseY2,sloj);
	}
	
	var newEnvelope = GetZoomExtentEx(1,mouseX1,mouseX2,mouseY1,mouseY2);
	
	cmd = "cmd=140";
	cmd += "&minx=" + newEnvelope.minx;
	cmd += "&maxx=" + newEnvelope.maxx;
	cmd += "&miny=" + newEnvelope.miny;
	cmd += "&maxy=" + newEnvelope.maxy;
	cmd += "&layers=" + layers;
	command(cmd);	
}
//*********************************************************************************************************
//Funkcija zumira trenutnu kartu prije nego stigne nova karta od ArcIMS-a
function zoomInMapLayer(mouseX1, mouseX2, mouseY1, mouseY2, sloj){
	var dx = 0, dy = 0;
	var frameWidth = 0;
	var frameHeight = 0;
	var newWidth = 0;
	var newHeight = 0;
	var frameCenX = Math.abs(mouseX2 - mouseX1) / 2 + mouseX1;
	var frameCenY = Math.abs(mouseY2 - mouseY1) / 2 + mouseY1;
	var dOmjer = imageWidth/imageHeight;
	
	if ((Math.abs(mouseX1-mouseX2) < minZoomFrame) && (Math.abs(mouseY1-mouseY2) < minZoomFrame)){
		frameWidth = imageWidth * zinFactor;
		frameHeight = imageHeight * zinFactor;
				
		mouseX1 = frameCenX - frameWidth / 2;
		mouseX2 = frameCenX + frameWidth / 2;
		mouseY1 = frameCenY - frameHeight / 2;
		mouseY2 = frameCenY + frameHeight / 2;
	}
	//Racunanje frame da bude proporcionalan dimenzijama slike
	if(Math.abs(mouseX2 - mouseX1) >= Math.abs(mouseY2 - mouseY1)){
		frameWidth = mouseX2 - mouseX1;
		frameHeight = frameWidth / dOmjer;
		mouseY1 = Round(frameCenY - frameHeight / 2,0);
		mouseY2 = Round(frameCenY + frameHeight / 2,0);
	}
	else{
		frameHeight = mouseY2 - mouseY1;
		frameWidth = frameHeight * dOmjer;
		mouseX1 = Round(frameCenX - frameWidth / 2,0);
		mouseX2 = Round(frameCenX + frameWidth / 2,0);
	}
	
	//Provjeravanje da li slika ispda iz minimalnog ili maksimalnog mjerila
	if(frameWidth/imageWidth*mjerilo < minScale){
		mouseX1 = frameCenX - imageWidth / 2;
		mouseX2 = frameCenX + imageWidth / 2;
		mouseY1 = frameCenY - imageHeight / 2;
		mouseY2 = frameCenY + imageHeight / 2;
	}
	
	//Racunanje dimenzija privremene slike
	var screenCenX = imageWidth / 2;
	var screenCenY = imageHeight / 2;			
	
	newWidth = Math.abs(Round(imageWidth * imageWidth / (mouseX2 - mouseX1),0));
	newHeight = Math.abs(Round(imageHeight * imageHeight / (mouseY2 - mouseY1),0));
	dy = Round((screenCenY - frameCenY) * (imageHeight/ (mouseY2 - mouseY1)),0);
	dx = Round((screenCenX - frameCenX) * (imageWidth / (mouseX2 - mouseX1)),0);

	document.images.imgKarta.width = newWidth;
	document.images.imgKarta.height = newHeight;
	sloj.width = newWidth;
	sloj.height = newHeight;
	sloj.top = dy - (newHeight - imageHeight) / 2;
	sloj.left = dx - (newWidth - imageWidth) / 2;
}
//*********************************************************************************************************
function ZoomOut(mouseX1, mouseX2, mouseY1, mouseY2){
	var bPreZoom_ZO = false;
	if(bPreZoom_ZO){
		sloj = getLayer("mapLayer");
		zoomOutMapLayer(mouseX1,mouseX2,mouseY1,mouseY2,sloj);
	}
	
	var newEnvelope = GetZoomExtentEx(2,mouseX1,mouseX2,mouseY1,mouseY2);
	cmd = "cmd=150";
	cmd += "&minx=" + newEnvelope.minx;
	cmd += "&maxx=" + newEnvelope.maxx;
	cmd += "&miny=" + newEnvelope.miny;
	cmd += "&maxy=" + newEnvelope.maxy;
	cmd += "&layers=" + layers;
	command(cmd);	
}		
//*********************************************************************************************************
//Funkcija odzumira trenutnu kartu prije nego stigne nova karta od ArcIMS-a
function zoomOutMapLayer(mouseX1, mouseX2, mouseY1, mouseY2, sloj){
	var dx = 0, dy = 0;
	var frameWidth = 0;
	var frameHeight = 0;
	var newWidth = 0;
	var newHeight = 0;
	var frameCenX = Math.abs(mouseX2 - mouseX1) / 2 + mouseX1;
	var frameCenY = Math.abs(mouseY2 - mouseY1) / 2 + mouseY1;
	var dOmjer = imageWidth/imageHeight;
	
	if ((Math.abs(mouseX1-mouseX2) < minZoomFrame) && (Math.abs(mouseY1-mouseY2) < minZoomFrame)){
		frameWidth = imageWidth / zoutFactor;
		frameHeight = imageHeight / zoutFactor;
		mouseX1 = frameCenX - frameWidth / 2;
		mouseX2 = frameCenX + frameWidth / 2;
		mouseY1 = frameCenY - frameHeight / 2;
		mouseY2 = frameCenY + frameHeight / 2;
	}
	//Racunanje frame da bude proporcionalan dimenzijama slike
	if(Math.abs(mouseX2 - mouseX1) >= Math.abs(mouseY2 - mouseY1)){
		frameWidth = mouseX2 - mouseX1;
		frameHeight = frameWidth / dOmjer;
		mouseY1 = Round(frameCenY - frameHeight / 2,0);
		mouseY2 = Round(frameCenY + frameHeight / 2,0);
	}
	else{
		frameHeight = mouseY2 - mouseY1;
		frameWidth = frameHeight * dOmjer;
		mouseX1 = Round(frameCenX - frameWidth / 2,0);
		mouseX2 = Round(frameCenX + frameWidth / 2,0);
	}
	//Provjeravanje da li slika ispada iz minimalnog ili maksimalnog mjerila
	if((imageWidth/frameWidth)*mjerilo > maxScale){
		mouseX1 = frameCenX - imageWidth / 2;
		mouseX2 = frameCenX + imageWidth / 2;
		mouseY1 = frameCenY - imageHeight / 2;
		mouseY2 = frameCenY + imageHeight / 2;
	}
	//Racunanje dimenzija privremene slike
	var screenCenX = imageWidth / 2;
	var screenCenY = imageHeight / 2;			
	
	newWidth = (mouseX2 - mouseX1);
	newHeight = (mouseY2 - mouseY1);
	dy = Round((screenCenY - frameCenY) * ((mouseY2 - mouseY1)/imageHeight),0);
	dx = Round((screenCenX - frameCenX) * ((mouseX2 - mouseX1)/imageWidth),0);
		
	document.images.imgKarta.width = newWidth;
	document.images.imgKarta.height = newHeight;
	sloj.width = newWidth;
	sloj.height = newHeight;
	sloj.top = dy - (newHeight - imageHeight) / 2;
	sloj.left = dx - (newWidth - imageWidth) / 2;
}		
//*********************************************************************************************************
function GetZoomExtentEx(nType, mouseX1, mouseX2, mouseY1, mouseY2){
	var strRez = "";
	var cenx, ceny;
	var newEnvelope = new Envelope(0.0,0.0,0.0,0.0);
	var temp = 0;
	
	extWidth = maxx - minx;
	extHeight = maxy - miny;
	
	if ((Math.abs(mouseX1-mouseX2) < minZoomFrame) && (Math.abs(mouseY1-mouseY2) < minZoomFrame)){	 //zoom at
		cenx = minx + extWidth * mouseX1 / imageWidth;
		ceny = miny + extHeight * (imageHeight-mouseY1) / imageHeight; //Oprez: Y os slike "gleda" prema dolje
		
		if (nType == 1){	//zoom-in
			extWidth = extWidth * zinFactor;
			extHeight = extHeight * zinFactor;
		}
		else if(nType == 2){			//zoom-out
			extWidth = extWidth * zoutFactor;
			extHeight = extHeight * zoutFactor;
		}
		
		newEnvelope.minx = cenx - extWidth / 2.0;
		newEnvelope.maxx = cenx + extWidth / 2.0;
		newEnvelope.miny = ceny - extHeight / 2.0;
		newEnvelope.maxy = ceny + extHeight / 2.0;
	}
	else{ 								//zoom rec
	//stavljanje koordinata misa u "pravi" redosljed
		if (mouseX1 > mouseX2){
			temp = mouseX1;
			mouseX1 = mouseX2;
			mouseX2 = temp;
		}
		if (mouseY1 > mouseY2){
			temp = mouseY1;
			mouseY1 = mouseY2;
			mouseY2 = temp;
		}
		
		if(nType == 1){ // ZoomIn
			newEnvelope.minx = minx + extWidth * mouseX1 / imageWidth
			newEnvelope.maxx = maxx - extWidth * (imageWidth - mouseX2) / imageWidth
			//Oprez: Y os slike gleda prema dolje, stoga
			//se za racunanje miny koristi mouseY2, odnosno mouseY1 za maxy
			newEnvelope.miny = miny + extHeight * (imageHeight - mouseY2) / imageHeight
			newEnvelope.maxy = maxy - extHeight * mouseY1 / imageHeight
		}
		else if(nType == 2){ // ZoomOut
			extWidth = extWidth * imageWidth / (mouseX2 - mouseX1);
			extHeight = extHeight * imageHeight / (mouseY2 - mouseY1);
					
			newEnvelope.minx = minx - mouseX1 / imageWidth * extWidth;
			newEnvelope.maxx = minx + extWidth;
			newEnvelope.miny = miny - (imageHeight - mouseY2) / imageWidth * extHeight;
			newEnvelope.maxy = miny + extHeight;
		}
	}
	return newEnvelope;
}
//*********************************************************************************************************
function fullZoom(){

	//var newEnvelope = GetFullExtent();
	cmd = "cmd=140";
	cmd += "&minx=" + FULL_ZOOM_MINX;
	cmd += "&maxx=" + FULL_ZOOM_MAXX;
	cmd += "&miny=" + FULL_ZOOM_MINY;
	cmd += "&maxy=" + FULL_ZOOM_MAXY;
	cmd += "&layers=" + layers;

	command(cmd);	
}
//*********************************************************************************************************
function GetFullExtent(){
	var env = new Envelope(0.0,0.0,0.0,0.0);
		
	env.minx = arrLayers[0].minx;
	env.miny = arrLayers[0].miny;
	env.maxx = arrLayers[0].maxx;
	env.maxy = arrLayers[0].maxy;
	
	for(var i=0; i<arrLayers.length;i++){
		if (arrLayers[i] != null){
			if (arrLayers[i].minx < env.minx) env.minx = arrLayers[i].minx;
			if (arrLayers[i].miny < env.miny) env.miny = arrLayers[i].miny;
			if (arrLayers[i].maxx > env.maxx) env.maxx = arrLayers[i].maxx;
			if (arrLayers[i].maxy > env.maxy) env.maxy = arrLayers[i].maxy;
		}
	}
		
	return env;		
}
//*********************************************************************************************************
function zoomToActiveLayer(){
	var newEnvelope = GetActiveLayerExtent();
	
	cmd = "cmd=140";
	cmd += "&minx=" + newEnvelope.minx;
	cmd += "&maxx=" + newEnvelope.maxx;
	cmd += "&miny=" + newEnvelope.miny;
	cmd += "&maxy=" + newEnvelope.maxy;
	cmd += "&layers=" + layers;
	command(cmd);	
}
//*********************************************************************************************************
function  GetActiveLayerExtent(){
	var env = new Envelope(0.0,0.0,0.0,0.0);
		
	env.minx = arrLayers[ActiveLayerID].minx;
	env.miny = arrLayers[ActiveLayerID].miny;
	env.maxx = arrLayers[ActiveLayerID].maxx;
	env.maxy = arrLayers[ActiveLayerID].maxy;
		
	return env;
}
//*********************************************************************************************************
function clearSelect(){
	bSelectAcetate = false;
	cmd = "cmd=140";
	cmd += "&minx=" + minx;
	cmd += "&maxx=" + maxx;
	cmd += "&miny=" + miny;
	cmd += "&maxy=" + maxy;
	cmd += "&layers=" + layers;
	selectObjectID = "";
	command(cmd);
}
//*********************************************************************************************************
function coordTool(){
	var coords = new Coord(mX2,mY2);
	var mapPos = CalcCoordInRealWorld(coords);
	
	//var txt55X = document.getElementById ("txt55X");
	//var txt55Y = document.getElementById ("txt55Y");
	//txt55X.value = Round(mapPos.y,2);
	//txt55Y.value = Round(mapPos.x,2);
	
	var measureLayer = getLayer("measureLayer");
	var inHTML = '<img src="images/marker.gif" style="POSITION: absolute; TOP:' + (mY2-6) + 'px; LEFT: ' + (mX2-6) +'px;" />';
	document.getElementById('measureLayer').innerHTML =inHTML;
	
	var cmd = 'CoordTrans.aspx?x=' + mapPos.x + '&y=' + mapPos.y;
	loadPage('div_results',null,cmd);  
		
}
//*********************************************************************************************************
function findFeatures(){
	var layerName = Form1.txtActiveName.value;
	var sPage = "FindPage.aspx?layerID="+ActiveLayerID+"&layerName="+layerName;
	openPopup(sPage, "Atributi", 460,500);
}

function queryFeatures(){
	var layerName = Form1.txtActiveName.value;
	var sPage = "SQLPage.aspx?layerID="+ActiveLayerID+"&layerName="+layerName;
	openPopup(sPage, "Atributi", 470,500);
}
//*********************************************************************************************************
function setVisibleLayers(currScale)
{
	var layerName;
	for(var i=0; i<arrLayers.length;i++){
		if (arrLayers[i] != null){
		
			if (arrLayers[i].minscale  != 0 |  arrLayers[i].maxscale != 0){
				
				layerName = document.getElementById("cell"+i);
				
				if (layerName != null){				
					if ((arrLayers[i].minscale == 0 | arrLayers[i].minscale <= currScale) & (arrLayers[i].maxscale == 0 |  currScale <= arrLayers[i].maxscale))
						layerName.style.color = "black";
					else
						layerName.style.color = "gray";
				}
			}							
		}		
	}
	return;
}
//*********************************************************************************************************
function openPopup(strURL,strWindowName,width,height){	
	
	var option = "toolbar=0,directories=0,status=0,menubar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height;
	
	new_win=window.open(strURL,strWindowName,option);
	new_win.focus();
}				
//*********************************************************************************************************		
function exportMapToFile(){
	
	var url = "Export.aspx?cmd=140";
	url += "&minx=" + minx;
	url += "&maxx=" + maxx;
	url += "&miny=" + miny;
	url += "&maxy=" + maxy;
	url += "&layers=" + layers;
	url += "&width=" + imageWidth;
	url += "&height=" + imageHeight;
		
	window.location = url;		
}
//*********************************************************************************************************		
function printMap(){
	
	var mjerilo1 = Round(mjerilo,0);
	var url = "Print.aspx?cmd=140";
	
	url += "&minx=" + minx;
	url += "&maxx=" + maxx;
	url += "&miny=" + miny;
	url += "&maxy=" + maxy;
	url += "&layers=" + layers;
	url += "&scale=" + mjerilo1;
	
	window.open(url);		
}
//*********************************************************************************************************		
function tool2OnMouseDown(tool,cmd){
	
	cmd = "cmd=210";
	cmd += GetPanExtent(tool.name);
	cmd += "&layers=" + layers;
	command(cmd);
	
}
//*********************************************************************************************************			
function onLoad(){
		
	//var mapHeight = getMapHeight();
	//var mapWidth = getMapWidth();
	
	//resizeDivs2(mapWidth, mapHeight)
	

			
	//initialSelection(layers);

}
//*********************************************************************************************************	
function onResize(){
	window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("onResizeSub()",500);
}
//*********************************************************************************************************	
function onResizeSub(){
	return;
	var mapWidth = getMapWidth();//320
	var mapHeight= getMapHeight(); //225
	
	//if (mapWidth > 200 & mapHeight > 200)
		//resizeDivs(mapWidth-235, mapHeight-190); os prve verzije
		//resizeDivs(mapWidth-410, mapHeight-98);
	
	//resizeDivs(mapWidth-488, mapHeight-202);			
	resizeDivs(mapWidth-488, mapHeight-196);			
	//resizeDivs2(mapWidth, mapHeight);
	
}
//*********************************************************************************************************	
function resizeDivs2(mapWidth, mapHeight){
	
	var div_toc = document.getElementById("div_toc");
	div_toc.style.height = mapHeight -205;
	//var div_toc = document.getElementById("divSredinaLijevo");
	//div_toc.style.height = mapHeight -200;

}
//*********************************************************************************************************	
function resizeDivs(map_width, map_height){

	var d = new Array();	
	d[0]  = "frTop";
	d[1]  = "frBottom";
	d[2]  = "frLeft";
	d[3]  = "frRight";
	d[4]  = "imgKarta";
	d[5]  = "glassLayer";
	d[6] = "staklo";	
	d[7] = "mainLayer";
	d[8] = "mapLayer";
	d[9] = "div_gis_okvir";
	d[10] = "measureLayer";		
	
	for (var i = 0; i < d.length; i++){
		if (i != 4)
		{
		var layer = document.getElementById(d[i]);
		layer.style.width = map_width;
		layer.style.height = map_height;
		}
	}
	
	
	imageWidth = map_width;
	imageHeight = map_height;
	
	
	loadingImageX = (imageWidth - loadingImageWidth)/2; 
	loadingImageY = (imageHeight - loadingImageHeight)/2;
	var loadingLayer = document.getElementById("loadingLayer");
	loadingLayer.style.left = loadingImageX;
	loadingLayer.style.top = loadingImageY;
				
	cmd = "cmd=140";
	cmd += "&minx=" + minx;
	cmd += "&maxx=" + maxx;
	cmd += "&miny=" + miny;
	cmd += "&maxy=" + maxy;
	cmd += "&layers=" + layers;
	command(cmd);
	
}
//*********************************************************************************************************	
function getMapWidth () {

	var mapFrameWidth = window.innerWidth;
	
	if (mapFrameWidth == null){
		mapFrameWidth = document.body.clientWidth;
	}	
	return mapFrameWidth;
}
//*********************************************************************************************************	
function getMapHeight () {
	
	var mapFrameHeight = window.innerHeight;
	
	if (mapFrameHeight == null) {
		mapFrameHeight = document.body.clientHeight;
	}
	return mapFrameHeight;
}
//*********************************************************************************************************	
function refreshMap(){

	cmd = "cmd=140";
	cmd += "&minx=" + minx;
	cmd += "&maxx=" + maxx;
	cmd += "&miny=" + miny;
	cmd += "&maxy=" + maxy;
	cmd += "&layers=" + layers;
	command(cmd);
}
//*********************************************************************************************************	
function MoveMapToCoord(centX, centY){
	centX = parseFloat(centX);
	centY = parseFloat(centY);
	var minx_new = centX - (maxx -minx)/2.0;
	var maxx_new = centX + (maxx -minx)/2.0;
	var miny_new = centY - (maxy -miny)/2.0;
	var maxy_new = centY + (maxy -miny)/2.0;
	
	cmd = "cmd=140";
	cmd += "&minx=" + minx_new;
	cmd += "&maxx=" + maxx_new;
	cmd += "&miny=" + miny_new;
	cmd += "&maxy=" + maxy_new;
	cmd += "&layers=" + layers;
	command(cmd);
	var measureLayer = getLayer("measureLayer");
	var centarX1 = imageWidth/2;
	var centarY1 = imageHeight/2;
	var inHTML = '<img src="images/marker.gif" style="POSITION: absolute; TOP:' + (centarY1-6) + 'px; LEFT: ' + (centarX1-6) +'px;" />';
	document.getElementById('measureLayer').innerHTML =inHTML;
	

}

//*********************************************************************************************************	
function zoomToScale(){

	var txtScale = document.getElementById("txtScale");
	var newScale = txtScale.value;
	
	if (isNumber(newScale)==false)
		return;
	
	newScale = parseInt(newScale);
	
	if (isNaN(newScale))	
		return;
	
	CalcNewExtent1(newScale);
	cmd = "cmd=140";
	cmd += "&minx=" + minxPom;
	cmd += "&maxx=" + maxxPom;
	cmd += "&miny=" + minyPom;
	cmd += "&maxy=" + maxyPom;
	cmd += "&layers=" + layers;
	command(cmd);
	
}
//*********************************************************************************************************	
function zoomToScale1(newScale){

	if (isNumber(newScale)==false)
		return;
	
	newScale = parseInt(newScale);
	
	if (isNaN(newScale))	
		return;
	
	CalcNewExtent1(newScale);
	cmd = "cmd=140";
	cmd += "&minx=" + minxPom;
	cmd += "&maxx=" + maxxPom;
	cmd += "&miny=" + minyPom;
	cmd += "&maxy=" + maxyPom;
	cmd += "&layers=" + layers;
	command(cmd);
	
}
//*********************************************************************************************************	
function isNumber(inputStr)
{
    for(var ii=0; ii<inputStr.length; ii++)
    {
        var oneChar = inputStr.substring(ii, ii+1);
        if(oneChar<"0" || oneChar>"9")
        {
            alert("Molim vas, unosite samo brojeve.");
            return false;
        }
    }
    return true;
}
//*********************************************************************************************************	
function CalcNewExtent1(fScale){

	cenX = (maxx + minx)/2.0;
	cenY = (maxy + miny)/2.0;
	extWidth = maxx - minx;
	extHeight = maxy - miny;
	extWidth = imageWidth / 96.0 * 0.0254 * fScale;
	extHeight = imageHeight / 96.0 * 0.0254 * fScale;
	
	minxPom = cenX - extWidth / 2.0;
	maxxPom = cenX + extWidth / 2.0;
	minyPom = cenY - extHeight / 2.0;
	maxyPom = cenY + extHeight / 2.0;
}
//*********************************************************************************************************	
function selectObjects(objectids){
	
	bSelectAcetate = true;
	cmd = "cmd=140";
	cmd += "&minx=" + minx;
	cmd += "&maxx=" + maxx;
	cmd += "&miny=" + miny;
	cmd += "&maxy=" + maxy;
	cmd += "&layers=" + layers;
	selectObjectID = objectids;
	command(cmd);
}
//*********************************************************************************************************	
function zoomToSelected(objectID, minx1, maxx1, miny1, maxy1){
	
	var scaleX = (maxx1 - minx1) / imageWidth * 96.0 / 0.0254;
	var scaleY = (maxy1 - miny1) / imageHeight * 96.0 / 0.0254;
	var scaleXY = Math.max (scaleX,scaleY);


	//if (scaleXY < 20000){
		
		var cenX = (maxx1 + minx1)/2.0;
		var cenY = (maxy1 + miny1)/2.0;
		//var fScale = 20000;	
		var fScale = scaleXY * 3.0;	
		var extWidth = imageWidth / 96.0 * 0.0254 * fScale;
		var extHeight = imageHeight / 96.0 * 0.0254 * fScale;
	
		minx1 = cenX - extWidth / 2.0;
		maxx1 = cenX + extWidth / 2.0;
		miny1 = cenY - extHeight / 2.0;
		maxy1 = cenY + extHeight / 2.0;
	//}
	bSelectAcetate = true;
	
	cmd = "cmd=140";
	cmd += "&minx=" + minx1;
	cmd += "&maxx=" + maxx1;
	cmd += "&miny=" + miny1;
	cmd += "&maxy=" + maxy1;
	cmd += "&layers=" + layers;
	selectObjectID = objectID;
	command(cmd);

}
//*********************************************************************************************************	
function measureMouseMove(){
	
	var mouse1 = new Coord(mX1, mY1);
	var mouse2 = new Coord(mX2, mY2);
	
	if( MaxDistance (mouse1, mouse2)){				
		
		var tmpPolygon2 = CopyPolygon(vmlPolygon);
		var tmpPolygon3 = CopyPolygon(tmpPolygon);
		
		var coords = new Coord(mX2,mY2);
		var mapPos = CalcCoordInRealWorld(coords);
		
		AddPoint (tmpPolygon2, mX2, mY2);
		AddPoint (tmpPolygon3, mapPos.x, mapPos.y);
	
		drawMeasurePath2(tmpPolygon2, tmpPolygon3);
	}
}
//*********************************************************************************************************	
function measureMouseUp(){
		
		var coords = new Coord(mX2,mY2);
		var mapPos = CalcCoordInRealWorld(coords);	
		
		AddPoint (vmlPolygon, mX1, mY2);
		AddPoint (tmpPolygon, mapPos.x, mapPos.y);
		
		drawMeasurePath2(vmlPolygon,tmpPolygon);				
}

//*********************************************************************************************************	
function MaxDistance (point1, point2){
 	
 	var ddx = Math.abs(point2.x - point1.x);
    var ddy = Math.abs(point1.y - point2.y);
      
    if (Math.max( ddx, ddy) > 5) 
		return true;
	else 
		return false;
}
//*********************************************************************************************************	
//Funkcija command poziva salje upit za generiranjem slike		
//*********************************************************************************************************	
function command(upit){

	if(bImageReceived == true){				
		bImageReceived = false;
	
		getLayer("loadingLayer").visibility = "visible";
		var path = "MapImage.aspx?";
		var query = path + upit;
	
		query += GetAcetateParams();
		query += "&width=" + imageWidth;
		query += "&height=" + imageHeight;
		query += "&rnd1=" + Math.random();
		query += "&rnd2=" + Math.random();
		//alert(query);
		document.karta.src = query;
		
		document.getElementById("measureLayer").innerHTML ="";
				
		if ((!document.all) && (document.getElementById))	// Netscape 6.0 ima problema s hvatanjem
			window.setTimeout("NN6Watchdog()", 200);				//imageOnLoad eventa	
		
		return true;
	}
	else{
		alert('Prethodna operacija nije jos zavr\u0161ena!\nTek kad zavr\u0161i mo\u017Eete pokrenuti novu operaciju!');
		return false;
	}
}
//*********************************************************************************************************		
//Funkcija GetAcetateParams provjerava da li treba prikazati neki acitatni sloj i ako treba postavlja odgovarajuce paramatre u QueryString		
		function GetAcetateParams(){
			var strRez = "";
			strRez += "&activeLayerID=" + ActiveLayerID;
			if(bStreetAcetate){ //Ako treba prikazati marker
				strRez += "&streetSearch=true";
				strRez += "&streetMarkerX=" + streetMarkerX;
				strRez += "&streetMarkerY=" + streetMarkerY;
				strRez += "&streetLabel=" + streetLabel;
			}
			if(bMeasureAcetate){
				var strRes = getResponseEx("StoreParamsInSession.aspx","&action=120" + "&measureCoords=" + strMeasureCoords,"POST"); //Prenosenje parametara POST metodom (zbog eventualne velicine QueryString-a)
				if(strRes == "OK"){
					strRez += "&measure=true";
					//strRez += "&measureCoords=" + strMeasureCoords;
					strRez += "&measurePointNum=" + nBrojMeasureTocaka;
				}
			}
			if(bDrawPolyAcetate){
				//var strRes = getResponseEx("DrawPolygonTool.aspx",strDrawPolygonQuery,"POST"); //Prenosenje parametara POST metodom (zbog eventualne velicine QueryString-a)
				var strRes = getResponseEx("StoreParamsInSession.aspx","&action=110" + strDrawPolygonQuery,"POST"); //Prenosenje parametara POST metodom (zbog eventualne velicine QueryString-a)
				if(strRes == "OK"){
					strRez += "&drawPolygon=true";
				}
			}
			if(bIdentifyAcetate){
				strRez += "&identify=true";
				strRez += "&activeLayerType=" + arrLayers[ActiveLayerID].type;
				strRez += "&objectID=" + nIdentifyObjectID ;
			}
			if(bSelectAcetate){
				strRez += "&select=true";
				strRez += "&activeLayerType=" + arrLayers[ActiveLayerID].type;				
				//DRAZEN
				strRez += "&objectID=" +selectObjectID ;
			}
			if(bSLBAcetate !== 'undefined'){
				if(bSLBAcetate){
					//alert(strSLBQuery);
					var strRes = getResponseEx("StoreParamsInSession.aspx",strSLBQuery,"POST");
					if(strRes == "OK"){
						strRez += "&selectByLine=true";
					}
					else{
						alert('Neuspjelo preno\u0161enje parametara za selekciju linijom!');
					}
				}
			}

			return strRez;
		}
		
//*********************************************************************************************************
//Funkcija CalcNreExtent racuna novi ekstent za zadano mjerilo tako da su na sredini slike koordinate cenX i cenY
//Rezultate sprema u minxPom, maxxPom, minyPom, maxyPom		
function CalcNewExtent(cenX, cenY, fScale){
	extWidth = maxx - minx;
	extHeight = maxy - miny;
	extWidth = imageWidth / 96.0 * 0.0254 * fScale;
	extHeight = imageHeight / 96.0 * 0.0254 * fScale;
	
	minxPom = cenX - extWidth / 2.0;
	maxxPom = cenX + extWidth / 2.0;
	minyPom = cenY - extHeight / 2.0;
	maxyPom = cenY + extHeight / 2.0;
}
//*********************************************************************************************************
//Racunanje vrijednost koordinate u stvarnom svijetu, coordParam - broj piksela sa slike
// os - 1 ili 2, 1 - X os, 2 - Y os		
function CalcCoordInRealWorld(screenCoord){
	var pixelWidth = 0.0;
	var resultCoord = new Coord(0.0,0.0);
	
	pixelWidth = (maxx - minx) / imageWidth;
	resultCoord.x = minx + pixelWidth * screenCoord.x;
	pixelWidth = (maxy - miny) / imageHeight;
	resultCoord.y = miny + pixelWidth * (imageHeight - screenCoord.y);			
			
	return resultCoord;
}
//*********************************************************************************************************
//Funkcija GetPanExtent proracunava novi extent nakon koristenja alata mappan		
function GetPanExtent(toolName){
	var strRez = "";
	var cenx, ceny;
	
	extWidth = maxx - minx;
	extHeight = maxy - miny;
	
	switch (toolName){
		case "mappan":
			if ((Math.abs(mX1-mX2) < minPanPomak) && (Math.abs(mY1-mY2) < minPanPomak)){	 //pan at
					cenx = minx + extWidth * mX1 / imageWidth;
					ceny = miny + extHeight * (imageHeight-mY1) / imageHeight; 
					minx = cenx - extWidth / 2.0;
					maxx = cenx + extWidth / 2.0;
					miny = ceny - extHeight / 2.0;
					maxy = ceny + extHeight / 2.0;
			}
			else{
				minx = minx + extWidth * (mX1 - mX2) / imageWidth;
				maxx = minx + extWidth;
				miny = miny + extHeight * (mY2 - mY1) / imageHeight;
				maxy = miny + extHeight;
			}
			break;
			
		case "mapl":
			minx = minx - extWidth * panFactor;
			maxx = maxx - extWidth * panFactor;			
			break;
			
		case "mapr":
			minx = minx + extWidth * panFactor;
			maxx = maxx + extWidth * panFactor;
			break;
			
		case "mapu":
			miny = miny + extHeight * panFactor;
			maxy = maxy + extHeight * panFactor;
			break;
		
		case "mapd":
			miny = miny - extHeight * panFactor;
			maxy = maxy - extHeight * panFactor;
			break;
			
		case "maplu":
			minx = minx - extWidth * panFactor;
			maxx = maxx - extWidth * panFactor;			
			miny = miny + extHeight * panFactor;
			maxy = maxy + extHeight * panFactor;
			break;
		
		case "mapru":
			minx = minx + extWidth * panFactor;
			maxx = maxx + extWidth * panFactor;			
			miny = miny + extHeight * panFactor;
			maxy = maxy + extHeight * panFactor;
			break;
			
		case "mapld":
			minx = minx - extWidth * panFactor;
			maxx = maxx - extWidth * panFactor;				
			miny = miny - extHeight * panFactor;
			maxy = maxy - extHeight * panFactor;		
			break;
		
		case "maprd":
			minx = minx + extWidth * panFactor;
			maxx = maxx + extWidth * panFactor;				
			miny = miny - extHeight * panFactor;
			maxy = maxy - extHeight * panFactor;							
			break;			
		}				
			
		strRez += "&minx=" + minx;
		strRez += "&maxx=" + maxx;
		strRez += "&miny=" + miny;
		strRez += "&maxy=" + maxy;
		
		return strRez;
}
//*********************************************************************************************************
//Funkcija CalcSelectFrame() racuna koordinate envelope za alate identify i select
//Rezultate sprema u varijable minxPom, maxxPom, minyPom, maxyPom		
function CalcSelectFrame(){
	var pixelWidth = 0.0;
	var pixelHeight = 0.0;
	var nPom = 0;
			
	pixelHeight = (maxy-miny) / imageHeight;
	pixelWidth = (maxx-minx) / imageWidth;
	var nPom;
	if(Math.abs(mX2 - mX1) < minSelectFrame || Math.abs(mY2 - mY1) < minSelectFrame){
		minxPom = minx + (mX1 - identifyOffset) * pixelWidth;
		maxxPom = minx + (mX1 + identifyOffset) * pixelWidth;
		minyPom = miny + (imageHeight - (mY1 + identifyOffset)) * pixelHeight;
		maxyPom = miny + (imageHeight - (mY1 - identifyOffset)) * pixelHeight;	
		//minxPom = minx + (mX1) * pixelWidth  - identifyOffset;
		//maxxPom = minx + (mX1) * pixelWidth  + identifyOffset;
		//minyPom = miny + (imageHeight - mY1) * pixelHeight  - identifyOffset;
		//maxyPom = miny + (imageHeight - mY1) * pixelHeight +  identifyOffset;										
	}
	else{
		if(mX1 > mX2){
			nPom = mX1;
			mX1 = mX2;
			mX2 = nPom;
		}
		if(mY1 > mY2){
			nPom = mY1;
			mY1 = mY2;
			mY2 = nPom;					
		}
				
		minxPom = minx + mX1 * pixelWidth;
		maxxPom = minx + mX2 * pixelWidth;
		minyPom = miny + (imageHeight - mY2) * pixelHeight;
		maxyPom = miny + (imageHeight - mY1) * pixelHeight;
				
		minxPom = Round(minxPom,0);
		maxxPom = Round(maxxPom,0);
		minyPom = Round(minyPom,0);
		maxyPom = Round(maxyPom,0);
	}
}
//*********************************************************************************************************				
		
function openLegend(i){
	openPopup("Legenda"+i+".aspx", "Legenda", 460,500);
}
function openPopup1(i){
var stranica = "";
	switch(i)
	{
		case 1:
			stranica = "odluka.htm";
			break;
		case 2:
			stranica = "uvjeti.htm";
			break;
		case 3:
			stranica = "napomena.htm";
			break;
	}
	openPopup(stranica, "PROSTORNI_PLAN", 400,400);
}
