/**
	EVENT GOOGLE MAPS API FUNCTIONS
**/

/**
* Function to initialize Tour Dates Map
*
* @name 	initialize_tour_map
* @access 	public
* @param 	string div_map
* @return 	void
* @author 	Ever Barreto
* @email	ever@amediacreative.com
*/
function initialize_tour_map(div_map) {
	
	map = new GMap2(div_map);
	
	// Default to Hybrid Map
	map.setMapType(G_HYBRID_MAP);
	
	// Large Map Control
	//map.addControl(new GLargeMapControl());
	map.addControl(new GSmallMapControl());
	
	// Map Overview Control
	//map.addControl(new GOverviewMapControl());
				
	// Map Type Control
	map.addControl(new GMapTypeControl());
				
	// Center the Map to US
	map.setCenter(new GLatLng(40, -98), 3);
	
	// Get Markers
	GDownloadUrl(_markers_url, function(data) {
		
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		
		for (var i = 0; i < markers.length; i++) {
			
			// Get Data from XML
			var tour_date = markers[i].getAttribute("tour_date");
			var tour_venue = markers[i].getAttribute("tour_venue");
			var tour_address1 = markers[i].getAttribute("tour_address1");
			var tour_address2 = markers[i].getAttribute("tour_address2");
			var venue_url = markers[i].getAttribute("venue_url");
			var ticket_url = markers[i].getAttribute("tickets_url");
			
			// Marker Lat+Lang
			var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
			
			// Tour
			var tourDate = {
				latlng: latlng,
				date: tour_date,
				venue: tour_venue,
				address1: tour_address1,
				address2: tour_address2,
				venue_url: venue_url,
				ticket_url: ticket_url
			};
			
			var latlngHash = (latlng.lat().toFixed(6) + "" + latlng.lng().toFixed(6));
			
			latlngHash = latlngHash.replace(".","").replace(".", "").replace("-","");
			
			if (locations[latlngHash] == null) {
				
				locations[latlngHash] = []
				
			}
			
			locations[latlngHash].push(tourDate);
			
		}
		
		for (var latlngHash in locations) {
			
			var tourDates = locations[latlngHash];
			
			if (tourDates.length > 1) {
				
				map.addOverlay(createClusteredMarker(tourDates));
				
			}
			else {
				
				map.addOverlay(createMarker(tourDates));
			
			}
		}
		
		// Set Zoom based on Markers
		map.setZoom(map.getBoundsZoomLevel(bounds));
		
		// Set Map Center based on Markers
		map.setCenter(bounds.getCenter());
		
	});

}

function createMarker(tourDates) {
	
	var tourDate = tourDates[0];
	var marker = new GMarker(tourDate.latlng);
	var html = "<b>" + tourDate.date + "</b>:<br/><b><a href=\"" + urldecode(tourDate.venue_url) + "\" style=\"color: #000000; text-decoration: underline\">" + tourDate.venue + '</a></b><br/><br/>' + tourDate.address1 + '<br/>' + tourDate.address2 + "<br/><a href=\"" + urldecode(tourDate.ticket_url) + "\" style=\"color: #000000; text-decoration: underline\">Get Tickets</a>";
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	
	// Extend boundaries
	bounds.extend(marker.getPoint());
	
	return marker;
	
}

function createClusteredMarker(tourDates) {

	var marker = new GMarker(tourDates[0].latlng);
	var html = "";
	
	for (var i = 0; i < tourDates.length; i++) {
		html = "<b>" + tourDates[i].date + "</b>:<br/><b><a href=\"" + urldecode(tourDates[i].venue_url) + "\" style=\"color: #000000; text-decoration: underline\">" + tourDates[i].venue + '</a></b><br/><br/>' + tourDates[i].address1 + '<br/>' + tourDates[i].address2 + "<br/><a href=\"" + urldecode(tourDates[i].ticket_url) + "\" style=\"color: #000000; text-decoration: underline\">Get Tickets</a>";
	}
	
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	
	// Extend boundaries
	bounds.extend(marker.getPoint());
	
	return marker;
	
}

function urldecode(str) {
    // Decodes URL-encoded string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}
