
//common functions and parameters

//general settings
var theSelection = ""; 
var newUrl ="";

var windowX = 0;
var windowY = 0;

var left = 0;
var right = 628;
var top = 0;
var bottom = 425;
var fullLeft = 0;
var fullRight = 628;
var fullTop = 0;
var fullBottom = 425;

var newLeft = 0;
var newBottom = 0;
var newRight = 0;
var newTop = 0;

//parameters for CGI-Form
var imgxy = "628 425";
var savequery = true;
var mapext = "shapes";
var mode = "browse";
var zoomdir = 1;
var zoomsize = 2;
var imgbox = "-1 -1 -1 -1";

var printWin; 

var fullExtent = "135844 -134097 264891 -31910";

//Browser checking
if (navigator.userAgent.indexOf("MSIE") != -1 ) {
	//MSIE oder Netscape 6
	isIE = true;
	isNav = false;
	isNav6 = false;
} else if (document.getElementById&&!document.all){
	isIE = false;
	isNav = false;
	isNav6 = true;
} else {
	// MSIE
	isIE = false;
	isNav = true;
	isNav6 = false;
}

function setButton() 
{
   if (tool == "identify") parent.ButtonFrame.document.identify.src="/images/select_bridge_over.gif";
   if (tool == "zoomin")   parent.ButtonFrame.document.zoomin.src="/images/zoomin_low.gif";
   if (tool == "zoomout")  parent.ButtonFrame.document.zoomout.src="/images/zoomout_low.gif";
   if (tool == "panning")  parent.ButtonFrame.document.pan.src="/images/pan_low.gif"; 
   if (tool == "panningn")  parent.ButtonFrame.document.pan1.src="/images/pan_low.gif";  
}


function zoomLock()
{
  var scale = parent.MapFrame.document.mapserv.curscale.value.split(".")[0];
 
  if (scale<2500)
  {  //alert("scale lessthan 5000"); 
     history.go(-1);   
  }
}


