var MapTour = new Class({
	Implements: [Options,Events],
	
	options:{
		divMap: 'mappaGoogle',
		baseIcon:'',
		markeri: Array(),
		eleLegenda:'maplist',
		creaLeg: true,
		eleLegDesc: Array(),
		centerLt:0,
		centerLg:0,
		zumme:8
	},

	initialize: function(options){
	       this.setOptions(options);
	},

	setMap: function(eles){
		prop = this;
		if (GBrowserIsCompatible()) {
			//icona per il marker
			this.options.baseIcon = new GIcon();
			this.options.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			this.options.baseIcon.iconSize = new GSize(20, 34);
			this.options.baseIcon.shadowSize = new GSize(37, 34);
			this.options.baseIcon.iconAnchor = new GPoint(9, 34);
			this.options.baseIcon.infoWindowAnchor = new GPoint(9, 2);
			this.options.baseIcon.infoShadowAnchor = new GPoint(18, 25);
			/////////////////////
			
			//mappa
			map = new GMap(document.getElementById(this.options.divMap));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			
			totali = eles.length;
			
			//aggiungo i marker
			eles.each(function(item,id){
				var ltlg = new GLatLng(item['lt'],item['lg']);
				var mark = prop.creaMarker(ltlg,id,item['desc']);
				map.addOverlay(mark);
				this.options.markeri[id] = mark;
				this.options.eleLegDesc[id] = item['luogo'];
				this.options.centerLt += item['lt'];
				this.options.centerLg += item['lg'];
			}.bind(prop));
			
			map.setCenter(new GLatLng((this.options.centerLt/totali).round(6),(this.options.centerLg/totali).round(6)),prop.options.zumme);
			
			//legenda
			if(this.options.creaLeg){
				this.creaLegenda();
			}
			
		}else{
			$(this.options.divMap).set('html','Sorry your browser is not compatible with GoogleMaps');
		}
	},

	creaMarker: function(point,index,desc){
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
		letteredIcon = new GIcon(this.options.baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(desc);
		});
		return marker;
	},

	clickMark: function(i){
		GEvent.trigger(this.options.markeri[i], "click");
	},

	creaLegenda: function(){
		prop = this;
		eleRes = $(this.options.eleLegenda);
		
		this.options.eleLegDesc.each(function(item,id){
			var letter = String.fromCharCode("A".charCodeAt(0) + id);
			var li = new Element('li');
			li.set('html','<a href="javascript:gmap.clickMark('+id+')">('+letter+') - '+item+'</a>');
			li.inject(eleRes);
		});
	}
});