var oustaouMap = new Class ({

	map: {},

	pois: [],
	
	options: {
	},

	initialize: function() {
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById("map"));
			this.map.setCenter(new GLatLng(44.286944, 3.895722), 9);
        		this.map.addControl(new GLargeMapControl());
        		this.map.addControl(new GMapTypeControl());
        		this.map.addControl(new GOverviewMapControl());
        		this.map.setMapType(G_HYBRID_MAP);
			this.gdir = new GDirections(this.map, document.getElementById("directions"));
			GEvent.addListener(this.gdir, "load", this.onGDirectionsLoad);
        		GEvent.addListener(this.gdir, "error", this.onGErrors);
			this.getPois();
		}
	},

	addIcon: function(obj) {
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.iconSize = new GSize(13, 13);
		baseIcon.shadowSize = new GSize(0, 0);
		baseIcon.image = "img/map/icons/"+obj.icon;
		var markerOptions = { icon: baseIcon };
		var marker = new GMarker(new GLatLng(obj.lat, obj.lon), markerOptions);
		// Popup
		var outp = '<table border="0"><tr><td>';
		if ($defined(obj.image)) {
			outp+='<div style="border:1px solid #000000"><img src="img/map/'+obj.image+'" /></div>';
		}
		outp += '</td><td valign="center">';
		if ($defined(obj.name)) {
			outp+='<b>'+obj.name+'</b><br>';
		}
		if ($defined(obj.description)) {
			outp+='<div style="font-size:11px;width:170px;">'+obj.description+'</div><br>';
		}
		if ($defined(obj.address)) {
			outp+='<div style="font-size:12px;width:170px;">'+obj.address+'</div>';
		}
		if ($defined(obj.tel)) {
			outp+='<div style="font-size:12px;width:170px;">'+obj.tel+'</div>';
		}
		if ($defined(obj.mail)) {
			outp+='<div style="font-size:12px;width:170px;"><a href="mailto:'+obj.mail+'">'+obj.mail+'</a></div>';
		}
		if ($defined(obj.web)) {
			outp+='<div style="font-size:12px;width:170px;"><a href="'+obj.web+'" target="_blank">'+obj.web+'</a></div>';
		}
		outp += '</td></tr></table>';
		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(outp);
		});
		this.map.addOverlay(marker);
	},

	setDirections: function(from,to,locale) {
		this.gdir.load("from: " + from + " to: " + to, { "locale": locale });		
	},

	getPois: function(type) {
		var jSonRequest = new Json.Remote("pois.json", {
			onComplete: function(resp) {
				for(var i=0;i<resp.length;i++) {
					this.addIcon(resp[i]);
				}
			}.bind(this)
		}).send();
	},

	onGDirectionsLoad: function(obj) {
		$('directions').setHTML('');
		$('directions').setStyle('height','400px');
		$('directions').setStyle('border','1px solid #000000');
		window.scrollTo(0,550);
		//$('printDirections').setStyle('visibility','visible');
	},

	onGErrors: function(err) {
		$('directions').setHTML('Veuillez pr&eacute;ciser votre recherche');
	}
});

window.addEvent("domready", function() {
	var om = new oustaouMap;	
	$('submitDirections').addEvent("click", function(e) {
		om.setDirections($('fromAddress').value, $('toAddress').value, $('locale').value);
	});
	$('printDirections').addEvent("click", function(e) {
		$('directions').setStyles({position: 'absolute',top: '0px', left: '0px', width: '100%', height: '100%'});
	});
});

oustaouMap.implement(new Events, new Options);
