var bigGMap;
var setPoints = [];
var houseRefs = [];
var currentIconObject;
var zoomLevel = 8;
var geocoder = new google.maps.Geocoder();
var infoWindows = [];
var openInfoWindowsList = [];
var maptype = google.maps.MapTypeId.ROADMAP;
var batch = [];
var houseIconImage = new google.maps.MarkerImage("/Templates/Styles/Images/googlemap_icon_precise_arrow.png",
                          new google.maps.Size(46, 60),
// The origin for this image is 0,0.
                          new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at 0,32.
                          new google.maps.Point(24, 46));

var lightIconImage = new google.maps.MarkerImage("/Templates/Styles/Images/googlemap_icon_markerlight.gif",
                          new google.maps.Size(46, 60),
// The origin for this image is 0,0.
                          new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at 0,32.
                          new google.maps.Point(24, 46));


var houseIconShadow = new google.maps.MarkerImage("/Templates/Styles/Images/googlemap_icon_precise_shadow.png",
// The shadow image is larger in the horizontal dimension
// while the position and offset are the same as for the main image.
                          new google.maps.Size(46, 60),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(24, 46));


var approxIconImage = new google.maps.MarkerImage("/Templates/Styles/Images/googlemap_icon_unprecise_arrow.png",
                          new google.maps.Size(45, 60),
// The origin for this image is 0,0.
                          new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at 0,32.
                          new google.maps.Point(24, 46));

var approxIconShadow = new google.maps.MarkerImage("/Templates/Styles/Images/googlemap_icon_unprecise_shadow.png",
// The shadow image is larger in the horizontal dimension
// while the position and offset are the same as for the main image.
                          new google.maps.Size(46, 60),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(24, 46));
var markerCluster;              
function IsValidGpsCoor(gpsCoorString) {
    return (gpsCoorString != null && gpsCoorString != 0 && gpsCoorString < 90);
}

var count1 = 0;
var count2 = 0;
var count3 = 0;

/* this is a v2 version which is used only on the redesigned website */
function bigGMap_drawGoogleMap_v3(divId) {
    setPoints = [];
    houseRefs = [];


    var mapDiv = document.getElementById(divId);
    if (mapDiv == null) {
        return;
    }


    var latlng = new google.maps.LatLng(48.8069, 18.5449);
    var myOptions = {
        zoom: 3,
        center: latlng,
        mapTypeId: maptype
    };

    bigGMap = new google.maps.Map(mapDiv, myOptions);

    if (markers.length == 0) {
        var point = new google.maps.LatLng(52.23315, 14.0625);
        bigGMap.setCenter(point);
        bigGMap.setZoom(zoomLevel);

        for (var counter = 0; counter < whereToSearchMarkers.length; counter++) {
            var whereToMarker = whereToSearchMarkers[counter];
            var address =
                    {
                        address: whereToMarker[0]
                    };

            geocoder.geocode(address, bigGMap_addCountryToMap_v3);
        }
    }
    else {
        for (var counter = 0; counter < markers.length; counter++) {
            var marker = markers[counter];
            if (counter == 0)
                bigGMap_CenterMap(marker);

            bigGMap_AddMarker_v3(marker);
        }
        
    }
    markerCluster = new MarkerClusterer(bigGMap, batch);
    markerCluster.refresh();
}

function bigGMap_CenterMap(markerObject) {
    var markerName = markerObject[0];
    var latitude = markerObject[1];
    var longitude = markerObject[2];
    var fullAddress = markerObject[3];

    if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude)) {
        bigGMap.setCenter(new google.maps.LatLng(latitude, longitude));
        bigGMap.setZoom(zoomLevel);
    }
    else {
        geocoder.geocode({ 'address': fullAddress }, bigGMap_CenterMapCallBack);
    }
}

function bigGMap_CenterMapCallBack(response, status) {
    if (status == google.maps.GeocoderStatus.OK) {

        var point = response[0].geometry.location;
        bigGMap.setCenter(point);
        bigGMap.setZoom(zoomLevel);
    }
    else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
        place = response[0].formatted_address;

        var countryName = place.substring(place.indexOf(',') + 2, place.length);

        geocoder.geocode({ 'address': countryName }, bigGMap_CenterMapCountryCallBack);
    }
}

