// pragma page ""
// pragma page "Global::Function::createMarker"
////////////////////////////////////////////////////////////////
//
// Create map market object
//
////////////////////////////////////////////////////////////////
function createMarker(
  vPt
, vText
)
{
	var vMrkr = new GMarker( vPt );

	GEvent.addListener( vMrkr , "click" , function()
	{
		vMrkr.openInfoWindowHtml( vText );
	} );

	return vMrkr;
}


// pragma page "Global::Function::destroyMapDirections"
////////////////////////////////////////////////////////////////
//
// Destroy map and directions objects
//
////////////////////////////////////////////////////////////////
function destroyMapDirections(
)
{
	GUnload();
}

// pragma page "Global::Function::initMapDirections"
////////////////////////////////////////////////////////////////
//
// Initialize map and directions objects
//
////////////////////////////////////////////////////////////////
function initMapDirections(
  vMap
, vDir
, vTo
, sText
)
{
	gTo   = vTo;
	gText = sText;

	if( GBrowserIsCompatible() ) 
	{      
        gMap = new GMap2      (        vMap );
        gDir = new GDirections( gMap , vDir );

		//
		// Register error event listeners
		//
        GEvent.addListener( gDir , "error"      , onErrorDirections );
        GEvent.addListener( gDir , "addoverlay" , onLoadDirections  );

		//
		// Add map controls
		//
		gMap.addControl( new GSmallMapControl()	);

		//
		// Center on location
		//
		gMap.setCenter( gTo , 13 );

		//
		// Set key points on map
		//
		gMrkr = new createMarker( gTo
							    , gText
							    );

		gMap.addOverlay( gMrkr );

		gMrkr.openInfoWindowHtml( gText );
	}
}

// pragma page "Global::Function::onErrorDirections"
////////////////////////////////////////////////////////////////
//
// Catch error directions event
//
////////////////////////////////////////////////////////////////
function onErrorDirections()
{
	if( gDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
	{
		alert( "From Location Error:\r\nNo corresponding geographic location could be found for the specified from address. This may be due to the address being relatively new, or it may have been incorrectly entered." );
	}
	else 
	{
		//
		// gDir.getStatus().code == G_GEO_SERVER_ERROR
		// gDir.getStatus().code == G_GEO_MISSING_QUERY
		// gDir.getStatus().code == G_GEO_BAD_KEY
		// gDir.getStatus().code == G_GEO_BAD_REQUEST
		//
		alert( "System Error:\r\nWe were unable to process your request for directions at this time. Please retry your request." );
	}
}

// pragma page "Global::Function::onLoadDirections"
////////////////////////////////////////////////////////////////
//
// Catch load directions event
//
////////////////////////////////////////////////////////////////
function onLoadDirections()
{
	gMap.removeOverlay( gMrkr );
	
	gMrkr = gDir.getMarker( 1 ) 
	
	gMrkr.openInfoWindowHtml( gText );
} 

// pragma page "Global::Function::setDirections"
////////////////////////////////////////////////////////////////
//
// Set directions with user data
//
////////////////////////////////////////////////////////////////
function setDirections(
  sFrom
, sTo
, sLcle
) 
{
	gDir.load( "from: " + sFrom + " to: " + sTo , { "locale" : sLcle } );
}

// pragma page "Global::Function::Declarations"
////////////////////////////////////////////////////////////////
//
// Global variables
//
////////////////////////////////////////////////////////////////
var gMap;
var gDir;
var gMrkr;
var gText;
var gTo;

