// 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, mpa, a1, a2, city, st, zip, phone, fax, sun_thurs_open, sun_thurs_close, fri_sat_open, fri_sat_close, gm_first_name, gm_last_name, take_out_ind, lat, lon, dis, comingsoon )
{
	this.ID = id;
	this.MPA = mpa;
	this.Address1 = a1;
	this.Address2 = a2;
	this.City = city;
	this.State = st;
	this.Zip = zip;
	this.Phone = phone;
	this.Fax = fax;
	this.Hours_Sun_Thurs_Open = sun_thurs_open;
	this.Hours_Sun_Thurs_Close = sun_thurs_close;
	this.Hours_Fri_Sat_Open = fri_sat_open;
	this.Hours_Fri_Sat_Close = fri_sat_close;
	this.Gm_First_Name = gm_first_name;
	this.Gm_Last_Name = gm_last_name;
	this.Take_Out_Ind = take_out_ind;
	this.Lat = lat;
	this.Lon = lon;
	this.dis = dis;
	this.comingsoon = comingsoon;
}

var numResults = 0;
var displayLowerIndex = 0;
var displayUpperIndex = 3;
var resultsLayer;


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 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 DisableBirdseyePopup()
	{
		document.getElementById("MSVE_obliqueNotifyBeak").style.display = "none";
   	    document.getElementById("MSVE_obliqueNotifyContent").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:1px solid black'><img src='../images/LongHorn_25x25.gif' height='25' width='25' alt='' /></div>" );
			//var iconURL = "http://dev.virtualearth.net/mapcontrol/v6.1/i/bin/6.1.20080306152009.13/pins/RedCircle" + (i + 1) + ".gif";
			//shape.SetCustomIcon( iconURL );
			var pinHTML = "<div class='pinStyle'>{0}</div>";
            shape.SetCustomIcon(pinHTML.replace('{0}', i + 1));
			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("rl_locator_border_box");
		
		var table = document.createElement("table");
		table.setAttribute("class", "r1_locator_list");
                table.className = "r1_locator_list";
		table.setAttribute("cellpadding", "0");
		table.setAttribute("cellspacing", "0");

              
		var tbody = document.createElement("tbody");           
		
		var headerRow = document.createElement("tr");
		headerRow.setAttribute("valign", "top");
		headerRow.setAttribute("class", "white");
                headerRow.className = "white";
		
		var th1 = document.createElement("th");
		th1.setAttribute("style", "width:22px;");
		th1.style.width = "22px";
                th1.innerHTML = "&#160;";
		headerRow.appendChild(th1);
                

		
		var th2 = document.createElement("th");
		th2.setAttribute("style", "width:31px");
                th2.style.width = "31px";
		headerRow.appendChild(th2);

		
		var th3 = document.createElement("th");
		th3.setAttribute("style", "width:205px");
                th3.style.width = "205px";
		headerRow.appendChild(th3);

		
		var th4 = document.createElement("th");
		th4.setAttribute("style", "width:80px; text-align: center; font-weight: bold;");
		th4.style.width = "80px";
                th4.style.textAlign = "center";
                th4.style.fontWeight = "bold";
		headerRow.appendChild(th4);

		
		var th5 = document.createElement("th");
		th5.setAttribute("style", "width:232px;text-align: center;");
                th5.style.width = "232px";
                th5.style.textAlign = "center";
                th5.style.fontWeight = "bold";
		headerRow.appendChild(th5);
		
                var th6 = document.createElement("th");
		th6.setAttribute("style", "width:185px;text-align: center;");
		th6.style.width = "185px";
                th6.style.textAlign = "center";
		headerRow.appendChild(th6);
		
		var th7 = document.createElement("th");
		th7.setAttribute("style", "width:12px");
		th7.style.width = "12px";
		headerRow.appendChild(th7);
             
                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 );
		
	}
	else
	{
		var resultsDiv = document.getElementById("noresults");
		resultsDiv.innerHTML = "We're Sorry. No results were found near your location";
	}
}

// Creates a row in the results table
function BuildResultsTableRow( result, idx )
{
	var row = document.createElement("tr");
	if ( idx % 2 == 0 )
	{
	   row.setAttribute("class", "white");
	   row.className = "white";
	  
	}
	else
	{
	   row.setAttribute("class", "blue");
	   row.className = "blue";
	   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( "22") );
	row.appendChild( BuildIconCell( idx,"31" ) );
	row.appendChild( BuildDetailsCell( result, "205", "/locator/redir/return.asp?busid=" + result.ID) );
	if ( String(result.comingsoon) == "1" )
	{
		row.appendChild( BuildComingSoonCell("41"));//coming soon
	}
	else
	{
		row.appendChild( BuildEmptyCell( "41") ); //coming soon
	}
	
	row.appendChild( BuildDistanceCell( result.dis,"80" ));
	row.appendChild( BuildMenuCell(result.ID,"232"));
	row.appendChild( BuildDirectionsCell(detailsLink, "185",result.ID  ));
	row.appendChild( BuildEmptyCell("12")) ;
	
	return row;
}

