﻿var OCF = {};
OCF.ParishMap = function(clientID) {
    this.ClientID = clientID;

    this.Map = null;
    this.ItemsPerPage = 18;
    this.CurrentPage = 0;
    this.CreateMap = function() {
        if (GBrowserIsCompatible()) {
            var thisObject = this;
            this.Map = new GMap2(document.getElementById(this.ClientID));
            this.Map.setCenter(new GLatLng(45.0890355, -99.31640625), 3);
            this.Map.setMapType(G_NORMAL_MAP);
            this.Map.addControl(new GLargeMapControl());
        }
    }


    this.GetParishes = function(zipcode, radius, completed) {
        var map = this.Map;
        var thisobject = this;
        // map.setCenter(new GLatLng(47.614495, -122.341861), 13);
        map.clearOverlays();
        var parishcount

        GDownloadUrl("parishmapdata.axd?zipcode=" + zipcode + "&radius=" + radius + "&UnquieID=" + this.guid(), function(data) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("parish");
            var points = new Array();
            parishcount = markers.length;
            for (var i = 0; i < markers.length; i++) {

                // Get address parts
                var name = thisobject.GetParishData(markers[i], "strOrgName");
                var address1 = thisobject.GetParishData(markers[i], "strAddress1");
                var address2 = thisobject.GetParishData(markers[i], "strAddress2");
                var city = thisobject.GetParishData(markers[i], "strCity");
                var state = thisobject.GetParishData(markers[i], "strState");
                var zip = thisobject.GetParishData(markers[i], "strZip");
                var phone = thisobject.GetParishData(markers[i], "strPhone");
                var latlng = new GLatLng(parseFloat(thisobject.GetParishData(markers[i], "latitude")),
                                  parseFloat(thisobject.GetParishData(markers[i], "longitude")));
                points[i] = latlng;
                var contacts = new Array()
                var parishcontacts = markers[i].getElementsByTagName("clergy");
                for (var j = 0; j < parishcontacts.length; j++) {
                    contacts[j] = parishcontacts.item(j).childNodes[0].nodeValue;
                }
                var parish = { latlng: latlng, name: name,
                    address1: address1, address2: address2,
                    city: city, state: state, zip: zip,
                    contacts: contacts, phone: phone
                }
                map.addOverlay(thisobject.createParish(parish));
            }
            if (parishcount > 1)
                thisobject.fitMap(map, points);
            else
                map.setCenter(new GLatLng(45.0890355, -99.31640625), 3);
            if (completed != null)
                completed(parishcount);

        });
    }

    this.createParish = function(parish) {
        var newIcon = MapIconMaker.createMarkerIcon({ width: 32, height: 32, primaryColor: "#00ff00" });
        var marker = new GMarker(parish.latlng, { icon: newIcon });
        var html = "<div class='mapPopUp'>";
        html += "<h4>" + parish.name + "</h4> <br/>" + parish.address1;
        if (parish.address2 != "")
            html += "<br/>" + parish.address2;
        html += "<br/>" + parish.city + ", " + parish.state + "  " + parish.zip + "<br/>";
        html += "<p><b>Contacts:</b></p>";
        for (var i = 0; i < parish.contacts.length; i++) {
            html += "<p>" + parish.contacts[i] + "</p>";
        }
        if (parish.phone != "")
            html += "<p>" + parish.phone + "</p>";
        // Generate link for directions, opens in separate window
        var encodedAddress = escape(parish.address1 + " " + parish.address2 + " " + parish.city + " " + parish.state + " " + parish.zip);
        html += "<br/><a href='http://maps.google.com/maps?daddr=" + encodedAddress + "&hl=en' target='_blank'>Get Directions</a>";
        html += "</div>";
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(html);
        });
        return marker;
    }

    this.fitMap = function(map, points) {
        var bounds = new GLatLngBounds();
        for (var i = 0; i < points.length; i++) {
            bounds.extend(points[i]);
        }
        var zoomlevel = map.getBoundsZoomLevel(bounds)
        if (zoomlevel == 19)
            zoomlevel = 16;
        map.setZoom(zoomlevel);
        map.setCenter(bounds.getCenter());
    }

    this.S4 = function() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); }

    this.guid = function() { return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4()); }

    this.GetParishData = function(marker, elementName) {
        var result = "";
        var element = marker.getElementsByTagName(elementName);
        if (element != null) {
            var item = element.item(0);
            if (item != null) {
                var childNode = item.childNodes[0];
                if (childNode != null) {
                    result = childNode.nodeValue;
                }
            }
        }
        return result;
    }

}

