/* Site search with the Google AJAX Search API.
 *
 * http://snarfed.org/space/site+search+with+the+Google+AJAX+Search+API
 * Copyright 2006 Ryan Barrett <pyblosxom@ryanb.org>.
 * Licensed under GPL v2.
 */
function site_search(domain, query) {	
  // must not be empty
  if (!query.match('\\S')) {
    alert('Please enter a search.');
    return;
  }
  this.query = query;

  // save the current page
  if (document.getElementById('old' + results_div_id))
   site_search_back();

  old_page = document.getElementById(results_div_id);  
  old_page.id = 'old' + results_div_id
  old_page.style.display = 'none';

  // tell the user we're searching
  this.page = document.createElement('div');
  this.page.id = results_div_id;
  this.page.setAttribute('class', results_div_class);  
  old_page.parentNode.insertBefore(this.page, old_page);

  // set up the search
  this.gwebsearch = new google.search.WebSearch();
  this.gwebsearch.setNoHtmlGeneration();
  this.gwebsearch.setResultSetSize(GSearch.LARGE_RESULTSET);  
  this.gwebsearch.setLinkTarget(GSearch.LINK_TARGET_SELF);
  this.gwebsearch.setSiteRestriction(domain);  

  //this.gwebsearch.clearResults();
  this.gwebsearch.setSearchCompleteCallback(this, site_search.prototype.done); 
  
  // go!
  this.gwebsearch.execute(query);
}

site_search.prototype.done = function() { 
  this.clear();  
  // set up the page
  var div = createDiv('[<a href="#" onclick="site_search_back(); return false;"><< back to ' + document.title + '</a>]','gsc-back');
  this.page.appendChild(div);
  div = createDiv('Results for <b>' + this.query + '</b>:</div>','gsc-title');
  this.page.appendChild(div);
  
  var gwebsearch = this.gwebsearch;
  var results = gwebsearch.results;  
  this.page.innerHtml = ''; 
  
  for (i = 0; i < results.length; i++) {
	var result = results[i];	  
	var div = createDiv("<p><a href='" + 
		result.url + "'>" + result.title + "</a><br/>" + result.content + "<br/>" + 
		"<span class='greenstyle'>" + result.url + "</span><br/>" +		
		"</p>","gs-snippet");	
    this.page.appendChild(div);
  }  

  cursor = gwebsearch.cursor;
  if (!cursor && results.length == 0) {
		this.page.innerHTML += '<p>No results.</p>';	
  } else {
	  if(cursor) {
	  	var cursorContainer = createDiv(null,"gsc-cursor");	 		
		for(i = 0; i < cursor.pages.length; i++) {		
			var cursorNumberClass = "linkStyle2";		
			if(i == cursor.currentPageIndex)			
				cursorNumberClass += " selected";				
			
			var spanTag = document.createElement("span");
			spanTag.setAttribute("class",cursorNumberClass);			
			spanTag.innerHTML = " " + cursor.pages[i].label + " ";
			if(i != cursor.currentPageIndex) {						
				spanTag.onclick = bindMethod(this,this.gotoPage,[gwebsearch,i]);
			}			
			cursorContainer.appendChild(spanTag);
		}
		this.page.appendChild(cursorContainer);
		div = createDiv('<a href="' + cursor.moreResultsUrl + '">More Results...</a>');
		this.page.appendChild(div);		
	  }
  }
}

site_search.prototype.clear = function() {
	removeChildren(this.page);
}

site_search.prototype.gotoPage = function(gwebsearch,pageIndex) {	
	gwebsearch.gotoPage(pageIndex);
}

function bindMethod(obj,method,arg) {
	return function() {		
		method.apply(obj,arg);
	}
}

function removeChildren(parent) {
	while (parent.firstChild) {
		parent.removeChild(parent.firstChild);
    }
}

function createDiv(opt_text, opt_className) {
	var el = document.createElement("div");
	if (opt_text) {
		el.innerHTML = opt_text;
	}
	if (opt_className) { el.className = opt_className; }
	return el;
}

function site_search_back() {
  results = document.getElementById(results_div_id);  

  old_page = document.getElementById('old' + results_div_id);
  old_page.id = results_div_id;
  old_page.style.display = '';

  results.parentNode.removeChild(results);
}

