﻿var lastX = 0;
var lastY = 0;
var startX = 0;
var startY = 0;
//var endX = 0;
//var endY = 0;
var dstartX = 0;
var dstartY = 0;
var dendX = 0;
var dendY = 0;
var tool = 'Pan';
var IE = document.all?true:false;
var ovBoxSize = 2;
//var antKartLagF = 0;
//var antKartLagB = 0;
var k_x = 0;
var k_y = 0;


function EndreWindow()
{
//OmLastVindu=0;
OmLastVindu = OmLastVindu + 1;
self.setTimeout('SendEndreWindow(OmLastVindu);',1000);
}

//function SendEndreWindow(nr)
//{
//if (OmLastVindu==nr)
//{
//if( typeof( window.innerWidth ) == 'number' ) {
//    //Non-IE
//    ParameterX.value = window.innerWidth;
//    ParameterY.value = window.innerHeight;
//  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//    //IE 6+ in 'standards compliant mode'
//    ParameterX.value = document.documentElement.clientWidth;
//    ParameterY.value = document.documentElement.clientHeight;
//  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//    //IE 4 compatible
//    ParameterX.value = document.body.clientWidth;
//    ParameterY.value = document.body.clientHeight;
//  }
//    //ParameterX.value = document.body.clientWidth;
//    //ParameterY.value = document.body.clientHeight;
//    ParameterType.value = "WindowSize";
//    postBackHiddenField(HiddenPostBackField)
//    return true;
//}
//}


function getMouseClick(e) {

if (IE) 
{ // grab the x-y pos.s if browser is IE
tempX = event.offsetX;//clientX + document.body.scrollLeft;
tempY = event.offsetY;//clientY + document.body.scrollTop;
}
else {  // grab the x-y pos.s if browser is NS
tempX = e.layerX;//e.pageX;
tempY = e.layerY;//e.pageY;
}  
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}  
    //alert("x: " + tempX + " y: " + tempY);
    ParameterX.value = tempX;
    ParameterY.value = tempY;
    postBackHiddenField(HiddenPostBackField)
    return true;
  }
    
function getMouseMove(e) 
{
if (IE) 
{ // grab the x-y pos.s if browser is IE
tempX = event.offsetX;//event.clientX + document.body.scrollLeft;
tempY = event.offsetY;//event.clientY + document.body.scrollTop;
}
else {  // grab the x-y pos.s if browser is NS
tempX = e.layerX;//e.pageX;
tempY = e.layerY;//e.pageY;
}  
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}
if (!e) e = window.event;
k_x = parseInt(ks_startx + (tempX * ks_deltaX));
k_y = parseInt(ks_starty - (tempY * ks_deltaY));

