﻿/// <reference path="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAA9JOQeJ0ibL7hptkY7_zghT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQc5HzaJNMlGCvZRCdCjOFzzIq3Bg" />
/// <reference path="http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/src/markermanager.js" />

var hfFacilityName;
var hfPointX;
var hfPointY;
var hfTmpItem;
var hfTmpLatLongItem;
var btnFacilityInformation;

function InitializeFieldReferences(hfFacilityNameID, hfPointXID, hfPointYID, hfTmpItemID, hfTmpLatLongItemID, btnFacilityInformationID )
{
    hfFacilityName = $get(hfFacilityNameID);
    hfPointX = $get(hfPointXID);
    hfPointY = $get(hfPointYID);
    hfTmpItem = $get(hfTmpItemID);
    hfTmpLatLongItem = $get(hfTmpLatLongItemID);
    btnFacilityInformation = $get(btnFacilityInformationID);
    loadMap();
}

/////////////////// start map related code
// Global Variables          
var geocoder = new GClientGeocoder();                               
var batch = [];

//------------------------------  
var tmpItem = null;
var tmpLatlongItems = null;
    
function loadMap() 
{
    if (GBrowserIsCompatible())
    {                   
        var map = new GMap2(document.getElementById("map_canvas"));                                   
        map.addControl(new GLargeMapControl());  
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(37.4419, -98.1419),4);                                                                                
        map.enableContinuousZoom();
        map.enableDoubleClickZoom();                
        //map.enableScrollWheelZoom();
                             
        for (var i = 0; i < markers.length; i++)
        {   
            showAddress(markers[i],map);                        
        }
        
        var mgr = new MarkerManager(map);
        mgr.addMarkers(batch, 4);
        mgr.refresh();  
        
        if(hfPointX.value != "" && hfPointY.value != "")
        {
            var point = new GLatLng(hfPointX.value, hfPointY.value); 
            hfPointX.value = "";
            hfPointY.value = "";
            
            var myHtml = "<b style='font-family: Verdana; font-size: 9pt;'>" + hfTmpItem.value + "<br/><br/>" + hfTmpLatLongItem.value + "</b>";
            map.openInfoWindowHtml(point, myHtml);
        }              
    }
 }
        
function showAddress(itemPlant,objMap)
{
    
    var Items = itemPlant.split("|");         

    if (Items != null)
    {            
        var latlongItems = Items[2].split("?");                
        if (Items[4] == "True")
        {
                //alert('blue');
                // Create our "tiny" marker icon
                var blueIcon = new GIcon(G_DEFAULT_ICON);
                blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
                blueIcon.iconSize = new GSize(32,32);
                blueIcon.shadowSize = new GSize(58, 32);
                blueIcon.iconAnchor = new GPoint(15, 32);
                blueIcon.infoWindowAnchor = new GPoint(15, 2);

                // Set up our GMarkerOptions object
                markerOptions = { icon:blueIcon };
                
                var point = new GLatLng(latlongItems[0], latlongItems[1]);                                                 
                var marker = new GMarker(point, markerOptions);
                GEvent.addListener(marker, "click", function() 
                {
                    //var myHtml = "<b style='font-family: Verdana; font-size: 9pt;'>" + Items[1] + "<br/><br/>" + latlongItems[2] + "</b>";
                    //objMap.openInfoWindowHtml(point, myHtml);
                    hfTmpItem.value = Items[3];//Items[1];
                    hfTmpLatLongItem.value = Items[1];//latlongItems[2];
                    
                    hfPointX.value = latlongItems[0];
                    hfPointY.value = latlongItems[1];
                    
                                          
                    // Set Set Selected Marker (Plant) Information                    
                    //ShowPlantInformation(Items[1], latlongItems[2], Items[3], Items[4]);
                    ShowFacilityInformation(Items[0]);
                
                });  
                //objMap.addOverlay(marker);  
                //batch.push(marker);   
                                
         }
         else
         {   
                var point = new GLatLng(latlongItems[0], latlongItems[1]);                                                                                       
                var marker = new GMarker(point);                        
                
                GEvent.addListener(marker, "click", function() 
                {
                    hfTmpItem.value = Items[3];//Items[1];
                    hfTmpLatLongItem.value = Items[1];//latlongItems[2];
                    
                    hfPointX.value = latlongItems[0];
                    hfPointY.value = latlongItems[1];
                    
                    //var myHtml = "<b style='font-family: Verdana; font-size: 9pt;'>" + Items[1] + "<br/><br/>" + latlongItems[2] + "</b>";
                    //objMap.openInfoWindowHtml(point, myHtml);                                                                                         
                
                    // Set Set Selected Marker (Plant) Information                    
                    //ShowPlantInformation(Items[1], latlongItems[2], Items[3], Items[4]);
                    //alert('here in master page');
                    ShowFacilityInformation(Items[0]);
                
                });  
                
                
                //objMap.addOverlay(marker);  
                //batch.push(marker);                    
          }
            
            objMap.addOverlay(marker);
            batch.push(marker);
    } 
               
} 
   
   /* 
   function ShowInfoWindow(objMap,Items,latlongItems)
   {
        //alert('inside ShowInfoWindow')
        var hfPointX = $get('<%= hfPointX.ClientID %>');
        var hfPointY = $get('<%= hfPointY.ClientID %>');
        
        if(hfPointX.value != "" && hfPointY.value != "")
        {
            
            //alert('inside if');
            //alert('if hfpointx'+hfPointX.value);
            //alert('if hfpointy'+hfPointY.value);
            var point = new GLatLng(hfPointX.value, hfPointY.value);         
            var marker = new GMarker(point);                        
          
            hfPointX.value = "";
            hfPointY.value = "";
              
            var myHtml = "<b style='font-family: Verdana; font-size: 9pt;'>" + Items[1] + "<br/><br/>" + latlongItems[2] + "</b>";
            objMap.openInfoWindowHtml(point, myHtml);
            
            objMap.addOverlay(marker);  
            batch.push(marker); 
         }
        else
        {
            //alert('inside else');
            var point = new GLatLng(latlongItems[0], latlongItems[1]);         
            var marker = new GMarker(point);                        
            //alert('else hfpoint x'+hfPointX.value)
            //alert('else hfpoint y'+hfPointY.value)
            //alert('else hfmarker'+hfMarkers.value)
            GEvent.addListener(marker, "click", function() 
            {
                var myHtml = "<b style='font-family: Verdana; font-size: 9pt;'>" + Items[1] + "<br/><br/>" + latlongItems[2] + "</b>";

                hfPointX.value = latlongItems[0];
              
                hfPointY.value = latlongItems[1];
              
                hfMarkers.value = markers;
             
//                alert("Markers: <br/>" + markers);
                objMap.openInfoWindowHtml(point, myHtml);
                //hfItems.value = point
      
                // Set Set Selected Marker (Plant) Information                    
                //ShowPlantInformation(Items[1], latlongItems[2], Items[3], Items[4]);
                //alert('here in master page');
                //if(postBack == true)
                //{
                    ShowFacilityInformation(Items[1]);
                //}
            
            });  
            
            objMap.addOverlay(marker);  
            batch.push(marker);  
        }
               
    }
    */

    //------------------------------              
    function ShowFacilityInformation(strFacilityName)
    {       
//    alert(strFacilityName);
        hfFacilityName.value = strFacilityName;
        //__doPostBack('', '');
        btnFacilityInformation.click();
    } 
 

 
///////////////////end map code


