﻿var scGoogleWebSearch;
var nNumSmallPageResults = 4;
var nNumLargePageResults = 8;
        
google.load('search', '1');
            
function PopulatePaginationNav() {
    var sPaginationHTML = "";
    // The cursor object has all things to do with pagination
    var cGoogleCursor = scGoogleWebSearch.cursor;
    // Get the current page
    var nCurrentPage = cGoogleCursor.currentPageIndex;
    // Variable set to each page
    var pGooglePage;
    
    for (var i = 0; i < cGoogleCursor.pages.length; i++) {
        pGooglePage = cGoogleCursor.pages[i];
        
        if (i == nCurrentPage) {
            sPaginationHTML += " " + pGooglePage.label + " / ";
        }
        else {
            sPaginationHTML += " " + "<a class=\"search-results-pagination\" href='javascript:scGoogleWebSearch.gotoPage(" + i + ");'>" + pGooglePage.label + "</a>" + " / ";
        }
    }
    
    $('#resultsPagination').html(sPaginationHTML);
    $('#resultsPaginationFooter').html(sPaginationHTML);
}

function GetMinResultCount() {
    var nCurrentPage = scGoogleWebSearch.cursor.currentPageIndex + 1
    return (((nCurrentPage - 1) * nNumSmallPageResults) + 1);
}

function GetMaxResultCount() {
    var nCurrentPage = scGoogleWebSearch.cursor.currentPageIndex + 1
    var nMaxResultCount = (((nCurrentPage - 1) * nNumSmallPageResults) + nNumSmallPageResults);
    var nTotalResultCount = GetTotalResultCount();
    
    if (nMaxResultCount <= nTotalResultCount) {
        return nMaxResultCount;
    }
    else {
        return nTotalResultCount;
    }
}

function GetTotalResultCount() {
    return scGoogleWebSearch.results.length * scGoogleWebSearch.cursor.pages.length;
}

function searchComplete() {
    var sResultsHTML = "";
    var rGoogleResults;
    var rGoogleResult;
    
    if (scGoogleWebSearch.results && scGoogleWebSearch.results.length > 0) {
        // Loop through our results, printing them to the page.
        var rGoogleResults = scGoogleWebSearch.results;
        for (var i = 0; i < rGoogleResults.length; i++) {
            rGoogleResult = rGoogleResults[i];
            sResultsHTML += "<div class=\"search-results\"><a class=\"header\" href=\"" + unescape(rGoogleResult.url) + "\">" + rGoogleResult.titleNoFormatting + "</a><br />" + rGoogleResult.content + "<br /><a class=\"url\" href=\"" + unescape(rGoogleResult.url) + "\">" + unescape(rGoogleResult.url) + "</a></div>";
        }
    }
    
    // Populate the results with our string
    $('#searchResults').html(sResultsHTML);
    
    // Populate the result count info
    $('#resultsCount').html("<i>Showing results " + GetMinResultCount() + " - " + GetMaxResultCount() + " of " + GetTotalResultCount());
    
    // Populate the pagination
    PopulatePaginationNav();
}

function OnLoad() {
//  trace('OnLoad');
  // Create a search control
  scGoogleWebSearch = new google.search.WebSearch();

  // Add in a full set of searchers
  //searchControl.addSearcher(new google.search.WebSearch());
  
  // Set the Search Control to get the most number of results
  scGoogleWebSearch.setResultSetSize(google.search.Search.SMALL_RESULTSET);
  
  // Search our account which has arnoldbread and arnold.bimbobakeries
  //scGoogleWebSearch.setUserDefinedLabel("arnoldbread.com");
  //scGoogleWebSearch.setUserDefinedClassSuffix("scGoogleWebSearch");
  //scGoogleWebSearch.setSiteRestriction("arnold.bimbobakeries.com");
 // scGoogleWebSearch.setSiteRestriction("002129076921819381850:zb66o7kfghe");

	scGoogleWebSearch.setSiteRestriction("002129076921819381850:hkv5_dkmm6k");

  // We want to format the results so create a callback function
  scGoogleWebSearch.setSearchCompleteCallback(this, searchComplete, null);
    
  //var options = new google.search.SearcherOptions();
  //options = new google.search.SearcherOptions();
  //options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  //searchControl.addSearcher(new google.search.WebSearch(), options);
  
  //var drawOptions = new google.search.DrawOptions();
  //drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_LINEAR);
  
  // This separates the search form so we can hide it. We do not need because we are
  // manually parsing through the results
  //drawOptions.setSearchFormRoot('searchForm2');
  
  //searchControl.draw('searchResults', drawOptions);

  // execute our search
  scGoogleWebSearch.execute($.query.get('sTerms'));
  
  $('#resultsHeader').html("Results for \"" + unescape($.query.get('sTerms')) + "\"");
}

google.setOnLoadCallback(OnLoad);