;(function ($){
	$.ui.ajaxpopup.subclass('ui.googleSearch', {
		_init: function (){
			var self = this, form = self.element.closest('form');
			self.contents = this.options.contents;
			var websearch = new google.search.WebSearch();
			websearch.setSiteRestriction(form.find('[name=sitesearch]').val());
			websearch.setLinkTarget (google.search.Search.LINK_TARGET_SELF);
			websearch.setSearchCompleteCallback(self, function(){
				// create the backup url from the original form
				self.searchurl = form.attr('action')+'?'+form.serialize();
				// if the ajax search can't find it, use full Google (why google.com/search finds things that google.com?q=site:xxx doesn't is beyond me)
				if (websearch.results.length == 0) window.location = self.searchurl;
				self.contents = $.map(websearch.results, function(x) {return x.html} );
				self._trigger('found', 0, [websearch.results]);
				self.show();
			});
			form.submit(function(evt){
				websearch.execute (self.element.val());
				return false;
			});
		},
		show: function(){
			this.box().find('a.moreresults').attr('href', this.searchurl);
			this.box().find('div.resultsdiv').html(this.contents);
			this._super();
		},
		options: {
			url: '/inc/search.html',
			trigger: null,
			contents: $([]) // the initial value of the search results DIV. Could be set to a loading... image
		}
	});

})(jQuery);
