var StreetViewClass = new Class({
	initialize: function(options) {
		options = $merge({
			'yaw':0,		
			'pitch':0,
			'zoom':0,
			'latitude':0,
			'longitude':0,
			'actviateLinkLabel':'',
			'viewLinkLabel':''
			}, options);
		this.yaw = options.yaw;
		this.pitch=options.pitch;
		this.zoom = options.zoom;
		this.latitude = options.latitude;
		this.longitude = options.longitude;	
		this.actviateLinkLabel = options.actviateLinkLabel;
		this.viewLinkLabel = options.viewLinkLabel;
		this.pano = null;
		this.panoId = 'streetViewPano';
		this.streetMgmtBlock = 'streetViewMgmt';
		this.linkId = 'streetViewLnk';
		window.addEvent('domready', this.init.bind(this));
	},
	
	init: function(){
		this.pano = new GStreetviewPanorama(document.getElementById(this.panoId));
		if (this.latitude > 0){
			this.updatePano();
		}
		GEvent.addListener(this.pano, "error", this.handleNoFlash.bind(this));
	},
	
	updatePano: function(){
		$(this.panoId).empty();
		point = new GLatLng(this.latitude, this.longitude);
		myPOV = {yaw:this.yaw, pitch:this.pitch, zoom:this.zoom};
		this.pano.setLocationAndPOV(point, myPOV);	
	},
	
    handleNoFlash: function(errorCode) {
		/*if (errorCode == 603) {
		  alert("Error: Flash doesn't appear to be supported by your browser; however it is needed for Street View");
		  return;
		}*/
    },
    	
	updateLatLng: function(lat, lng){
		this.latitude = $('streetViewForm.yaw').latitude  = lat;
		this.longitude = $('streetViewForm.longitude').value = lng;
		this.yaw = $('streetViewForm.yaw').value = 0;
		this.pitch = $('streetViewForm.pitch').value = 0;
		this.zoom = $('streetViewForm.zoom').value =0;
		this.updatePano();
	},
    	
	saveStreetView: function(){
		$('streetViewForm.yaw').value = this.pano.getPOV().yaw;
		$('streetViewForm.pitch').value = this.pano.getPOV().pitch;
		$('streetViewForm.zoom').value = this.pano.getPOV().zoom;
		$('streetViewForm.latitude').value = this.pano.getLatLng().lat(); 
		$('streetViewForm.longitude').value = this.pano.getLatLng().lng();
		this.hide();
		this.updateLinkLabel(true);		
	},
	
	deleteStreetView: function(){
		$('streetViewForm.yaw').value = 0;
		$('streetViewForm.pitch').value = 0;
		$('streetViewForm.zoom').value = 0;
		$('streetViewForm.latitude').value = 0;
		$('streetViewForm.longitude').value = 0;
		this.hide();
		this.updateLinkLabel(false);			
	},
	
	activate: function(lat, lng){
		if (this.latitude == 0){
			this.updateLatLng(lat, lng);
		}
		this.show();
	},
	
	show: function(){
		if ($(this.streetMgmtBlock))
			$(this.streetMgmtBlock).setStyle('left', '23%');
	},
	
	hide: function(){
		if ($(this.streetMgmtBlock))
			$(this.streetMgmtBlock).setStyle('left', '-500px');	
	},
	
	updateLinkLabel: function(active){
		if ($(this.linkId)){
			$(this.linkId).setHTML(active ? this.viewLinkLabel : this.actviateLinkLabel);
		}
	}
	
});

