var GMapx, GMapDriver, GLocation;

GMapx = new Class({
	UI: {
		gDefault: true,
		showGoogleBar: false,
		enableDragging: false
	},
	O: {
		lat: 37.4419,
		lng: -122.1419,
		zoom: 5
	},
	
	map: null,
	
	onMapLoaded: function(){},
	onGenerateMarkerHtml: function(){},
	onMarkersSet: function(){},
	loadMap: function(mapCanvas, initLoc){
		this.map = new GMap2($(mapCanvas));
		
		if (initLoc) {
			this.map.setCenter(new GLatLng(initLoc.lat, initLoc.lng), initLoc.zoom);
			this.map.addOverlay(new GMarker(new GLatLng(initLoc.lat, initLoc.lng)));
		} else {
			this.map.setCenter(new GLatLng(this.O.lat, this.O.lng), this.O.zoom);
		}
		
		if (this.UI.gDefault) {this.map.setUIToDefault();}
		if (this.UI.showGoogleBar) {this.map.enableGoogleBar();}
		if (this.UI.enableDragging) { this.map.enableDragging();}
	}
});

GMapDriver = new Class({
	Extends: GMapx,
	Implements: [Events, Options],
	
    options: {
        mapContainerId: 'map_canvas'
    },

 	bounds: {
        max_lat: null
        , min_lat: null
        , max_lng: null
        , min_lng: null
    },

	initialize: function (initLoc, options) {
		this.setOptions(options);
		
		this.loadMap(this.options.mapContainerId, initLoc);
    },

	addLocations: function(locs) {
		locs.each(function(location) {
			$(location.id).addEvent('click', this.showLocation.bind(this).pass(location));
		}, this);
	},
	
	showLocation: function(location) {
		this.map.setCenter(new GLatLng(location.lat, location.lng), location.zoom);
		this.map.clearOverlays();
		this.map.addOverlay(new GMarker(new GLatLng(location.lat, location.lng)));
	},

 	onClickMarker: function(location) {
		var node, img, title, node_text;
		node = new Element('div');
		img = new Element('img', {'src': location.image});
		title = new Element('h3', {'html': location.title, 'styles': {margin: 0, padding: 0}});
		node_text = new Element('p', {'html': location.text});
		
		img.inject(node);
		title.inject(node);
		node_text.inject(node);
		
		this.openInfoWindowHtml(node);
	}
});

GLocation = new Class({

});
