/***
 * create a map with single locations
 */
function dopopmap(marker) {
	var spot = document.getElementById('maplace');
	if (spot.style.display == '') {
		spot.style.display = 'block';
	}
	var singlelocmap = new Map("maplace");
	singlelocmap.addControls(0);
	singlelocmap.center(marker.point, 13);
	singlelocmap.populate( marker );
	return false;
}

/***
 * create a map with multiple locations
 * @param {Object} id
 * @param {Object} api
 */
var Map = function( id, api ) {
	this.id = id;
	this.api = api;
	switch ( this.api ) {
		case 'Ymap':
			break;
		case 'MSMap':
			break;
		default:
			this.map = new GMap2(document.getElementById(id));
     }
};

Map.prototype = {
	//initialize the map
	init : function() {
		return this.map;
	},
	//populate the map with a set of markers and their boundary
	populate : function( markers, bound ) {
		if (typeof(bound) != 'undefined') {
			for (var i = 0; i < markers.length; i++) {
				markers[i].populate(this.map);
				markers[i].bind();
				bound.add(markers[i].point);
			}
		} else {
			markers.populate(this.map);
			markers.bind();
		}
	},
	//add control type to the map
	addControls : function( type ) {
		switch ( this.api ) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				switch ( type ) {
					case 0:
						this.map.addControl(new GSmallMapControl());
						break;
					case 1:
						this.map.addControl(new GSmallMapControl());
						this.map.addControl(new GMenuMapTypeControl());
						break;
						break;
					default:
						this.map.addControl(new GLargeMapControl());
						this.map.addControl(new GMapTypeControl());
						break;
				}
				break;
		}
	},
	removeControls : function() {
		switch ( this.api ) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				this.map.removeControl(GSmallMapControl);
				break;
		}
	},
	//center the map
	center : function( point, zoom ) {
		switch (this.api) {
			case 'YMap' : 
				break;
			case 'MSMap' :
				break;
			default:
				this.map.setCenter( point, zoom );
				break;
		}
	},
	zoom : function( level ) {
		switch (this.api) {
			case 'YMap' : 
				break;
			case 'MSMap' :
				break;
			default:
				this.map.setZoom( level );
				break;
		}	
	}
};

/***
 * marker object constructor
 * @param {Object} point
 * @param {Object} icon
 * @param {Object} info
 * @param {Object} api
 */
var Marker = function( lat, lng, info, icon, api ) {
	switch ( api ) {
		case 'YMap':
			this.api = api;
			break;
		case 'MSMap':
			this.api = api;
			break;
		default:
			this.point = new GLatLng(lat, lng);
			this.info = info;
			this.icon = icon;
			this.api = 'GMap';
			this.marker = new GMarker( this.point, icon );
			break;
	}
};

Marker.prototype = {
	//populate the marker onto the map
	populate : function( map ) {
		switch( this.api ) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				map.addOverlay(this.marker);
				break;
		}
	},
	//bind and event to the marker
	bind: function(e){
		var event = null;
		switch (this.api) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				(typeof(e)) ? event = "click" : event = e;
				var info = new InfoWindow(this.marker, this.info, this.api);
				GEvent.addListener(this.marker, event, function(){
					info.open();
				});
				break;
		}
	}
};

/***
 * info window creation
 * @param {Object} marker
 * @param {Object} info
 * @param {Object} api
 */
var InfoWindow = function( marker, info, api ) {
	this.info = info;
	this.marker = marker;
	this.api = api;
};

InfoWindow.prototype = {
	open: function(e){
		switch(this.api) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				this.marker.openInfoWindowHtml( this.info )
		}
	}	
};

/***
 * create a boundary object
 * @param {Object} api
 */
var Bound = function( api ) {
	switch ( api ) {
		case 'YMap':
			this.api = api;
			break;
		case 'MSMap':
			this.api = api;
			break;
		default:
			this.api = 'GMap';
			this.bound = new GLatLngBounds();
			break;
	}
};

Bound.prototype = {
	//adds a point to a bound
	add : function( point ) {
		switch( this.api ) {
			case 'YMap':
				break;
			case 'MSMap':
				break;
			default:
				this.bound.extend( point );
				break;
		}
	},
	//returns the center of the bound
	center : function() {
		switch (this.api) {
			case 'YMap':
			case 'MSMap':
			default:
				return this.bound.getCenter();
		}
	},
	//returns the zoom level of te bound
	zoom : function( map ) {
		switch (this.api) {
			case 'YMap':
			case 'MSMap':
			default:
				return map.getBoundsZoomLevel(this.bound);
		}
	}
};