// 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 timeFormat(s) {
	s = new String(s);
	s = s.replace(':00', '').replace(' A.M.', 'AM').replace(' P.M.', 'PM');
	return s;
}

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, newhours, 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 = timeFormat(sun_thurs_open);
	if(newhours == 'True' && this.Hours_Sun_Thurs_Open == '11AM') {
		this.Hours_Sun_Thurs_Open = '4PM';
}
	this.Hours_Sun_Thurs_Close = timeFormat(sun_thurs_close);
	this.Hours_Fri_Sat_Open = timeFormat(fri_sat_open);
	if(newhours == 'True' && this.Hours_Fri_Sat_Open == '11AM') {
	this.Hours_Fri_Sat_Open = '4PM';
	}
	this.Hours_Fri_Sat_Close = timeFormat(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.newhours = newhours;
	this.comingsoon = comingsoon;
}
 
var numResults = 0;
var displayLowerIndex = 0;
var displayUpperIndex = 14;
var resultsLayer;

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 += 15;
	displayUpperIndex +=15;
	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 -= 15;
    if ( displayLowerIndex <= 0 )
    {
	displayLowerIndex = 0;
  	document.getElementById("prevButton").style.display = "none";
    }
	
    displayUpperIndex = displayLowerIndex + 14
    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 IconClick(eventArgs)
{ 
	var  shape = map.GetShapeByID(eventArgs.elementID);
	if (shape)
	{
		var id = shape.GetTitle();
		if(!id) return false;   //  if no restaurant id don't leave page - this is for central star
		//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/redir/menu.asp?RestaurantID=" + id ;  
			window.location ="/locator/redir/menu.asp?busid=" + id ;  
	
		}
	}
}

function SuppressInfoBox( eventArgs ){ return true; }

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 ) ));
			//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("onclick", IconClick);
			map.AttachEvent("onmouseover", SuppressInfoBox);
			if ( i > 14 )
            {
				shape.Hide();
			}
		}
	}

	if ( ResultsArray.length <= 15 )
	{
		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("og_locator_border_box");

		var table = document.createElement("table");
		table.setAttribute("class", "og_locator_hits");
		table.className = "og_locator_hits";
		table.setAttribute("cellSpacing", "0");
		table.setAttribute("cellPadding", "0");
		table.setAttribute("border", "0");
		table.setAttribute("width", "818");
		table.setAttribute("align", "center");
		var tbody = document.createElement("tbody");
		
		var headerRow = document.createElement("tr");
		headerRow.setAttribute("class", "og_locator_tr_header");
		headerRow.className = "og_locator_tr_header";
		headerRow.setAttribute("style", "margin: 0px 0px 0px 0px;padding: 0px 0px 0px 0px;");
		headerRow.appendChild( BuildResultsTableHeaderCell( "<img src='/images/locator/tr_header_left.gif' alt='' width='5' height='21' border='0' />" , "", "7" , "") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "No." , "", "42" , "") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "View Menu" , "", "125" , "text-align: center;") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "Address" , "", "200" , "text-align: center;") ); 
	//	headerRow.appendChild( BuildResultsTableHeaderCell( "" , "", "1" , "") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "Hours of Operation" , "text-align: center;", "160" , "") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "Distance" , "", "88" , "text-align: center;") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "Get Directions" , "", "164" , "text-align: center;") );
		headerRow.appendChild( BuildResultsTableHeaderCell( "<img src='/images/locator/tr_header_right.gif' alt='' width='5' height='21' border='0' />" , "", "5" , "text-align: right;") );
		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. Please search again.";
	}
}

