// JavaScript Document




  
  
 
  // =========================================================================
  // ====== Custom Tile Image Handler Function for Google Maps API  ==========	
  // ====== Images generated from GMapImageCutter Java App    ================
  // ====== All Image Tiles are 256px X 256px    =============================
  
  function customGetTileURL(a,b) {
    
  //converts tile x,y into keyhole string

     var c=Math.pow(2,b);

        var d=a.x;
        var e=a.y;
        var f="t";
        for(var g=0;g<b;g++){
            c=c/2;
            if(e<c){
                if(d<c){f+="q"}
                else{f+="r";d-=c}
            }
            else{
                if(d<c){f+="t";e-=c}
                else{f+="s";d-=c;e-=c}
            }
        }
        return url+f+".jpg"
    }
	


 

 
  // ======================================================================
  // ====== Function that sets Core Hole/Run Section images      ==========


  

   function getCoreSection(data) {
     
      var selectedMapType = map.getCurrentMapType().getName();
      map.clearOverlays()
	  var section = selectedMapType.substr(8,1); 
	  



      shapeAr = new Array();
  //    oldShapeAr = new Array();
	  measureAr = new Array();
	  reqIdNumAr = new Array();
	  shapeTypeAr = new Array();
      piSampleNumAr = new Array();
	  
	
         // ********    CUSTOMIZED FOR CORE HOLE AND RUN    ***********
		 // ***********************************************************

		switch (hole_run)
		{
			case 'E_1':
  			  var hrTitle = "Hole E - Run 1 - Section ";
			  var tileURL = "hole_e/hole_E_run_1_sec_";
			  var gXMLURL = "E_1_";
			  holeSelIndex = 0;
			  sectionCnt = 8;
  			break;
			case 'E_2':
  			  var hrTitle = "Hole E - Run 2 - Section ";
			  var tileURL = "hole_e/hole_E_run_2_sec_";
			  var gXMLURL = "E_2_";
			  holeSelIndex = 1;
			  sectionCnt = 6;
  			break;
			case 'G_1':
  			  var hrTitle = "Hole G - Run 1 - Section ";
			  var tileURL = "hole_g/hole_G_run_1_sec_";
			  var gXMLURL = "G_1_";
			  holeSelIndex = 2;
			  sectionCnt = 6;
  			break;
			case 'G_2':
  			  var hrTitle = "Hole G - Run 2 - Section ";
			  var tileURL = "hole_g/hole_G_run_2_sec_";
			  var gXMLURL = "G_2_";
			  holeSelIndex = 3;
			  sectionCnt = 9;
  			break;
			case 'G_3':
  			  var hrTitle = "Hole G - Run 3 - Section ";
			  var tileURL = "hole_g/hole_G_run_3_sec_";
			  var gXMLURL = "G_3_";
			  holeSelIndex = 4;
			  sectionCnt = 2;
  			break;
			case 'G_4':
  			  var hrTitle = "Hole G - Run 4 - Section ";
			  var tileURL = "hole_g/hole_G_run_4_sec_";
			  var gXMLURL = "G_4_";
			  holeSelIndex = 5;
			  sectionCnt = 7;
  			break;
			case 'G_5':
  			  var hrTitle = "Hole G - Run 5 - Section ";
			  var tileURL = "hole_g/hole_G_run_5_sec_";
			  var gXMLURL = "G_5_";
			  holeSelIndex = 6;
			  sectionCnt = 7;
  			break;
			case 'G_6':
  			  var hrTitle = "Hole G - Run 6 - Section ";
			  var tileURL = "hole_g/hole_G_run_6_sec_";
			  var gXMLURL = "G_6_";
			  holeSelIndex = 7;
			  sectionCnt = 6;
  			break;
   			default:
  			  var hrTitle = "Hole E - Run 1 - Section ";
			  var tileURL = "hole_e/hole_E_run_1_sec_";
			  var gXMLURL = "E_1_";
			  holeSelIndex = 0;
			  sectionCnt = 8;
  			break;
        }
	
	
	  document.title = hrTitle + section;
	  document.getElementById('holeSecTitle').innerHTML = "You are currently viewing " + hrTitle + section;
	
	 
	  url = "/images/es_data_page/safod_core_data/" + tileURL + section + "_tiles/";
	  var gURL = "/data/safod_core_data/" + gXMLURL + section;
 

		switch (section)
		{
			case '1':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_01);
  			break;
			case '2':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_02);
  			break;
			case '3':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_03);
  			break;
			case '4':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_04);
  			break;
			case '5':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_05);
  			break;
			case '6':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_06);
  			break;
			case '7':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_07);
  			break;
			case '8':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_08);
  			break;
			case '9':
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_09);
  			break;
   			default:
  			map.setCenter(new GLatLng(0,-25.55), 2, sec_01);
  			break;
        }
	  
	    
      // ==============================================================
      // === Google AJAX Call to URL:  /safod_core/section_data   ======= 

		GDownloadUrl(gURL, function(data, responseCode) {
		
  		var xml = GXml.parse(data);
  		var sectionMarkers = xml.documentElement.getElementsByTagName("marker");
		
  		  for (var i = 0; i < sectionMarkers.length; i++) {
		    var xrLat = parseFloat(sectionMarkers[i].getAttribute("xrlat"));
			var yrLng = parseFloat(sectionMarkers[i].getAttribute("yrlng"));
			var xlLat = parseFloat(sectionMarkers[i].getAttribute("xllat"));
			var ylLng = parseFloat(sectionMarkers[i].getAttribute("yllng"));
			
			//var oldxrLat = parseFloat(sectionMarkers[i].getAttribute("oldxrlat"));
			//var oldyrLng = parseFloat(sectionMarkers[i].getAttribute("oldyrlng"));
			//var oldxlLat = parseFloat(sectionMarkers[i].getAttribute("oldxllat"));
			//var oldylLng = parseFloat(sectionMarkers[i].getAttribute("oldyllng"));
			

			
			var infoWin = GXml.value(sectionMarkers[i].getElementsByTagName("infowindow")[0]);
			var piNum = sectionMarkers[i].getAttribute("piNum");
		//	var modified = sectionMarkers[i].getAttribute("modified");
			var shape = sectionMarkers[i].getAttribute("shape");
		    var baseDepth = sectionMarkers[i].getAttribute("baseDepth");
			var measurement = sectionMarkers[i].getAttribute("measurement");
			var piColor	 = sectionMarkers[i].getAttribute("piColor");
			
			var tableData = GXml.value(sectionMarkers[i].getElementsByTagName("tabledata")[0]);

			measureAr.push(sectionMarkers[i].getAttribute("measurement"));
			reqIdNumAr.push(parseFloat(sectionMarkers[i].getAttribute("reqIdNum")));
			shapeTypeAr.push(sectionMarkers[i].getAttribute("shape"));
			piSampleNumAr.push(piNum);
			
		 	 
		     // ==============================================================
      		 // === Create Shape for request item by Shape Type Value ======= 
			 
		    if(shape == 'R') {
			
			    cntrMarkerAr[shapeAr.length] =  null;
			    var markerPoint = new GLatLng(yrLng, xrLat);
		
	            var rectAr =[];
	    		rectAr.push(new GLatLng(yrLng, xlLat));
	    		rectAr.push(new GLatLng(ylLng, xlLat));
	    		rectAr.push(new GLatLng(ylLng, xrLat));	  
	    		rectAr.push(new GLatLng(yrLng, xrLat));
    		    rectAr.push(new GLatLng(yrLng, xlLat));	
				
	       		drawPolygon(rectAr, piColor, infoWin, markerPoint);
				
			   
			    //var oldMarkerPoint = new GLatLng(oldyrLng, oldxrLat);
		
	           /*  var rectAr =[];
	    		 rectAr.push(new GLatLng(oldyrLng, oldxlLat));
	    		 rectAr.push(new GLatLng(oldylLng, oldxlLat));
	    		 rectAr.push(new GLatLng(oldylLng, oldxrLat));	  
	    		 rectAr.push(new GLatLng(oldyrLng, oldxrLat));
    		     rectAr.push(new GLatLng(oldyrLng, oldxlLat));	
				
	       		drawPolygon(rectAr, '#ffffff', infoWin, oldMarkerPoint, 1);
				
				*/

		     }
		    else if(shape == 'C') {
  
	  			xlylMarkerAr[shapeAr.length] =  null;
      			xryrMarkerAr[shapeAr.length] =  null;
				
			    var markerPoint = new GLatLng(ylLng, xlLat);
		        drawCircle(safodMapProjection, markerPoint, 12, piColor, infoWin);
							
			   // var oldMarkerPoint = new GLatLng(oldylLng, oldxlLat);
		      //  drawCircle(safodMapProjection, oldMarkerPoint, 12, '#ffffff', infoWin, 1);
				
				
		     }
		    else{
							  
	  			xlylMarkerAr[shapeAr.length] =  null;
      			xryrMarkerAr[shapeAr.length] =  null;
				
			    var markerPoint = new GLatLng(ylLng, xlLat);
		        drawPoint(safodMapProjection, markerPoint, 8, piColor, infoWin);
				
				/*
				var oldMarkerPoint = new GLatLng(oldylLng, oldxlLat);
	           drawPoint(safodMapProjection, oldMarkerPoint, 8, '#ffffff', infoWin, 1);
			   
			   */
		     }		    		
	
		 
		   // ============================================================================
           // === Create Marker for Info Window Display on Request Item Mouse Over ======= 	
           // === or Shape Mouse Click  ================================================== 	
			
		    shapeMarker[i] = markerPoint;
			markerHTML[i] = infoWin;
			
		  }
		  
		 document.getElementById('sectionData').innerHTML = tableData;
	    
  		});
		
     
	
   


   }



   
   function resizeIE() {

     window.moveTo(0,0);
	 window.resizeTo(screen.availWidth - 2, screen.availHeight - 2);
	 window.resizeTo(screen.availWidth, screen.availHeight);
		
	}

  // ============================================================
  // ====== Handler for Mouse Over of Polygons and Circles  =====

  function showInfo(id) {

      map.openInfoWindowHtml(shapeMarker[id], markerHTML[id]);
	
	   
   }
   



  // ============================================================
  // ====== Polygon Construction Method  =====

  function drawPolygon(points, color, infoWin, markerPoint) {
      polObj=new GPolygon(points, color, 3, 1, color, 0.03);
      map.addOverlay(polObj);
	   
	  
	  GEvent.addListener(polObj, "click", function() {
          map.openInfoWindowHtml(markerPoint, infoWin);
		  // TB_init();
        });

	  shapeAr.push(polObj);
	  //polObj.hide();
   }



  // ============================================================  
  // ===== Draw a Circle with a Center GLatLng and a Radius =====
    
  function drawCircle(proj, point, radius, color, infoWin, markerPoint){

    var zoom = map.getZoom();
    var centerPt = proj.fromLatLngToPixel(point, zoom);
           
   	var circlePoints = new Array(); 

	with (Math) {
		for (var a = 0 ; a < 361 ; a+=10 ) {
		var aRad = a*(PI/180);
		y = centerPt.y + radius * sin(aRad)
		x = centerPt.x + radius * cos(aRad)
			
			
		var p = new GPoint(x,y);
	
	        
		circlePoints.push(proj.fromPixelToLatLng(p, map.getZoom()));
	}
		

		cirObj = new GPolygon(circlePoints,color,3,1);
		map.addOverlay(cirObj);
		
		GEvent.addListener(cirObj, "click", function() {
          map.openInfoWindowHtml(markerPoint, infoWin);
		  // TB_init();
        });
		shapeAr.push(cirObj);
		//cirObj.hide();
	}
  }




  // ============================================================
  // ====== Gmap Point ConstructionMethod  ==========================

  function drawPoint(proj, point, radius, color, infoWin, markerPoint) {
    
    var zoom = map.getZoom();
    var centerPt = proj.fromLatLngToPixel(point, zoom);
           
   	var circlePoints = new Array();

	with (Math) {
		for (var a = 0 ; a < 361 ; a+=60 ) {
		var aRad = a*(PI/180);
		y = centerPt.y + radius * sin(aRad)
		x = centerPt.x + radius * cos(aRad)
			
			
		var p = new GPoint(x,y);
	
	        
		circlePoints.push(proj.fromPixelToLatLng(p, map.getZoom()));
	}
		
		ptObj = new GPolygon(circlePoints,color,1,1,color,.41);
		map.addOverlay(ptObj);
		
		
	    GEvent.addListener(ptObj, "click", function() {
          map.openInfoWindowHtml(markerPoint, infoWin);
		  // TB_init();
        });
		shapeAr.push(ptObj);
		//ptObj.hide();
	}
	
   }



     
  // ===============================================
  // ====== Show All Request Map Objects    ========	
  
  function showAllRequest() {
 
   for (i=0; i < shapeAr.length; i++) {  
       var shapeObj = shapeAr[i];
       shapeObj.show(); 
	}
	

    MM_showHideLayers('sectionData','','show');
  }
   
  // ===============================================
  // ====== Hide All Request Map Objects    ========	
  
  function hideAllRequest() {
 
   for (i=0; i < shapeAr.length; i++) {  
       var shapeObj = shapeAr[i];
         shapeObj.hide();
	   
	   	if(xlylMarkerAr[i])map.removeOverlay(xlylMarkerAr[i]);
		if(xryrMarkerAr[i])map.removeOverlay(xryrMarkerAr[i]);
		if(cntrMarkerAr[i])map.removeOverlay(cntrMarkerAr[i]); 
	
	}


     MM_showHideLayers('sectionData','','hide');
  }
  
  // ===============================================
  // ====== Show/Hide Request Map Objects   ========	
  
  function showHideRequest(id) {
   
  var shapeObj = shapeAr[id];
  if(shapeObj.isHidden() == true) {shapeObj.show();}
  else {  shapeObj.hide(); }
    
  }
  
   // ====================================================
  // ====== Show/Hide Request Map Objects By PI   ========	
  
  function showHideGroup(reqCnt, prevGrpCnt) {

   for(i = reqCnt; i < reqCnt+prevGrpCnt;  i++) {
      
	    var shapeObj = shapeAr[i];
        if(shapeObj.isHidden() == true) {shapeObj.show();}
        else {  shapeObj.hide(); }
    }
	
	   
  }
