/*
 * pass events about changes to the map type/zoom level/location to state manager (stateManager.js)
 *
 *
 *
 */
//namespace for plugin
UGMO.prototype.UGMOStateManager = {};
 
UGMO.prototype.enableStateManager = function(){
    this.UGMOStateManager.oldShow = this.show;
    this.show = this.addStateManager;
};
 
UGMO.prototype.addStateManager = function(){
    this.UGMOStateManager.oldShow.apply(this);
    
 	var ugmo_							= this;
	
	if(typeof(ugmo_.map) !== 'undefined'){
		// add map information to url
		
		// add event handlers for type change, location change and zoom level change
		// map type change
		google.maps.event.addListener(ugmo_.map, 'maptypeid_changed', function(){
			stateManager.change('mapType', ugmo_.map.getMapTypeId());
			stateManager.update(false);
		});
		
		// end of drag
		google.maps.event.addListener(ugmo_.map, 'dragend', function(){
			var center = ugmo_.map.getCenter();
			
			stateManager.change('lat', center.lat());
			stateManager.change('lon', center.lng());
			stateManager.update(false);
		});
		
		// end of zoom
		google.maps.event.addListener(ugmo_.map, 'zoom_changed', function(){
			stateManager.change('zoom', ugmo_.map.getZoom());
			stateManager.update(false);
		});
	}else{
		throw('The google map object hasn\'t been initialised. Make sure you call the addStateManager function AFTER calling ugmo.show()!');
	}
};

/* function to update location, zoom level and type of map */
UGMO.prototype.updateState = function(lat, lon, zoom, mapType){
    
	var center = this.map.getCenter();
	// adjust center when necessary
	if((typeof(lat) !== 'undefined' && lat !== center.lat()) || (typeof(lon) !== 'undefined' && lon !== center.lng())){
		this.map.setCenter(new google.maps.LatLng(lat, lon));
	}
	
	// adjust zoom level when necessary
	if(typeof(zoom) !== 'undefined' && parseInt(zoom) !== this.map.getZoom()){
		this.map.setZoom(parseInt(zoom));
	}
	
	// adjust map type when necessary
	if(typeof(mapType) !== 'undefined' && mapType !== this.map.getMapTypeId()){
		this.map.setMapTypeId(mapType);
	}
};
 
