var worldMapDebug = false;
var WorldMapWrapper = function () {
	this.debugTime = new Date();
	var INTERTVAL = (worldMapDebug)?3 * 1000: 10 * 1000;
	this.active = true;
    this.worldMap = new GMap2(document.getElementById("worldMap"));
    this.worldMap.setCenter(new GLatLng(0,0.0), 0);
	this.worldMap.disableDragging() ;
	this.worldMap.disableDoubleClickZoom();
	setInterval(this.getMoveAction(), INTERTVAL);
};
WorldMapWrapper.prototype.drawOrbit = function (polyline) {
	if (this.polyline) this.worldMap.removeOverlay(this.polyline);
	this.polyline = polyline;
    this.worldMap.addOverlay(this.polyline);
};
WorldMapWrapper.prototype.getMoveAction = function () {
	var self = this;
	return function () { self.move() };
};
WorldMapWrapper.prototype.move = function () {
	var sat = SatMap.getSelectedSat();
	if (!sat || !sat.positionLoaded) return;
	var now = new Date();
	if (worldMapDebug) {
		this.debugTime.setTime(this.debugTime.getTime()+1000*60*2);
		now = this.debugTime;
	}
	var posData = sat.getNearestPosition(now);
	var pos = posData.position;
	var latLng = new GLatLng(pos.latitude, pos.longitude);
	
	this.worldMap.panTo(new GLatLng(0,pos.longitude),0);
	this.marker.setPoint(new GLatLng(pos.latitude,pos.longitude));
	this.marker.show();
};
WorldMapWrapper.prototype._createIcon = function (url) {
		if (this.marker)
			this.worldMap.removeOverlay(this.marker);
		this.marker = null;
		var icon = new GIcon();
	    icon.image = url; 
	    icon.iconAnchor = new GPoint(16, 16);
	    var markeropts = new Object();
	    markeropts.icon = icon;
		this.marker = new GMarker(new GLatLng(0,0),markeropts);
		this.worldMap.addOverlay(this.marker);	
		this.marker.hide();
}

WorldMapWrapper.prototype.show = function () {
	this.active = true;
	$('worldMap').show();
};
WorldMapWrapper.prototype.hide = function () {
	this.active = false;
	$('worldMap').hide();
};
WorldMapWrapper.prototype.escape = function () {
	$('worldMap').hide();
};
WorldMapWrapper.prototype.restore = function () {
	if (this.active && menu.isCurrentModeDetail()) this.show();
};

WorldMapWrapper.prototype.setSat = function (sat) {
	this.move();
	this._createIcon(sat.iconUrl);
};
