var map = '';
var markers = '';
var map_markers = Array();
var max_zoom = 12;
var min_zoom = 5; // 8
var zoom;
var map_enabled = false;

// Can enable/disable map
function toggle_map()
{
	// If the map is currently enabled, disable it
	if(map_enabled)
	{
		map_enabled = false;
		set_cookie('map_disabled', 'yes');

		// Hide the map
		$('#map').hide();
		// Change the button text
		$('#map_button').html('show map');
	}
	// If the map is currently disabled, enable it
	else
	{
		map_enabled = true;
		set_cookie('map_disabled', 'no');

		// Show the map
		$('#map').show();
		// Change the button text
		$('#map_button').html('hide map');
	}
//	map_slider.toggle_slide();
}

function init_map()
{
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
//	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(focus_loc.lat, focus_loc.lng));
	
	update_map();
}

function update_map()
{
	// use for later once we're allowing moving
	//var params = getAdjustedMapBounds(map, document.getElementById("map"));

	update_map_results();
}

function update_map_results()
{
	// Set zoom in to max
	map.setZoom(max_zoom);
	zoom = max_zoom;
	var map_bounds = map.getBounds();

	// for now we're not querying anything, so just use 'markers' var
	var low_lat = 9999;
	var high_lat = -9999;
	var low_lng = 9999;
	var high_lng = -9999;
	for(var i = 0; i < markers.length; i++)
	{
		// Track lat
		if(markers[i].lat < low_lat)
		{
			low_lat = parseFloat(markers[i].lat);
		}
		if(markers[i].lat > high_lat)
		{
			high_lat = parseFloat(markers[i].lat);
		}

		// Track lng
		if(markers[i].lng < low_lng)
		{
			low_lng = parseFloat(markers[i].lng);
		}
		if(markers[i].lng > high_lng)
		{
			high_lng = parseFloat(markers[i].lng);
		}

		var point = new GLatLng(parseFloat(markers[i].lat),
								parseFloat(markers[i].lng));

		// Create box_info
		var info_box_contents = '';
		if(markers[i].logo_src != '')
		{
			info_box_contents += '<div style="width: 255px;">';
			info_box_contents += '<div style="float: left; margin: 5px; width: 60px;"><img src="' + markers[i].logo_src + '" width="60" /></div>';
			info_box_contents += '<div style="float: left; margin: 5px; width: 170px;">';
		}
		else
		{
			info_box_contents += '<div style="width: 180px;">';
			info_box_contents += '<div style="float: left; margin: 5px; width: 170px;">';
		}
		info_box_contents += '<b>' + markers[i].name + '</b><br /><br />' + markers[i].address + '<br />' + markers[i].city + ', ' + markers[i].state + ' ' + markers[i].zip + '<br /><b>' + markers[i].phone + '</b></div><div class="spacer">&nbsp;</div></div>';

		map_markers[i] = new ltf_marker(point, '', info_box_contents, markers[i].id, i+1, markers[i].is_linked);
		map.addOverlay(map_markers[i]);

		// If the marker is not in the zoom area, zoom out until it is
		while(!map_bounds.contains(point) && zoom > min_zoom)
		{
			zoom--;
			map.setZoom(zoom);
			map_bounds = map.getBounds();
//			alert(markers[i].name);
		}
	}
	var center_lat = (low_lat+high_lat)/2;
	var center_lng = (low_lng+high_lng)/2;
	map.setCenter(new GLatLng(parseFloat(center_lat), parseFloat(center_lng)));
	map.setZoom(++zoom);
	
	if(get_cookie('map_disabled') == 'yes')
	{
		map_enabled = false;
		$('#map').hide();
	}
	else
	{
		map_enabled = true;
	}
	$('#map').css('visibility', '');
}

// Create the ltf_marker object
function ltf_marker(point, icon, info_box_contents, id, index, is_linked)
{
	this.id = id;
	this.index = index;
	this.is_linked = is_linked;

	this.inheritFrom = GMarker;
	this.inheritFrom(point, icon);
	
	this.info_box_contents = info_box_contents;
}

// Extend the GMarker object
ltf_marker.prototype = new GMarker(new GLatLng(1,1));

ltf_marker.prototype.initialize = function(map) {
	// Reference the map object for later use
	this.map = map;

	// Call initialize on the parent object (GMarker)
	GMarker.prototype.initialize.call(this, map);

	// Set the icon image
	this.setImage('http://www.localthaifood.com/images/google_icons/icon_' + this.index + '.png');

	// Bind events for clickable text
//	$(this.id).onmouseover = $(this.id).marker.on_mouse_over;
//	GEvent.bind($(this.id), "mouseover", this, this.on_mouse_over);
//	GEvent.bind($(this.id), "mouseout", this, this.on_mouse_out);
//	GEvent.bind($(this.id), "mouseclick", this, this.on_mouse_click);

	// Bind events for map marker
	GEvent.bind(this, "mouseover", this, this.on_mouse_over);
	GEvent.bind(this, "mouseout", this, this.on_mouse_out);
	GEvent.bind(this, "click", this, this.on_mouse_click);
}

