/**
 * Require MooTools framework core v:1.2
 * @author dtardia
 */
var MapPanel = new Class({
	
	initialize: function(container, lat,lon){
		this.container = $(container);
		this.minWidth = Math.round((this.container.getSize().x/4)*2.7);
		this.coordinates = new GLatLng(lat,lon);
		this.geocoder = new GClientGeocoder();
		this.leftPanel = new Element('div', {
			'styles': {
				'height': this.container.getSize().y,
				'width': this.container.getSize().x,
				'overflow':'hidden',
				'position':'relative'
			}
		});
		this.container.adopt(this.leftPanel)
		this.rightPanel = new Element('div', {
			'styles': {
				'height': this.container.getSize().y,
				'width': 0,
				'overflow':'hidden',
				'position':'absolute',
				'right':0,
				'top':0
			}
		});
		this.rightPanel_container = new Element('div', {
			'styles': {
				'height': this.container.getSize().y-25,
				'width': this.container.getSize().x-this.minWidth,
				'float':'left',
				'overflow':'auto',
				'font-size':'1em',
				'position':'relative',
				'top':15
			}
		});
		
		this.rightPanel_close = new Element('a', {
			'styles': {
				'font-size':'1em',
				'position':'absolute',
				'top':2,
				'right':2
			},
			'html':'Chiudi percorso',
			'href':'javascript:void(0)'
		});
		this.container.adopt(this.rightPanel.adopt(this.rightPanel_container,this.rightPanel_close));
		this.rightPanel_close.addEvent('click',this.clearDirections.bindWithEvent(this));
		this.leftFx = new Fx.Tween(this.leftPanel)
		this.rightFx = new Fx.Tween(this.rightPanel);
		this.rightFx.addEvent('complete',this.onResize.bindWithEvent(this));

		window.addEvent('domready', this.setMap.bindWithEvent(this));
	},
	open: function(){
		this.leftFx.start('width',this.leftPanel.getSize().x,this.container.getSize().x);
		this.rightFx.start('width',this.rightPanel.getSize().x,0);
		
	},
	close: function(){
		this.rightPanel_container.setStyle("width", this.container.getSize().x-this.minWidth-this.rightPanel_container.getStyle('padding-left').toInt()-this.rightPanel_container.getStyle('padding-right').toInt())
		this.leftFx.start('width',this.leftPanel.getSize().x,this.minWidth);
		this.rightFx.start('width',this.rightPanel.getSize().x,this.container.getSize().x-this.minWidth);
	},
	setMap: function(){
		if (GBrowserIsCompatible()) {
       		this.map = new GMap2(this.leftPanel);
			this.gdir = new GDirections(this.map, this.rightPanel_container);
			GEvent.addListener(this.gdir, "addoverlay", this.onAddoverlay.bindWithEvent(this));			
			GEvent.addListener(this.gdir, "error", this.mapsHandleErrors.bindWithEvent(this));
			//
        	this.map.setCenter(this.coordinates, 15);
			this.map_zoom = this.map.getZoom();
			this.marker = new GMarker(this.coordinates);
			this.map.addOverlay(this.marker);
			//			
			this.info_win = new Element('div', {
				'styles': {
					'float':'left'
				}
			});
			this.link_test = new Element('a', {
				'styles': {
					'float':'left',
					'font-size':'1em'
				},
				'href': 'javascript:void(0)',
				'html': 'Calcola il percorso'
			});
			this.form_box = new Element('div', {
				'styles': {
					'float':'left',
					'overflow':'hidden',
					'height':0
				}
			});
			this.form_input = new Element('input', {
				'styles': {
					'float':'left',
					'font-size':'1em'
				},
				'type':'text',
				'value':'inserisci qui l\'indirizzo'
			});
			this.form_submit = new Element('input', {
				'styles': {
					'float':'left',
					'font-size':'1em'
				},
				'type':'button',
				'value':'cerca'
			});
			this.form_submit.panel = this;
			this.form_submit.addEvent('click', this.setDirections.bindWithEvent(this));
			this.form_box.adopt(this.form_input,this.form_submit);
			this.link_test.panel = this;
			this.link_test.addEvent('click', this.onsearchClick.bindWithEvent(this.link_test));
			//
			this.info_win.adopt(this.link_test,new Element('br',{'clear':'all'}),this.form_box);
			this.marker.openInfoWindowHtml(this.info_win);
			this.marker.bindInfoWindowHtml(this.info_win);
      	}
	}
	,setDirections:function () {
		if(this.form_input.value!="inserisci qui l\'indirizzo")
		var dir_str = "from:"+this.form_input.value+ " to: La nostra sede - via Biffi, Saronno@" + this.coordinates.lat()+","+this.coordinates.lng();
		this.gdir.load(dir_str,{ "locale": "it" });
    }
	,clearDirections: function(){
		this.gdir.clear();
		this.open();
	}
	,onAddoverlay: function(){		
		this.close();
		this.marker.closeInfoWindow();
		this.marker.hide();
	}
	,onResize : function(){
		if (this.rightPanel.getSize().x != 0) {
			this.map.checkResize();
		}else{
			this.marker.show();
			this.marker.openInfoWindowHtml(this.info_win);
			this.map.checkResize();
			this.map.setZoom(this.map_zoom);
		}
		this.map.panTo((this.coordinates));
	}
	,onsearchClick :function(){
		var dim = (this.panel.form_box.getSize().y>0)?(0):(this.panel.form_submit.getSize().y+5);
		var txt = (this.panel.form_box.getSize().y>0)?('Calcola il percorso'):('Chiudi la ricerca');
		this.panel.form_box.tween('height', dim);
		this.set('html',txt);
	}
	,mapsHandleErrors:function (){
		switch (this.gdir.getStatus().code){
			case G_GEO_BAD_REQUEST:
			alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
			break;
			case G_GEO_SERVER_ERROR:
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + this.gdir.getStatus().code);
			break;
			case G_GEO_MISSING_QUERY:
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + this.gdir.getStatus().code);
			break;
			case G_GEO_UNKNOWN_ADDRESS:
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + this.gdir.getStatus().code);
			break;
			case G_GEO_UNAVAILABLE_ADDRESS:
			alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + this.gdir.getStatus().code);
			break;
			case G_GEO_UNKNOWN_DIRECTIONS:
			alert("Non è stato possibile calcolare il percorso tra il punto di partenza e "+this.form_input.value+" \n Error code: " + this.gdir.getStatus().code);
			break;
			case G_GEO_BAD_KEY:
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + this.gdir.getStatus().code);
			break;
			default:
			alert("An unknown error occurred.\n Error code: "+this.gdir.getStatus().code);
			break;
		}
	}
});
