/*
*****************************************************
By Simon White, February 2007. Updated Dec 2008. 
This Javascript is based in part on code provided by
http://www.econym.demon.co.uk/googlemaps/
*****************************************************
*/

// format: x, y, simple_desc, price, log_desc, link

 var map = null; // for IE7

		// center map on middle of highest / lowest co-ords
		// initialise lowest / highest
		lowest_x = my_data1[0];
		lowest_y = my_data1[1];
		highest_x = my_data1[0];
		highest_y = my_data1[1];
		// loop through to find lowest / highest
		for (x=0; x<=my_data1.length; x=x+6) {
			point_x = parseFloat(my_data1[x]);
			point_y = parseFloat(my_data1[x+1]);
			if (point_x < lowest_x) lowest_x = point_x;
			if (point_x > highest_x) highest_x = point_x;
			if (point_y < lowest_y) lowest_y = point_y;
			if (point_y > highest_y) highest_y = point_y;	
		}
		// calculate center as middle of our rectangle  
		center_x = (highest_x - lowest_x)/2 + (+lowest_x);
		center_y = (highest_y - lowest_y)/2 + (+lowest_y);		
		x_width = highest_x - lowest_x;
		y_width = highest_y - lowest_y;

// start Google map stuff

var pin_counter = 0;
var ICON_PATH = "map_icons/";

// function to toggle active tab

function act_tab(type) {
	var tabs = new Array();
	tabs['hotel'] = document.getElementById("hotels_tab");
	tabs['resto'] = document.getElementById("restos_tab");
	tabs['theatre'] = document.getElementById("theatres_tab");
	tabs['evt'] = document.getElementById("events_tab");		

	for (var i in tabs) {
		my_cur_pane_obj = document.getElementById('info_pane_' + i);
		uscore = tabs[i].src.lastIndexOf('_');
		tabState = tabs[i].src.substring(uscore);
		tabPrefix = tabs[i].src.substring(0,uscore);
		if (i == type) {
			my_cur_pane_obj.style.display = 'block';
			if (tabState == '_off.gif') {
				tabs[i].src = tabPrefix + '_on.gif';
				}
//			alert(tabPrefix + '::' + tabState);
			}
		else {
			my_cur_pane_obj.style.display = 'none';			
			if (tabState == '_on.gif') {
				tabs[i].src = tabPrefix + '_off.gif';
				}
			}
	}
}

// function to toggle display of markers

function toggle(clicked) {
	// clicked contains checkbox with cat. to show/hide
	data_type = clicked.name;
	data_value = eval(clicked.value);
	data_selected = clicked.checked;

	if (data_selected) {
		// alert ('add_data(' + data_value + ',' +  data_type + ')');
		add_data(data_value, data_type);
		}
	else {
		// alert ('remove_data(' +  data_type + ')');
		remove_data(data_type);
	}	
}

