﻿// JScript File

//<script type="text/javascript">
    //<![CDATA[
    var highlightCircle;
    var currentMarker;
    var map;
    var marker;
     
    // Creates a "circle" using 20-sided GPolygon at the given point
    // Circle polygon object is global variable as there is only one highlighted marker at a time
    // and we want to remove the previously placed polygon before placing a new one.

    function highlightCurrentMarker(){
      var markerPoint = currentMarker.getPoint();

      var polyPoints = Array();

      if (highlightCircle) {
        map.removeOverlay(highlightCircle);
      }

      var mapNormalProj = G_NORMAL_MAP.getProjection();
      var mapZoom = map.getZoom();
      var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);

      var polySmallRadius = 20;

      var polyNumSides = 20;
      var polySideLength = 18;

      for (var a = 0; a<(polyNumSides+1); a++) {
	    var aRad = polySideLength*a*(Math.PI/180);
	    var polyRadius = polySmallRadius; 
       	    var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
	    var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
	    var polyPixel = new GPoint(pixelX,pixelY);
	    var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
	    polyPoints.push(polyPoint);
      }
      // Using GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
      highlightCircle = new GPolygon(polyPoints,"#000000",2,0.0,"#FF0000",.5);
      map.addOverlay(highlightCircle);
   }

    // Creates a marker at the given point with the given number label
    function createMarker(point, number, url) {
      var marker1;
      if(number=='' && url=='')
        marker1 = new GMarker(point, {draggable:true});
      else
        marker1 = new GMarker(point, {draggable:false});
  
      // This line highlights the marker when it's clicked
      GEvent.addListener(marker1, "click", function() {
          currentMarker = marker1;
//          highlightCurrentMarker();	
          if(url!='') location = url;
      });

      // This line highlights the marker when its moused over
      GEvent.addListener(marker1, "mouseover", function() {
          currentMarker = marker1;
//          highlightCurrentMarker();	
          if(number!='') marker1.openInfoWindowHtml(number);
      });

      // This line highlights the marker while dragging
      GEvent.addListener(marker1, "drag", function() {
          currentMarker = marker1;
//          highlightCurrentMarker();	
      });

      return marker1;
    }
    
          
    var gdir;
    var geocoder = null;
    var addressMarker;
    function loadMap(latitude,longtitude,containerMap, containerDirection) {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById(containerMap));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());        
        map.setCenter(new GLatLng(latitude, longtitude), 15);        
        //map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();  
        GEvent.addListener(map, "zoomend", function() {
	        //highlightCurrentMarker();	
        });
        
        gdir = new GDirections(map, document.getElementById(containerDirection));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        //setDirections("San Francisco", "Mountain View", "en_US");
        
        var point = new GLatLng(latitude, longtitude);
        marker = createMarker(point, '','');
        map.addOverlay(marker);

      }
    }
    
    function loadMapSearch(container) {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById(container));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());        
        map.setCenter(new GLatLng(0, 0), 15);        
        //map.enableDoubleClickZoom();
        map.enableScrollWheelZoom(); 
        map.setZoom(10); //15         
        GEvent.addListener(map, "zoomend", function() {
	        //highlightCurrentMarker();	
        });       
      }
    }

    function GoBackLocationMap(lat,lon)
    {
        var pointBack = new GLatLng(lat, lon);
        map.removeOverlay(marker);
        if (highlightCircle) {
            map.removeOverlay(highlightCircle);
        }
        map.setCenter(pointBack, 15);
        marker = createMarker(pointBack, '','');
        map.addOverlay(marker);
    }

    function setDirections(fromAddress, toAddress, locale) {      
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });        
    }
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);	    
	   else alert("An unknown error occurred.");
	   
	}
    function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.
      // checkHeight();
      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}


    /*
    [0][1] : lat, long
    [2] : image 
    [3] : url detail
    [4] : name
    [...] : properties
    */
    function LoadSearchMap()
    {
        loadMapSearch('mapDiv');
        
        var hidMapList = getValueFromTextField('hid_MapIDs');
        var arr = hidMapList.split('#');
        
        for(var i=0; i< arr.length-1; i++)
        {
            var arrItm = arr[i].split('|');
            var point1 = new GLatLng(arrItm[0], arrItm[1]);
            var html = "<table class=\"text_11\" style=\"color:#151515; \">"
            
            html += "<tr>";
            if(arrItm[2]  != "")
            {
                html += "<td align=\"center\" >";            
                html += "<a href=\"" + arrItm[3]  + "\">"
                    + "<img src=\"" + arrItm[2]  + "\" width=75 height=75 /></a><br/>";
                html += "<div style=\"margin-top:5px \" ><a href=\"" + arrItm[3]  + "\"><b>More Details</b></a></div>";
                html += "</td>";                
            }
            
            html += "<td width=\"200px\"><div  style=\"margin-left:10px; line-height:15px\">";
            //Name
            html += "<div style='margin-bottom:5px' ><a class=\"text_12 bold \" href=\"" + arrItm[3]  + "\" ><u>" + arrItm[4] + "</u></a></div>"
            
            for(var j = 5; j<arrItm.length;j++)
            {
                html += arrItm[j] + "<br />";
            }           
            if(arrItm[2]  == "")
            {
                html += "<div style=\"margin-top:5px \" ><a href=\"" + arrItm[3]  + "\"><b>More Details</b></a></div>";
            }
            html += "</div></td>";
            html += "</tr></table>";            
            var marker1 = createMarker(point1, html,arrItm[3]);
            if(i==0) map.setCenter(point1);
            map.addOverlay(marker1); 
        }        
        hide("div_Loading");
    }
	    
    //]]>
    //</script>
