$(function () {
  // Make external links open in new window
  $('.extlink a,a.extlink').click(function () {
    window.open(this.href);
    return false;
  });
});

$(document).ready(function () {

  // Google Site Search
  var config = {
    siteURL: 'primecare.uk.net', // Change this to your site
    searchSite: true,
    type: 'web',
    append: false,
    perPage: 8, 		// A maximum of 8 is allowed by Google
    page: 0				// The start page
  }

  $('#submitButton').click(function () {
    googleSearch();
    return false;
  });

  function googleSearch(settings) {
    settings = $.extend({}, config, settings);
    settings.term = settings.term || $('#s').val();
    settings.term = 'site:' + settings.siteURL + ' ' + settings.term;
    var apiURL = 'http://ajax.googleapis.com/ajax/services/search/' + settings.type + '?v=1.0&callback=?';
    var resultsDiv = $('#resultsDiv');
    var sTerm = $('#s').val();

    $.getJSON(apiURL, { q: settings.term, rsz: settings.perPage, start: settings.page * settings.perPage }, function (r) {
      var results = r.responseData.results;
      $('#more').remove();
      if (results.length) {
        // If results were returned, add them to a pageContainer div, after which append them to the #resultsDiv:
        var pageContainer = $('<div class="pageContainer">');
        pageContainer.append('<h1>Your search results for the term "' + sTerm + '"...</h1>')
        for (var i = 0; i < results.length; i++) {
          // Creating a new result object and firing its toString method:
          pageContainer.append(new result(results[i]) + '');
        }

        if (!settings.append) {
          // This is executed when running a new search instead of clicking on the More button:
          resultsDiv.empty();
        }
        pageContainer.append('<div class="clear"></div>')
							   .hide().appendTo(resultsDiv)
							   .fadeIn('slow')
                  .click(function () {
                    $(this).fadeOut();
                  })

        var cursor = r.responseData.cursor;

        // Checking if there are more pages with results and deciding whether to show the More button:
        if (+cursor.estimatedResultCount > (settings.page + 1) * settings.perPage) {
          $('<div>', { id: 'more' }).appendTo(resultsDiv).click(function () {
            googleSearch({ append: true, page: settings.page + 1 });
            $(this).fadeOut();
          });
        }
      }
      else {
        // No results were found for this search.
        resultsDiv.empty();
        $('<p class="notFound">No Results Were Found!</p>').hide().appendTo(resultsDiv).fadeIn();
      }
    });
  }

  function result(r) {
    var arr = [];
    arr = [
					  '<div class="webResult">',
					  '<h3><a href="', r.unescapedUrl, '" target="_blank">', r.title, '</a></h3>',
					  '<p>', r.content, '</p>',
					  '<a href="', r.unescapedUrl, '" target="_blank">', r.visibleUrl, '</a>',
					  '</div>'
				  ];
    this.toString = function () {
      return arr.join('');
    }
  }
});

