// 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
//LongHorn Version
//Updated 02 February 2010 by Bryan Curtis

var numResults = 0;
var displayLowerIndex = 0;
var displayUpperIndex = 3;
var resultsLayer;

function SearchResult(id, c, l, a1, a2, city, st, zip, ctry, phone, fax, lat, lon, dis, comingsoon, hrs_st, hrs_fs)
{
	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.Lat = lat;
	this.Lon = lon;
	this.dis = dis;
	this.comingsoon = comingsoon;
	this.hours_sun_thurs = hrs_st;
	this.hours_fri_sat = hrs_fs;
}

function IconClick(eventArgs)
{ 
	var  shape = map.GetShapeByID(eventArgs.elementID);
	if (shape)
	{
		//var id = shape.GetTitle();
		var id = shape.Id;
		//first to check if the we actually clicked on the shape
		if (eventArgs.elementID !=null)
		{
			//redirect the user to the new page
			window.location ="/locator_VE/detail.asp?Address=" + BuildDetailsLink(ResultsArray[id]);
	
		}
	}
}

function SuppressInfoBox( eventArgs ){ return true; }

function ShowNoResults()
{	
	
	// 1. Hide the prev link
	document.getElementById("prevButton").style.display = "none";
	// 2.  Hide the next link	
	document.getElementById("nextButton").style.display = "none";
	
}

function ShowNextPage()
{
	// 1. Hide the rows from displayLowerIndex to displayUpperIndex
	// 5. Adjust the visible shapes
	for ( var i = displayLowerIndex; i <= displayUpperIndex; i++ )
	{
		var row = document.getElementById("resultRow" + i);
		row.style.display = "none";
		resultsLayer.GetShapeByIndex(i).Hide();
	}
	displayLowerIndex += 4;
	displayUpperIndex +=4;
	if (displayUpperIndex > ResultsArray.length -2 )
		displayUpperIndex = ResultsArray.length -2;
	
// 2. Show the next page of results	
	for ( var i = displayLowerIndex; i <= displayUpperIndex; i++ )
	{
		var row = document.getElementById("resultRow" +i);
		row.style.display = "";
		resultsLayer.GetShapeByIndex(i).Show();
	}
	
	// 3. Show the prev link
	document.getElementById("prevButton").style.display = "inline";
	// 4.  Hides the next link if necessary
	if ( displayUpperIndex == (ResultsArray.length -2))
	document.getElementById("nextButton").style.display = "none";
	
}

function ShowPrevPage()
{
    // 1. Hide the rows from displayLowerIndex to displayUpperIndex
    // 5. Adjust the visible shapes
    for ( var i = displayLowerIndex; i <= displayUpperIndex; i++ )
    {
	var row = document.getElementById("resultRow" + i);
	row.style.display = "none";
	resultsLayer.GetShapeByIndex(i).Hide();
    }
    
    displayLowerIndex -= 4;
    if ( displayLowerIndex <= 0 )
    {
	displayLowerIndex = 0;
  	document.getElementById("prevButton").style.display = "none";
    }
	
    displayUpperIndex = displayLowerIndex + 3
    document.getElementById("nextButton").style.display = "inline";	

    for ( var i = displayLowerIndex; i <= displayUpperIndex; i++ )
    {
	var row = document.getElementById("resultRow" +i);
	row.style.display = "";
	resultsLayer.GetShapeByIndex(i).Show();
    }
}

function DisplaySearchResults()
{
	if ( ResultsArray.length == 1 )
	{
		BuildResultsTable( false );
		return;
	}
	
	resultsLayer = new VEShapeLayer();
	map.AddShapeLayer( resultsLayer );
	numResults = ResultsArray.length + 1;
	//Add each result to the map
	for ( var i = 0; i < ResultsArray.length; i++ )
	{
        if ( ResultsArray[i] != null )
		{			
				var shape = new VEShape( VEShapeType.Pushpin, new VELatLong( parseFloat(ResultsArray[i].Lat), 
																			 parseFloat(ResultsArray[i].Lon ) ));
				//shape.SetCustomIcon( "<div style='border:0px solid black'><img src='../images/locator/Map_Leather_Baloon_26x39.gif' height='39' width='26' alt='' /></div>" );
				//resultsLayer.AddShape( shape );	
				//var iconURL = "http://dev.virtualearth.net/mapcontrol/v6.1/i/bin/6.1.20080306152009.13/pins/RedCircle" + (i + 1) + ".gif";
				
				var pinHTML = "<div class='balloon_number'>{0}</div>";
            	shape.SetCustomIcon(pinHTML.replace('{0}', i + 1));
				//shape.SetTitle( ResultsArray[i].ID );
				//resultsLayer.AddShape( shape );
			
				//var iconURL;
				//iconURL = "<div class=\"balloon_number\">" + (i + 1) + "</div>"
				//shape.SetCustomIcon( iconURL );
				shape.SetTitle( ResultsArray[i].ID );
				resultsLayer.AddShape( shape );
				map.AttachEvent("onmouseover", SuppressInfoBox);
				if ( i > 3 )
                		{
					shape.Hide();
				}
		}
	}
	if ( ResultsArray.length <= 4 )
        {
		document.getElementById("prevButton").style.display = "none";
		document.getElementById("nextButton").style.display = "none";
    	}
        else
        {
		document.getElementById("prevButton").style.display = "none";
    	}
	map.SetMapView( resultsLayer.GetBoundingRectangle() );
	BuildResultsTable( true );
}