function refreshMap(mode) {

       	switch (mode) {
	
		case "newLayer" :
			parent.MapFrame.document.mapserv.mode.value = "browse";
			parent.MapFrame.document.mapserv.zoomdir.value = "0";			 
			refresh();
		break;
		
		case "zoomTo" :
		        parent.MapFrame.document.mapserv.mode.value = "itemnquery";
			var theArray = ["249A","Z11CZ","Z11CY"];
			var noPhoto = [1];
			zoomToSelected(theArray,noPhoto,10)	 
		break;
		
		case "Print" :
		      show_print();		     	
		break;
		
		case "DownLoad" :
		      show_download();		   
		break;
		
		case "Search" :		       
		      search();		       
		break;
				
		case "ClearSearch" :
		      clearSearch();		       
		break;		
		
		case "point": 
		     
		     if (parent.MapFrame.document.mapserv.mode.value == "itemnquery")
   		     {
   		        xWidth=xmax - xmin;
			x = xmin + (xWidth/2);
			yWidth=ymax - ymin;
			y = ymin + (yWidth/2);

			xString = x.toString();
			yString = y.toString();
		       			
			point = xString + " " + yString;
			
		       URLString = new String;
		       imageext = new String;

		       URLString = "/cgi-bin/mapserv.exe?map="+escape(parent.MapFrame.document.mapserv.map.value);

		       parseImgExt();                       

		       for (i=0; i<newext.length; i++) 
			  {   imageext += newext[i];
			      if (i<newext.length-1) imageext += "+";
			  }

		        URLString += "&imgext=" + imageext;  		 

		       URLString += "&zoomdir=" + parent.MapFrame.document.mapserv.zoomdir.value;
		       URLString += "&zoomsize=" + zoomsize;
		       URLString += "&imgxy="+ point;
		            
		       
		       if (theSelection != "")
   		       { 			 		      
			     URLString += "&mode=itemnquery";
			     URLString += "&qlayer=bridge&map_querymap_size=" + imgxy  + "&map_bridge_header=demo.html&map_bridge_template=zoomto.html&map_web_error=../zoomaway.html&map_bridge_filter=" + theSelection;
		       } 
		       	
		     //  alert(URLString);	 
		       parent.MapFrame.location.href = URLString;		       
		        
		     }  
		     else
		      {
			if (parent.MapFrame.document.mapserv.imgbox.value != "-1 -1 -1 -1") {
				parent.MapFrame.document.mapserv.imgbox.value = "-1 -1 -1 -1";
			}
			
			xWidth=xmax - xmin;
			x = xmin + (xWidth/2);
			yWidth=ymax - ymin;
			y = ymin + (yWidth/2);
			
			xString = x.toString();
			yString = y.toString();
			
			point = xString + " " + yString;
			
			parent.MapFrame.document.mapserv.imgxy.value = point;			
			parent.MapFrame.document.mapserv.submit();
		     }
		 	 
	                        
		break;
		
		
		case "box":		        
		        
		        if (parent.MapFrame.document.mapserv.mode.value == "itemnquery")
			{		        			        
			       URLString = new String;
			       imageext = new String;

			       URLString = "/cgi-bin/mapserv.exe?map="+escape(parent.MapFrame.document.mapserv.map.value);

			       parseImgExt();
			       
			        maxx = xmax.toString();
				minx = xmin.toString();
				maxy = ymax.toString();
				miny = ymin.toString();
				box = minx + " " + miny + " " + maxx + " " + maxy;
				parent.MapFrame.document.mapserv.imgbox.value = box;     

			       for (i=0; i<newext.length; i++) 
				  {   imageext += newext[i];
				      if (i<newext.length-1) imageext += "+";
				  }

			       URLString += "&imgext=" + imageext;
			       URLString += "&imgbox=" + box;
			       URLString += "&zoomdir=" + parent.MapFrame.document.mapserv.zoomdir.value;
			       URLString += "&zoomsize=" + zoomsize;
			       URLString += "&imgxy="+mouseX+"+"+mouseY;		       
			     
 			        
 			        if (theSelection != "")
			 	{
		 	             URLString += "&mode=itemnquery";
			 	     URLString += "&qlayer=bridge&map_querymap_size=" + imgxy + "&map_bridge_header=demo.html&map_bridge_template=zoomto.html&map_web_error=../zoomaway.html&map_bridge_filter=" + theSelection;
			        }
			       
			      //  alert(URLString);
		                parent.MapFrame.location.href = URLString;       	                
		                
		       }
		       
		       else
		       {	        
				maxx = xmax.toString();
				minx = xmin.toString();
				maxy = ymax.toString();
				miny = ymin.toString();
				box = minx + " " + miny + " " + maxx + " " + maxy;
				parent.MapFrame.document.mapserv.imgbox.value = box;				 
				parent.MapFrame.document.mapserv.submit();
		       }
		       
			
		break;
 	}
 	
}

function parseImgExt() {
	ext = parent.MapFrame.document.mapserv.imgext.value;	 
	newext = ext.split(" ");
}

//initialising mouse event Capturing
function setEvents() {
	if (isNav) {
		parent.MapFrame.document.mapimage.captureEvents(Event.MOUSEMOVE);
		parent.MapFrame.document.mapimage.captureEvents(Event.MOUSEDOWN);
		parent.MapFrame.document.mapimage.captureEvents(Event.MOUSEUP);
		parent.MapFrame.document.mapimage.onmousemove = getMouse;
		parent.MapFrame.document.mapimage.onmousedown = whichTool;
		parent.MapFrame.document.mapimage.onmouseup = stop;
	} else if (isNav6) {
		parent.MapFrame.document.getElementById("mapimage").onmousemove=getMouse;
		parent.MapFrame.document.getElementById("mapimage").onmousedown = whichTool;
		parent.MapFrame.document.getElementById("mapimage").onmouseup = stop;
	} else {
		parent.MapFrame.document.onmousemove = getMouse;
		parent.MapFrame.document.onmousedown = whichTool;
		parent.MapFrame.document.onmouseup = stop;		
	}
	
	
}

//Set which funtion to start 
function whichTool(e)
{
	getImageXY(e);
	if (windowX<posleft || windowY<postop || windowX > posleft+layerwidth || windowY > postop+layerheight) {
			insideMap=false;	
	} else {
			insideMap = true;
			if (tool == "zoomin" || tool == "zoomout") startBox(e);
			if (tool == "panning") startPan(e);
			if (tool == "identify") identify(e);
			if (tool == "panningn") panMove(e);
	}
}


function stop(e) 
{
	if (tool == "zoomin" || tool == "zoomout") stopBox(e);
	if (tool == "panning")  stopPan(e); 
	if (tool == "identify") { makeQueryURL(e); }
	if (tool == "panningn") panMoveURL(e);
}