// function to create markers
function add_data(my_data, data_type) {
	for (x=0; x < my_data.length; x=x+6) {
		point_lat = parseFloat(my_data[x]);
		point_lon =  parseFloat(my_data[x+1]);
		add_point = false;	
		if (data_type!='resto' && data_type!='theatre' && data_type!='monument') { // it's not ents
			point = new GLatLng(point_lat, point_lon);
			if (data_type=='evt') {
  				if ((point_lat >= lowest_x && point_lat <= highest_x) && (point_lon >= lowest_y && point_lon <= highest_y)) {
    				station_name = '<div class="ent_info"><strong>' + my_data[x+2] + '</strong><br /> ' + my_data[x+4] + '</div>';
    				station_name += "<div class=\"clear\"></div>";
    				hotel_link = '';
    				add_point = true;			
				}	
			}
			if (data_type=='station') {
				station_name = "<span class=\"station_name\">" + unescape(my_data[x+2].replace(/\+/g,  " ")) + "</span>";
				station_name += "</a><div class=\"station_price\">&agrave; partir de: <strong>" + my_data[x+3] + "&nbsp;&euro;</strong></div>";
				station_name += "<br style=\"clear:both\" />";				
				hotel_link = "<br /><span class=\"ent_info\">" + my_data[x+4] + "</span>";
				hotel_link += "<div><a class=\"my_link\" href=\"" + my_data[x+5] + "\">";
				hotel_link += "voir les offres dans cette station</a></div>";
				add_point = true;
			}
		}
		
		if (data_type=='resto' || data_type=='theatre') { // it is ents
			if ((point_lat >= lowest_x && point_lat <= highest_x) && (point_lon >= lowest_y && point_lon <= highest_y)) {
				point = new GLatLng(point_lat, point_lon);
				station_name = "<span class=\"station_name\">" + unescape(my_data[x+2].replace(/\+/g,  " ")) + "</span>";
				station_name += "<div class=\"station_price\">&agrave; partir de: <strong>" + my_data[x+3] + "&nbsp;&euro;</strong></div>";
				station_name += "<br style=\"clear:both\" />";				
				hotel_link = "<div><a class=\"my_link\" href=\"" + my_data[x+5] + "&intcmp=maps_arbo\">plus d\'infos</a></div>";
				add_point = true;
			}
			else {
				add_point = false;
			}
		}
		
		if (data_type=='monument') { // it is a monument
			if ((point_lat >= lowest_x && point_lat <= highest_x) && (point_lon >= lowest_y && point_lon <= highest_y)) {
				point = new GLatLng(point_lat, point_lon);
				station_name = my_data[x+2];			
				hotel_link = '';
				add_point = true;
			}
			else {
				add_point = false;
			}			
		}		

		if (add_point) { // ok to add the marker
			map.addOverlay(createMarker(point, pin_counter, station_name, hotel_link, data_type));
			pin_counter++;
		}
	}
}


    function load() {	
	
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		// initialise vars
		info_pane_html_stations = "<h3>Liste des stations</h3>";
		set_markers = new Array();
		set_markers_content = new Array();
		set_markers['station'] = new Array();
		set_markers_content['station'] = new Array();
		
		// add controls
		map.addControl(new GLargeMapControl());
		map.addControl(new GHierarchicalMapTypeControl());
		map.addControl(new GScaleControl());
		map.addControl(new GOverviewMapControl());
		map.addMapType(G_PHYSICAL_MAP); // add terrain mapping
		map.setMapType(G_PHYSICAL_MAP); // and set as default
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		
		// set NE and SW points of lowest / highest coords
		// DEBUG:
		// alert ("lowest x: (" + lowest_x + ") highest x: (" + highest_x + ") lowest y: (" + lowest_y + ") highest y: (" + highest_y + ")"); 
		my_southwest = new GLatLng(lowest_x, lowest_y);
		my_northeast = new GLatLng(highest_x, highest_y);
		// create Google API bounds (a rectangle that exactly contains all points)
		my_bounds = new GLatLngBounds(my_southwest, my_northeast);
		// find required zoom level to display all points in one map
		zoom_required = map.getBoundsZoomLevel(my_bounds);

		// DEBUG:
		// alert ("center_x: (" + center_x + ") center_y: (" + center_y + ")");

		// create map by setting center
        map.setCenter(new GLatLng(center_x, center_y), zoom_required);
		
		// add them fellas

		add_data(my_data1, 'station'); // LMN hotels
		
		document.getElementById("info_pane_stations").innerHTML = info_pane_html_stations;
      }
	  
      // Add a move listener to restrict the bounds range ?
      /* GEvent.addListener(map, "move", function() {
        checkBounds();
      }); */

	  
	  // add listener to change zoom if click on infowindow
	  GEvent.addListener(map, "infowindowprepareopen", function() {
	  	current_zoom = map.getZoom();
//		alert(current_zoom);
		// this will change zoom levels if reqd.
		/* if (current_zoom < 10) {
			map.setZoom(10);
			}; */
	  });
  
	  
      // The allowed region which the whole map must be within
	  max_x = highest_x + x_width;
	  max_y = highest_y + y_width;
	  min_x = lowest_x - x_width;
	  min_y = lowest_y - y_width;

      var allowedBounds = new GLatLngBounds(new GLatLng(min_x,min_y), new GLatLng(max_x,max_y));
      
      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
      }
	  
	}
	
	function setGeoRegion(lat,lon) {
		map.setZoom(8);
		map.setCenter(new GLatLng(lat,lon));
	}
	
	function click_pane(i, type) {
		set_markers[type][i].openInfoWindowHtml(set_markers_content[type][i], {maxWidth:250});
		}

	function createMarker(point, number, name, my_link, type) {
		my_icon = new GIcon();
		my_icon.iconSize = new GSize(17, 19);
		my_icon.iconAnchor = new GPoint(6, 6);
		my_icon.shadow = ICON_PATH + "shadow-lightblue.png";
		my_icon.infoWindowAnchor = new GPoint(10, 10);
		
		if (type == 'evt') {
		my_icon.image = ICON_PATH + "marker_evt2.png"; 
		var marker = new GMarker(point, my_icon);
		set_markers[type][number] = marker;
		set_markers_content[type][number] = name;
		info_pane_html_events += '<a href="javascript:click_pane(' + number + ',\'' + type + '\')">' + name + '</a><br style="clear:both" />';
		} 

		if (type == 'station') {
		// my_icon.image = ICON_PATH + "ski.png"; 
		my_icon.image = ICON_PATH + "lightblue" + (number+1) + ".png"; 
		var marker = new GMarker(point, my_icon);
		set_markers[type][number] = marker;
		set_markers_content[type][number] = name + my_link;
		info_pane_html_stations += '<a class="map_click" href="javascript:click_pane(' + number + ',\'' + type + '\')"><img class="icon_number" src="' + ICON_PATH + 'lightblue' + (number+1) + '.png" /></a> <a class="map_click" href="javascript:click_pane(' + number + ',\'' + type + '\')">' + name + '<br style="clear:both" />'; 
		}

		if (type == 'resto') {
		my_icon.image = ICON_PATH + "marker_resto2.png";
		var marker = new GMarker(point, my_icon);
		set_markers[type][number] = marker;
		set_markers_content[type][number] = name + my_link;
		info_pane_html_restos += '<a class="map_click" href="javascript:click_pane(' + number + ',\'' + type + '\')">' + name + '</a><br style="clear:both" />';
		}

		if (type == 'theatre') {
		my_icon.image = ICON_PATH + "marker_theatre2.png"; 
		var marker = new GMarker(point, my_icon);
		set_markers[type][number] = marker;
		set_markers_content[type][number] = name + my_link;
		info_pane_html_theatres += '<a class="map_click" href="javascript:click_pane(' + number + ',\'' + type + '\')">' + name + '</a><br style="clear:both" />';
		}

		if (type == 'monument') {
		my_icon.image = ICON_PATH + "marker_monument2.png"; 
		var marker = new GMarker(point, my_icon);
		set_markers[type][number] = marker;
		set_markers_content[type][number] = name;
		}

		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(name + my_link, {maxWidth:250});
		// if (type!="monument") { act_tab(type); }
		});
		  return marker;
	}
	
	function remove_data(data_type) {
		// set_markers[type][number] => contains all markers
		for (var z in set_markers[data_type]) {
			map.removeOverlay(set_markers[data_type][z]);
		}
	}
	
	function re_center() {
		map.setCenter(new GLatLng(center_x, center_y), zoom_required);
	}