ltf_marker.prototype.on_mouse_over = function() {
	if(links_on)
	{
		document.getElementById('result_icon_'+this.id).src=document.getElementById('result_icon_'+this.id).src.replace('.png', '_highlighted.png');
//		$('#result_icon_'+this.id).attr('src', $('#result_icon_'+this.id).attr('src').replace('.png', '_highlighted.png'));

		// Bold the restaurant name
		//$('name_' + this.id + '_field').style.fontWeight='bold';
	
		// Highlight the result div
		//$('dr_' + this.id).style.backgroundColor='#fffdc7';
	}

	if(!links_on || !map_enabled)
	{
		return;
	}

	var map_bounds = map.getBounds();
	if(map_bounds.contains(this.getPoint()))
	{
		// Set the highlighted image
		this.setImage('http://www.localthaifood.com/images/google_icons/icon_' + this.index + '_highlighted.png');
	
		if(this.info_box_obj == null)
		{
			this.info_box_obj = document.createElement('div');
			this.info_box_obj.innerHTML = '<div id="map_info_box_' + this.id + '" class="map_info_box">' + this.info_box_contents + '</div>';
			this.info_box_obj.style.display = 'none';
			this.info_box_obj.style.position = 'absolute';
	
			document.body.appendChild(this.info_box_obj);
		}

		// Set the highlight image
		//	this.setImage('');
	
		// Display the object (out of sight) now so we can reference its size later
		this.info_box_obj.style.top = '-9999px';
		this.info_box_obj.style.display = 'block';
	
		/* Find the right position for the info_box_obj, then display! */
		var info_box_x = 0;
		var info_box_y = 0;
	
		// Add the pos of the map within the page
		var map_pos = find_pos(document.getElementById('map'));
		info_box_x += map_pos.x;
		info_box_y += map_pos.y;

		// Add the pixel distance from the top left corner of the map to the marker
		var tlc_pos = this.map.fromLatLngToDivPixel(this.map.fromContainerPixelToLatLng(new GPoint(0,0)));
		var marker_pos = this.map.fromLatLngToDivPixel(this.getPoint());
		info_box_x += (marker_pos.x - tlc_pos.x);
		info_box_y += (marker_pos.y - tlc_pos.y);

		// Subtract the "relative" width and height of the marker's icon
		// "relative" because we should now be looking at the bottom middle of the marker icon
		info_box_x -= this.getIcon().iconAnchor.x;
		info_box_y -= this.getIcon().iconAnchor.y;
	
		// Subtract the width and height of the info_box_obj itself
		info_box_x -= this.info_box_obj.offsetWidth;
		// If the marker is on the bottom half of the map, display the
		// info above, else let it display below
		if(marker_pos.y - tlc_pos.y > 150)
		{
			info_box_y -= this.info_box_obj.offsetHeight;
		}
		else
		{
			// Make sure the top of the info box is below the bottom of the icon
			info_box_y += this.getIcon().iconSize.height;
		}

		if(info_box_x < map_pos.x+10)
		{
			info_box_x = map_pos.x+10;
		}

		// Set the info_box_obj pos
		this.info_box_obj.style.top = info_box_y + 'px';
		this.info_box_obj.style.left = info_box_x + 'px';
	}
}

ltf_marker.prototype.on_mouse_out = function() {
	if(links_on)
	{
		document.getElementById('result_icon_'+this.id).src=document.getElementById('result_icon_'+this.id).src.replace('_highlighted.png', '.png');
//		$('#result_icon_'+this.id).attr('src', $('#result_icon_'+this.id).attr('src').replace('_highlighted.png', '.png'));
		
		// Unbold the restaurant name
		//$('name_' + this.id + '_field').style.fontWeight='normal';
	
		// Unighlight the result div
		//$('dr_' + this.id).style.backgroundColor='';
	}

	if(!links_on || !map_enabled)
	{
		return;
	}

	var map_bounds = map.getBounds();
	if(map_bounds.contains(this.getPoint()))
	{
		// Set regular icon image
		this.setImage('http://www.localthaifood.com/images/google_icons/icon_' + this.index + '.png');
	
		this.info_box_obj.style.display = 'none';
	}
}

ltf_marker.prototype.on_mouse_click = function() {
	if(!links_on || !map_enabled)
	{
		return;
	}

	if(this.is_linked == 1)
	{
		// go to details page
		document.location='http://www.localthaifood.com/restaurant/details/&id=' + this.id;
	}
	else
	{
		// center map at location and zoom in
		this.map.setCenter(this.getPoint(), 11);
	}
}