// get the coords at mouse position
function getMouse(e) {
	getImageXY(e);
	//if (!isNav) 
	 //	document.all.theTop.style.cursor = "crosshair";
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>layerwidth)
			mouseX = layerwidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>layerheight)
			mouseY = layerheight;
		x2=mouseX;
		y2=mouseY;
		setClip();
		return false;
	} else if (panning){
		if (isNav6) {
			if (mouseX<posleft)
			 	mouseX = posleft;
			if (mouseX>layerwidth+posleft)
				mouseX = layerwidth+posleft;
			if (mouseY<postop)
				mouseY = postop;
			if (mouseY>layerheight+postop)
				mouseY = layerheight+postop;
			x2=mouseX;
			y2=mouseY;
			moveLayer("mapimage", x2 + panOffsetX, y2 + panOffsetY) ;
			/*clipLayer("mapimage", -(posleft - panOffsetX - x2),
								-(postop - panOffsetY - y2) , 
								-(layerwidth  + posleft - panOffsetX - x2), 
								-(layerheight + postop - panOffsetY - y2) );*/
			return false;
		} else {
			if (mouseX<0)
			 	mouseX = 0;
			if (mouseX>layerwidth)
				mouseX = layerwidth;
			if (mouseY<0)
				mouseY = 0;
			if (mouseY>layerheight)
				mouseY = layerheight;
			x2=mouseX;
			y2=mouseY;
			//wertecheck
			tmpleft = posleft - panOffsetX - x2;
			tmptop = postop - panOffsetY - y2;
			topright = layerwidth  + posleft - panOffsetX - x2;
			topbottom = layerheight + postop - panOffsetY - y2;
			mausstring = tmpleft.toString() + " " + tmptop.toString() + " " +  topright.toString() + " " +  topbottom.toString();
			window.status = mausstring;
			//
			moveLayer("mapimage", x2 + panOffsetX, y2 + panOffsetY) ;
			clipLayer("mapimage", posleft - panOffsetX - x2,
								postop - panOffsetY - y2 , 
								layerwidth  + posleft - panOffsetX - x2, 
								layerheight + postop - panOffsetY - y2 );
			return false;
		}
	}else 
	   	return true;
	  return true;
}

function getImageXY(e) {
	if (isNav||isNav6) {
		windowX=e.pageX;
		windowY=e.pageY;
	} else {
		windowX=parent.MapFrame.event.clientX  + parent.MapFrame.document.body.scrollLeft;
		windowY=parent.MapFrame.event.clientY  + parent.MapFrame.document.body.scrollTop;
	}
	// subtract offsets from page left and top
	if (isNav6) {
		mouseX = windowX;
		mouseY = windowY;
	} else {
		mouseX = windowX-posleft;
		mouseY = windowY-postop;
	}
	
	x2 = mouseX;
	y2 = mouseY;
}	

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
		var pixelX = (right-left) / layerwidth;
		mapX = pixelX * xIn + left;
		var pixelY = (top-bottom) / layerheight;
		mapY = pixelY * (layerheight - yIn) + bottom;
}


function refresh()
{  
   // alert("refresh"); 
   tempCheck = new Array;
   URLString = new String;
   
   URLString = "/cgi-bin/mapserv.exe?map="+escape(parent.MapFrame.document.mapserv.map.value);
   
   parseImgExt();
   
   URLString += "&imgext="
   for (i=0; i<newext.length; i++) 
   {   URLString += newext[i];
       if (i<newext.length-1) URLString += "+";
   }
   
   URLString += "&zoomdir=" + parent.MapFrame.document.mapserv.zoomdir.value;
   URLString += "&imgxy="+mouseX+"+"+mouseY;
   URLString += "&mapext="+parent.MapFrame.document.mapserv.imgext.value;
 
   if (theSelection != "")
   {          
        URLString += "&mode=itemnquery";
        URLString += "&qlayer=bridge&map_querymap_size=" + imgxy  + "&map_bridge_header=demo.html&map_bridge_template=zoomto.html&map_web_error=../zoomaway.html&map_bridge_filter=" + theSelection;
   }
   else
   { 
     URLString += "&mode=browse";
   }  
      
   parent.MapFrame.location.href = URLString;
   
   
}  
 

