/* Google maps */

window.onunload = function(){
	GUnload();
}

var accomm;
var map;
var mapIcon;
var marker;
var pr_address;
var s_address;

var iconNormal	= "http://maps.google.com/mapfiles/kml/pal2/icon20.png";
var iconHover	= "http://maps.google.com/mapfiles/kml/pal3/icon36.png";
var iconShadow	= "http://maps.google.com/mapfiles/kml/pal2/icon20s.png";

function make_address( country, city, street, house, y, x, selected )
{
	if (!selected) selected = false;
	$("#map-view").show();
	makeIcons();
	map = new GMap2( document.getElementById( "map" ) );
	var zoom = 14;

	var status = false;
	if ( x && y ) status = true;
	
	if ( y == null ) y = 47.558052;
	if ( x == null ) x = 7.58387;

	$("#f-geo").val( y + ',' + x );

	map.addControl( new GLargeMapControl() );
	map.addControl( new GMapTypeControl() );

	if (selected){
		pr_address = $('#'+house).val()+' '+$('#'+street).val()+' '+watch_text(city)+' '+watch_text(country);
		s_address = $('#'+street).val()+' '+watch_text(city)+' '+watch_text(country);
	} else {
		pr_address = house+' '+street+' '+city+' '+country;
		s_address = street+' '+city+' '+country;
	}
	
	var geocoder = new GClientGeocoder();
	$('#debug_window').append('<p><b>Google map &raquo; address: </b><u>'+pr_address+'</u></p>');
	geocoder.getLatLng(
		pr_address,
		function(point)
		{
			if ( !point )
			{
				$('#debug_window').append('<p><b>&nbsp;</b><u>not found, go to '+x+':'+y+'</u></p>');
				map.setCenter( new GLatLng(y, x), zoom );
				addMarker( new GLatLng(y, x), s_address, '<div><b>Address</b><br />' + s_address + '<div align="right"><a  href="javascript: void(0);" onclick="window.open( \'/view/{id}\', \'\', \'height=600,width=700,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no\' );">View details...</a></div></div>' );					
				
				return false;
			}
			else
			{
				$('#debug_window').append('<p><b>address found at</b><u> '+point+'</u></p>');
				//if ( status )
				//{
				//	map.setCenter( new GLatLng(y, x), zoom );
				//	addMarker( new GLatLng(y, x), s_address, '<div><b>Address</b><br />' + s_address + '<div align="right"><a  href="javascript: void(0);" onclick="window.open( \'/view/'+accomm+'\', \'\', \'height=600,width=700,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no\' );">View details...</a></div></div>' );					
				//}
				//else
				//{
					map.setCenter( point, zoom );
					addMarker( point, s_address, '<div><b>Address</b><br />' + s_address + '<div align="right"><a  href="javascript: void(0);" onclick="window.open( \'/view/'+accomm+'\', \'\', \'height=600,width=700,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no\' );">View details...</a></div></div>' );	
				//}
				
				return true;	
			}
		}
	);
}

function makeIcons(){
	mapIcon = new GIcon({
		image			: iconNormal,
		iconSize		: new GSize(32, 32),
		shadowSize		: new GSize(56,32),
		iconAnchor		: new GPoint(16, 32),
		infoWindowAnchor: new GPoint(16, 0)
	});
}

function addMarker( point, title, html ){
	marker = new GMarker(point, {icon: mapIcon, draggable: false, title: title});
	GEvent.addListener(marker, 'click', function(){
		marker.openInfoWindowHtml( html );
	});
	GEvent.addListener(marker, 'mouseover', function(){
		marker.setImage(iconHover);
	});
	GEvent.addListener(marker, 'mouseout', function(){
		marker.setImage(iconNormal);
	});
	map.addOverlay(marker);
}