//var validationGroup = "";
//var side_bar_html = "";
//var gmarkers = [];
//var htmls = [];
//var i = 0;
var smallMap_mapObject;
var currentIconObject;
var currentMarkerName;
var geocoder = null;
//var bounds = new GLatLngBounds();

var approxIcon;	/// var for holding approx marker icon object
    
    // the icon information is passed to this function
    function createMarker(point,icon,html,name)
    {
        var marker = new GMarker(point,icon);
        // save the info we need to use later for the side_bar
        //      gmarkers[i] = marker;
        //      htmls[i] = html;
        //      i++;
        return marker;
    }
    
    function drawGoogleMap(divId)
    {       
        if (GBrowserIsCompatible()) 
        {
            // This icon is a different shape, so we need our own settings       
            var houseIcon = new GIcon();
            houseIcon.image = houseIconUrl;      
            houseIcon.iconSize = new GSize(20, 34);
            houseIcon.shadowSize = new GSize(36, 34);
            houseIcon.iconAnchor = new GPoint(5, 34);
            houseIcon.infoWindowAnchor = new GPoint(5, 2);
            houseIcon.infoShadowAnchor = new GPoint(14, 25);

            var redHouseIcon = new GIcon();
            redHouseIcon.image = redHouseIconUrl;      
            redHouseIcon.iconSize = new GSize(20, 34);
            redHouseIcon.shadowSize = new GSize(36, 34);
            redHouseIcon.iconAnchor = new GPoint(5, 34);
            redHouseIcon.infoWindowAnchor = new GPoint(5, 2);
            redHouseIcon.infoShadowAnchor = new GPoint(14, 25);

			// global var Icon for approx markers (i.e. markers placed by way of geocoding region, country
			// TODO: these should be changed to new marker info
			/// (begin)
            approxIcon = new GIcon();
            approxIcon.image = redHouseIconUrl;      
            approxIcon.iconSize = new GSize(20, 34);
            approxIcon.shadowSize = new GSize(36, 34);
            approxIcon.iconAnchor = new GPoint(5, 34);
            approxIcon.infoWindowAnchor = new GPoint(5, 2);
            approxIcon.infoShadowAnchor = new GPoint(14, 25);
            /// (end)

            var mapDiv = document.getElementById(divId)
            if (mapDiv == null) return;
            smallMap_mapObject = new GMap2(mapDiv);
            smallMap_mapObject.checkResize();
            smallMap_mapObject.setCenter(new GLatLng(48.8069, 18.5449), 2);
            for (var counter=0; counter < markers.length; counter++)
            { 
                var marker = markers[counter];
                if (counter == 0)
                    smallMap_CenterMap(marker);
                smallMap_AddMarker(marker, houseIcon);
            }
        }
    }
    
    function smallMap_CenterMap(markerObject)
    {
        var markerName = markerObject[0];
        var latitude = markerObject[1];
        var longitude = markerObject[2];
        var fullAddress = markerObject[3];

        if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude))
        {
            smallMap_mapObject.setCenter(new GLatLng(latitude,longitude), zoomLevel);
        }
        else
        {
            geocoder = new GClientGeocoder();
            geocoder.getLocations(fullAddress, smallMap_CenterMapCallBack);
        }
    }