StatusLinja.value = "Koordinat i UTM Sone 33 (Euref89) øst: " + k_x + " , nord: " + k_y + " , skala 1:" + ks_scale + " , x:" + tempX + " , y:" + tempY;
//if (dstartX==0){dstartX=tempX;}
//if (dstartY==0){dstartY=tempY;}
//document.Show.MouseX.value = tempX;
//document.Show.MouseY.value = tempY;
if (MusNede == 1)
{
switch(tool) {

case "Pan":
//if (!e) e = window.event;
var children = mapThing.childNodes;                // Now get all children of n
    for(var i=0; i < children.length; i++) 
    {
    //alert(children[i].nodeName);
        // Loop through the children
    if (children[i].nodeName != "undefined"){
    if (children[i].nodeName == "IMG"){
    children[i].style.left =  children[i].offsetLeft+e.clientX-lastX+"px";
    children[i].style.top  = children[i].offsetTop+e.clientY-lastY+"px";
    }
    }
}
var children = backThing.childNodes;                // Now get all children of n
    for(var i=0; i < children.length; i++) 
    {
    //alert(children[i].nodeName);
        // Loop through the children
    if (children[i].nodeName != "undefined"){
    if (children[i].nodeName == "IMG"){
    children[i].style.left =  children[i].offsetLeft+e.clientX-lastX+"px";
    children[i].style.top  = children[i].offsetTop+e.clientY-lastY+"px";
    }
    }
}

//for (i=0;i<mapLayers.length;i++)
//{
//mapLayers[i].style.left =  mapLayers[i].offsetLeft+e.clientX-lastX+"px";
//mapLayers[i].style.top  = mapLayers[i].offsetTop+e.clientY-lastY+"px";
//}
lastX = e.clientX;
lastY = e.clientY;
break;

case "Zoom In":
var ztop;
var zbottom;
var zleft;
var zright;

tempX = e.clientX;
tempY = e.clientY;
	if (tempX>startX) {
		zright=tempX;
		zleft=startX;
	} else {
		zleft=tempX;
		zright=startX;
	}
	if (tempY>startY) {
		zbottom=tempY;
		ztop=startY;
	} else {
		ztop=tempY;
		zbottom=startY;
	}
	
	if ((startX != tempX) && (startY != tempY)) {
		//clipLayer("zoomBox",zleft,ztop,zright,zbottom);
		boxIt(zleft,ztop,zright,zbottom);
		/*
		clipLayer("zoomBoxTop",zleft,ztop,zright,ztop+ovBoxSize);
		clipLayer("zoomBoxLeft",zleft,ztop,zleft+ovBoxSize,zbottom);
		clipLayer("zoomBoxRight",zright-ovBoxSize,ztop,zright,zbottom);
		clipLayer("zoomBoxBottom",zleft,zbottom-ovBoxSize,zright,zbottom);
		*/
	}
break;

case "Zoom Out":
var ztop;
var zbottom;
var zleft;
var zright;

tempX = e.clientX;
tempY = e.clientY;
	if (tempX>startX) {
		zright=tempX;
		zleft=startX;
	} else {
		zleft=tempX;
		zright=startX;
	}
	if (tempY>startY) {
		zbottom=tempY;
		ztop=startY;
	} else {
		ztop=tempY;
		zbottom=startY;
	}
	
	if ((startX != tempX) && (startY != tempY)) {
		//clipLayer("zoomBox",zleft,ztop,zright,zbottom);
		boxIt(zleft,ztop,zright,zbottom);
		/*
		clipLayer("zoomBoxTop",zleft,ztop,zright,ztop+ovBoxSize);
		clipLayer("zoomBoxLeft",zleft,ztop,zleft+ovBoxSize,zbottom);
		clipLayer("zoomBoxRight",zright-ovBoxSize,ztop,zright,zbottom);
		clipLayer("zoomBoxBottom",zleft,zbottom-ovBoxSize,zright,zbottom);
		*/
	}
break;


case "Select Rectangle":
var ztop;
var zbottom;
var zleft;
var zright;

tempX = e.clientX;
tempY = e.clientY;
	if (tempX>startX) {
		zright=tempX;
		zleft=startX;
	} else {
		zleft=tempX;
		zright=startX;
	}
	if (tempY>startY) {
		zbottom=tempY;
		ztop=startY;
	} else {
		ztop=tempY;
		zbottom=startY;
	}
	
	if ((startX != tempX) && (startY != tempY)) {
		//clipLayer("zoomBox",zleft,ztop,zright,zbottom);
		boxIt(zleft,ztop,zright,zbottom);
		/*
		clipLayer("zoomBoxTop",zleft,ztop,zright,ztop+ovBoxSize);
		clipLayer("zoomBoxLeft",zleft,ztop,zleft+ovBoxSize,zbottom);
		clipLayer("zoomBoxRight",zright-ovBoxSize,ztop,zright,zbottom);
		clipLayer("zoomBoxBottom",zleft,zbottom-ovBoxSize,zright,zbottom);
		*/
	}
break;	
}
}

return false;

}

function getMouseDown(e)
{

if (!e) e = window.event;
lastX = e.clientX;
lastY = e.clientY;
startX = e.clientX;
startY = e.clientY;
//getLayers();
MusNede = 1;
//switch(tool) {

//case "Zoom In":
//			x1=startX;
//			y1=startY
//			x2=x1+1;
//			y2=y1+1;
//			zleft=x1;
//			ztop=y1;
//			zbottom=y1;
//			zright=x1
//			//boxIt(x1,y1,x2,y2);
//break;

//}

//if (!IE)
//{
//document.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);

//mapThing.releaseEvents(Event.MOUSEDOWN);

DBakgrunnThing.onmousedown = null;

//}

DBakgrunnThing.onmousemove = getMouseMove;
DBakgrunnThing.onmouseup = getMouseUp;
return false;
}

