
var map_data= new Array();
var map1;
var infowindow = null;
var geocoder;

var markers=new Array();
var infos=new Array();


//Variables
var biz_type="";
var biz_city="";
var biz_state="";
var biz_zip="";
var biz_letter="";



function codeAddress(var address) 
{
	
	geocoder.geocode( { 'address': address}, function(results, status) {
	
	if (status == google.maps.GeocoderStatus.OK) 
	{
		return results[0].geometry.location
	} 
	
	else
	return false;
	
	});
}




function clearMarkers()
{
	for(var i=0; i<markers.length; i++)
	{
		markers[i].setMap(null);
	}
}


function clearInfo()
{
	for(var i=0; i<infos.length; i++)
	{
		infos[i].close();
	}
}



function loadBiz(id)
{
	clearInfo();
	
	if(map1.getZoom() < 8)
	map1.setZoom(14);
	
	map1.setCenter(markers[id].getPosition());
		
	google.maps.event.trigger(markers[id], 'click');
	return false;
}


function make_map()
{
	clearInfo();
	clearMarkers();
	
	markers=[];
	infos=[];
	
	var latlngbounds = new google.maps.LatLngBounds( );
	
	if(map_data != null && map_data.length>0)
	{
		$("#map_list").html("");
	
		for ( var i = 0; i < map_data.length; i++ )
		{
			var point = new google.maps.LatLng(map_data[i].lat,map_data[i].lng);
			
			var name_parts = map_data[i].name.split(" Dba ");
			
			if(name_parts.length>1)
			map_data[i].name=name_parts[1];
			
			var web_link="";
			var email_link="";
			
			if(map_data[i].website!="")
			{
				web_link = "<br><a target='blank' href='"+map_data[i].website+"'>"+map_data[i].website+"</a>"
			}
			
			if(map_data[i].email!="")
			{
				email_link = "<br><a href='mailto:"+map_data[i].email+"'>"+map_data[i].email+"</a>"
			}

			var new_marker = new google.maps.Marker({ position: point, map: map1, title: map_data[i].name , html: map_data[i].address+"<br>"+map_data[i].city+","+map_data[i].state+" "+map_data[i].zip+web_link+email_link+"<br>"+map_data[i].type+"<br>"+map_data[i].alt_type})
			
			if(map_data[i].lat == "" || map_data[i].lng == "")
			{
				coded_point = codeAddress(map_data[i].address+" "+map_data[i].city+" "+map_data[i].state+" "+map_data[i].zip);
				
				if(coded_point!=FALSE)
					new_marker.setPosition(coded_point);
			}
			
			google.maps.event.addListener(new_marker, 'click', function(){
			
			var contentString = "<b>"+this.title+"</b><p>"+this.html+"</p>";
			
			
			clearInfo();

			infowindow = new google.maps.InfoWindow({content: contentString});
			
			infos.push(infowindow);

			infowindow.open(map1,this);});
			 
			latlngbounds.extend(point);
			
			markers.push(new_marker); 
			
			$("#map_list").append("<table id='maplink_table' width='100%' cellpadding='0' cellspacing='0'></table>");
			
			var rowType="";
			
			if((i+3)%2==0)
			rowType="even";
			
			else
			rowType="odd";
			
			$("#maplink_table").append("<tr class='row_"+rowType+"'><td><a href='#' onclick='return loadBiz("+i+")' class='map_link'>"+map_data[i].name+"</a></td></tr>");
		}
		
		
		map1.fitBounds(latlngbounds);

		if(map1.getZoom() > 14)
		map1.setZoom(14);
		
		if(markers.length==1)
		google.maps.event.trigger(markers[0], 'click');
	}
	
	
	else
	{
		$("#map_list").html("<center><b>Sorry, No businesses matched your search.</b></center>");
		map1.setCenter(new google.maps.LatLng(34.620398, -83.799755));
		map1.setZoom(7);
		
		clearInfo();
				
		markers=[];
		infos=[];
	}
}

//Load Map Data
function load_map_data()
{	
	$.getJSON("/cust_data.php",{letter:biz_letter,city:biz_city,state:biz_state,zip:biz_zip,type:biz_type}, function(data){ map_data=data; make_map()});
}

function clearAll()
{
	biz_type="";
	biz_city="";
	biz_state="";
	biz_zip="";
	biz_letter="";
	
	clearInfo();
		
	markers=[];
	infos=[];
	
	load_map_data();
}



//Initialize the map
$(document).ready(function(){

var latlng = new google.maps.LatLng(34.620398, -83.799755);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map1 = new google.maps.Map(document.getElementById("map_canvas"),myOptions);



//Set up the select input

$("#biz_letter").change(function () {  $("#biz_letter option:selected").each(function() { biz_letter=$(this).val(); clearMarkers(); load_map_data()}); });
$("#biz_type").change(function () {  $("#biz_type option:selected").each(function() { biz_type=$(this).val(); clearMarkers(); load_map_data()}); });
$("#biz_city").change(function () {  $("#biz_city option:selected").each(function() { biz_city=$(this).val(); clearMarkers(); load_map_data()}); });
$("#biz_state").change(function () {  $("#biz_state option:selected").each(function() { biz_state=$(this).val(); clearMarkers(); load_map_data()}); });
$("#biz_zip").change(function () {  $("#biz_zip option:selected").each(function() { biz_zip=$(this).val(); clearMarkers(); load_map_data()}); });

$(':input','#mapForm').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');

//Load the full map the first time
clearAll();

});