//    function smallMap_CenterMap2(latitude, longitude, fullAddress)
//    {
//        if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude))
//        {
//            smallMap_mapObject.setCenter(new GLatLng(latitude,longitude), zoomLevel);
//        }
//        else
//        {
//            geocoder = new GClientGeocoder();
//            geocoder.getLatLng(fullAddress, function(point)
//            {
//                if (point)
//                {
//                    smallMap_mapObject.setCenter(point, zoomLevel);
//                }
//            });
//        }
//    }

    function smallMap_AddMarker(markerObject, iconObject)
    {
        var markerName = markerObject[0];
        var latitude = markerObject[1];
        var longitude = markerObject[2];
        var fullAddress = markerObject[3];
        
        if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude))
        {
            var point = new GLatLng(latitude,longitude);
            var marker = createMarker(point,iconObject, markerName, markerName);
            smallMap_mapObject.addOverlay(marker);
        }
        else
        {
            currentIconObject = iconObject;
            currentMarkerName = markerName;
            geocoder = new GClientGeocoder();
            geocoder.getLocations(fullAddress, smallMap_AddApproxMarkerCallBack); /// smallMap_AddMarkerCallBack -> smallMap_AddApproxMarkerCallBack
        }
    }
    
    function IsValidGpsCoor(gpsCoorString)
    {
        return (gpsCoorString!=null && gpsCoorString!=0 && gpsCoorString<90);
    }
    
    function smallMap_CenterMapCallBack(response)
    {
        if (response.Status.code == '200')
        {
            place = response.Placemark[0];
            var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
            smallMap_mapObject.setCenter(point, zoomLevel);
        }
    }

	/* Callback function for adding a marker from geocoding
	 * only change to the original function is that a 'approx' icon is used instead of the normal one
	 */
	/// (begin)
    function smallMap_AddApproxMarkerCallBack(response)
    {
        if (response.Status.code == '200')
        {  
            place = response.Placemark[0];
            var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
            var marker = createMarker(point, approxIcon, currentMarkerName, currentMarkerName);
            if (smallMap_mapObject.getCenter()==null)
            {
                smallMap_mapObject.setCenter(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]), zoomLevel);
            }
            smallMap_mapObject.addOverlay(marker);
            //bounds.extend(point);
            //zoomfit();
        }
    }
    /// (end)

    function smallMap_AddMarkerCallBack(response)
    {
        if (response.Status.code == '200')
        {  
            place = response.Placemark[0];
            var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
            var marker = createMarker(point, currentIconObject, currentMarkerName, currentMarkerName);
            if (smallMap_mapObject.getCenter()==null)
            {
                smallMap_mapObject.setCenter(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]), zoomLevel);
            }
            smallMap_mapObject.addOverlay(marker);
            //bounds.extend(point);
            //zoomfit();
        }
    }

      // fits the markers to the page    
//    function zoomfit()
//    {
//        newzoom = smallMap_mapObject.getBoundsZoomLevel(bounds);
//        newcenter = bounds.getCenter();
//        smallMap_mapObject.setCenter (newcenter,newzoom);
//    }

    function drawGoogleMap_v2(divId)
    {       
        if (GBrowserIsCompatible()) 
        {
            // This icon is a different shape, so we need our own settings
            houseIcon_v2 = new GIcon();
            houseIcon_v2.image = houseIconUrl;   
			houseIcon_v2.image = "/Templates/Styles/Images/googlemap_icon_precise_arrow.png";
			houseIcon_v2.shadow = "/Templates/Styles/Images/googlemap_icon_precise_shadow.png";
			houseIcon_v2.iconSize = new GSize( 45, 60 );
			houseIcon_v2.shadowSize = new GSize( 45, 60);
			houseIcon_v2.iconAnchor = new GPoint( 23, 46 );
			houseIcon_v2.infoWindowAnchor = new GPoint( 23, 3 );
   
			// global var Icon for approx markers (i.e. markers placed by way of geocoding region, country
            approxIcon_v2 = new GIcon();
			approxIcon_v2.image = "/Templates/Styles/Images/googlemap_icon_unprecise_arrow.png";
			approxIcon_v2.shadow = "/Templates/Styles/Images/googlemap_icon_unprecise_shadow.png";
			approxIcon_v2.iconSize = new GSize( 45, 60 );
			approxIcon_v2.shadowSize = new GSize( 45, 60);
			approxIcon_v2.iconAnchor = new GPoint( 23, 46 );
			approxIcon_v2.infoWindowAnchor = new GPoint( 23, 3 );

            var mapDiv = document.getElementById(divId)
            if (mapDiv == null) return;
            smallMap_mapObject = new GMap2(mapDiv);
            smallMap_mapObject.checkResize();
            smallMap_mapObject.setCenter(new GLatLng(48.8069, 18.5449), 2);
            for (var counter=0; counter < markers.length; counter++)
            { 
                var marker = markers[counter];
                if (counter == 0)
                    smallMap_CenterMap(marker);
                smallMap_AddMarker_v2(marker, houseIcon_v2, approxIcon_v2 );
            }
        }
    }

    function smallMap_AddMarker_v2( markerObject, exactIconObject, approxIconObject )
    {
        var markerName = markerObject[0];
        var latitude = markerObject[1];
        var longitude = markerObject[2];
        var fullAddress = markerObject[3];
        
        if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude))
        {
            var point = new GLatLng(latitude,longitude);
            var marker = createMarker(point,exactIconObject, markerName, markerName);
            smallMap_mapObject.addOverlay(marker);
        }
        else
        {
            currentIconObject = approxIconObject;
            currentMarkerName = markerName;
            geocoder = new GClientGeocoder();
            geocoder.getLocations(fullAddress, smallMap_AddMarkerCallBack); /// smallMap_AddMarkerCallBack -> smallMap_AddApproxMarkerCallBack
        }
    }
