// some default values
var HT_DEFAULT_LONGITUDE = -500;
var HT_DEFAULT_LATITUDE  = -500;
var HT_CENTER_LONGITUDE  = -100.634765625;
var HT_CENTER_LATITUDE   = 39.436192999314066;
var HT_CENTER_ZOOM       = 13;

// Check if the given input coordinates are valid
function HTIsValid(i_long, i_lat, i_zoom)
{
  var result = false;
  result = i_long > HT_DEFAULT_LONGITUDE && i_lat > HT_DEFAULT_LONGITUDE && 0 < i_zoom && i_zoom < 18;
  return result;
}

function HTGPointXDistance(i_point_1, i_point_2)
	{
		
		var x_distance = 0;
		
		if (i_point_1.x > i_point_2.x)
		{
			x_distance = i_point_1.x - i_point_2.x;
		}
		else
		{
			x_distance = i_point_2.x - i_point_1.x;
		}

		return x_distance;
	}

	function HTGPointYDistance(i_point_1, i_point_2)
	{
		
		var y_distance = 0;

		if (i_point_1.y > i_point_2.y)
		{
			y_distance = i_point_1.y - i_point_2.y;
		}
		else
		{
			y_distance = i_point_2.y - i_point_1.y;
		}

		return y_distance;
	}

	function HTCheckMaxXDistance(i_point_1, i_point_2, i_max_x_distance)
	{
		var new_distance = HTGPointXDistance(i_point_1, i_point_2)
		if (new_distance > i_max_x_distance)
			return new_distance;
		else
			return i_max_x_distance;
	}

	function HTCheckMaxYDistance(i_point_1, i_point_2, i_max_y_distance)
	{
		var new_distance = HTGPointYDistance(i_point_1, i_point_2)
		if (new_distance > i_max_y_distance)
			return new_distance;
		else
			return i_max_y_distance;
	}

	function HTGetNewBounds(i_point, i_bounds)
	{
		if (i_bounds == null)
			return new GBounds(i_point.x, i_point.y, i_point.x, i_point.y);
		else 
		{
			min_x = i_bounds.minX;
			min_y = i_bounds.minY;
			max_x = i_bounds.maxX;
			max_y = i_bounds.maxY;

			if (i_point.x < min_x)
				min_x = i_point.x;
			if (i_point.y < min_y)
				min_y = i_point.y;
			if (i_point.x > max_x)
				max_x = i_point.x;
			if (i_point.y > max_y)
				max_y = i_point.y;
			
			return new GBounds(min_x, min_y, max_x, max_y);
		}
	}

	function HTGetCenter(i_bounds)
	{
		var center_x = i_bounds.minX + (i_bounds.maxX - i_bounds.minX)/2;
		var center_y = i_bounds.minY + (i_bounds.maxY - i_bounds.minY)/2;
		return new GPoint(center_x, center_y);
	}

	function HTGetZoomLevel(i_bounds, i_map_width, i_map_height)
	{
		var width = i_bounds.maxX - i_bounds.minX;
		var height = i_bounds.maxY - i_bounds.minY;
		
		var zoom_x_level = HTGetXZoomLevel(width,i_map_width);
		var zoom_y_level = HTGetYZoomLevel(height,i_map_height);

		if (zoom_x_level > zoom_y_level)
			return zoom_x_level + 1;
		else 
			return zoom_y_level + 1;
	}

	function HTGetXZoomLevel(width, pixels)
	{
		ticks_per_100_pixels = width/pixels * 100
		
		var zoom_level = 8;

		if (ticks_per_100_pixels < 0.0010728836059570312)
			zoom_level = 0;
		else if (ticks_per_100_pixels < 0.0021457672119140625)
			zoom_level = 1;
		else if (ticks_per_100_pixels < 0.004291534423828125)
			zoom_level = 2;
		else if (ticks_per_100_pixels < 0.00858306884765625)
			zoom_level = 3;
		else if (ticks_per_100_pixels < 0.0171661376953125)
			zoom_level = 4;
		else if (ticks_per_100_pixels < 0.034332275390625)
			zoom_level = 5;
		else if (ticks_per_100_pixels < 0.06866455078125)
			zoom_level = 6;
		else if (ticks_per_100_pixels < 0.1373291015625)
			zoom_level = 7;
		else if (ticks_per_100_pixels < 0.274658203125)
			zoom_level = 8;
		else if (ticks_per_100_pixels < 0.54931640625)
			zoom_level = 9;
		else if (ticks_per_100_pixels < 1.0986328125)
			zoom_level = 10;
		else if (ticks_per_100_pixels < 2.197265625)
			zoom_level = 11;
		else if (ticks_per_100_pixels < 4.39453125)
			zoom_level = 12;
		else if (ticks_per_100_pixels < 8.7890625)
			zoom_level = 13;
		else if (ticks_per_100_pixels < 17.578125)
			zoom_level = 14;
		else if (ticks_per_100_pixels < 35.15625)
			zoom_level = 15;
		else if (ticks_per_100_pixels < 70.3125)
			zoom_level = 16;
		else if (ticks_per_100_pixels < 140.625)
			zoom_level = 17;

		return zoom_level;
	}
	
	function HTGetYZoomLevel(height, pixels)
	{
		ticks_per_100_pixels = height/pixels * 100;
		
		var zoom_level = 8;

		if (ticks_per_100_pixels < 0.0008518375642386407)
			zoom_level = 0;
		else if (ticks_per_100_pixels < 0.0017036753222541278)
			zoom_level = 1;
		else if (ticks_per_100_pixels < 0.0034073514189122042)
			zoom_level = 2;
		else if (ticks_per_100_pixels < 0.006814702826612044)
			zoom_level = 3;
		else if (ticks_per_100_pixels < 0.013629405563499121)
			zoom_level = 4;
		else if (ticks_per_100_pixels < 0.027258810409144022)
			zoom_level = 5;
		else if (ticks_per_100_pixels < 0.05451781367956935)
			zoom_level = 6;
		else if (ticks_per_100_pixels < 0.10903637582451846)
			zoom_level = 7;
		else if (ticks_per_100_pixels < 0.21807556161675734)
			zoom_level = 8;
		else if (ticks_per_100_pixels < 0.43614818184036846)
			zoom_level = 9;
		else if (ticks_per_100_pixels < 0.87227282529818)
			zoom_level = 10;
		else if (ticks_per_100_pixels < 1.7443571122869414)
			zoom_level = 11;
		else if (ticks_per_100_pixels < 3.48719856208871)
			zoom_level = 12;
		else if (ticks_per_100_pixels < 6.962041996212628)
			zoom_level = 13;
		else if (ticks_per_100_pixels < 13.818568850095708)
			zoom_level = 14;
		else if (ticks_per_100_pixels < 26.65131308605596)
			zoom_level = 15;
		else if (ticks_per_100_pixels < 45.18524998797266)
			zoom_level = 16;
		else if (ticks_per_100_pixels < 57.60525031960005)
			zoom_level = 17;


		return zoom_level;
	}
	
	/*function HTPixelsToLat(pixels, zoom_level)
	{
		if (zoom_level = 1)
			return 0.0008518375642386407/(pixels*100);
		else if (zoom_level = 1)
		  return 0.0017036753222541278/(pixels*100);
		else if (zoom_level = 2)
		  return 0.0034073514189122042/(pixels*100);
		else if (zoom_level = 3)
		  return 0.006814702826612044)/(pixels*100);
		else if (zoom_level = 4)
		  return 0.013629405563499121)/(pixels*100);
		else if (zoom_level = 5)
		  return 0.027258810409144022)/(pixels*100);
		else if (zoom_level = 6)
		  return 0.05451781367956935)/(pixels*100);
		else if (zoom_level = 7)
		  return 0.10903637582451846)/(pixels*100);
		else if (zoom_level = 8)
		  return 0.21807556161675734/(pixels*100);
		else if (zoom_level = 9)
		  return 0.43614818184036846/(pixels*100);
		else if (zoom_level = 10)
		  return 0.87227282529818/(pixels*100);
		else if (zoom_level = 11)
		  return 1.7443571122869414/(pixels*100);
		else if (zoom_level = 12)
		  return 3.48719856208871/(pixels*100);
		else if (zoom_level = 13)
		  return 6.962041996212628/(pixels*100);
		else if (zoom_level = 14)
		  return 13.818568850095708/(pixels*100);
		else if (zoom_level = 15)
		  return 26.65131308605596/(pixels*100);
		else if (zoom_level = 16)
		  return 45.18524998797266/(pixels*100);
		else 
		  return 57.60525031960005/(pixels*100);
  }*/

	function HTGetCenterForPoints(ia_points)
	{
		var marker_bounds = null;

		for (var i = 0; i < ia_points.length; i++) 
		{
			var point = ia_points[i];
			if (point == null)
				continue;
			marker_bounds = HTGetNewBounds(point, marker_bounds);			
		}
		return HTGetCenter(marker_bounds)
	}


	function HTGetZoomLevelForPoints(ia_points, i_map_width, i_map_height)
	{
		var marker_bounds = null;

		for (var i = 0; i < ia_points.length; i++) 
		{
			var point = ia_points[i];
			if (point == null)
				continue;
			marker_bounds = HTGetNewBounds(point, marker_bounds);			
		}
		return HTGetZoomLevel(marker_bounds, i_map_width, i_map_height)
	}

	//---------------------
	//center the map on the objects and add the markers
	function HTFitAndCenter(i_map, ia_points, ia_markers, i_map_width, i_map_height, i_map_center_x, i_map_center_y, i_input_zoom_level)
	{
		var zoom_level = HTGetZoomLevelForPoints(ia_points,i_map_width,i_map_height);
		var center     = HTGetCenterForPoints(ia_points);
		
		if (i_map_center_x > -200)
		{
		  center.x = i_map_center_x;
		}
		
		if (i_map_center_y > -100)
		{
		  center.y = i_map_center_y;
		}
		
		if (i_input_zoom_level > 0)
		{
		  zoom_level = i_input_zoom_level;
		}
		
		
		i_map.centerAndZoom(center, zoom_level);
		
		
		HTSetMapTypeAndZoom(i_map, center, zoom_level)
		
		for (var i = 0; i < ia_markers.length; i++) 
		{
			i_map.addOverlay(a_markers[i]);
		}
		
		
	}

	function HTSetMapTypeAndZoom(i_map, i_center, i_zoom_level)
	{
	  if (i_zoom_level < 3)
	  {
	    i_zoom_level = 3;
	    i_map.zoomTo(i_zoom_level);
	  }
	  
	  if ((i_center.y >= 25) && (i_center.y <= 70))
	  {
	    if ((i_center.x < -50) && (i_center.x > -170))
	    {  // USA
	      return;
	    }
	  }
	  
	  if ((i_center.y >= 25) && (i_center.y <= 49))
	  {
	    if ((i_center.x < 145) && (i_center.x > 125))
	    {  // JAPAN
	      return;
	    }
	  }
	  
	  if ((i_center.y >= 49) && (i_center.y <= 60))
	  {
	    if ((i_center.x < 2) && (i_center.x > -13))
	    {  // UK
	      return;
	    }
	  }
	  
	  i_map.setMapType(G_HYBRID_TYPE);
	}
	
	function _HTSetTarget(index)
  {
    document.getElementById('SetData').action = document.getElementById('SetData').action + '#piemel'
  }
    	