function BuildResultsTable( foundResults )
{
    if ( foundResults )
	{
		var contentDiv = document.getElementById("searchResults");
		
		var table = document.createElement("table");
		table.setAttribute("class", "locator_table");
        table.className = "locator_table";
		table.setAttribute("id", "longhorn_locator_table");	
		table.setAttribute("width", "650");	
		table.setAttribute("cellpadding", "3");		
		table.setAttribute("cellspacing", "0");			
		table.setAttribute("border", "0");		
		
		table.setAttribute("summary", "LongHorn Restaurants near you");			
		var tbody = document.createElement("tbody");           
		//Header Row
		var headerRow = document.createElement("tr");
		headerRow.setAttribute("class", "table_header");
		headerRow.className = "table_header";		
				
		var th1 = document.createElement("th");
		th1.setAttribute("scope", "col");
		//th1.setAttribute("style", "width:27px;");
		//th1.style.width = "27px";
		th1.setAttribute("height", "24");
		th1.setAttribute("width", "27");
		
        th1.innerHTML = "No.";		
		headerRow.appendChild(th1);
		
		var th2 = document.createElement("th");
		th2.setAttribute("scope", "col");		
		th2.setAttribute("width", "185");
		//th2.setAttribute("style", "width:185px;");		
		//th2.style.width = "185px";
        th2.innerHTML = "<div align=\"left\">Address</div>";
		headerRow.appendChild(th2);
		
		
		var th3 = document.createElement("th");
		th3.setAttribute("scope", "col");
		//th3.setAttribute("style", "width:141px;");		
		//th3.style.width = "141px";
		th3.setAttribute("width", "141");
        th3.innerHTML = "Hours of Operation";
		headerRow.appendChild(th3);
		
		
		var th4 = document.createElement("th");
		th4.setAttribute("scope", "col");
		//th4.setAttribute("style", "width:108px;");		
		//th4.style.width = "108px";
		th4.setAttribute("width", "108");
        th4.innerHTML = "Distance";
		headerRow.appendChild(th4);
		
		var th5 = document.createElement("th");
		th5.setAttribute("scope", "col");
		//th5.setAttribute("style", "width:157px;");		
		//th5.style.width = "157px";
		th5.setAttribute("width", "157");
        th5.innerHTML = "Menu and Directions";
		headerRow.appendChild(th5);
					
		tbody.appendChild( headerRow );
		
		//
		for ( var i = 0; i < ResultsArray.length; i ++ )
		{
		     if ( ResultsArray[i] != null )
                        tbody.appendChild( BuildResultsTableRow( ResultsArray[i], i ));
		}
		//
		table.appendChild( tbody );
		contentDiv.insertAdjacentElement( "afterEnd", table );
		//contentDiv.innerHTML = table;
		//Fix table in MSIE 7+
		document.getElementById('longhorn_locator_table').border="0px";		
		document.getElementById('longhorn_locator_table').cellSpacing="0px";
		document.getElementById('longhorn_locator_table').cellPadding="3px";
		
	}
	else
	{
		var resultsDiv = document.getElementById("searchResults");
		resultsDiv.innerHTML = "We're Sorry. No results were found near your location";
		ShowNoResults();
	}
}

