//<![CDATA[ 

/*
* GMaps_load: Load map structure.
*/
var map;
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;

function GMaps_load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GSmallMapControl());
	}
}

/*
* GMaps_showAddress: Send a request to google with a formated address ("Street Number" Street, City, State)
* and receive the latitude and longitude of this address. It needs a callback function since it uses AJAX.
*/
function Gmaps_showAddress(address, html, debug, force_search) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(point) { GMaps_displayMarker(point, address, html, debug, force_search) });
}

/*
* GMaps_getMarker: retrieve a marker. Type can be normal (red) or highlighted (yellow).
*/
function GMaps_getMarker(point, type) {
	var icon     = new GIcon();
	var img_path = document.getElementById('gm_image_path').value;

	if(type == 'normal')
		icon.image = img_path+'/icon_gm_marker_red.png';
	else if(type == 'highlighted')
		icon.image = img_path+'/icon_gm_marker_yellow.png';

	icon.shadow           = img_path+'/icon_gm_marker_red_shadow.png';
	icon.iconSize         = new GSize(12, 20);
	icon.shadowSize       = new GSize(22, 20);
	icon.iconAnchor       = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	return new GMarker(point, icon);
}


  function createMarker(point,name,html) {
    var marker = GMaps_getMarker(point, 'normal')
     // The info window version with the "to here" form open
    to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
       '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
       '<input type="text" SIZE=25 MAXLENGTH=40 name="saddr" id="saddr" value=""  style="border:1px solid #7F9DB9;" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT" style="font:12px normal Arial,Helvetica,sans-serif;">' +
       '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
              // "(" + name + ")" + 
       '"/>';
    // The info window version with the "to here" form open
    from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
       '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
       '<input type="text" SIZE=25 MAXLENGTH=40 name="daddr" id="daddr" value="" style="border:1px solid #7F9DB9;" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT" style="font:12px normal Arial,Helvetica,sans-serif;">' +
       '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
              // "(" + name + ")" + 
       '"/>';
    // The inactive version of the direction info
    html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
     GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
 }

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
 
/*
* GMaps_displayMarker: Add marker to the map.
*/
function GMaps_displayMarker(point, address, html, debug, force_search){
	if (!point) {
		/* address not found => hide map and show message */
		div_map       = document.getElementById('map');
		//div_map_error = document.getElementById('map_error');
		//div_map.style.display = "none";
		if (debug == "on") {
			alert("GMap point not found for this address");
		}
        
        if (force_search==0) Gmaps_showAddress(address, html, debug, 1);
        if (force_search==1) Gmaps_showAddress(address, html, debug, 2);
        
	    //document.getElementById("map_error").style.display = "block";
	    //document.getElementById("map_error").innerHTML += "<p class=\"warning\">"+address+"</p>";
        
	} else {
		map.setCenter(point, 10);
		html_address = "<b>Address:</b><br>";
		html_address += address;
      	var marker = createMarker(point,'',html_address)
      	map.addOverlay(marker);
	}
}

/*
* GMaps_showMouseOver: Action performed when mouse is over the observed marker.
*/
function GMaps_showMouseOver (html){
	GMaps_enablePopupLayer(html);
}

/*
* GMaps_showMouseOut: Action performed when mouse was over and move out the observed marker.
*/
function GMaps_showMouseOut (){
	GMaps_disablePopupLayer();
}

document.onmousemove = GMaps_captureMousePosition;
var xMousePos = 0; // Horizontal position of the mouse on the screen
var yMousePos = 0; // Vertical position of the mouse on the screen

/*
* GMaps_captureMousePosition: Retrieve x and y coordinates for actual mouse position.
*/
function GMaps_captureMousePosition(e) {
	if (document.layers) { // Netscape 4 but honestly who cares at this point
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	} else if (document.all) { // IE
		xMousePos = window.event.clientX+document.body.scrollLeft;
		yMousePos = window.event.clientY+document.body.scrollTop;
	} else if (document.getElementById) { // Netscape Mozilla
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	}
}

/*
* GMaps_enablePopupLayer: Display a pop up div over mouse position.
*/
function GMaps_enablePopupLayer(html){
	var float_layer = document.getElementById('float_layer');
	float_layer.style.visibility = 'visible';
	//float_layer.style.left = (xMousePos - (float_layer.offsetWidth+250));
	//float_layer.style.top = (yMousePos - (float_layer.offsetHeight+250));
	float_layer.innerHTML = html;
}

/*
* GMaps_disablePopupLayer: Hide the pop up div.
*/
function GMaps_disablePopupLayer(){
	var float_layer = document.getElementById('float_layer');
	float_layer.style.visibility = 'hidden';
	float_layer.innerHTML = '';
}
//]]>