/*The most basic GoogleMapConfiguration */
var YouTube = new Class({
	elem: null,
	initialize: function(container){
		this.elem = container;
		this.url = this.elem.title;
		this.elem.set('html', '<object class="youtube" width="320" height="265"><param name="movie" value="' + this.url +'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed class="youtube" src="' + this.url + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object>');
	}
});

var Ads = new Class({
	elem: null,
	initialize: function(container){
		this.elem = container;
		this.elem.set('html', '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="468" height="60" id="new_orgsupp468x60" ><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value=" http://www.easyfundraising.org.uk/files/new_orgsupp468x60.swf?clickThrough=http://www.easyfundraising.org.uk/combatartsscotland&target=_blank" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src=" http://www.easyfundraising.org.uk/files/new_orgsupp468x60.swf?clickThrough=http://www.easyfundraising.org.uk/combatartsscotland&target=_blank " quality="high" bgcolor="#ffffff" width="468" height="60" name="new_orgsupp468x60" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage=" http://www.macromedia.com/go/getflashplayer" /></object>');
	}
});

window.addEvent('domready', function(){
	$$('div.youtube').each(function(e, i){
		new YouTube(e);
	});
	$$('.ads').each(function(e, i){
		new Ads(e);
	});
});

var GoogleMap = new Class({
	elem: null,
	map: null,
	options: {'height':'350px'},
	initialize: function(container, marker_list, options) {
		if(typeof(options) == undefined) options = {};
		if(typeof(marker_list) == undefined) marker_list = new Hash();
		this.plotted_markers = new Hash();
		this.marker_list = marker_list;
		this.elem = container;
		
		this.base_icon = new GIcon(G_DEFAULT_ICON);
		this.base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		this.base_icon.iconSize = new GSize(20, 34);
		this.base_icon.shadowSize = new GSize(37, 34);
		this.base_icon.iconAnchor = new GPoint(9, 34);
		this.base_icon.infoWindowAnchor = new GPoint(9, 2);
		
		this.elem.empty();
		this.elem.setStyles({'visibility':'visible', 'display':'block', 'height': this.options.height, 'border': '2px solid #b0af9d', 'overflow':'hidden'});
		this.init_map();
		this.plot_points(this.get_marker_list());
	},
	
	init_map: function(){
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(this.elem);
			this.map.addControl(new GSmallMapControl());
		}
	},
	
	get_icon: function(letter) {
      // Create a lettered icon for this point using our icon class
      var letteredIcon = new GIcon(this.base_icon);
      letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
      return letteredIcon;
    },
	
	get_marker_list: function(){
		return this.marker_list;
	},
	
	focus_and_pop: function(marker_id){
		marker = this.plotted_markers[marker_id];
		GEvent.trigger(marker, 'click');
	},
	
	plot_points: function(marker_list) {
		var bounds = new GLatLngBounds();
		marker_list.each(function(e, i){
			var point = new GLatLng(e['latitude'], e['longitude']);
			bounds.extend(point);
			var marker = new GMarker(point, {icon: this.get_icon(i)});
			marker.text = new Element('div').set('html', e['text']);
			GEvent.addListener(marker, "click", function() {
				this.openInfoWindow(new Element('div').adopt(this.text));
			});
			this.map.addOverlay(marker)
			this.plotted_markers[i] = marker;
		}.bind(this));
		
		if (!bounds.isEmpty()) {
			this.map.setCenter(bounds.getCenter(), Math.min(15, this.map.getBoundsZoomLevel(bounds) - 1));
		}
	}
});