﻿// JScript File

    var map;    
    var mapCanvas;
    var markerBatch;
    var iconJCB;
    var iconJCBBlue;
    var dealerInfoPage;
    var specificIndustryType;
    var centerMarker;
    var markerManager;
    var controlPrefix;
    var localSearch = new GlocalSearch();
    
    function createMarkerBatch()
    {
        // Read the data from xml file
        var request = GXmlHttp.create();
        
        var dealerList = document.getElementById(controlPrefix + "hidDealerList").value;
        
        request.open("GET", dealerList, true);
        request.onreadystatechange = function() {

            if (request.readyState == 4) {
                
                var xmlDoc = GXml.parse(request.responseText);
                // obtain the array of markers and loop through it
                var markerNodes = xmlDoc.documentElement.getElementsByTagName("m");
                for (var i = 0; i < markerNodes.length; i++) {
           
                    // obtain the attribues of each marker
                    var lat = parseFloat(markerNodes[i].getAttribute("lt"));
                    var lng = parseFloat(markerNodes[i].getAttribute("lg"));            
                    var dealerID = parseFloat(markerNodes[i].getAttribute("d"));            
                    var type = parseInt(markerNodes[i].getAttribute("t"));
                                                   
                    var point = new GLatLng(lat,lng);
                    
                    // create the marker and add it to the markers array                
                    var marker = null;
                    if (type == 1) // Distributor is type 1                 
                        marker = createMarker(point, dealerID, true);
                    else
                        marker = createMarker(point, dealerID, false);
                        
                    markerBatch.push(marker);                
                    
                }                    
                // add all the dealer markers -- if available
                if (markerBatch.length > 0)
                {
                    markerManager.addMarkers(markerBatch, 3);
                }   
                
                //alert(markerBatch.length);
                 
                // add the center marker for the selected address
                markerManager.addMarker(centerMarker, 3);
                // update the map
                markerManager.refresh();          
            }
        
        }
      
        request.send(null);
    }
    
    function load(controlIDPrefix) {      
    
      // store the controlIDPrefix to be used throughtout the code
      controlPrefix = controlIDPrefix
      
      // update the main search button text
      document.getElementById("butMainSearch").value = document.getElementById(controlPrefix + "butSearch").value

      if (GBrowserIsCompatible()) {
      
        mapCanvas = document.getElementById("map_canvas");
        
        if (mapCanvas != null) {                    
       
            // set up the map
            map = new GMap2(mapCanvas);
            // add the standard pan/zoom control
            map.addControl(new GLargeMapControl());
            //map.addControl(new GScaleControl());
            //map.addControl(new GMapTypeControl());
            //map.addControl(new GOverviewMapControl());
            
            // capture the mouse wheel for zooming
            map.enableScrollWheelZoom();   
            
            // create the jcb icon
            iconJCB = new GIcon();
            iconJCB.image = document.getElementById(controlPrefix + "hidIconJCBImage").value;
            iconJCB.shadow = document.getElementById(controlPrefix + "hidIconJCBShadow").value;
            iconJCB.iconSize = new GSize(32, 32);
            iconJCB.shadowSize = new GSize(39, 32);
            iconJCB.iconAnchor = new GPoint(0, 32);
            iconJCB.infoWindowAnchor = new GPoint(0, 32);
            iconJCB.imageMap = new Array(0,0,32,32);
            
            
            
            // create the jcb blue icon
            iconJCBBlue = new GIcon();
            iconJCBBlue.image = "http://www.jcbpowersystems.co.uk/images/psflagicon.gif"; 
            iconJCBBlue.shadow = "http://www.jcbpowersystems.co.uk/images/psflagiconShad.png";   
            iconJCBBlue.iconSize = new GSize(32, 32);
            iconJCBBlue.shadowSize = new GSize(39, 32);
            iconJCBBlue.iconAnchor = new GPoint(0, 32);
            iconJCBBlue.infoWindowAnchor = new GPoint(0, 32);
            iconJCBBlue.imageMap = new Array(0,0,32,32);
                   

            // get the page the dealer info comes from
            dealerInfoPage = document.getElementById(controlPrefix + "hidGMDealerInfoPage").value         

            // center the map on the selected postcode
            var lat = parseFloat(document.getElementById(controlPrefix + "hidCenterLatitude").value);
            var lon = parseFloat(document.getElementById(controlPrefix + "hidCenterLongitude").value);

            // make the marker draggable
            centerMarker = new GMarker(new GLatLng(lat, lon), {draggable: true}); //, icon: iconYouAreHere});
            
            GEvent.addListener(centerMarker, "click", function() {
                // and show the info
                centerMarker.openInfoWindowHtml("<iframe frameborder='0' src='" + dealerInfoPage + "?UAD=true" +
                                                "&STREET=" + document.getElementById(controlPrefix + "hidStreet").value + 
                                                "&TOWN=" + document.getElementById(controlPrefix + "hidTown").value + 
                                                "&POSTCODE=" + document.getElementById(controlPrefix + "hidPostcode").value + 
                                                "&COUNTRY=" + document.getElementById(controlPrefix + "hidCountry").value + 
                                                "&DRAGGED=" + document.getElementById(controlPrefix + "hidMarkerDragged").value + 
                                                "' width='300px' height='175px'></iframe>");
                });
                        
            GEvent.addListener(centerMarker, "dragstart", function() {

                // store the current center point 
                var latLng = centerMarker.getPoint();
                if (latLng != null)
                {
                    document.getElementById(controlPrefix + "hidCenterLongitude").value = latLng.lng();
                    document.getElementById(controlPrefix + "hidCenterLatitude").value = latLng.lat();                
                }
                
                // close any info windows
                map.closeInfoWindow();
            });
           
            GEvent.addListener(centerMarker, "dragend", function() {
                            
                // get the latitude and longitude of the moved marker
                var latLng = centerMarker.getPoint();
                if (latLng != null)
                {
                    // only update the dragged status and the lat and lon if we actually have moved
                    if (document.getElementById(controlPrefix + "hidCenterLongitude").value != latLng.lng() ||
                        document.getElementById(controlPrefix + "hidCenterLatitude").value != latLng.lat())
                    {
                        // update the current center
                        document.getElementById(controlPrefix + "hidCenterLongitude").value = latLng.lng();
                        document.getElementById(controlPrefix + "hidCenterLatitude").value = latLng.lat();  

                        // set the marker dragged flag so we no longer show the address
                        document.getElementById(controlPrefix + "hidMarkerDragged").value = latLng.lng() + "," + latLng.lat();
                    }                
                }
            });       
                        
            // center the map with a zoom giving the viewport approx 50 miles across
            map.setCenter(centerMarker.getPoint(), 6);
            
            // create the marker manager
            markerManager = new MarkerManager(map);
                        
           // create all the markers                                                           
            specificIndustryType = parseInt(document.getElementById(controlPrefix + "hidIndustryType").value);
            markerBatch = []            
            createMarkerBatch();   
                        

            
                        
        }
      }
    }
    
    // A function to create the marker and set up the event window
    // Dont try to unroll this function. It has to be here for the function closure
    // Each instance of the function preserves the contends of a different instance
    // of the "marker" and "html" variables which will be needed later when the event triggers.    
    function createMarker(point,dealerid, isDistributor) {

        // Set up our GMarkerOptions object literal
        if (isDistributor)
            markerOptions = { icon:iconJCB };    
        else
            markerOptions = { icon:iconJCBBlue };
    
        var marker = new GMarker(point, markerOptions);
        //var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
            // and show the info
            
            var dealerInfoUrl = "<div style='Width:320px;Height:185px;'><iframe frameborder='0' src='" + dealerInfoPage + "?DID=" + dealerid + 
                                "&LAT=" + document.getElementById(controlPrefix + "hidCenterLatitude").value + "&LON=" + 
                                document.getElementById(controlPrefix + "hidCenterLongitude").value + 
                                "&TAB={0}' width='320px' height='185px'></iframe></div>"
            
            var tab1 = new GInfoWindowTab("Address", dealerInfoUrl.replace("{0}", 1));
            var tab2 = new GInfoWindowTab("Contact", dealerInfoUrl.replace("{0}", 2));
            
            marker.openInfoWindowTabsHtml([tab1, tab2]);
            });
        return marker;
    }    

    function SearchResults()
    {
        // if we have any results set the hidden control values
        if (localSearch.results[0])
        {
            document.getElementById(controlPrefix + "hidUKPostcodeLatitude").value = localSearch.results[0].lat;
            document.getElementById(controlPrefix + "hidUKPostcodeLongitude").value = localSearch.results[0].lng;
            
            // and do a normal postback
            document.getElementById(controlPrefix + "butSearch").click();            
        }
    }
    
    function SearchForUKPostcode() {
    
        // only want to do this for uk
        var countryID = document.getElementById(controlPrefix + "cddlCountry_ddlCountries").value
        
        if (countryID == 1)
        {
            var postcode = document.getElementById(controlPrefix + "txtPostCode").value;
           
            if (postcode != null && postcode != "")
            {
                localSearch.setSearchCompleteCallback(null, SearchResults);
                localSearch.execute(postcode + ", UK");
            }
            else
            {
                // do a normal postback
                document.getElementById(controlPrefix + "butSearch").click();
            }
            
        }
        else
        {
            // do a normal postback
            document.getElementById(controlPrefix + "butSearch").click();
        }
    }
    

