// This file contains the definition of a Search Result object
// It also contains the logic to write the results out on the LocationSearchAlt.asp page

function SearchResult(id, c, l, a1, a2, city, st, zip, ctry, phone, fax, email, lat, lon )
{
	this.ID = id;
	this.Code = c;
	this.Location = l;
	this.Address1 = a1;
	this.Address2 = a2;
	this.City = city;
	this.State = st;
	this.Zip = zip;
	this.Country = ctry;
	this.Phone = phone;
	this.Fax = fax;
	this.Email = email;
	this.Lat = lat;
	this.Lon = lon;
}

//makes the Pin on the map clickable
	function IconClick(eventArgs)
	{ 
	
		//first to check if the we actually clicked on the shape
			if (eventArgs.elementID !=null)
			{
				//Retrieve a Reference to the Shape
				var  shape = map.GetShapeByID(eventArgs.elementID);
				var id = shape.GetTitle();

				//redirect the user to the new page
				window.location ="/locations2/map.asp?RestaurantID=" + id+ "" ;
 
			}
	}
	function SuppressInfoBox( eventArgs ){ return true; }

function DisplaySearchResults()
{
	if ( ResultsArray.length == 1 )
	{
		BuildResultsTable( false );
		return;
	}
	
	var resultsLayer = new VEShapeLayer();
	map.AddShapeLayer( resultsLayer );
	
	//Add each result to the map
	for ( var i = 0; i < ResultsArray.length; i++ )
	{
        if ( ResultsArray[i] != null )
		{
			var storeLatLong = new VELatLong( parseFloat(ResultsArray[i].Lat), parseFloat(ResultsArray[i].Lon));

			var shape = new VEShape( VEShapeType.Pushpin, storeLatLong );
			shape.SetCustomIcon( "<img class='bb_png' src='/images/locator/bb_map_icon.png' height='32' width='32' alt='' />" );
			shape.SetTitle( ResultsArray[i].ID );
			resultsLayer.AddShape( shape );

			map.AttachEvent("onclick", IconClick);
			map.AttachEvent("onmouseover", SuppressInfoBox);

			
		}
	}
	
	map.SetMapView( resultsLayer.GetBoundingRectangle() );
	BuildResultsTable( true );
}

function BuildResultsTable( foundResults )
{
    if ( foundResults )
	{
		var table = document.createElement("table");
		table.setAttribute("class", "ResultsTable");
		var tbody = document.createElement("tbody");
		for ( var i = 0; i < ResultsArray.length; i += 2 )
		{
			if ( (i + 1) == ResultsArray.length )
				tbody.appendChild( BuildResultsTableRow( ResultsArray[i], null ));
			else
				tbody.appendChild( BuildResultsTableRow( ResultsArray[i], ResultsArray[i+1] ));
		}
		table.appendChild( tbody );
		document.getElementById("searchResults").appendChild( table );
	}
	else
	{
		var resultsDiv = document.getElementById("searchResults");
		resultsDiv.innerHTML = "We're Sorry. No results were found near your location";
	}
}

// Creates a row in the results table
function BuildResultsTableRow(r1, r2)
{
	var row = document.createElement("tr");
	row.setAttribute("class", "ResultsTable");
	row.appendChild( BuildResultsTableCell( r1) );
	row.appendChild( BuildResultsTableCell( r2) );
	
	return row;
}

// Creates a Cell in the results table
function BuildResultsTableCell( r1 )
{
	var cell = document.createElement("td");
	cell.setAttribute("class", "ResultsTableTD");
	if ( r1 != null )
	{
		if ( document.location.pathname.indexOf("/menus/group_event/PopUpResult.asp") != -1 )
		{
		   cell.innerHTML = "<a onClick='javascript:window.resizeTo(800,600);' href='/pdf/group_events/BahamaBreeze_GroupEvents_" + r1.ID + ".pdf' >" + r1.Location +"</a><br/>";
		}
		else if ( document.location.pathname.indexOf("/locations2/locationSearchAlt.asp") != -1 )
		{
		   cell.innerHTML = "<a href='/locations2/map.asp?RestaurantID=" + r1.ID + "'>" + r1.Location +"</a><br/>";
		}
		else
		{
			cell.innerHTML = "<a href='/process.asp?recordid=" + r1.ID + "&address=" + r1.Address1 +"&city=" + r1.city + "&state=" + r1.state + "&zip=" + r1.zip + "'>" + r1.Address1 +"</a><br/>";
		}
	
		cell.innerHTML += r1.Address1 + " " + r1.Address2 + "<br/>";
		cell.innerHTML += r1.City + ", " + r1.State + " " + r1.Zip + "<br/>";
		cell.innerHTML += "Phone: " + r1.Phone + "<br/>";
		cell.innerHTML += "Fax: " + r1.Fax + "<br/>";
		cell.innerHTML += "<a href='javascript:window.opener.location.href = \"/menus/group_event/contact_manager.asp?email=" +  r1.Email  + "&RestaurantID=" + r1.ID + "&address=" + r1.Address1 + "&city=" + r1.City + "&state=" + r1.State + "&zip=" + r1.Zip + "&phone=" + r1.Phone + "&fax=" + r1.Fax + "\";window.close();'>"+ r1.Email +"</a><br/>";
	}
	
	return cell;
}


