// CSS to hide bits of the Google HTML
// #googleSearch { padding: 0.5em; }
// .gs-watermark /*Google watermark*/, .gs-visibleUrl-short /*displays domain name*/ { display: none; }
// .gsc-branding, .gsc-result { padding-bottom: 0.5em; }


function executeSearch(search) { 

    var webSearch = new GwebSearch(); 
	webSearch.setSiteRestriction('http://www.nottingham.ac.uk/csc/course/');
	webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	webSearch.setLinkTarget(google.search.Search.LINK_TARGET_SELF);

	// execute an inital search 
    webSearch.setSearchCompleteCallback(null, showResults, [webSearch]); 
    webSearch.execute(document.getElementById(search).value); 
} 
function showResults(webSearch) { 
    // show web results 
    var webResults = document.getElementById("searchResults"); 
    
    if (webSearch.results && webSearch.results.length > 0) 
    { 
        for (var i = 0; i < webSearch.results.length; i++) 
        { 
            var result = webSearch.results[i]; 
            webResults.appendChild(result.html.cloneNode(true)); // bug here ?????
        } 
    } 
    else
    {
		var noResultElement = document.createTextNode('No result found.');
    	webResults.appendChild(noResultElement); 
    }

	// show Google branding 
    GSearch.getBranding(document.getElementById("searchBranding"));
} 


// http://www.snook.ca/archives/javascript/your_favourite_1#c2532
document.getElementsByClassName = function(className)
{   var outArray = new Array();
    var item;
    try {
        var xpathResult = document.evaluate('//*[@class = "' + className + '"]', document, null, 0, null);
        while (item = xpathResult.iterateNext())
            outArray[outArray.length] = item;
    }
    catch(err) {
        // ie fix
        var currentIndex = 0;
        var allElements = document.getElementsByTagName('*');
        for(var i=0; i < allElements.length; i++)
        {   if(allElements[i].className.match(className))
            {   outArray[currentIndex] = allElements[i];
                currentIndex++;
            }
        }
    }
    return outArray;
}

// Get the searchString before the page has completely loaded
var searchString = document.getElementById('Search').value; 

if (searchString != '')
{
	// Load the Google Search API *before* the onload event is called
	document.write(unescape("%3Cscript src='http://www.google.com/uds/api?file=uds.js&v=1.0' type='text/javascript'%3E%3C/script%3E"));	
}

	
window.onload = function(){
    	
	// This should be called after the Google Search API has finished loaded

	// Check we're on a search page	
	if (searchString != '')
	{
		
		// Add a new div to the end of div.content-search 	
		addHtml = '<div id="googleSearch"><h2>Short Courses</h2><p>Search for "' + searchString  + '" on the <a href="http://www.nottingham.ac.uk/csc/">Central Short Courses</a> website</p><div id="searchBranding"></div><div id="searchResults"></div></div>';		
		document.getElementsByClassName('content-search')[0].innerHTML += addHtml;			
		
		// get the search string from input#Search
		executeSearch('Search');
	}
};
