// Add site spesific javascript here


//Class to make google map v3
OddFellowGoogleMapV3 = Class.create({

    //divelement
    mapElement : "",

    //map obj
    gmapObj: "",

    //map info
    centerLatitude: "",
    centerLongitude: "",
    zoom: "",
    markerTitle: "",

    //json array with addresses and infoWindowHtml, given in corepublish-articles
    markersArray: [],

    markerObj: "",

    //icon
    iconObj: "",

    initialize : function(centerLatitude, centerLongitude, zoom, markersArray){
        this.centerLatitude = centerLatitude;
        this.centerLongitude = centerLongitude;
        this.zoom = zoom;
        this.markersArray = markersArray;
        this.markerTitle = "Ordenshus,loger og leire." ;

        this.mapElement = $('mymap');

        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(centerLatitude, centerLongitude);
        var myOptions = {
          zoom: zoom,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("mymap"), myOptions);


        //add point-markers to the map
        this.loopMarkers();

    },
    //create marker icons, with our own icon images (not in use)
    createIcon : function() {
        this.iconObj = new GIcon();

        this.iconObj.image = "/images/googlemap/OddFellow_ico.png";
        this.iconObj.shadow = "/images/googlemap/OddFellow_shadow.png";

        this.iconObj.iconSize = new GSize(18, 27);
        this.iconObj.shadowSize = new GSize(1, 20);

        this.iconObj.iconAnchor = new GPoint(8, 27);
        this.iconObj.infoWindowAnchor = new GPoint(5, 1);

    },
    //get one single marker to place on a given map-point.
    //The marker uses our own icon, and adds given html into a info window shown on marker click
    getMarker : function(point, markerTitle, infoWindowHtml) {
        var marker = new GMarker(point,{
            title:markerTitle,
            icon:this.iconObj
        });

        GEvent.addListener(marker, "click", function()  {
            marker.openInfoWindowHtml(infoWindowHtml);
        });

        return marker;

    },

    // Geocode address and place marker on map
    codeAddress : function (address, html) {
        geocoder.geocode( {
            'address': address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });

                google.maps.event.addListener(marker, 'mouseover', function() {
                    infowindow.setContent(html);
                    infowindow.open(map,marker);
                });

            } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                alert("Adresse kunne ikke stedfestes:" + address);
            } else {
                alert("Det oppstod en feil da Google Maps forsøkte å finne adressen. Feil: " + status);
            }
        });
    },

    //take all marker-points given to the class and place them on the google map
    loopMarkers : function(){
        //this.createIcon();

        //array with all markers, contains point and html for info-window
        var markers = [];

        //loop through markersArray and get array of map-markers
        for (var i = 0; i < this.markersArray.length; i++) {
             //this.codeAddress(this.markersArray[i]['address'],this.markersArray[i]['articleHtml']);
             var address = this.markersArray[i]['address'];
             var popupHtml = this.markersArray[i]['articleHtml'];
             var delayedCommand = "OddFellowGoogleMapV3.codeAddress('" + address +"', '" + popupHtml + "')";
             // Google enforces rate limiting for geocodeing queries
             var millisecDelay = i*1100;
             setTimeout(delayedCommand, millisecDelay);
        }


    }

});

var infowindow = new google.maps.InfoWindow(
{
    size: new google.maps.Size(150,50)
});