function BuildDetailsLink( result )
{
   return "detail.asp?adr=" + result.Address1 +
	           "&cty=" + result.City +
			   "&st=" + result.State +
			   "&zip=" + result.Zip +
			   "&ph=" + result.Phone +
			   "&fx=" + result.Fax +
			"&dlat=" + result.lat +
			"&dlon=" + result.lon +
			   "&sto=" + result.Hours_Sun_Thurs_Open +
			   "&stc=" + result.Hours_Sun_Thurs_Close +
			   "&fso=" + result.Hours_Fri_Sat_Open +
			   "&fsc=" + result.Hours_Fri_Sat_Close +
			   "&gmf=" + result.Gm_First_Name +
			   "&gml=" + result.Gm_Last_Name +
			   "&to=" + result.Take_Out_Ind;
}

// Creates a Cell in the results table
function BuildEmptyCell( width)
{
    var cell = document.createElement("td");
    cell.style.width = width + "px";
    
    return cell;
}

function BuildIconCell( idx, width )
{
	//var iconURL = "http://dev.virtualearth.net/mapcontrol/v6.1/i/bin/6.1.20080306152009.13/pins/RedCircle" + (idx + 1) + ".gif";
	
	var cell = document.createElement("td");
	cell.innerHTML = "<div class='pinStyle'>" + (idx + 1) + "</div>";
	cell.style.width = width + "px";
	return cell;
}
 
function BuildDetailsCell( result, width, link )
{
    var cell = document.createElement("td");
		   
	 cell.innerHTML = "<a href=\"" + link + "\"><b>" + result.Address1 + "</b></a><br/>";
	 cell.innerHTML += result.City + "&nbsp;" + result.State + "&nbsp;" + result.Zip + "<br/>";
	 cell.innerHTML += result.Phone;
	 cell.style.width = width + "px";
	 return cell;
			   
}

function BuildComingSoonCell(width)
{
    var cell = document.createElement("td");
//	cell.setAttribute("style", "text-align:center;font-weight: bold;color:#cb0000;");
 //       cell.style.textAlign = "center";
	cell.innerHTML = "<img src='/images/locator/coming_soon.gif' alt='Coming Soon' width='41' height='20' border='0' />";
//	cell.style.width = width +"px";
	return cell;
}

function BuildDistanceCell( distance, width )
{
    var units = " Miles";
    distance = parseFloat( distance );
	
	if ( document.location.hostname.indexOf("redlobster.ca") != -1 )
	{
	   units = " Km";
	   distance = distance * 1.6; 
	}
	
	var cell = document.createElement("td");
	cell.setAttribute("style", "text-align:center;font-weight:bold;");
        cell.style.textAlign = "center";
        cell.style.fontWeight = "bold";
	cell.innerHTML = distance.toFixed(2) + units;
        cell.style.width = width + "px";
	
	return cell;
}

function BuildMenuCell(id, width)
{
    var cell = document.createElement("td");
	cell.setAttribute("style", "text-align:center;");
        cell.style.textAlign = "center";
	cell.innerHTML = "<a href=\"/locator/redir/menu.asp?busid=" + id  +"\"><img src=\"../images/locator/button_view_menu.gif\" title=\"View Menu\" alt=\"View Menu\" width=\"118\" height=\"20\" /></a>";
	cell.style.width = width +"px";
	return cell;
}

function BuildDirectionsCell( link, width, id )
{
    var cell = document.createElement("td");
	cell.setAttribute("style", "text-align:center;");
        cell.style.textAlign = "center";
	cell.innerHTML = "<a href=\"/locator_ve/redir/directions.asp?busid=" + id  +"\"><img src=\"../images/locator/button_driving_directions.gif\" title=\"Driving Directions\" alt=\"Driving Directions\" width=\"178\" height=\"20\" /></a>";
	//cell.innerHTML = "<a href=\"" + link +"\"><img src=\"../images/locator/button_driving_directions.gif\" title=\"Driving Directions\" alt=\"Driving Directions\" width=\"178\" height=\"20\" /></a>";
	cell.style.width = width +"px";
	return cell;
}



