$(function()
{
	var map_info = $("td.google_map_info");
	
	if(map_info.length > 0)
	{
		var latlng = new google.maps.LatLng(intelli.config.googlemap_latitude * 1, intelli.config.googlemap_longtitude * 1);
		
		var myOptions =
		{
			zoom: intelli.config.googlemap_default_zoom * 1,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(document.getElementById("google_map"), myOptions);
		
		var bounds = new google.maps.LatLngBounds();
		var geocoder = new google.maps.Geocoder();
		var infowindows = new Array();
		var markers_count = map_info.length;
		
		map_info.each(function(i, v)
		{
			var id_listing = $(this).attr("id").replace('listing_', '');
			var address = $(this).children("input[name='address']").val();
			var city = $(this).children("input[name='city']").val();
			var state = $(this).children("input[name='state']").val();
			var zip = $(this).children("input[name='zip']").val();
			var country = $(this).children("input[name='country']").val();
			var title = $(this).children("input[name='title']").val();
			var description = $(this).children("input[name='description']").val();
			var url = $(this).children("input[name='url']").val();
	
			var full_address = address + ' ' + city + ', ' + state + ' ' + zip + ', ' + country;
	
			var lat = $(this).children("input[name='lat']").val();
			var lng = $(this).children("input[name='lng']").val();
			
			var zoom = $(this).children("input[name='zoom']").val();
	
			if('' != lat && 'undefined' != typeof lat && '' != lng && 'undefined' != typeof lng)
			{
				var html = '';
				var point =	new google.maps.LatLng(lat * 1, lng * 1);
	
				var marker = new google.maps.Marker({
					position: point, 
					map: map, 
					title: title
				});
				
				marker.setMap(map);
				
				bounds.extend(point);
				
				if('' != zoom)
				{
					map.setZoom(zoom * 1);
				}
				
				if (markers_count > 1)
					map.fitBounds(bounds);
				else
					map.setCenter(point);
				
				infowindows[i] = new google.maps.InfoWindow({
					content: getInfoWindowContent({url: url, title: title, description: description})
				});
				
				google.maps.event.addListener(marker, "click", function()
				{
					$.each(infowindows, function(a, b)
					{
						if(b)
						{
							b.close();
						}
					});
					
					infowindows[i].open(map, marker);
				});
				
				html = '<a href="#" id="googlemap_find_map_' + id_listing + '">';
				html += '<img title="'+ intelli.lang.googlemap_find_on_map + '" src="plugins/googlemap/templates/img/find_on_map.png" /></a>';	
				
				$("#tdlisting" + id_listing + " div.stat").append(html);
				
				$("#googlemap_find_map_" + id_listing).click(function()
				{
					map.panTo(point);
					
					$.each(infowindows, function(a, b)
					{
						if(b)
						{
							b.close();
						}
					});
					
					infowindows[i].open(map, marker);
					
					return false;
				});
			}
			else
			{
				if('' != full_address.split(' ').join('').replace(/\,/g, ''))
				{
					geocoder.geocode({'address': full_address}, function(results, status)
					{
						if(status == google.maps.GeocoderStatus.OK)
						{
							var marker = new google.maps.Marker({
								map: map, 
								position: results[0].geometry.location
							});
							
							bounds.extend(results[0].geometry.location);
							
							if('' != zoom)
							{
								map.setZoom(zoom * 1);
							}
							
							if (markers_count > 1)
								map.fitBounds(bounds);
							else
								map.setCenter(results[0].geometry.location);
							
							infowindows[i] = new google.maps.InfoWindow({
								content: getInfoWindowContent({url: url, title: title, description: description})
							});
		
							google.maps.event.addListener(marker, "click", function()
							{
								$.each(infowindows, function(a, b)
								{
									if(b)
									{
										b.close();
									}
								});
								
								infowindows[i].open(map, marker);
							});
						}
					});
				}
			}
		});
	}
	else if('1' == intelli.config.googlemap_show_without_data && ('' != intelli.config.googlemap_latitude && 'undefined' != typeof intelli.config.googlemap_latitude && '' != intelli.config.googlemap_longtitude && 'undefined' != typeof intelli.config.googlemap_longtitude))
	{
		var latlng = new google.maps.LatLng(intelli.config.googlemap_latitude * 1, intelli.config.googlemap_longtitude * 1);
		var myOptions =
		{
			zoom: intelli.config.googlemap_default_zoom * 1,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.HYBRID
		};
		var map = new google.maps.Map(document.getElementById("google_map"), myOptions);

	}
	else
	{

		$("#google_map").parents("div.box").remove();

	}
	
	function getInfoWindowContent(o)
	{
		var html = '';
		
		html += '<div style="width: 250px">';
		html += '<a href="' + o.url + '"><strong>' + o.title +'</strong></a></h3>';
		html += '<p>' + o.description + '</p>';
		html += '</div>';
	
		return html;
	}
});

