﻿
S2WLocationsMap =
{
  /**
  * initialize js for page
  *
  * @param   object  args   object with option key-values, e.g. { title: 'name?', onOk: function(result) { alert(result); }}
  */
  initialize: function(args) {  
    S2WLocationsMap.uniqueId = helpers.getOptionOrDefault(args, 'uniqueId', 1);
    S2WLocationsMap.width = helpers.getOptionOrDefault(args, 'width', "286px");
    S2WLocationsMap.height = helpers.getOptionOrDefault(args, 'height', "286px");
    S2WLocationsMap.zoom = helpers.getOptionOrDefault(args, 'zoom', 7);
    S2WLocationsMap.mapTypeId = helpers.getOptionOrDefault(args, 'mapTypeId', google.maps.MapTypeId.ROADMAP);
    S2WLocationsMap.language = helpers.getOptionOrDefault(args, 'language', 'nl');
    S2WLocationsMap.wsUrl = helpers.getOptionOrDefault(args, 'wsUrl', '?');
    S2WLocationsMap.channelIds = helpers.getOptionOrDefault(args, 'channelIds', '1, 146');
  },
  
	initializeS2WMap: function() {
		var now = new Date();
		var today = now.toString('yyyy-MM-dd');

		S2WLocationsMap.SetMapCanvasDimensions();

		$.getJSON(S2WLocationsMap.wsUrl, {
			channelIds: S2WLocationsMap.channelIds
		},
		function(results) {
			S2WLocationsMap.ShowMap(results);
		});
	},

	ShowMap: function(locations) {  
  
    var myOptions = {
      zoom: S2WLocationsMap.zoom,
      mapTypeId: S2WLocationsMap.mapTypeId,
      mapTypeControl: false,    
      language: S2WLocationsMap.language
    };
		S2WLocationsMap.map = new google.maps.Map(document.getElementById('S2WLocationCanvas' + S2WLocationsMap.uniqueId), myOptions);

		var bounds = new google.maps.LatLngBounds();

		for (i = 0; i < locations.length; i++) {
		  if (locations[i].locationLatitude != 0 && locations[i].locationLongitude != 0) {
				S2WLocationsMap.AddMarker(S2WLocationsMap.map, locations[i]);
				bounds.extend(new google.maps.LatLng(locations[i].locationLatitude, locations[i].locationLongitude));
			}
		}
	  S2WLocationsMap.map.fitBounds(bounds);
	},

	AddMarker: function(map, location) {
	var latlng = new google.maps.LatLng(location.locationLatitude, location.locationLongitude);

		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: location.locationName,
			icon: "/css/s2m/images/logoSmall.png",
			clickable: true
			//icon: new google.maps.MarkerImage({url: "/css/s2w/images/logoSmall.png", scaledSize: new google.maps.Size({width: 34, height: 34})})
		});
		
    google.maps.event.addListener(marker, 'click', function() {
      var infowindow = new google.maps.InfoWindow(
        { content:
             location.locationName + '<br />' +
             location.locationAddress + '<br />' +
             location.locationZipcode + ' ' + location.locationCity + '<br />' +
             location.locationPhone + '<br />' +
             '<a href="http://' + location.locationUrl + '" target=_blank>' + location.locationUrl + '</a><br />'             
        });
      infowindow.open(map, marker);
    });
	},
	
	SetMapCanvasDimensions: function() {
		var width = S2WLocationsMap.width;
		var height = S2WLocationsMap.height;
		if (width == "") 
			width = "286px";
		if (height == "") 
			height = "286px";
      
		$('#S2WLocationCanvas' + S2WLocationsMap.uniqueId).attr('style', "width: " + width + "; height: " + height);
	}
};
