 var mapviewer, marker, geocoder;
 var small_pan_zoom_widget;


        function onLoad() {
            //Add the map 
            mapviewer = MMFactory.createViewer( document.getElementById( 'mapviewer' ) );
            mapviewer.goToPosition( new MMAddress( { 'qs' : 'KA3 2GN', 'country_code' : 'GB' } ) , 11 );

            // Create the geocoder
            geocoder = new MMGeocoder( handleGeocodeResults );
            
            geocodePostCodes();
            
            small_pan_zoom_widget = new MMSmallPanZoomWidget ();
            mapviewer.addWidget ( small_pan_zoom_widget );
        }

        function geocodePostCodes() {
            // Create an array of MMAddress objects for geocoding
            var addresses = new Array;

            addresses.push( new MMAddress( { 'postal_code' : 'KA3 2GN', 'country_code' : 'GB' } ) );

            //addresses.push( new MMAddress( { 'postal_code' : 'KA26 0NA', 'country_code' : 'GB' } ) );

            //addresses.push( new MMAddress( { 'postal_code' : 'G2 4JR', 'country_code' : 'GB' } ) );
            
            //addresses.push( new MMAddress( { 'postal_code' : 'KA7 1PR', 'country_code' : 'GB' } ) );

            if (addresses.length > 0)
                geocoder.geocode( addresses );
        }

        function handleGeocodeResults( result_set ) {
            // Clear any old overlays from the map
            mapviewer.removeAllOverlays();

            var markers = new Array;
            var addresses = result_set;
            var office = new Array;
            office[0] = '<p>CJM Accountancy<br />15 Baleshrae Crescent<br />Southcraigs<br/>Kilmarnock<br/>KA3 2GN</p>';
            //office[1] = '<p>CJM Accountancy<br />37 Main Street<br />Ballantrae<br />KA26 0NA</p>';
            //office[2] = '<p>CJM Accountancy<br />272 Bath Street<br />Glasgow<br />G2 4JR</p>';
            //office[2] = '<p>CJM Accountancy<br />130 High Street<br />Ayr<br/>KA7 1PR</p>';
            
            var icon = new MMIcon( 'http://www.gallowaywebdesign.co.uk/js/map-icon.png' );
            icon.iconSize = new MMDimensions( 39, 30 );
            icon.iconAnchor = new MMPoint( 16, 16 );

            // Iterate the addresses and create a marker for each one
            for (var i = 0, j = addresses.length; i < j; i++) {
                var result = addresses[i];
                for (var k = 0, l = result.length; k < l; k++) {
                    if (result[k] && result[k].coords) {
                        var marker = mapviewer.createMarker( result[k].coords, {'icon' : icon} );
                        marker.setInfoBoxContent( office[i] );
                        markers.push( marker );
                    }
                }
            }

            // Auto scale the map
            var auto_scale = mapviewer.getAutoScaleLocation( markers );
            mapviewer.goToPosition( auto_scale );
        }


        MMAttachEvent( window, 'load', onLoad );
