var map;
var objectIcon;
var address = 'Kolbenova 50/510';
var toAddress = 'Kolbenova 50/510, 190 00 Praha 9, Česká republika';
var zoom = 14;
var geocoder;
var gdir;

function initIcon()
{
	objectIcon = new GIcon();
	objectIcon.image = "/_clip/pepsico-map-logo.png";
	objectIcon.iconSize = new GSize(126, 54);
	objectIcon.iconAnchor = new GPoint(63, 49);
	objectIcon.infoWindowAnchor = new GPoint(63, 1);
}

function addMarker(point)
{
	htmlAddr = '<div id="infownd">'
			+ '<h1>PepsiCo / General Bottlers CR s.r.o.</h1>'
			+ '<h2>Adresa:</h2>'
			+ '<p>Kolbenova 50/510<br />190 00 Praha 9<br />Česká republika</p>'
			+ '</div>';

//	htmlDirs = '<div>'
//			+ '<p>Odkud: <input type="text" id="from" /></p>'
//			+ '<p>Kam: <input type="text" id="to" value="'+toAddress+'" /></p>'
//			+ '<p><input type="button" value="Vyhledat trasu" onclick="searchDirections(); return false;" /></p>'
//			+ '</div>';


	htmlDirs = getDirsHtml();

	marker = new GMarker(point, {icon:objectIcon});
	GEvent.addListener(marker, "click", function() {
		//marker.openInfoWindowHtml(html);
		marker.openInfoWindowTabsHtml([new GInfoWindowTab('Adresa', htmlAddr), new GInfoWindowTab('Trasy',htmlDirs)]);
	});
	map.addOverlay(marker);
}

function initializeMap()
{
	if (GBrowserIsCompatible())
	{
		geocoder = new GClientGeocoder();
		initIcon();
		map = new GMap2(document.getElementById("map_holder"));
		gdir = new GDirections(map, document.getElementById("directions"));
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		showAddress(address);
		map.setUIToDefault();
		
	}
}

function showAddress(address) {geocoder.getLatLng(address, function(point) {if (!point) {alert(address + " not found");} else {addMarker(point);map.setCenter(point, zoom);}});}

function searchDirections()
{
	toAddress = document.getElementById('to').value;
	fromAddress = document.getElementById('from').value;
	how = document.getElementById('how').value;
	getDirections(fromAddress, toAddress, how);
}

function getDirections(fromAddress, toAddress, how)
{
	tm = getTravelMode(how);
	gdir.load("from: " + fromAddress + " to: " + toAddress, {travelMode:tm});
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

   else alert("An unknown error occurred.");

}

function getDirsHtml()
{
	//return '<p><table><tbody><tr><td>Odkud:</td><td><input type="text" value="" autocomplete="off" maxlength="2048" tabindex="3" title="Startovní adresa" name="from" id="from"/></td><td rowspan="2"><div title="Vyhledat obrácenou trasu" class="dir_rev_wrapper"><a href="javascript:void(0)" id="dir_rev" style="top: -9px;" onclick="reverseDirs()"><div style="width: 18px; height: 18px; overflow: hidden; position: relative;"><img style="position: absolute; left: 0px; top: -236px; border: 0px none; padding: 0px; margin: 0px;" src="http://maps.gstatic.com/intl/cs_ALL/mapfiles/dir/dl.png"/></div></a></div></td></tr><tr><td>Kam:</td><td><input type="text" value="'+toAddress+'" autocomplete="off" maxlength="2048" tabindex="3" title="Cílová adresa" name="to" id="to"/></td></tr><tr><td colspan="3"><div><select id="how"><option value="d">Autem</option><option value="r">Veřejnou dopravou</option><option value="w">Pěšky</option></select><input type="button" value="Vyhledat trasu" onclick="searchDirections(); return false;" /></div></td></tr></tbody></table></p>';
	return '<p><table><tbody><tr><td>Odkud:</td><td><input type="text" value="" autocomplete="off" maxlength="2048" tabindex="3" title="Startovní adresa" name="from" id="from"/></td><td rowspan="2"><div title="Vyhledat obrácenou trasu" class="dir_rev_wrapper"><a href="javascript:void(0)" id="dir_rev" style="top: -9px;" onclick="reverseDirs()"><div style="width: 18px; height: 18px; overflow: hidden; position: relative;"><img style="position: absolute; left: 0px; top: -236px; border: 0px none; padding: 0px; margin: 0px;" src="http://maps.gstatic.com/intl/cs_ALL/mapfiles/dir/dl.png"/></div></a></div></td></tr><tr><td>Kam:</td><td><input type="text" value="'+toAddress+'" autocomplete="off" maxlength="2048" tabindex="3" title="Cílová adresa" name="to" id="to"/></td></tr><tr><td colspan="3"><div><select id="how"><option value="d">Autem</option><option value="w">Pěšky</option></select><input type="button" value="Vyhledat trasu" onclick="searchDirections(); return false;" /></div></td></tr></tbody></table></p>';
}

function reverseDirs()
{
	to = document.getElementById('to').value;
	from = document.getElementById('from').value;

	document.getElementById('to').value = from;
	document.getElementById('from').value = to;
}

function getTravelMode(how)
{
	var tm = null;
	switch (how)
	{
		case 'd':
			tm = G_TRAVEL_MODE_DRIVING;
			break;
		case 'w':
			tm = G_TRAVEL_MODE_WALKING;
			break;
		case 'r': // unknown constant
		default:
			tm = G_TRAVEL_MODE_DRIVING;
			break;
	}
	return tm;
}