function getMouseUp(e)
{
MusNede = 0;
if (!e) e = window.event;
endX = e.clientX;
endY = e.clientY;
//var difx = startX-dstartX;
//var dify = startY-dstartY;
//dstartX = 0;
//dstartY = 0;
hideLayer("zoomBoxTop");
hideLayer("zoomBoxLeft");
hideLayer("zoomBoxRight");
hideLayer("zoomBoxBottom");
//if (!IE) {
  //document.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);
  //mapThing.captureEvents(Event.MOUSEDOWN);
  DBakgrunnThing.onmousedown = getMouseDown;
  //}
  
  DBakgrunnThing.onmousemove = getMouseMove;
  DBakgrunnThing.onmouseup = null;
  
    ParameterX.value = (startX-DBakgrunnThing.offsetLeft-2) + ',' + (endX-DBakgrunnThing.offsetLeft-2) ;
    ParameterY.value = (startY-DBakgrunnThing.offsetTop-2) + ',' + (endY-DBakgrunnThing.offsetTop-2) ;
    postBackHiddenField(HiddenPostBackField);
return false;
}

		function revertToolPic() {
			// reset tool icons to non-set mode
			//alert(lastMode);
	   		document.getElementById('zoomin').src="images/zoomin_1.gif";
			document.getElementById('zoomout').src="images/zoomout_1.gif";
			document.getElementById('identify').src="images/identifyall_1.gif";
			document.getElementById('pan').src="images/pan_1.gif";
			//if (t.useMeasure) document.getElementById('measure').src="images/measure_1.gif";
			//if (t.useSelect) {
			document.getElementById('rect').src="images/select_rect_1.gif";
			//	document.getElementById('line').src="images/select_1.gif";
			//}
			//if (t.useBufferShape) document.getElementById('buffershape').src="images/buffershape_1.gif";
			//if (t.useHyperLink) document.getElementById('hyperlink').src="images/hotlink_1.gif";
			//if (t.useHyperLinkAny) document.getElementById('hyperlink').src="images/hotlink_1.gif";
			//if (t.useIdentifyAll) document.getElementById('identifyall').src="images/identifyall_1.gif";
	   }
	   function setTool(functName) {
	   		// set clicked button icon to set mode
	   		//if (functName!=t.modeName) {
			   	revertToolPic();
			   	tool = functName;
			   	ParameterType.value = tool;
				//t.focus();
				// alert(functName);
			  	if (functName=="Zoom In") {
					document.getElementById('zoomin').src="images/zoomin_2.gif";
					  DBakgrunnThing.onmousedown = getMouseDown;
					  DBakgrunnThing.onmousemove = getMouseMove;
					  DBakgrunnThing.onmouseup = null;
					  DBakgrunnThing.onclick = null;
					  DBakgrunnThing.style.cursor="crosshair";  	
				} else if (functName=="Zoom Out") {
					document.getElementById('zoomout').src="images/zoomout_2.gif";
					  DBakgrunnThing.onmousedown = getMouseDown;
					  DBakgrunnThing.onmousemove = getMouseMove;
					  DBakgrunnThing.onmouseup = null;
					  DBakgrunnThing.onclick = null;
					  DBakgrunnThing.style.cursor="crosshair";  	
				} else if (functName=="Identify") {
					document.getElementById('identify').src="images/identifyall_2.gif";
		              DBakgrunnThing.onmousedown = null;
					  DBakgrunnThing.onmousemove = null;
					  DBakgrunnThing.onmouseup = null;
					  DBakgrunnThing.onclick = getMouseClick;
					  DBakgrunnThing.style.cursor="help";
				} else if (functName=="Pan") {
					document.getElementById('pan').src="images/pan_2.gif";
			          DBakgrunnThing.onmousedown = getMouseDown;
					  DBakgrunnThing.onmousemove = getMouseMove;
					  DBakgrunnThing.onmouseup = null;
					  DBakgrunnThing.onclick = null;
					  //getLayers();
  					  DBakgrunnThing.style.cursor="move";
				} else if (functName=="Select Rectangle") {
				    document.getElementById('rect').src="images/select_rect_2.gif";
					  DBakgrunnThing.onmousedown = getMouseDown;
					  DBakgrunnThing.onmousemove = getMouseMove;
					  DBakgrunnThing.onmouseup = null;
					  DBakgrunnThing.onclick = null;
					  DBakgrunnThing.style.cursor="crosshair";  	
				} else if (functName=="Select Rectangle") {
					document.getElementById('rect').src="images/select_rect_2.gif";
				} else if (functName=="Buffer Shape") {
					document.getElementById('buffershape').src="images/buffershape_2.gif";
				} else if (functName=="Select Line/Polygon") {
					document.getElementById('line').src="images/select_2.gif";
				} else if (functName=="HyperLink") {
					document.getElementById('hyperlink').src="images/hotlink_2.gif";
				} else if (functName=="Identify All") {
						document.getElementById('identifyall').src="images/identifyall_2.gif";
				}
			//}
		}
		
