var EarthWrapper = function () {
	this.active = true;
	this.fullScreenOn = false;
    earthMap = new GMap2(document.getElementById("earth"));
    earthMap.setCenter(new GLatLng(0,0.0), 0);
	earthMap.setMapType(G_SATELLITE_3D_MAP);
	var self = this;
	earthMap.getEarthInstance(function (obj){
		try {
			earth = obj;
			var navControl = earth.getNavigationControl();
			navControl.setVisibility(earth.VISIBILITY_HIDE);
			
			var lookAt = earth.getView().copyAsLookAt(earth.ALTITUDE_RELATIVE_TO_GROUND);
			lookAt.setTilt(55);
			lookAt.setRange(DEFAULT_RANGE);
			
			earth.getView().setAbstractView(lookAt);
		} catch (e) {
			Message.showMessage(Message.EARTH_PLUGIN_NOT_FOUND);
		}
	});
	var INTERTVAL = 1.5 * 1000;
	setInterval(this.getIntervalAction(), INTERTVAL);
};
EarthWrapper.prototype.flyTo =  function (latitude,longitude,direction) {
	if (!earth) return;
	var lookAt = earth.getView().copyAsLookAt(earth.ALTITUDE_RELATIVE_TO_GROUND);
	if (direction)
		lookAt.setHeading(direction);
	lookAt.setTilt(DEFAULT_TILT);
	earth.getOptions().setFlyToSpeed(earth.SPEED_TELEPORT);
	
	lookAt.setRange(DEFAULT_RANGE);
	lookAt.setLatitude(latitude);
	lookAt.setLongitude(longitude);
	
	earth.getView().setAbstractView(lookAt);
};
EarthWrapper.prototype.getIntervalAction = function () {
	var self = this;
	return function (){self.intervalAction()};
}
EarthWrapper.prototype.intervalAction = function(){
	var sat = SatMap.getSelectedSat();
	if (!sat) 
		return;
	var date = new Date();
	sat.calcCurrentPosition(date);
	var posData = sat.currentPosition;
	if (posData && !Player.playing) {
		this.flyTo(posData.position.latitude, posData.position.longitude, posData.direction);
	}
	//{index:0,posCount:positions.length,position:position, direction:direction};
};
EarthWrapper.prototype.setPlaceMark = function () {
	if (!('counter' in window)) {
	  window.counter = 1;
	}

	var icon = earth.createIcon('');
	icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
	var style = earth.createStyle('');
	style.getIconStyle().setIcon(icon);
	
	var lookAt = earth.getView().copyAsLookAt(earth.ALTITUDE_RELATIVE_TO_GROUND);
	var point = earth.createPoint('');
	point.setLatitude(urlInfo.currentPosition.lat());
	point.setLongitude(urlInfo.currentPosition.lng());
	
	var pointPlacemark = earth.createPlacemark('');
	pointPlacemark.setName("現在地");
	pointPlacemark.setGeometry(point);
	pointPlacemark.setStyleSelector(style);
	earth.getFeatures().appendChild(pointPlacemark);
}

EarthWrapper.prototype.show = function () {
	this.active = true;
	var areaOffset = Element.cumulativeOffset($('earthContainer'));
    $('earth').style.top = (areaOffset[1]) + 'px';
    $('earth').style.left = (areaOffset[0]) + 'px';
};
EarthWrapper.prototype.hide = function () {
	this.active = false;
	$('earth').style.left = '-400px';	
};
EarthWrapper.prototype.restore = function () {
	if (this.active && !this.fullScreenOn) this.show();
	if (this.fullScreenOn) this.fullScreen();
};
EarthWrapper.prototype.fullScreen = function () {
	window.scrollTo(0, 0);
	//Sound.play('sound/toyama.mp3');
	setFlashAttributes();
	this.fullScreenOn = true;
	$('earth').style.top = '0px';
	$('earth').style.left = '0px';
	var windowSize = Util.getWindowSize();
	var targetWidth = $('horizontalBar').clientWidth;
	$('earth').style.width = targetWidth+ 'px';
	$('earth').style.height = windowSize.height-20 + 'px';
	$('exitEarthFullScreenIcon').show();
	$('exitEarthFullScreenIcon').style.top = windowSize.height-20 + 'px';
	$('exitEarthFullScreenIcon').style.left =   '0px';
	$('exitEarthFullScreenIcon').style.width = targetWidth + 'px';
};
EarthWrapper.prototype.exitFullScreen = function () {
	this.fullScreenOn = false;
	$('exitEarthFullScreenIcon').style.display = 'none';
	$('earth').style.height = $('earthContainer').style.height;
	$('earth').style.width = $('earthContainer').style.width;
	this.restore();
};
