// ------------------------------------------------------------------------------------------------
// GENERIC

	// merge dicts
	function MergeDicts(old_dict, new_dict){
		var merged_dict = {}
		
		for (var i in old_dict){ // copy dict
			merged_dict[i] = old_dict[i]
		}
		
		for (var i in new_dict){ // merge data
			merged_dict[i] = new_dict[i]
		}
		
		return merged_dict
	}

// ------------------------------------------------------------------------------------------------
// RUBBER MAPS
	
	// Rmaps
	function Rmaps(){
		var rmaps = this
		rmaps.locations = []
		
		// add markers to the map
		rmaps.SetMarkers = function(locations, override_icon) {
			var offset_id = rmaps.locations.length // used when markers have already been set
			
			// loop over markers
			var num_locations = locations.length
			for (var i = 0; i < num_locations; i++) {
				var location = locations[i]
				location.lat_lng = new google.maps.LatLng(location.lat, location.long)
				
				var marker_config = {
					id: i + offset_id, // used to lookup extra information
					position: location.lat_lng,
					map: rmaps.map,
					title: location.title.replace(/&amp;/g, '&')
				}
				
				// add icon
				rmaps.AddFancyIcon(marker_config, location, override_icon) // adds icon attributes to marker_config by reference
				
				// add marker
				var gmarker = new google.maps.Marker(marker_config)
			
				// make clickable
				google.maps.event.addListener(gmarker, 'click', function(e) {
					var contents = rmaps.MakePopup(this.id)
					rmaps.popup.setContent(contents)
					
					// popup on point
					rmaps.popup.setPosition(this.getPosition())
					rmaps.popup.open(rmaps.map)
					
					// popup on center top of icon
					// rmaps.popup.open(rmaps.map, this)
				})
			}
			
			
			// extend locations 
			rmaps.locations = rmaps.locations.concat(locations) // used need to when SetMarkers is called multiple times 
			
		
			// center map on markers
			// if the override init config does not contain 'center'
			if (! rmaps.config.center){
				var total_locations = rmaps.locations.length
				var lat_lng_bounds = new google.maps.LatLngBounds()
				for (var i = 0; i < total_locations; i++) {
					lat_lng_bounds.extend(rmaps.locations[i].lat_lng)
				}
				rmaps.map.setCenter(lat_lng_bounds.getCenter())
				rmaps.map.fitBounds(lat_lng_bounds)
			}
		}
		
		// make popup
		rmaps.MakePopup = function(id){
			var m = rmaps.locations[id]
			
			var html = ''
			
			// conditional markup - allows fields to be optional
			if (m.title){ 	html += '<h4 class="name">'+m.title+'</h5>' }
			if (m.organisation_url){ 		html += '<a class="site" href="'+m.organisation_url+'">'+m.organisation_url+'</a>' }
			if (m.description){ 		html += '<p class="desc">'+m.description+'<p>' }
			
			if (m.products.length){ 
				
				var products = []
				for (var i=0; i< m.products.length; i++){
					var product_title = m.products[i].name
					var product_class = m.products[i].id
					var product_link = m.products[i].link
					
					if (product_link){
						products.push('<li class="'+product_class+'"><a href="'+product_link+'" title="'+product_title+'">'+product_title+'</a></li>')
					} else {
						products.push('<li class="'+product_class+'">'+product_title+'</li>')
					}
				}
				html += '<ul class="products">'+products.join('')+'</ul>' 
			}
			
			// 'popup' class for CSS selectors
			return '<div class="popup">'+html+'</div>'
		}
		
		// make icon
		rmaps.AddFancyIcon = function(config, location, forced_icon_key){
			// ref: http://code.google.com/apis/maps/documentation/javascript/overlays.html#ComplexIcons
			// currently assumes product icons are the same dimensions
			
			// icon precedence
			//     1. forced_icon (passed to SetMarkers function)
			//     2. location.icon (stored on location object)
			//     3. Rmaps.icons.standard (delib logo)
			
			var override_icon = (forced_icon_key) ? Rmaps.icons[forced_icon_key] : location.icon
			var icon = MergeDicts(Rmaps.icons.standard, override_icon)
			
			// marker icon
			config.icon = new google.maps.MarkerImage(icon.image,
				new google.maps.Size(icon.width, icon.height), // icon WxH 
				new google.maps.Point(0, 0), // 0,0 top left
				new google.maps.Point(icon.point_x, icon.point_y)) // point of marker
			
			// clickable region of the icon
			config.shape = {
				coord: icon.clickable_region,
				type: 'poly'
			} 
		}
		
		// init
		rmaps.init = function(target_ele, override_config){
			// could extend to allow override map defaults
			var ele = document.getElementById(target_ele)
			
			// dimensions
			if (override_config){
				if (override_config.height){ $(ele).height(override_config.height) } // frequently need to set
				if (override_config.width){ $(ele).width(override_config.width) } // rarely need to ever set as default is 100% of container
			}
			
			// map config
			var default_config = {
				panControl: false,
				streetViewControl: false,
				mapTypeControl: false,
				zoomControl: true,
				zoomControlOptions: {
					style: google.maps.ZoomControlStyle.SMALL
				},
				mapTypeId: google.maps.MapTypeId.TERRAIN // .ROADMAP .TERRAIN .SATELLITE .HYBRID
			}
			
			// merge config
			this.config = MergeDicts(default_config, override_config)
			
			// create map
			rmaps.map = new google.maps.Map(ele, this.config)
			
			// popup
			rmaps.popup = new google.maps.InfoWindow({
				content: ''
			})
			google.maps.event.addListener(rmaps.map, 'click', function(e) { // close popup by clicking on map
				rmaps.popup.close()
			})
		}
	}

// ------------------------------------------------------------------------------------------------
// ICON DEFINITIONS
	
	Rmaps.icon_path = '/++resource++site_resources/all/img/'
	
	// product icons
	Rmaps.product_icon = { // product square 
		width: 44,
		height: 35,
		point_x: 14,
		point_y: 35,
		clickable_region: [
			0, 0,
			27, 0,
			27, 26,
			0, 26
			]
	}
	
	Rmaps.icons = {
		quick_consult: MergeDicts(Rmaps.product_icon, {image: Rmaps.icon_path+'marker_quick_consult.png'} ),
		planning_app: MergeDicts(Rmaps.product_icon, {image: Rmaps.icon_path+'marker_planning_app.png'} ),
		citizen_space: MergeDicts(Rmaps.product_icon, {image: Rmaps.icon_path+'marker_citizen_space.png'} ),
		budget_simulator: MergeDicts(Rmaps.product_icon, {image: Rmaps.icon_path+'marker_budget_simulator.png'} ),
		dialogue_app: MergeDicts(Rmaps.product_icon, {image: Rmaps.icon_path+'marker_dialogue_app.png'} )
	}
	
	// default icon
	Rmaps.icons.standard = {
		image: Rmaps.icon_path+'marker_delib.png',
		width: 41,
		height: 33,
		point_x: 12,
		point_y: 33,
		clickable_region: [
			7, 0,
			2, 5, 
			0, 14,
			0, 21,
			5, 25,
			16, 23,
			24, 17,
			24, 11,
			14, 0
			] // pairs of coords
	}