function bigGMap_CenterMapCountryCallBack(response, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var point = response[0].geometry.location;
        bigGMap.setCenter(point);
        bigGMap.setZoom(6);
    }
}



function bigGMap_addCountryToMap_v3(response, status) {
    if (status == google.maps.GeocoderStatus.OK) {

        var point = response[0].geometry.location;
        
        var image = new google.maps.MarkerImage(countryHasPropertiesImage + '?countryName=' + response[0].formatted_address,
                          new google.maps.Size(40, 35),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(0, 0));

        var marker = new google.maps.Marker({
            position: point,
            map: bigGMap,
            icon: image,
            title: response[0].formatted_address
        });
    }
}


/* v3 version for use in redesigned pages */
function bigGMap_AddMarker_v3(markerObject) {
    var markerName = markerObject[0];
    var latitude = markerObject[1];
    var longitude = markerObject[2];
    var fullAddress = markerObject[3];
    var houseRefNumber = markerObject[4];

    if (IsValidGpsCoor(longitude) && IsValidGpsCoor(latitude)) {
        var latitudeF = parseFloat(latitude);
        var longitudeF = parseFloat(longitude);

        var point = new google.maps.LatLng(latitudeF, longitudeF);

        markerName = markerName;

        bigGMap_createMarker(point, houseIconImage, houseIconShadow, markerName);
        
    }
    else {
        
        var aMarkerName = markerName;
        geocoder.geocode({ 'address': fullAddress }, function (response, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                var point = response[0].geometry.location;

                var houseRefOnPoint = PointHasMark(point.lat(), point.lng());
                if (houseRefOnPoint != "") {
                    var movedLatitude = RandomMove(point.lat());
                    var movedLongitude = RandomMove(point.lng());


                    var movedPoint = new google.maps.LatLng(movedLatitude, movedLongitude);
                    bigGMap_createMarker(movedPoint, approxIconImage, approxIconShadow, aMarkerName, true);    /// currentIconObject -> approxIcon

                    setPoints.push(point);
                    houseRefs.push(houseRefNumber)
                }
                else {

                    bigGMap_createMarker(point, approxIconImage, approxIconShadow, aMarkerName, true);   /// currentIconObject -> approxIcon
                    
                    if (bigGMap.getCenter() == null) {
                        bigGMap.setCenter(point);
                        bigGMap.setZoom(zoomLevel);
                    }

                    setPoints.push(point);
                    houseRefs.push(houseRefNumber)
                }

            }
        });
    }
}
function RandomMove(coorValue) {
    var randNum = Math.random();
    if (randNum > 0.5) {
        return coorValue - 0.003;
    }
    else {
        return coorValue + 0.003;
    }
}

function PointHasMark(aLat, aLon) {
    if (setPoints.length == 0) return false;
    for (var counter = 0; counter < setPoints.length; counter++) {
        var aPoint = setPoints[counter];
        if (aPoint.y == aLat && aPoint.x == aLon) {
            var matchingHouseRef = houseRefs[counter];
            return matchingHouseRef;
        }
    }
    return "";
}
    

// the icon information is passed to this function
function bigGMap_createMarker(point, icon, shadow, html, approx) {
    var bigmarker = new google.maps.Marker({
        position: point,
        map: bigGMap,
        icon: icon,
        shadow: shadow,
        title: ''
    });
    if (approx) {
        markerCluster.addMarker(bigmarker);
    }
    batch.push(bigmarker);
    var infowindow = new google.maps.InfoWindow(
    { content: html
    });
    infoWindows.push(infowindow);
    var index = infoWindows.length - 1;

    google.maps.event.addListener(bigmarker, "click", function () {
        _gaq.push(['_trackEvent', 'Mapclicks', 'mapmarkerclick']);
        for (var c = 0; c < openInfoWindowsList.length; c++) {
            infoWindows[openInfoWindowsList[c]].close();
        }
        openInfoWindowsList.push(index);
        infoWindows[index].open(bigGMap, bigmarker);
    });
}   