function boxIt(theLeft,theTop,theRight,theBottom) {
		clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+ovBoxSize);
		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+ovBoxSize,theBottom);
		clipLayer("zoomBoxRight",theRight-ovBoxSize,theTop,theRight,theBottom);
		clipLayer("zoomBoxBottom",theLeft,theBottom-ovBoxSize,theRight,theBottom);	
		showLayer("zoomBoxTop");
		showLayer("zoomBoxLeft");
		showLayer("zoomBoxRight");
		showLayer("zoomBoxBottom");

}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);
		    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight + "px";
			layer.width	= newWidth + "px";
			//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
			//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
			//alert(cliptop + " " + clipleft);
			layer.top	= cliptop  + "px";
			layer.left	= clipleft + "px";
			//layer.display= "none";

}
// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getLayer(name);		
   		 layer.visibility = "hidden";
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
   	 layer.visibility = "visible";
}
// get the layer object called "name"
function getLayer(name) {
		var theObj = document.getElementById(name);
		return theObj.style
}

//function TellKartLagF()
//{
//    AjaxOppdaterDel();
//    antKartLagF = 0;
//    
//    var children = mapThing.childNodes;                // Now get all children of n
//    for(var i=0; i < children.length; i++) 
//    {
//    //alert(children[i].nodeName);
//        // Loop through the children
//    if (children[i].nodeName != "undefined"){
//        if (children[i].nodeName == "IMG"){
//            antKartLagF++;
//            }
//        }
//    }

//}

//function TellKartLagB()
//{
//    AjaxOppdaterDel();
//    antKartLagB = 0;
//    
//    var children = backThing.childNodes;                // Now get all children of n
//    for(var i=0; i < children.length; i++) 
//    {
//    //alert(children[i].nodeName);
//        // Loop through the children
//    if (children[i].nodeName != "undefined"){
//        if (children[i].nodeName == "IMG"){
//            antKartLagB++;
//            }
//        }
//    }

//}

function KartLastet(lag)
{
//switch(lag) {
//case 'F':
//if (antKartLagF == 0)
//    {
//    TellKartLagF();
//    }
//antKartLagF=antKartLagF-1;
//break;

//case 'B':
//if (antKartLagB == 0)
//    {
//    TellKartLagB();
//    }
//antKartLagB=antKartLagB-1;
//break;
//}
lag_f_oppdatering=lag_f_oppdatering-1;

if ( lag_f_oppdatering == 0)
    {
    document.getElementById('Progress').style.visibility = 'hidden';
    }
if (lag_f_oppdatering == -1)
    {
    document.getElementById('Progress').style.visibility = 'hidden';
    }
}

//var mapLayers = new Array();
//var mapThing ;
//function getLayers() {
//mapLayers = new Array();
//mapThing = document.getElementById('ctl00_Hovedinnhold_MapMap1_Ramme');
//var children = mapThing.childNodes;                // Now get all children of n
//    for(var i=0; i < children.length; i++) {    // Loop through the children
//    if (typeof children[i].nodeName != "undefined")
//    if (typeof children[i].nodeName == "IMG")
//        alert(children[i].id);      // Recurse on each one
//    }
//}
