google.load("search", "1");

var webSearch = null;

function addPaginationLinks() {
  // The cursor object has all things to do with pagination
  var cursor = webSearch.cursor;
  var curPage = cursor.currentPageIndex; // check what page the app is on
  var pagesDiv = document.createElement('div');
  
  var strPages = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>";
  for (var i = 0; i < cursor.pages.length; i++) {
    var page = cursor.pages[i];
    if (i) {
      strPages += " . ";
    }
    if (curPage == i) { // if we are on the curPage, then don't make a link
      strPages += page.label;
    } else {
      // If we aren't on the current page, then we want a link to this page.
      // So we create a link that calls the gotoPage() method on the searcher.
      strPages += "<a href='javascript:webSearch.gotoPage("+i+");'>" + page.label + "</a>";
    }
  }
  
  strPages = strPages + "</td><td align='right'><a href='javascript:window.location=\"" + cursor.moreResultsUrl + "\"'>More results from Google >></a></td></tr></table>";

  pagesDiv.innerHTML = strPages;

  var contentDiv = document.getElementById('searchpaging');
  contentDiv.innerHTML = "";
  contentDiv.appendChild(pagesDiv);
}

function searchComplete() {
  // Grab our content div, clear it.
  var contentDiv = document.getElementById('content');
  contentDiv.innerHTML = '';
  
  // Check that we got results
  if (webSearch.results && webSearch.results.length > 0) {    
    var results = webSearch.results;
    for (var i = 0; i < results.length; i++) {
      var result = results[i];

      var resultContainer = document.createElement('div');
      resultContainer.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td class='newsCell'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td class='courseList'><span class='eventTitle'>" + result.title + "</span><br><br>" + result.content + "<br><a href='" + result.unescapedUrl + "'>Read more</a></td></tr></table></td></tr></table>";
      contentDiv.appendChild(resultContainer);
    }
    // Now add the paging links so the user can see more results.
    addPaginationLinks(webSearch);
  }
  else {
    displayNoResults();
  }
}

function displayNoResults() {
  // Grab our content div, clear it.
  var contentDiv = document.getElementById('content');
  contentDiv.innerHTML = '';
  
  var resultContainer = document.createElement('div');
  resultContainer.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td class='newsCell'>Your search return no results.</td></tr></table>";    
  contentDiv.appendChild(resultContainer);
}
    
function initSearch(strSearchText) {
  if (strSearchText != undefined && strSearchText != "") {
    // Add in a full set of searchers
    webSearch = new google.search.WebSearch();
    webSearch.setSiteRestriction('006972439625759579140:h69je9elbbw');
    webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
  
    // Here we set a callback so that anytime a search is executed, it will call
    // the searchComplete function and pass it our webSearch searcher.
    // When a search completes, our webSearch object is automatically
    // populated with the results.
    webSearch.setSearchCompleteCallback(this, searchComplete, null);      
    // Execute an inital search
    webSearch.execute(strSearchText);
  }
  else {
    displayNoResults();
  }
}
