//init a google search object
google.load('search', '1');
//init a search object
var gSearch;

function searchComplete() {
  // Check that we got results
  if (gSearch.results && gSearch.results.length > 0) {
    // Grab our content div, clear it.
    var contentDiv = document.getElementById('content');
    contentDiv.innerHTML = '';

    // Loop through our results, printing them to the page.
    var results = gSearch.results;
    for (var i = 0; i < results.length; i++) {
      // For each result write it's title and image to the screen
      var result = results[i];
      var resultContainer = document.createElement('div');

	  if (result.image == undefined)
	  {
		newspic="<img style='float:left;margin:4px;border:2px solid black;width:200px;height:150px;' src='http://www.shrinktheweb.com/xino.php?embed=1&STWAccessKeyId=c4ce7a3898d6307&Size=lg&stwUrl=" + result.url + "'>";
	  } else {
		newspic="<div style='font-size:9px;float:left;margin:4px;padding:4px;text-align:center;border:2px solid black;'><img onmouseover='this.src=\""+result.image.url+"\"' onmouseout='this.src=\""+result.image.tbUrl+"\"' src='"+ result.image.tbUrl +"'><br>Image source:<br><a href="+result.image.originalContextUrl+">"+ result.image.publisher +"</a></div>";	  
	  }

    newsbody= 
	  "<div class='presult' onclick='location.href=\""+result.unescapedUrl+"\";'>"+newspic+"<h3><a href='" + result.unescapedUrl + "'>" + result.title + "</a></h3><span style='color:#061282;font-size:10px'>"+result.publisher+" - "+result.publishedDate+"</span><br>" + result.content +"<br><span style='color:#286e38;font-style:italic'>"+result.unescapedUrl+"</span>";
	  
	if (result.relatedStories && result.relatedStories.length > 0) {
	var related = result.relatedStories;
		newsbody = newsbody + "<div style='text-align:center'><H3>Related:</H3>";
		for (var j = 0; j < related.length; j++) {
			rel = related[j];
			newsbody = newsbody + "&nbsp;+&nbsp;<a href='"+rel.unescapedUrl+"'>"+rel.title+"</a>";		
		}
		newsbody = newsbody + "&nbsp;+&nbsp;</div>";
	}
	
	newsbody = newsbody + "</div>";
	resultContainer.innerHTML = newsbody;
	
      contentDiv.appendChild(resultContainer);
	  newspic = '';
    }

    // Now add the paging links so the user can see more results.
    addPaginationLinks(gSearch);
  }
}

function OnLoad() {
  // Our gSearch instance.
  gSearch = new google.search.NewsSearch();

  gSearch.setSearchCompleteCallback(this, searchComplete, null);
  gSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET)
  
  searchterm=querySt('s');
  if (searchterm!=null && searchterm!="")
  {
	document.getElementById('s').value = searchterm;
  }else{
	searchterm=window.location.hash.replace(/#/gi, "");
	if (searchterm!=null && searchterm!="")
	{
		document.getElementById('s').value = searchterm;
	} else {	
		searchterm=getCookie('sterm');
		if (searchterm!=null && searchterm!="")
		{
			document.getElementById('s').value = searchterm;
		}
	}
  }  
  
  if(document.getElementById('s').value=='Search'){
      gSearch.execute("today");
  }else{
      gSearch.execute(document.getElementById('s').value);
  }
     document.getElementById('s').focus();
	 // here is the new search code
	currenthash = '';
	searchtype = '1';
	timedSearch();
}
google.setOnLoadCallback(OnLoad);