var s_marker;
var d_marker;

var source_addr;
var destination_addr;
var action_url;
var directions;
var geocoder = null;

var config = {
	"lang_id": 1,
	"showTotalOnly": false
};

function mapsLoaded() {

	if (GBrowserIsCompatible())
	{
		if (config.maps.field)
		{
			config.maps.map = new google.maps.Map2(config.maps.field);
			config.maps.map.enableScrollWheelZoom();
			config.maps.map.addControl(new GSmallMapControl());
			config.maps.map.addControl(new GScaleControl());
			config.maps.map.addControl(new GMapTypeControl());

			if ((config.maps.settings.latitude) &&
				(config.maps.settings.longitude) &&
				(config.maps.settings.zoom))
			{
				config.maps.map.setCenter(
					new google.maps.LatLng(
						config.maps.settings.latitude,
						config.maps.settings.longitude
					),
					config.maps.settings.zoom
				);
			}
			config.maps.loaded = true;
		}
	}

	initialize();
}

function loadMaps() {
	if (config.maps.field)
	{
		google.load("maps", "2.x", {"callback" : mapsLoaded});
	}
}

function initMapLoader() {
	var script = document.createElement("script");
	script.src = "http://www.google.com/jsapi?key=" + config.maps.key + "&callback=loadMaps";
	script.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(script);
}

function initDirection(newConfig)
{

	if (!config.lang_id)
	{
		config.lang_id = 1;
	}

	if (!config.maps)
	{
		config.maps = {};
	}

	config.maps.init = false;
	config.maps.loaded = false;

	if (typeof newConfig == "object")
	{
		if (!newConfig.lang_id)
		{
			newConfig.lang_id = config.lang_id;
		}

		config = newConfig;

		if (!config.maps)
		{
			config.maps = {};
		}

		config.maps.init = false;
		config.maps.loaded = false;
		if ((config.maps.key) && (config.maps.field))
		{
			if (!config.maps.settings)
			{
				config.maps.settings = {
				};
			}
			initMapLoader();
			config.maps.init = true;
		}
	}
}

function setDirection()
{
	config.maps.map.clearOverlays();

	directions = new GDirections(config.maps.map);
	GEvent.addListener(directions, "addoverlay", onGDirectionsShow);
	GEvent.addListener(directions, "error", handleErrors);
	directions.clear();
	//alert("from: "+source_addr+" to: "+destination_addr);
	directions.load("from: "+source_addr+" to: "+destination_addr);
}

function onGDirectionsShow(D)
{
	dest_marker = directions.getMarker(1);

	d_point = dest_marker.getLatLng();
	dest_marker.hide();
	var markerIcon = new GIcon();
	markerIcon.image = "/images/icons/marker_red.png";
	markerIcon.shadow = "/images/icons/marker_shadow.png";
	markerIcon.iconSize = new GSize(32, 32);
	markerIcon.shadowSize = new GSize(59, 32);
	markerIcon.iconAnchor = new GPoint(15, 32);
	markerOptions = {"icon": markerIcon, "title": destination_addr, "clickable": true}
	d_marker = new GMarker(d_point, markerOptions);
	dest_marker.hide();

	GEvent.addListener(d_marker, "click", function(coords){
			config.maps.map.openInfoWindowHtml(coords, jQuery("#addres_box").html());
	});

	config.maps.map.addOverlay(d_marker);


	source_marker = directions.getMarker(0);
	s_point = source_marker.getLatLng();
	source_marker.hide();
	var markerIcon = new GIcon();
	markerIcon.image = "/images/icons/marker_green.png";
	markerIcon.shadow = "/images/icons/marker_shadow.png";
	markerIcon.iconSize = new GSize(32, 32);
	markerIcon.shadowSize = new GSize(59, 32);
	markerIcon.iconAnchor = new GPoint(15, 32);
	markerOptions = {icon: markerIcon, title: source_addr, "clickable": true};
	s_marker = new GMarker(s_point, markerOptions);
	source_marker.hide();

	GEvent.addListener(s_marker, "click", function(coords){
			config.maps.map.openInfoWindowHtml(coords, D.A.Placemark[0].address + " " + D.A.Placemark[0].AddressDetails.Country.CountryNameCode);
	});

	config.maps.map.addOverlay(s_marker);

	if (source_addr != destination_addr)
	{
		var f = jQuery("#info_direction");
		//alert(action_url);
		f.find("#link_direction").attr("href", action_url);
		f.slideDown(1000);
	}
}

function hideInfos()
{
	jQuery("#info_direction").slideUp(1000);
	jQuery("#error602").slideUp(1000);
	jQuery("#error500").slideUp(1000);
	jQuery("#error601").slideUp(1000);
	jQuery("#error603").slideUp(1000);
	jQuery("#error610").slideUp(1000);
	jQuery("#error400").slideUp(1000);
	jQuery("#errorUnknown").slideUp(1000);
}

function handleErrors()
{
	hideInfos()
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		jQuery("#error602").slideDown(1000)
	else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
		jQuery("#error500").slideDown(1000)
	else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
		jQuery("#error601").slideDown(1000)
	else if (directions.getStatus().code == G_UNAVAILABLE_ADDRESS)
		jQuery("#error603").slideDown(1000)
	else if (directions.getStatus().code == G_GEO_BAD_KEY)
		jQuery("#error610").slideDown(1000)
	else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
		jQuery("#error400").slideDown(1000)
	else jQuery("#errorUnknown").slideDown(1000);
}

function showAddress(address) {
	config.maps.map.clearOverlays();
	if (geocoder) {
		geocoder.getLatLng(
		address,
			function(point) {
				if (!point) {
					//alert(address + " not found");
				} else {
					config.maps.map.setCenter(point, 13);
					var marker = new GMarker(point);
					config.maps.map.addOverlay(marker);
					marker.bindInfoWindowHtml(jQuery("#addres_box").html());
					//marker.openInfoWindowHtml(jQuery("#addres_box").html());
				}
			}
		);
	}
}

function updateDirection()
{
	hideInfos();
	//var fields = getFormData(jQuery('#direction_form')[0]);
	//destination_addr = jQuery(fields.fields['daddr']).val();
	destination_addr = $('#daddr').val();
	//source_addr = jQuery(fields.fields['saddr']).val();
	source_addr = $('#saddr').val();
	hl = $('#direction_form input[name=hl]').val();
	//action_url = fields.action + "?" + fields.request;
	action_url = $('#direction_form').attr('action')+"?saddr="+source_addr+"&daddr="+destination_addr+"&hl="+hl;
	if (source_addr == config.default_text)
	{
		source_addr = destination_addr;
		showAddress(destination_addr);
	} else {
		setDirection();
	}
}

function initialize()
{
	geocoder = new GClientGeocoder();
	//var fields = getFormData(jQuery('#direction_form')[0]);
	//destination_addr = jQuery(fields.fields['daddr']).val();
	destination_addr = $('#daddr').val();
	source_addr = destination_addr;
	showAddress(destination_addr);
}

