var MapWrapper = function () {
	this.active = true;
    map = new GMap2(document.getElementById("map"));
    map.enableGoogleBar();
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(DEFAULT_LOCATION.latitude,DEFAULT_LOCATION.longitude), MAP_DEFAULT_ZOOM_LEVEL);
    map.returnToSavedPosition();
	map.disableDoubleClickZoom();
	map.enableScrollWheelZoom();
	map.enableDragging();
    GEvent.addListener(map, "dblclick", function(overlay, latLng){
		urlInfo.registerPosition(latLng);
       SatMap.checkStreet(latLng);
    });
	var self = this;
    GEvent.addListener(map, "moveend", function(){
        self.refreshStatus();
    });
	map.addOverlay(new GStreetviewOverlay());
};
MapWrapper.prototype.show = function () {
	this.active = true;
	var areaOffset = Element.cumulativeOffset($('mapContainer'));
    $('map').style.top = (areaOffset[1]) + 'px';
    $('map').style.left = (areaOffset[0]) + 'px';
};
MapWrapper.prototype.hide = function () {
	this.active = false;
	$('map').style.left = '-600px';
};
MapWrapper.prototype.restore = function () {
	if (this.active) this.show();
};
MapWrapper.prototype.refreshStatus = function (){
    var position = map.getCenter();
	urlInfo.registerPosition(position);
	Player.reset();
	if (!this.marker || !this.marker.getLatLng().equals(position))
		this.moveIcon(position.lat(), position.lng());
}


MapWrapper.prototype.moveIcon = function (latitude,longitude) {
	if (!this.marker) {      
		this._createMarker(latitude,longitude);
	}
	this.marker.setPoint(new GLatLng(latitude,longitude));
};
MapWrapper.prototype._createMarker = function (latitude,longitude) {
		var icon = new GIcon();
	    icon.image = "http://www.tori.st/api/sat/images/spacestation.gif";
	    icon.iconSize = new GSize(32, 32);
	    icon.iconAnchor = new GPoint(16, 16);
	
	    var markeropts = new Object();
	    markeropts.icon = icon;
		this.marker = new GMarker(new GLatLng(latitude,longitude), {draggable:true});
		var self = this;
	    GEvent.addListener(this.marker, "dragend", function(latLng){
			map.setCenter(latLng);
	    });
		map.addOverlay(this.marker);
};