﻿/**
* @fileoverview Wrapper for Google Maps API v3
*/

// Create NetR namespace if it doesn't exist
if (typeof (NetR) === 'undefined') {
    var NetR = {};
}

NetR.GMaps = function() {
    var map = null;
    // Default options
    var options = {
        defaultCenter: [63.442651, 15.629881],
        defaultZoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        showMapTypes: true,
        mapContainerId: '',
        mapElementId: 'map',
        mapClass: '',
        markerAtCenter: false,
        markers: [],
        markersXML: '',
        XMLMarkerNodeName: '',
        XMLMarkerLinkText: '',
        loadComplete: null
    };

    /**
    * Creates a marker on the map, with info bubble displaying on mouseover
    * @param lat Latitude for marker location
    * @param lon Longitude for marker location
    * @param url URL for more info about marker location
    * @param name Name of location
    * @param linkURLs URLs of links that have corresponding map markers
    */
    function createMarker(lat, lon, url, name, body, linkURLs) {
        var domain = "http://" + document.domain;
        
        //var point = new GLatLng(lat, lon);
        var point = new google.maps.LatLng(lat, lon)

        var image = new google.maps.MarkerImage('/i/icons/mapmarker.png',
        // This marker is 20 pixels wide by 32 pixels tall.
            new google.maps.Size(30, 40),
        // 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(15, 40)
        );

        var shadow = new google.maps.MarkerImage('/i/icons/mapmarker_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(51, 40),
            new google.maps.Point(0, 0),
            new google.maps.Point(15, 40)
        );

        var marker = new google.maps.Marker({
            position: point,
            map: this.map,
            shadow: shadow,
            icon: image,
            shape: {
                coord: [1, 1, 1, 40, 30, 40, 30, 1],
                type: 'poly'
            },
            title: name
        });

        if (url && name && body) {
            var windowHtml = '<div class="bubble"><a class="action" href="' + url + '">' + name + '</a><br />' + body + '</div>';
            var infowindow = new google.maps.InfoWindow({
                content: windowHtml
            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(this.map, marker);
            });
        }

        if (url && linkURLs[domain + url]) {
            $(linkURLs[domain + url]).bind('mouseover', windowHtml);
        }
    }

    /**
    * Creates markers from XML document on map
    * @param xml The XML document returned by an Ajax request
    */

    function createMarkers(xml) {
        var points = xml.getElementsByTagName(options.XMLMarkerNodeName);
        var linkURLs = {};
        if (options.restaurantList) {
            var linkContainer = document.getElementById(options.restaurantList);
            if (linkContainer) {
                var links = linkContainer.getElementsByTagName("a");
                for (var i = 0, len = links.length; i < len; i++) {
                    linkURLs[links[i].href] = links[i];
                }
            }
        }
        var point;
        for (var i = 0, len = points.length; i < len; i++) {
            point = points[i];
            createMarker(point.getAttribute('latitude'), point.getAttribute('longitude'), point.getAttribute('URL'), point.getAttribute('name'), point.getAttribute('body'), linkURLs);
        }
    }

    /**
    * Performs Ajax call to get XML data for markers
    * @requires jQuery
    * @param {string} XMLPath URL for XML document
    */
    function loadMarkersFromXML(XMLPath) {
        $.ajax({
            type: 'GET',
            dataType: 'xml',
            url: XMLPath,
            success: function(xml) {
                createMarkers(xml);
            }
        });
    }

    /**
    * Loads map into element with id from options.mapContainerId
    */
    function load() {
        if (document.getElementById(options.mapContainerId)) {
            // Create element
            var mapContainer = document.getElementById(options.mapContainerId);
            //var mapElement = document.createElement('div');
            //var mapElement = document.getElementById(options.mapContainerId);
            //mapElement.id = options.mapElementId;

            //            if (options.mapClass && options.mapClass.length > 0) {
            //                //mapElement.className = options.mapClass;
            //            }

            mapContainer.innerHTML = '';
            //mapContainer.appendChild(mapElement);

            var centerPoint = new google.maps.LatLng(options.defaultCenter[0], options.defaultCenter[1]);

            // Init map
            this.map = new google.maps.Map(mapContainer, {
                center: centerPoint,
                zoom: options.defaultZoom,
                mapTypeId: options.mapTypeId,
                mapTypeControl: options.showMapTypes,
                mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
            });

            // Marker at center point?
            if (options.markerAtCenter) {
                createMarker(options.defaultCenter[0], options.defaultCenter[1]);
                /*new google.maps.Marker({
                position: centerPoint,
                map: this.map
                });*/
            }

            // Add additional markers?
            if (options.markers && options.markers.length > 0) {
                for (var i = 0, len = options.markers.length; i < len; i++) {
                    this.map.addOverlay(new GMarker(new GLatLng(options.markers[i][0], options.markers[i][1])));
                    //createMarker(options.markers[i][0], options.markers[i][1]);
                }
            }

            if (options.markersXML && options.markersXML.length > 0) {
                loadMarkersFromXML(options.markersXML, createMarkers);
            }

            // Execute loadComplete function (if it exists)
            if (typeof (options.loadComplete) === "function") {
                options.loadComplete(this.map);
            }
        }
    }

    /**
    * Accessor function for map object
    */
    function getMap() {
        return this.map;
    }

    /**
    * Initialization
    */
    function init(opts) {
        if (!document.getElementById || !document.createElement) {
            return;
        }
        // If options were supplied, apply them to the option Object.
        for (var key in opts) {
            if (options.hasOwnProperty(key)) {
                options[key] = opts[key];
            }
        }
        load();
    }

    return {
        init: init,
        createMarkers: createMarkers,
        getMap: getMap
    };
} ();