// Creates a row in the results table
function BuildResultsTableRow( result, idx )
{
	var row = document.createElement("tr");
	var tdClass = "";
	
	if ( idx % 2 == 0 )
	{
	   row.setAttribute("class", "color_odd");
	   row.className = "color_odd";
	   tdClass = "color_odd";
	}
	else
	{
	   row.setAttribute("class", "color_even");
	   row.className = "color_even";
	   tdClass = "color_even";
	}

	//we only want to show the first 4 elements
	if (idx > 14)
	{
	   row.style.display="none";
	}

	//row.setAttribute("valign", "top");
	row.id = "resultRow" + idx;
	
	row.appendChild( BuildResultsTableCell( "&nbsp;" , tdClass, "" , "") );
	row.appendChild( BuildResultsTableCell( "<div class='pinStyle'>" + (idx + 1) + "</div>" , "", "", "vertical-align:middle;" ) );
	if (result.ID == '1824') 
	{
		row.appendChild( BuildResultsTableCell( "" , tdClass, "", "text-align:center;" ) );
	row.appendChild( BuildResultsTableCell( "" + result.Address1 + "</a><br/>" + result.City + " " + result.State + " " + result.Zip + "<br/>" + result.Phone, tdClass, "", "") );
		
	}
	else 
	{
	row.appendChild( BuildResultsTableCell( "<a href='/locator/redir/menu.asp?busid=" + result.ID + "'><img class='og_locator_button' src='/images/locator/button_view_menu.gif' alt='View Menu' width='105' height='18' border='0' /></a>" , tdClass, "", "text-align:center;" ) );
	row.appendChild( BuildResultsTableCell( "<a href='/locator/redir/menu.asp?busid=" + result.ID + "'>" + result.Address1 + "</a><br/>" + result.City + " " + result.State + " " + result.Zip + "<br/>" + result.Phone, tdClass, "", "" ) );
	}
	/* startCAS 
	Array.prototype.find = function(searchStr) {
	  var returnArray = false;
	  for (i=0; i<this.length; i++) {
	    if (typeof(searchStr) == 'function') {
	      if (searchStr.test(this[i])) {
	        if (!returnArray) { returnArray = [] }
	        returnArray.push(i);
	      }
	    } else {
	      if (this[i]===searchStr) {
	        if (!returnArray) { returnArray = [] }
	        returnArray.push(i);
	      }
	    }
	  }
	  return returnArray;
	}
	*/
	/* This array contains all the locations with Call-Ahead Seating.  Any time you change this array, you must also change the equivalient array in /ssi/declarations.inc
	
	arrCAS = new Array("1031", "1035", "1129", "1481", "1509" , "1575" , "1699" , "1743" , "1760" , "1051" , "1195" , "1197" , "1748" , "1226" , "1237", "1443", "1131", "1158", "1213", "1311", "1351", "1394", "1608", "1780", "1243", "1324", "1464", "1137", "1310", "1383", "1471", "1472", "1247", "1742", "1263", "1095", "1219", "1622", "1754", "1764", "1150", "1326", "1649", "1104", "1690", "1327", "1697", "1703", "1485", "1515", "1601", "1092", "1597", "1662", "1346", "1387", "1492", "1088", "1431", "1550", "1577", "1241", "1246", "1574", "1713", "1296", "1430", "1524", "1687", "1465", "1688", "1553", "1612", "1667", "1721", "1223", "1224", "1707");

	if(arrCAS.find(String(result.ID))) {
		row.appendChild( BuildResultsTableCell( "<img style='margin-right: 1px;' class='og_locator_button' src='/images/locator/cas_btn.gif' alt='Call us before you come in and if there is a wait, we will place your name on our list. Call-ahead seating is not a reservation.' title='Call us before you come in and if there is a wait, we will place your name on our list. Call-ahead seating is not a reservation.' width='82' height='38' border='0' />" , tdClass, "", "padding 0px;" ) ); 
	// cas
	} else {
		row.appendChild( BuildResultsTableCell( "" , tdClass, "", "text-align: center;" ) );
	}
	
	*/
/* end CAS */
	if ( String(result.comingsoon) == "1" )
	{
		row.appendChild( BuildResultsTableCell( "<img class='og_locator_button' src='/images/locator/coming_soon.gif' alt='Coming Soon' width='65' height='34' border='0' />" , tdClass, "", "text-align: left;padding 0px;" ) ); // comingsoon
	}
	else
	{
		row.appendChild( BuildResultsTableCell( result.Hours_Sun_Thurs_Open + "-" + result.Hours_Sun_Thurs_Close + " Sun.-Thurs." + "<br>" + result.Hours_Fri_Sat_Open + "-" + result.Hours_Fri_Sat_Close + " Fri. & Sat. ", tdClass, "", "" ) ); // return hours
	}
	
	
	row.appendChild( BuildResultsTableCell( BuildDistanceCell( result.dis ) , tdClass, "", "text-align: center;" ) );
	
	row.appendChild( BuildResultsTableCell( "<a href='/locator/redir/directions.asp?busid=" + result.ID + "'><img class='og_locator_button' src='/images/locator/button_driving_directions.gif' alt='Driving Directions' width='136' height='18' border='0' /></a>" , tdClass, "", "text-align:center" ) );

	return row;
}

function BuildResultsTableCell( strInnerHTML, strClass, strWidth , strStyle )
{
	var cell = document.createElement("td");

	if (strClass && strClass.length > 0)
	{
		cell.setAttribute("class", strClass);
		cell.className = strClass;
	}
	if (strWidth && strWidth.length > 0)
	{
		cell.setAttribute("width", strWidth);
	}	
	if (strStyle && strStyle.length > 0)
	{
		cell.setAttribute("style", strStyle);
	}	
	
	cell.innerHTML = strInnerHTML;
	
	return cell;		   
}

function BuildResultsTableHeaderCell( strInnerHTML, strClass, strWidth , strStyle )
{
	var cell = document.createElement("th");

	if (strClass && strClass.length > 0)
	{
		cell.setAttribute("class", strClass);
		cell.className = strClass;
	}
	if (strWidth && strWidth.length > 0)
	{
		cell.setAttribute("width", strWidth);
		cell.style.width = strWidth;
	}	
	if (strStyle && strStyle.length > 0)
	{
		cell.setAttribute("style", strStyle);
	}	
	
	cell.innerHTML = strInnerHTML;
	
	return cell;		   
}

function BuildDistanceCell( distance )
{
    var units = " Miles";
    distance = parseFloat( distance );
	
	if ( document.location.hostname.indexOf("olivegarden.ca") != -1 )
	{
	   units = " Km";
	   distance = distance * 1.6; 
	}
	
	var cell = document.createElement("td");
	cell.innerHTML = distance.toFixed(2) + units;

	return cell.innerHTML;
}

