// needs gulf_class_gmaps.js and mootools 1.11 
// edit by Marcel for website testing/implementation/presentation

var extendedClassGMaps = ClassGMaps.extend({

	initialize:function(options){
		
		this.parent( options);
		this.filling_station_info_array = new Array;
	
	},
	

	retrieve_data: function(responseXML) {

		if(!responseXML) {
  			return;
		}

		var filling_stations = responseXML.documentElement.getElementsByTagName('filling_station');
			
 		for(var i=0; i < filling_stations.length; i++) {
			
			var filling_station_info = new Array;
				
 			filling_station_info['filling_station_id'] = filling_stations[i].childNodes[0].firstChild.nodeValue; 
 			filling_station_info['filling_station_name'] = filling_stations[i].childNodes[1].firstChild.nodeValue;
 			filling_station_info['X_WGS84'] = filling_stations[i].childNodes[2].firstChild.nodeValue;
			filling_station_info['Y_WGS84'] = filling_stations[i].childNodes[3].firstChild.nodeValue;
		
			var fuel  = filling_stations[i].getElementsByTagName('fuel');				
		
			for(var j=0; j < fuel.length; j++) {
				var fuel_type = fuel[j].firstChild.nodeValue;
				if(fuel_type) {
					filling_station_info[fuel_type] = true;
				}
			}	
		
			this.filling_station_info_array.push(filling_station_info);	
		}
		
   	},	
	

	retrieve_markers: function(fuel_type) {

		if(!this.map) {
  			return false;
		}
	
		this.map.clearOverlays(); 

 		for(var i=0; i < this.filling_station_info_array.length; i++) {

			if(this.filling_station_info_array[i][fuel_type] || fuel_type == "ALL") {
 			
				var filling_station_id = this.filling_station_info_array[i]['filling_station_id'];
 				var filling_station_name = this.filling_station_info_array[i]['filling_station_name'];
 				var X_WGS84 	= this.filling_station_info_array[i]['X_WGS84'];
				var Y_WGS84 	= this.filling_station_info_array[i]['Y_WGS84'];
				var marker = this.create_marker(new GLatLng(Y_WGS84, X_WGS84));			
			
				marker.id = filling_station_id;


				var tooltip = new Tooltip(marker, filling_station_name , 140);
				tooltip.initialize(this.map);
				tooltip.redraw(true);
				marker.tooltip = tooltip;
		
				GEvent.addListener(marker, 'mouseover', function() {
					this.tooltip.show();
				});
		
				GEvent.addListener(marker, 'mouseout', function() {
					this.tooltip.hide();
				});
			
				GEvent.addListener(marker, 'click', function() {
					//window.location ='js/test.php?section=brandstofprijzen&filling_station_id=' + this.id  + '&m=1&l=nl' ;
				});

				this.map.addOverlay(marker);
			}
		}  
	},


	get_fuel_type: function(fuel_type) {

		var str = "";
 	
		for(var i=0; i < this.filling_station_info_array.length; i++) {
					
			if(this.filling_station_info_array[i][fuel_type]  || fuel_type == "ALL") {
 				var id = this.filling_station_info_array[i]['filling_station_id'];
				var url = "js/test.php?section=brandstofprijzen&m=1&l=nl&filling_station_id=" + id; 
 				var filling_station_name 	= this.filling_station_info_array[i]['filling_station_name'];
				// str += "<a href='" + url + "'>" + filling_station_name + "</a><br />";
				str += filling_station_name + "<br/>";
			}
		}	
	
		var objPompstationBox = document.getElementById(this.options.pompstationbox);
		if(objPompstationBox) {
			objPompstationBox.innerHTML = str;	
		}
   	}

		
});




	extendedClassGMaps.implement(new Options);
	var objClassGMaps = null;




	function init() {

			objClassGmaps = new extendedClassGMaps({	map_div: "map",
									centerlatitude: 52.321910,
									centerlongitude: 5.3,
									gmapzoom: 7,
									gmaptypecontrol:true,
									gsmallmapcontrol:true,
									url: 'js/test.php',
									pompstationbox: 'pompstationbox'
								});


		objClassGmaps.create();
	    	
	   	var request = GXmlHttp.create();
	   	request.open('GET', objClassGmaps.options.url, true);
	   		request.onreadystatechange = function() {
			if(request.readyState == 4) {
				objClassGmaps.retrieve_data(request.responseXML);
				objClassGmaps.retrieve_markers("ALL");
				objClassGmaps.get_fuel_type("ALL");
			}
   		} 
		request.send(null);



	}	



 window.onload = init;
 window.onunload = GUnload;
 
