/**
 * Streetview Manager
 * class to manage google street view in an html container
 * 
 * @author Manuel Boy | polargold GmbH
 */
var StreetView = function() {
	
	/** basic settings */
	this.settings = {
		bgColor: '#FFFFFF',
		svContainerId: 'streetview',
		flashContainerId: 'launcher'
	}
	
	this.client;
	this.panorama;
	this.elem;
	this.flashApplication;
	
	/** track the streetview state */
	this.isInitialized = false;
	this.panoramaIsInitialized = false;
	
	this.currentLat;
	this.currentLon;
	this.currentPitch;
	this.currentYaw;
	
	/**
	 * Initialize the streetview application and the flash container
	 */
	this.init = function(options) {
		this.elem = document.getElementById(this.settings.svContainerId);
		this.elem.innerHTML = '';
		this.elem.style.backgroundColor = this.settings.bgColor;
		this.elem.style.position = 'absolute';
		this.flashApplication = document.getElementById(this.settings.flashContainerId);
		this.isInitialized = true;
	};

	/**
	 * Initialize the streetview panorama
	 */
	this.initializePanorama = function() {
		this.client = new google.maps.StreetviewClient();
		this.panorama = new google.maps.StreetviewPanorama(this.elem);
		var self = this;
		google.maps.Event.addListener(this.panorama, "error", function() {
			// general error
		});
		google.maps.Event.addListener(this.panorama, "yawchanged", function(newYaw) {
			self.currentYaw = newYaw;
			self.updateFlashApplication();
		});
		google.maps.Event.addListener(this.panorama, "pitchChanged", function(newPitch) {
			self.currentPitch = newPitch;
			self.updateFlashApplication();
		});
		google.maps.Event.addListener(this.panorama, "initialized", function(location) {
			self.currentLat = location.latlng.lat();
			self.currentLon = location.latlng.lng();
			self.updateFlashApplication();
		});
		this.panoramaIsInitialized = true;
	};
	
	/**
	 * Navigate to a location in streetview
	 */
	this.navigateTo = function(latitude, longitude, yaw, pitch) {
		var self = this;
		if (this.client) {
			this.client.getNearestPanoramaLatLng(new GLatLng(latitude, longitude), function(latLon) {
				if (latLon) {
					self.panorama.setLocationAndPOV(latLon, yaw != null ? {yaw: yaw, pitch: pitch} : null);
				} else if (this.currentLat != null) {
					self.updateFlashApplication();
				} else {
					self.noPositionAvailable();
				}
			});
		}
	}
	
	/**
	 * Update the flash application and send current streetview position data
	 */
	this.updateFlashApplication = function() {
		this.flashApplication.onStreetViewUpdate(this.currentLat, this.currentLon, this.currentYaw, this.currentPitch);
	}

	/**
	 * Show the street view container
	 */
	this.show = function() {
		this.elem.style.display = 'block';
	};

	/**
	 * Hide the street view container
	 */
	this.hide = function() {
		this.elem.style.display = 'none';
	};
	
	/**
	 * Update dimensions and position of the street view container
	 */
	this.setPosition = function(width, height, left, top) {
    	if (width > 0 && height > 0) {
			this.elem.style.width = width + "px";
			this.elem.style.height = height + "px";
 			if (left != null) {
				this.elem.style.left = left + "px";
			}
			if (top != null) {
				this.elem.style.top = top + "px";
			}
			if (this.panorama) {
				this.panorama.checkResize();
			}
		}
	};
	
	/**
	 * Send error back to flash application
	 */
	this.noPositionAvailable = function() {
		this.flashApplication.onStreetViewError();
	}
	
	/**
	 * Private log function (used in debugging)
	 */
	this._log = function(message) {
		if (message != null && message != '') {
			if (window.console) { console.log('Debug: ' + message + ''); }
		}
	}
	
}
