//<![CDATA[ 
var external_location = "";
/*
* GMaps_load: Load map structure.
*/
var map;
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, OSInfo) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(point) { GMaps_displayMarker(point, address, html, debug, OSInfo) });
}
        
/*
* 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.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);
}

/*
* GMaps_displayMarker: Add marker to the map.
*/
function GMaps_displayMarker(point, address, html, debug, OSInfo){
    
	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");
		}
		div_map_error.style.display = "block";
		div_map_error.innerHTML = "<p class=\"warning\">Google Map is not available for the current address.</p>";
	} else {
		map.setCenter(point, 15);
		marker = GMaps_getMarker(point, 'normal');
		if (html) {
			GEvent.addListener(marker,  'mouseover',  function() { GMaps_showMouseOver(html, OSInfo) } );
            GEvent.addListener(marker,  'mouseout',  function() { GMaps_showMouseOut() } );
			GEvent.addListener(marker,  'click',  function() { window.open(external_location) } );
		}
		map.addOverlay(marker);
	}
}

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

/*
* 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) {
    
    /** this is only for results page **/ 
    if (document.location.pathname.indexOf("/w/") != -1 || document.location.pathname.indexOf("/k/") != -1 || document.location.pathname.indexOf("/n/") != -1) {
	    if (document.layers) { // Netscape 4 but honestly who cares at this point
		    xMousePos = e.pageX;
		    yMousePos = e.pageY;
	    } else if (document.all) { // IE
            /** this is e.clientX/e.clientY Mozilla equivalent **/
		    xMousePos = window.event.clientX + document.body.scrollLeft;
		    yMousePos = window.event.clientY + document.body.scrollTop;
	    } else if (document.getElementById) { // Mozilla
            xMousePos = e.clientX;
            yMousePos = e.clientY;
	    }  
    } else {          
        /** this is only for detail page **/ 
        if (document.layers) { // Netscape 4 but honestly who cares at this point
            xMousePos = e.pageX;
            yMousePos = e.pageY;
        } else if (document.all) { // IE
            /** this is e.pageX/e.pageY Mozilla equivalent **/
            xMousePos = window.event.clientX + document.documentElement.scrollLeft;
            yMousePos = window.event.clientY + document.documentElement.scrollTop;
        } else if (document.getElementById) { // Mozilla
            xMousePos = e.pageX;
            yMousePos = e.pageY;
        }  
    }
    
}

/*
* GMaps_enablePopupLayer: Display a pop up div over mouse position.
*/
function GMaps_enablePopupLayer(html, OSInfo){
    
	var float_layer = document.getElementById('float_layer');
    
    var is_windows_vista = (OSInfo.indexOf("NT 6") != -1);
    
    /** Windows XP **/
    var resultsOffSetXPx = 1365;
    var resultsOffSetXPy = 260;
    var detailOffSetXPx  = 1220;
    var detailOffSetXPy  = 730;

    resultsDiffx = 200;
    resultsDiffy = 0;
    detailDiffx  = 200;
    detailDiffy  = 0;
    
    /** Windows Vista **/
    var resultsOffSetVistax = resultsOffSetXPx - resultsDiffx;
    var resultsOffSetVistay = resultsOffSetXPy - resultsDiffy;
    var detailOffSetVistax  = detailOffSetXPx  - detailDiffx;
    var detailOffSetVistay  = detailOffSetXPy  - detailDiffy;

    if (is_windows_vista) {
        /** this is only for results page **/ 
        if (document.location.pathname.indexOf("/w/") != -1) {
            var left = (xMousePos - resultsOffSetVistax);
            var top  = (yMousePos - resultsOffSetVistay);
        /** this is only for detail page **/
        } else {
            var left = (xMousePos - detailOffSetVistax);
            var top  = (yMousePos - detailOffSetVistay);
        }
    } else {
        /** this is only for results page **/ 
        if (document.location.pathname.indexOf("/w/") != -1) {
            var left = (xMousePos - resultsOffSetXPx);
            var top  = (yMousePos - resultsOffSetXPy);
        /** this is only for detail page **/
        } else {
            var left = (xMousePos - detailOffSetXPx);
            var top  = (yMousePos - detailOffSetXPy);
        }
    }
    
    //showCities is define on header.php
    if (showCities) {
        left = left - 140; 
        top  = top  + 475; 
    }
    
	float_layer.style.visibility = 'visible';
    float_layer.style.position = 'absolute';
    float_layer.style.left = left+"px";
    float_layer.style.top = top+"px";
	float_layer.innerHTML = html.replace(/\\'/g, '"');
    
}

/*
* 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 = '';
}

//]]>