function BuildResultsTableRow( result, idx )
{
	var row = document.createElement("tr");
	if ( idx % 2 == 0 )
	{
	   row.setAttribute("class", "row_normal");
	   row.className = "row_normal";
	  
	}
	else
	{
	   row.setAttribute("class", "row_alternate");
	   row.className = "row_alternate";
	   //row.style.backgroundColor = "#EBF5FA";
	}

//we only want to show the first 4 elements
	if (idx > 3)
	{
	   row.style.display="none";
	}

	row.setAttribute("valign", "top");
	row.id = "resultRow" + idx;
	
	//var detailsLink = BuildDetailsLink( result );
	//row.appendChild( BuildEmptyCell( "27") );
	row.appendChild( BuildIconCell( idx,"27" ) );
	//row.appendChild( BuildEmptyCell( "185") );
	row.appendChild( BuildDetailsCell( result, "185", "MyLongHorn.asp?show=home&RestaurantID=" + result.ID) );
	
	//row.appendChild( BuildEmptyCell( "141") );
	if ( String(result.comingsoon) == "1" )
	{
		row.appendChild( BuildComingSoonCell("141"));//coming soon
	}
	else
	{
		row.appendChild( BuildHoursCell(result, "141") ); //hours
		//row.appendChild( BuildEmptyCell( "141") );
	}
	//row.appendChild( BuildEmptyCell( "108") );
	row.appendChild( BuildDistanceCell( result.dis, "108" ));
	//row.appendChild( BuildEmptyCell( "157") );
	row.appendChild( BuildActionCell(result.ID,"157"));
	//row.appendChild( BuildDirectionsCell(detailsLink, "150", result.ID  ));
	//row.appendChild( BuildEmptyCell( "60") );
	
	return row;
}

function BuildIconCell( idx, width )
{
		
	var cell = document.createElement("td");
	
	cell.innerHTML = "<div class=\"balloon_number\">" + (idx + 1) + "</div>";	
	//cell.style.textAlign = "center";  
	cell.style.width = width;
	cell.setAttribute("align", "center");
	cell.setAttribute("valign", "middle");
	
	return cell;
}

function BuildHoursCell(result, width )
{
		
	var cell = document.createElement("td");
	
	cell.innerHTML = "<span class=\"text_hours\">Sunday - Thursday:<br />" + result.hours_sun_thurs + "<br />Friday & Saturday:<br />" + result.hours_fri_sat + "</span>";	
	//cell.style.textAlign = "center";  
	cell.style.width = width;
	cell.setAttribute("align", "center");
	cell.setAttribute("valign", "middle");	
	return cell;	
	
}


function BuildDetailsCell( result, width, link )
{
     var cell = document.createElement("td");
    cell.style.width = width;
	cell.setAttribute("align", "left");
	cell.setAttribute("valign", "middle");
    cell.innerHTML = "<span class=\"header_city\"><a href=\"" + link + "\">" + result.Location + "</a></span><br />";
	 cell.innerHTML += "<span class=\"table_row_text\"><strong>" + result.Address1 + "&nbsp;" + result.Address2 + "</strong><br />";
	 cell.innerHTML += result.City + ",&nbsp;" + result.State + "&nbsp;" + result.Zip + "<br />";
	 cell.innerHTML += "Phone: &nbsp;" + result.Phone + "</span>";	 
    return cell;
	
}


function BuildDistanceCell( distance, width )
{
    var units = " Miles";
    distance = parseFloat( distance );
			
	var cell = document.createElement("td");
	cell.setAttribute("align", "center");
	cell.setAttribute("valign", "middle");	
	//cell.style.textAlign = "center";        
	cell.innerHTML = "<span class=\"header_distance\">" + distance.toFixed(2) + units + "</span></div>";
    cell.style.width = width;
	return cell;
}

function BuildActionCell(id, width)
{
    var cell = document.createElement("td");
	//cell.setAttribute("style", "text-align:center;");
        //cell.style.textAlign = "center";
	cell.setAttribute("align", "center");
	cell.setAttribute("valign", "middle");
		
	cell.innerHTML = "<a href=\"MyLongHorn.asp?show=menu&RestaurantID=" + id  +"\"><img class=\"btn_view_menu\" src=\"../images/locator/btn_view_menu.gif\" title=\"View Menu\" alt=\"View Menu\" width=\"124\" height=\"20\" border=\"0\" /></a><br />";
	cell.innerHTML += "<a href=\"MyLongHorn.asp?show=directions&RestaurantID=" + id  +"\"><img class=\"btn_get_directions\" src=\"../images/locator/btn_get_directions.gif\" title=\"Driving Directions\" alt=\"Driving Directions\" width=\"124\" height=\"20\" border=\"0\" /></a>";
	cell.style.width = width;	
	return cell;	

}

function BuildComingSoonCell(width)
{
    var cell = document.createElement("td");
	cell.innerHTML = "<img src=\"/images/locator/coming_soon.gif\" alt=\"Coming Soon\" border=\"0\" />";
	cell.style.width = width;	
	cell.setAttribute("align", "center");
	cell.setAttribute("valign", "middle");	
	return cell;
}

// Creates a Cell in the results table
function BuildEmptyCell( width)
{
    var cell = document.createElement("td");
    //cell.style.width = width;
	cell.style.width = width + "px";
    cell.innerHTML = "&nbsp;";
    return cell;
}