﻿
    var
        //isIE6 = /MSIE 6/i.test(navigator.userAgent),
        isIE6 = (document.all && !window.opera && !window.XMLHttpRequest ? true : false),
        googleMapVersion =
        {
            Version2 : 0,
            Version3 : 1
        },
        googleMarkerSize =
        {
            Tiny : 0,
            Small : 1,
            Medium : 2
        },
        googleMarkerColor =
        {
            Black : 0,
            Brown : 1,
            Green : 2,
            Purple : 3,
            Yellow : 4,
            Blue : 5,
            Gray : 6,
            Orange : 7,
            Red : 8,
            White : 9
        },
        googleMapFormat =
        {
            GIF : 0,
            JPEG : 1,
            PNG : 2
        },
        googleMapType =
        {
            Roadmap : 0,
            Satellite : 1,
            Terrain : 2,
            Hybrid : 3
        },
        googleClientType =
        {
            Desktop : 0,
            Mobile : 1
        },
        infowindow = null
        currentUniqueID = 0;

    var googleHelper =
    {
        getMarkerSizeString: function(size)
        {
            switch (size)
            {
                case googleMarkerSize.Tiny:
                    return "tiny";
                case googleMarkerSize.Small:
                    return "small";
                default:
                    return "mid";
            }
        },
        
        getMarkerColorString: function(color)
        {
            switch (color)
            {
                case googleMarkerColor.Black:
                    return "black";
                case googleMarkerColor.Blue:
                    return "blue";
                case googleMarkerColor.Brown:
                    return "brown";
                case googleMarkerColor.Gray:
                    return "gray";
                case googleMarkerColor.Green:
                    return "green";
                case googleMarkerColor.Orange:
                    return "orange";
                case googleMarkerColor.Purple:
                    return "purple";
                case googleMarkerColor.White:
                    return "white";
                case googleMarkerColor.Yellow:
                    return "yellow";
                default:
                    return "red";

            }
        },
        
        getFormatString: function(googleMap)
        {
            switch (googleMap.format)
            {
                case googleMapFormat.GIF:
                    return "gif";
                case googleMapFormat.JPEG:
                    return "jpg";
                default:
                    return "png";
            }
        },
        
        getTypeString: function(googleMap)
        {
            switch (googleMap.type)
            {
                case googleMapType.Hybrid:
                    return "hybrid";
                case googleMapType.Satellite:
                    return "satellite";
                case googleMapType.Terrain:
                    return "terrain";
                default:
                    return "roadmap";
            }
        },
            
        getClientTypeString: function(googleMap)
        {
            switch (googleMap.clientType)
            {
                case googleClientType.Mobile:
                    return "true";
                default:
                    return "false";
            }
        }
    };

    function googleLatLng(latitude, longitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    function googleMarker(size, color, label, location, infoWindowHTML, infoHTML, infoId, image, shadow, groupName, groupLabel, tag, display)
    {
        this.size = size;
        this.color = color;
        this.label = label;
        this.location = location;
        this.infowindow = null;
        this.infoWindowHTML = infoWindowHTML;
        this.infoHTML = infoHTML;
        this.InfoId = infoId;
        this.googleMap = null;
        this.marker = null;
        this.image = image;
        this.shadow = shadow;
        this.groupName = groupName;
        this.groupLabel = groupLabel;
        this.tag = tag;
        this.isGroup = false;
        this.uniqueID = 0;
        this.visible = false;
        this.display = display;
    }

    googleMarker.prototype =
    {
        changeIcon: function(image)
        {
            var googleMarker = this;
            try
            {
                if (this.image != image)
                {
                    this.image = isIE6 ? image.replace(/\.png/gi, ".gif") : image;
                    if (googleMarker.marker)
                        googleMarker.marker.setImage(this.image);
                }
            }
            catch (e) { }
        },

        openInfoWindow: function(marker)
        {
            var googleMarker = this;

            if (this.googleMap.version == googleMapVersion.Version2)
                this.openInfoWindowV2(marker);
            else if (this.googleMap.version == googleMapVersion.Version3)
                this.openInfoWindowV3(marker);

            setTimeout(function() { googleMarker.changeCloseButton(); }, 100);
        },

        changeCloseButton: function()
        {
            var imgs = document.body.getElementsByTagName("IMG");
            for (var i = 0; i < imgs.length; i++)
            {
                if (imgs[i].getAttribute("src") != null && imgs[i].getAttribute("src").indexOf("iw_close.gif", 0) > -1)
                {
                    imgs[i].setAttribute("src", TEMPLATE_BASE_URL + "images/inyourarea/map/iw_close.gif");
                    imgs[i].style.left = "";
                    imgs[i].style.right = "0px";
                    imgs[i].style.marginTop = "10px";
                    imgs[i].style.marginRight = "20px";
                }
            }
        },

        openInfoWindowV2: function(marker)
        {
            this.googleMap.map.openInfoWindowHtml(marker, this.infoWindowHTML);
        },

        openInfoWindowV3: function(marker)
        {

        },

        openInfoWindowInternal: function()
        {
            var googleMarker = this;

            if (this.googleMap.version == googleMapVersion.Version2)
                this.openInfoWindowInternalV2(googleMarker);
            else if (this.googleMap.version == googleMapVersion.Version3)
                this.openInfoWindowInternalV3(googleMarker);

            setTimeout(function() { googleMarker.changeCloseButton(); }, 100);
        },

        openInfoWindowInternalV2: function(googleMarker)
        {
            googleMarker.marker.openInfoWindowHtml(this.infoWindowHTML);
        },

        openInfoWindowInternalV3: function(googleMarker)
        {
        },

        click: function(marker)
        {
            var 
                googleMarker = this,
                googleMap = googleMarker.googleMap,
                minLatitude = 10000,
                maxLatitude = -10000,
                minLongitude = 10000,
                maxLongitude = -10000,
                sw = null,
                ne = null,
                bounds = null;

            if (googleMarker.isGroup)
            {
                for (var i = 0; i < googleMap.markers.count; i++)
                {
                    if (googleMap.markers.items[i].groupName == googleMarker.groupName)
                    {
                        if (googleMap.markers.items[i].location.latitude < minLatitude)
                            minLatitude = googleMap.markers.items[i].location.latitude;

                        if (googleMap.markers.items[i].location.latitude > maxLatitude)
                            maxLatitude = googleMap.markers.items[i].location.latitude;

                        if (googleMap.markers.items[i].location.longitude < minLongitude)
                            minLongitude = googleMap.markers.items[i].location.longitude;

                        if (googleMap.markers.items[i].location.longitude > maxLongitude)
                            maxLongitude = googleMap.markers.items[i].location.longitude;
                    }
                }

                minLatitude = minLatitude - ((maxLatitude - minLatitude) * 0.25);
                maxLatitude = maxLatitude + ((maxLatitude - minLatitude) * 0.25);
                minLongitude = minLongitude - ((maxLongitude - minLongitude) * 0.25);
                maxLongitude = maxLongitude + ((maxLongitude - minLongitude) * 0.25);

                sw = new GLatLng(minLatitude, minLongitude);
                ne = new GLatLng(maxLatitude, maxLongitude);
                bounds = new GLatLngBounds(sw, ne);
                googleMap.map.setCenter(new GLatLng(googleMarker.location.latitude, googleMarker.location.longitude), googleMap.map.getBoundsZoomLevel(bounds));
            }
            else
            {
                googleMap.map.openInfoWindowHtml(new GLatLng(googleMarker.location.latitude, googleMarker.location.longitude), googleMarker.infoWindowHTML);
                googleMap.activeMarker = googleMarker;
            }

            if (googleMap.markerClick)
                googleMap.markerClick(googleMarker);

            googleMarker = null;
            minLatitude = null;
            maxLatitude = null;
            minLongitude = null;
            maxLongitude = null;
            sw = null;
            ne = null;
            bounds = null;
        }
    };

    function googleMarkerCollection(googleMap)
    {
        this.googleMap = googleMap;
        this.items = new Object();
        this.count = 0;
    }

    googleMarkerCollection.prototype =
    {

        add: function(googleMarker)
        {
            googleMarker.googleMap = this.googleMap;
            this.items[this.count] = googleMarker;
            this.count++;
            googleMarker.uniqueID = ++currentUniqueID;
            return googleMarker;
        },

        create: function(size, color, label, location, infoWindowHTML, infoHTML, infoId, image, shadow, groupName, groupLabel, tag, display)
        {
            var gma = new googleMarker(size, color, label, location, infoWindowHTML, infoHTML, infoId, image, shadow, groupName, groupLabel, tag, display);
            gma.googleMap = this.googleMap;
            this.items[this.count] = gma;
            this.count++;
            gma.uniqueID = ++currentUniqueID;
            return gma;
        },

        removeAt: function(index)
        {
            if (index >= 0 && index < this.count)
            {
                for (var i = index; i < this.count - 1; i++)
                {
                    this.items[i] = this.items[i + 1];
                }
                this.count--;
                this.items[this.count] = null;
            }
        },

        remove: function(googleMarker)
        {
            for (var i = 0; i < this.count; i++)
            {
                if (this.items[i] == googleMarker)
                {
                    this.removeAt(i);
                    break;
                }
            }
        },

        clear: function()
        {
            for (var i = 0; i < this.count; i++)
                this.items[i] = null;

            this.count = 0;
        }

    };

    function googleMarkerGroupCollection(googleMap)
    {
        this.googleMap = googleMap;
        this.items = new Object();
        this.count = 0;        
    }

    googleMarkerGroupCollection.prototype =
    {

        add: function(googleMarker)
        {
            googleMarker.googleMap = this.googleMap;
            this.items[this.count] = googleMarker;
            this.count++;
            googleMarker.uniqueID = ++currentUniqueID;
            googleMarker.isGroup = true;
            return googleMarker;
        },

        create: function(size, color, label, location, infoWindowHTML, infoHTML, infoId, image, shadow, groupName, groupLabel, tag, visible)
        {
            var gma = new googleMarker(size, color, label, location, infoWindowHTML, infoHTML, infoId, image, shadow, groupName, groupLabel, tag, visible);
            gma.googleMap = this.googleMap;
            this.items[this.count] = gma;
            this.count++;
            gma.uniqueID = ++currentUniqueID;
            gma.isGroup = true;
            return gma;
        },

        removeAt: function(index)
        {
            if (index >= 0 && index < this.count)
            {
                for (var i = index; i < this.count - 1; i++)
                {
                    this.items[i] = this.items[i + 1];
                }
                this.count--;
                this.items[this.count] = null;
            }
        },

        remove: function(googleMarker)
        {
            for (var i = 0; i < this.count; i++)
            {
                if (this.items[i] == googleMarker)
                {
                    this.removeAt(i);
                    break;
                }
            }
        },

        clear: function()
        {
            for (var i = 0; i < this.count; i++)
                this.items[i] = null;

            this.count = 0;
        },

        getByGroupName: function(groupName)
        {
            for (var i = 0; i < this.count; i++)
            {
                if (this.items[i].groupName == groupName)
                    return this.items[i];
            }
        }

    };

    function googleMap(name, version, divID, groupZoomLevel, webServiceURL, webServiceMethod)
    {
        this.name = name;
        this.version = version;
        this.div = document.getElementById(divID);
        this.loader = null;
        this.map = null;
        this.center = new googleLatLng(53.963761, -1.075888);
        this.markers = new googleMarkerCollection(this);
        this.markerGroups = new googleMarkerGroupCollection(this);
        this.visibleMarkers = new googleMarkerCollection(this);
        this.zoom = 5;
        this.minZoom = null;
        this.maxZoom = null;
        this.groupZoomLevel = groupZoomLevel;
        this.grouped = this.zoom < this.groupZoomLevel;
        this.activeMarker = null;
        this.webServiceURL = webServiceURL;
        this.webServiceMethod = webServiceMethod;
        this.initialising = false;
        this.initialised = false;
        this.renderUI = true;

        // Events
        this.visibleMarkersChanged = null;
        this.zoomEnd = null;
        this.markerClick = null;
        this.infoWindowClosed = null;
    }

    googleMap.prototype =
    {
        loadMarkersFromWebService: function ()
        {
            var 
                googleMap = this,
                ws = new indigo.communication.webService(this.webServiceURL);

            function success(dataSet)
            {
                var row = null;
                if (dataSet.tables.length > 0)
                {
                    for (var i = 0; i < dataSet.tables[0].rows.length; i++)
                    {
                        row = dataSet.tables[0].rows[i];

                        // Add Group
                        if (!googleMap.markerGroups.getByGroupName(row.getValue("GroupName")))
                            googleMap.markerGroups.create(row.getValue("Size"), row.getValue("Color"), row.getValue("GroupLabel"), new googleLatLng(parseFloat(row.getValue("Latitude")), parseFloat(row.getValue("Longitude"))), "group", row.getValue("InfoHTML"), row.getValue("InfoID"), row.getValue("MarkerGroupImage"), row.getValue("ShadowImage"), row.getValue("GroupName"), row.getValue("GroupLabel"), row.getValue("GroupTag"), (row.getValue("Display") == "true" ? true : false));

                        googleMap.markers.create(row.getValue("Size"), row.getValue("Color"), row.getValue("Label"), new googleLatLng(parseFloat(row.getValue("Latitude")), parseFloat(row.getValue("Longitude"))), row.getValue("InfoWindowHTML"), row.getValue("InfoHTML"), row.getValue("InfoID"), row.getValue("MarkerImage"), row.getValue("ShadowImage"), row.getValue("GroupName"), row.getValue("GroupLabel"), row.getValue("Tag"), (row.getValue("Display") == "true" ? true : false));
                    }
                }

                for (var i = 0; i < googleMap.markers.count; i++)
                {
                    googleMap.markers.items[i].visible = false;
                    googleMap.markers.items[i].marker = null;
                }

                for (var i = 0; i < googleMap.markerGroups.count; i++)
                {
                    googleMap.markerGroups.items[i].visible = false;
                    googleMap.markerGroups.items[i].marker = null;
                }

                if (googleMap.version == googleMapVersion.Version2)
                    googleMap.initialiseV2();
                else if (googleMap.version == googleMapVersion.Version3)
                    googleMap.initialiseV3();

                row = null;

                googleMap.initialising = false;
            }

            function failure(errorMessage)
            {
                alert(errorMessage);
            }

            ws.getDataset(googleMap.webServiceMethod, "Latitude=" + googleMap.center.latitude + "&Longitude=" + googleMap.center.longitude, true, success, failure);
        },

        initialise: function ()
        {

            var 
                googleMap = this;

            if (!googleMap.initialised)
            {
                googleMap.initialising = true;
                if (typeof googleMap.webServiceURL != "undefined" && googleMap.webServiceURL != "" && typeof googleMap.webServiceMethod != "undefined" && googleMap.webServiceMethod != "" && googleMap.markers.count == 0 && googleMap.initialised == false)
                {
                    googleMap.initialised = true;
                    googleMap.onLoad();
                    googleMap.loadMarkersFromWebService();
                }
                else
                {
                    googleMap.initialised = true;

                    if (googleMap.version == googleMapVersion.Version2)
                        googleMap.initialiseV2();
                    else if (googleMap.version == googleMapVersion.Version3)
                        googleMap.initialiseV3();

                    googleMap.initialising = false;
                }
            }
        },

        initialiseV2: function ()
        {
            var googleMap = this;

            if (GBrowserIsCompatible())
            {

                while (googleMap.div.childNodes.length > 0)
                    googleMap.div.removeChild(googleMap.div.childNodes[0]);

                var mapTypes = G_DEFAULT_MAP_TYPES;
                for (var i = 0; i < mapTypes.length; i++)
                {
                    mapTypes[i].getMaximumResolution = function (latlng) { return googleMap.minZoom; };
                    mapTypes[i].getMinimumResolution = function (latlng) { return googleMap.maxZoom; };
                }

                googleMap.map = new GMap2(googleMap.div, { mapTypes: mapTypes });
                googleMap.map.addControl(new GLargeMapControl());
                googleMap.map.setCenter(new GLatLng(googleMap.center.latitude, googleMap.center.longitude));
                googleMap.map.setCenter(new GLatLng(googleMap.center.latitude, googleMap.center.longitude), googleMap.zoom);

                if (!googleMap.renderUI)
                {
                    map.removeMapType(G_HYBRID_MAP);
                    map.removeMapType(G_SATELLITE_MAP);
                    map.removeMapType(G_NORMAL_MAP);
                }

                GEvent.addListener(googleMap.map, "tilesloaded", function ()
                {
                    googleMap.onLoaded();
                });

                GEvent.addListener(googleMap.map, "moveend", function ()
                {
                    var center = googleMap.map.getCenter();
                    googleMap.center = new googleLatLng(center.y, center.x);
                    googleMap.updateMarkers();
                });

                GEvent.addListener(googleMap.map, "zoomend", function ()
                {
                    googleMap.zoom = googleMap.map.getZoom();
                    googleMap.updateMarkers();
                    if (googleMap.zoomEnd)
                        googleMap.zoomEnd(googleMap);
                });

                GEvent.addListener(googleMap.map, 'infowindowclose', function ()
                {
                    if (googleMap.infoWindowClosed)
                        googleMap.infoWindowClosed(googleMap.activeMarker);
                    googleMap.activeMarker = null;
                });

                googleMap.updateMarkers();
            }
        },

        initialiseV3: function ()
        {
            try
            {
                var googleMap = this;
                var myOptions = {
                    zoom: googleMap.zoom,
                    center: new google.maps.LatLng(googleMap.center.latitude, googleMap.center.longitude),
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    disableDefaultUI: !googleMap.renderUI
                };
                googleMap.map = new google.maps.Map(googleMap.div, myOptions);

                //google.maps.event.addListener(googleMap.map, 'zoom_changed', function () { googleMap.updateMarkers(); googleMap.constrainZoom(googleMap.minZoom, googleMap.maxZoom); SetSelectedMarkers(selectedTag); });
                googleMap.updateMarkers();
            }
            catch (e) { }

            this.onLoaded();
        },

        onLoad: function ()
        {
            var googleMap = this;
            googleMap.loader = document.createElement('div');
            googleMap.loader.className = "Loading";

            var message = document.createElement('img');
            message.setAttribute("src", TEMPLATE_BASE_URL + "images/inyourarea/map/loading_data.jpg");
            message.setAttribute("alt", "One moment please, loading data...");
            message.className = "Message";
            googleMap.loader.appendChild(message);

            var loader = document.createElement('img');
            loader.setAttribute("src", TEMPLATE_BASE_URL + "images/inyourarea/map/loader.gif");
            loader.setAttribute("alt", "");
            loader.className = "Loader";
            googleMap.loader.appendChild(loader);

            googleMap.loader.appendChild(message);
            googleMap.loader.appendChild(loader);
            googleMap.div.parentNode.appendChild(googleMap.loader);
        },

        onLoaded: function ()
        {
            var googleMap = this;
            if (googleMap.loader)
            {
                try
                {
                    googleMap.loader.style.display = "none";
                    googleMap.loader = null;
                }
                catch (e) { }
            }
            googleMap.initialised = true;
        },

        updateMarkers: function ()
        {
            var googleMap = this;
            if (this.version == googleMapVersion.Version2)
                this.updateMarkersV2();
            else if (this.version == googleMapVersion.Version3)
                this.updateMarkersV3();

            if (this.visibleMarkersChanged)
                this.visibleMarkersChanged(googleMap);
        },

        updateMarkersV2: function ()
        {
            var bounds = this.map.getBounds(), southWest = bounds.getSouthWest(), northEast = bounds.getNorthEast();
            var googleMarker = null, lat = null, lng = null, inViewport = false, tempMarkerGroup = null, visible = false, display = true;

            this.visibleMarkers.clear();

            if (this.map.getZoom() < this.groupZoomLevel)
            {
                this.grouped = true;

                // Remove individual markers
                for (var i = 0; i < this.markers.count; i++)
                {
                    googleMarker = this.markers.items[i]
                    if (googleMarker.marker)
                    {
                        this.map.removeOverlay(googleMarker.marker);
                        googleMarker.marker = null;
                    }
                }

                for (var i = 0; i < this.markerGroups.count; i++)
                {
                    googleMarker = this.markerGroups.items[i]
                    lat = googleMarker.location.latitude;
                    lng = googleMarker.location.longitude;
                    visible = googleMarker.visible;
                    display = false;
                    inViewport = lat >= southWest.lat() - 0.01 && lat <= northEast.lat() && lng >= southWest.lng() && lng <= northEast.lng();

                    tempMarkerGroup = new googleMarkerCollection(this);
                    for (var j = 0; j < this.markers.count; j++)
                    {
                        if (this.markers.items[j].groupName == googleMarker.groupName)
                        {
                            tempMarkerGroup.add(this.markers.items[j]);
                            if (this.markers.items[j].display)
                                display = true;
                        }
                    }

                    if (tempMarkerGroup.count > 0)
                    {
                        inViewport = lat >= southWest.lat() - 0.01 && lat <= northEast.lat() && lng >= southWest.lng() && lng <= northEast.lng();
                        if (tempMarkerGroup.count == 1)
                        {
                            if (display)
                            {
                                if (inViewport)
                                {
                                    if (!tempMarkerGroup.items[0].marker)
                                        this.addMarker(tempMarkerGroup.items[0]);
                                }
                                else
                                {
                                    if (tempMarkerGroup.items[0].marker)
                                    {
                                        this.map.removeOverlay(tempMarkerGroup.items[0].marker);
                                        tempMarkerGroup.items[0].marker = null;
                                    }
                                }
                                this.visibleMarkers.add(tempMarkerGroup.items[0]);
                            }
                            else
                            {
                                if (tempMarkerGroup.items[0].marker)
                                {
                                    this.map.removeOverlay(tempMarkerGroup.items[0].marker);
                                    tempMarkerGroup.items[0].marker = null;
                                }
                            }
                        }
                        else
                        {
                            if (display)
                            {
                                if (inViewport)
                                {
                                    if (!googleMarker.marker)
                                        this.addMarker(googleMarker);
                                }
                                else
                                {
                                    if (googleMarker.marker)
                                    {
                                        this.map.removeOverlay(googleMarker.marker);
                                        googleMarker.marker = null;
                                    }
                                }
                                this.visibleMarkers.add(googleMarker);
                            }
                            else
                            {
                                if (googleMarker.marker)
                                {
                                    this.map.removeOverlay(googleMarker.marker);
                                    googleMarker.marker = null;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                this.grouped = false;

                // Remove group markers
                for (var i = 0; i < this.markerGroups.count; i++)
                {
                    googleMarker = this.markerGroups.items[i]
                    if (googleMarker.marker)
                    {
                        this.map.removeOverlay(googleMarker.marker);
                        googleMarker.marker = null;
                    }

                    tempMarkerGroup = new googleMarkerCollection(this);
                    for (var j = 0; j < this.markers.count; j++)
                    {
                        if (this.markers.items[j].groupName == googleMarker.groupName)
                            tempMarkerGroup.add(this.markers.items[j]);
                    }

                    if (tempMarkerGroup.count == 1)
                    {
                        if (tempMarkerGroup.items[0].marker)
                        {
                            this.map.removeOverlay(tempMarkerGroup.items[0].marker);
                            tempMarkerGroup.items[0].marker = null;
                        }
                    }
                }

                for (var i = 0; i < this.markers.count; i++)
                {
                    googleMarker = this.markers.items[i];
                    lat = googleMarker.location.latitude;
                    lng = googleMarker.location.longitude;

                    if (googleMarker.display)
                    {
                        inViewport = lat >= southWest.lat() - 0.01 && lat <= northEast.lat() && lng >= southWest.lng() && lng <= northEast.lng();
                        if (inViewport)
                        {
                            if (!googleMarker.marker)
                                this.addMarker(googleMarker);
                        }
                        else
                        {
                            if (googleMarker.marker)
                            {
                                this.map.removeOverlay(googleMarker.marker);
                                googleMarker.marker = null;
                            }
                        }
                        this.visibleMarkers.add(googleMarker);
                    }
                    else
                    {
                        if (googleMarker.marker)
                        {
                            this.map.removeOverlay(googleMarker.marker);
                            googleMarker.marker = null;
                        }
                    }
                }
            }
            bounds = null;
            southWest = null;
            northEast = null;
            lat = null;
            lng = null;
            tempMarkerGroup = null;
        },

        updateMarkersV3: function ()
        {
            this.clearMarkers();
            this.clearMarkerGroups();
            if (this.map.getZoom() < this.groupZoomLevel)
            {
                for (var i = 0; i < this.markerGroups.count; i++)
                {
                    var tempMarkerGroups = new googleMarkerCollection(null);
                    for (var j = 0; j < this.markers.count; j++)
                    {
                        if (this.markers.items[j].groupName == this.markerGroups.items[i].groupName)
                            tempMarkerGroups.add(this.markers.items[j]);
                    }

                    if (tempMarkerGroups.count > 0)
                    {
                        if (tempMarkerGroups.count == 1)
                        {
                            this.addMarker(tempMarkerGroups.items[0]);
                        }
                        else
                        {
                            this.addMarker(this.markerGroups.items[i]);
                        }
                    }
                }
            }
            else
            {
                for (var i = 0; i < this.markers.count; i++)
                {
                    this.addMarker(this.markers.items[i]);
                }
            }
        },

        constrainZoom: function (minZoom, maxZoom)
        {
            if (this.map.getZoom() > minZoom)
            {
                this.map.setZoom(minZoom);
            }
            if (this.map.getZoom() < maxZoom)
            {
                this.map.setZoom(maxZoom);
            }
        },

        clearMarkers: function ()
        {
            for (var i = 0; i < this.markers.count; i++)
            {
                if (this.markers.items[i].marker != null)
                    this.markers.items[i].marker.setMap(null);
            }
        },

        clearMarkerGroups: function ()
        {
            for (var i = 0; i < this.markerGroups.count; i++)
            {
                if (this.markerGroups.items[i].marker != null)
                    this.markerGroups.items[i].marker.setMap(null);
            }
        },

        addMarker: function (gma)
        {
            if (this.version == googleMapVersion.Version2)
                this.addMarkerV2(gma);
            else if (this.version == googleMapVersion.Version3)
                this.addMarkerV3(gma);
        },

        addMarkerV2: function (gma)
        {
            var icon = new GIcon(G_DEFAULT_ICON);
            if (gma.image == "")
            {
                icon.image = "templates/images/inyourarea/map/" + googleHelper.getMarkerColorString(gma.color) + "-dot" + (isIE6 ? ".gif" : ".png");
                icon.mozPrintImage = icon.image;
                icon.printImage = icon.image;
                if (isIE6) icon.shadow = "";
                icon.printShadow = icon.shadow;
            }
            else
            {
                icon.image = (isIE6 ? gma.image.replace(/\.png/gi, ".gif") : gma.image);
                icon.mozPrintImage = icon.image;
                icon.printImage = icon.image;
                icon.shadow = (isIE6 ? "" : gma.shadow);
                icon.printShadow = icon.shadow;
                icon.imageMap = new Array(1, 1, 4, 0, 34, 0, 36, 2, 38, 4, 38, 32, 36, 34, 34, 35, 26, 35, 20, 65, 18, 65, 13, 35, 4, 35, 1, 33, 0, 31, 0, 4, 1, 1);

                var img = new Image();
                img.src = icon.image;
                icon.iconSize = new GSize(img.width, img.height);
                icon.iconAnchor = new GPoint(img.width / 2, img.height);
                icon.infoWindowAnchor = new GPoint(img.width / 2, img.height);
                if (icon.shadow != "")
                {
                    img.src = icon.shadow;
                    icon.shadowSize = new GSize(img.width, img.height);
                }
                img = null;
            }
            gma.marker = new GMarker(new GLatLng(gma.location.latitude, gma.location.longitude), icon, false);
            gma.marker.title = "Test";

            GEvent.addListener(gma.marker, "click", function (marker)
            {
                gma.click(marker);
            });

            this.map.addOverlay(gma.marker);
        },

        addMarkerV3: function (gma)
        {
            //            var icon = new GIcon(G_DEFAULT_ICON);
            var icon = null, shadow = null;
            if (gma.image == "")
            {
                //                icon.image = "templates/images/googlemap/" + googleHelper.getMarkerColorString(color) + "-dot" + (isIE6 ? ".gif" : ".png");
                //                icon.mozPrintImage = icon.image;
                //                icon.printImage = icon.image;
                //                if (isIE6) icon.shadow = "";
                //                icon.printShadow = icon.shadow;
            }
            else
            {
                if (isIE6) gma.image.replace("png", "gif");
                var img = new Image();
                img.src = gma.image;

                icon = new google.maps.MarkerImage(gma.image,
                                                   new google.maps.Size(img.width, img.height),
                                                   new google.maps.Point(0, 0),
                                                   new google.maps.Point(img.width / 2, img.height));
                img = null;

                if (!isIE6)
                {
                    var img = new Image();
                    img.src = gma.shadow;

                    shadow = new google.maps.MarkerImage(gma.shadow,
                                                         new google.maps.Size(img.width, img.height),
                                                         new google.maps.Point(0, 0),
                                                         new google.maps.Point(0, img.height));
                    img = null;
                }
            }

            gma.googleMap = this.map;
            gma.marker = new google.maps.Marker({
                position: new google.maps.LatLng(gma.location.latitude, gma.location.longitude),
                map: this.map,
                icon: icon,
                shadow: shadow
            });

            gma.infowindow = new google.maps.InfoWindow({
                content: "Information for this item failed to load."
            });

            google.maps.event.addListener(gma.marker, 'click', function ()
            {
                if (gma.infoWindowHTML == "group")
                {
                    gma.googleMap.panTo(new google.maps.LatLng(gma.location.latitude, gma.location.longitude));
                    gma.googleMap.setZoom(10);
                }
                else
                    gma.openInfoWindow();
            });
        }
    };

    function googleMapCollection()
    {
        this.items = new Object();
        this.count = 0;
    }

    googleMapCollection.prototype =
    {

        add: function(googleMap)
        {
            this.items[this.count] = googleMap;
            this.count++;
            return googleMarker;
        },

        create: function(name, version, divID, groupZoomLevel, webServiceURL, webServiceMethod)
        {
            this.items[this.count] = new googleMap(name, version, divID, groupZoomLevel, webServiceURL, webServiceMethod);
            this.count++;
            return this.items[this.count - 1];
        },

        removeAt: function(index)
        {
            if (index >= 0 && index < this.count)
            {
                for (var i = index; i < this.count - 1; i++)
                {
                    this.items[i] = this.items[i + 1];
                }
                this.count--;
                this.items[this.count] = null;
            }
        },

        remove: function(googleMap)
        {
            for (var i = 0; i < this.count; i++)
            {
                if (this.items[i] == googleMap)
                {
                    this.removeAt(i);
                    break;
                }
            }
        },

        getByName: function(name)
        {
            for (var i = 0; i < this.count; i++)
            {
                if (this.items[i].name == name)
                {
                    return this.items[i];
                }
            }
            return null;
        }

    };

    function googleMapManager()
    {
        this.maps = new googleMapCollection();
    }

    googleMapManager.prototype =
    {
        initialise: function(name)
        {
            var map = this.maps.getByName(name);
            if (map)
                map.initialise();
        }
    };

    var googlemapmanager = new googleMapManager();