function show_print()
{
   var host1 = "http://www.pastonearch.org";
   var title = prompt("Enter title of the map ","");
    
   mapimg_path = host1 + parent.MapFrame.document.mapserv.mapimg1.value;     
   legimg_path = host1 + parent.MapFrame.document.mapserv.lege.value; 
   refimg_path = host1 + parent.MapFrame.document.mapserv.ref1.value; 
   scalimg_path = host1 + parent.MapFrame.document.mapserv.scalebar.value;
   
    //alert(refimg_path);
  
   printWin = window.open( "", "printwin", "width=650,height=535,scrollbars,resizable,screenX=0,screenY=0,left=0,top=0");
   printWin.document.write('<html><head><title>'+ "PennDOT PAMS" +'  </title>');
    
   printWin.document.write('</head>');
   printWin.document.write('<BODY BGCOLOR=#ffffff TEXT=#191970 >');
   printWin.document.write('<form name="printform">');
   printWin.document.write('<center>');    
   printWin.document.write('<font face="Arial, Helvetica, sans-serif" size="4.5pt" >');
   
   printWin.document.write('<table   width="600" >');
   printWin.document.write('<tr>'); 
   
   printWin.document.write('<td align="center"> <img src="/images/bridge_map_app.jpg"  width="589" height="80" > </td>');
   
   printWin.document.write('</tr>');       
   printWin.document.write('</table> </font>'); 
  
   printWin.document.write('<table border="1" width="600">');
   printWin.document.write('<tr> <td>');
   printWin.document.write('<br>');
   
   printWin.document.write('<font size=+3 face="Arial, Helvetica, sans-serif"  color="rgb(0 64 128)">');
   printWin.document.write('<center>');
   printWin.document.writeln(title+'<br><br>'); 
   printWin.document.write('</font>');
   printWin.document.writeln('<img src='+mapimg_path+' width="482" height="275" border="1">'+'<br><br>');
   printWin.document.write('</center>');
   printWin.document.write('<br>');
   
   printWin.document.write('<font face="Arial, Helvetica, sans-serif" size="4pt">');
   printWin.document.write('<center>');
   printWin.document.write('<table   width="482" >');
   printWin.document.write('<tr>');   
   printWin.document.write('<td> <b> Reference Map </td>');
   printWin.document.write('<td> &nbsp&nbsp&nbsp&nbsp   </td>');
   printWin.document.write('<td> <b> Legend </td>');   
   printWin.document.write('<td ALIGN=right> <IMG src="/images/print.gif" onClick="window.print();">&nbsp&nbsp<IMG src="/images/close.gif" onClick="window.close();">  </td>');
   printWin.document.write('</tr>');
   
   printWin.document.write('<tr>');   
   printWin.document.write('<td> <img src='+refimg_path+'  width="90" height="52" border="0"> </td>');
   printWin.document.write('<td> &nbsp   </td>');
   printWin.document.write('<td align="left"> <img src='+legimg_path+'  border="0"> </td>');   
   printWin.document.write('<td>');
   printWin.document.write('<table>');
   printWin.document.write('<tr>'); 
   printWin.document.write('<td align="center"> <img src= "/images/north.gif" align="bottom"   border="0">  </td>');   
   printWin.document.write('</tr>'); 
   printWin.document.write('<tr>'); 
   printWin.document.write('<td> <img src='+scalimg_path+' width="200"   height="18"  border="0">  </td>');   
   printWin.document.write('</tr>');
   printWin.document.write('</table>');
   printWin.document.write('</td>');
   printWin.document.write('</tr>');  
   
   printWin.document.write('</table>');  
   printWin.document.write('</center> </font>');
    
   printWin.document.writeln(' <br><br>'); 
   
   printWin.document.write('<center>');
   printWin.document.write(' <img src= "/images/dev1.png"  border="0"> ');   
   printWin.document.write('</center>');
   
   printWin.document.write('</center>');
   printWin.document.write('</td> </tr>');
   printWin.document.write('</table>');
   printWin.document.write('</form>');
   printWin.document.write('<p></body></html>');
   printWin.document.close();
   
}


function selOffMap()
{   
 
    var theurl = URLString.replace(/itemnquery/,"browse");
  
    var  theurl1 = theurl.replace(/layers/,"layer=bridge ");
 
    parent.MapFrame.document.location.href = theurl1; 

}
 

 
