window.addEvent("domready", function()
{
	getVars = getUrlVars();
	if(getVars['q'] != undefined || document.getElementById("emptySearch"))
	{
		//document.getElementById("mainContent").style.display = 'none';
	}	
});

google.load('search', '1');
resultDraw = false;

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}

	return vars;
}

// gSearchBox
function LoadGoogleSearch() {
	// Create a search control
	searchControl = new google.search.SearchControl();

	// We want 8 results back
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);

	// Set link target
	searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);

	searchControl.setSearchCompleteCallback(window, function() 
	{
		if(o_searchResultTabs = $$('.gsc-resultsRoot'))
		{
			o_searchResultTabs.each(function(o_tabContent)
			{
				if(!o_tabContent.getElement('.gs-webResult'))
				{
					o_tabContent.innerHTML = '<br /><br />Er zijn geen resultaten gevonden.';
				}
			});
		}
	});
	
	// Add Hoppinger Search
	searcher = new google.search.WebSearch();
	searcher.setSiteRestriction({crefUrl : "http://www.nc2018.nl/googlexml/cref_cse.xml"});
	//searcher.setUserDefinedLabel("Actal site");

	var options = new google.search.SearcherOptions();
	options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

	searchControl.addSearcher(searcher, options);



	// Create the searchForm
	//var container = $("topSearchContainer");
	//searchForm = new google.search.SearchForm(false, container);
	//searchForm.setOnSubmitCallback(null, newSearch);

	// Check the URL for a search
	getVars = getUrlVars();
	if(getVars['q'] != undefined)
	{
		newSearch(null, getVars['q']);
	} else if(document.getElementById("emptySearch"))
	{
		newSearch(null, "");
	}


}

/*
	Search based on second argument or otherwise use the form input
*/
function newSearch(form, val) {
	if(val)
	{
		search = val;
	}
	else if(form && form.input)
	{
		search = form.input.value;
	}
	else
	{
		search = '';
	}

	if(!resultDraw)
	{
		DrawGoogleResults(search);
	}
	if (search != '')
	{
		searchControl.execute(search);
	}
	return false;
}

/*
	Draw the (still) empty results in the content div
*/
function DrawGoogleResults(searchWord)
{
	if(document.getElementById("wideContent"))
		document.getElementById("wideContent").id = "mainContent";
	document.getElementById("emptySearch").style.display = 'block';

	// ** If i put overflow on the body it will have a double scroll in IE **
	if(document.style)
	{
		document.style.overflowY = "scroll";
	}
	else
	{
		document.body.style.overflowY = "scroll";
	}

	/*var pageHeader = document.getElementById("pageHeader");
	var headerText = "";
	for(i = 0; i < pageHeader.childNodes.length; i++)
	{
		if(pageHeader.childNodes[i].tagName == "H1")
		{
			headerText = pageHeader.childNodes[i].innerHTML;
			break;
		}
	}
	var pageHeader = document.createElement("H3");
	pageHeader.innerHTML = headerText;*/

	var googleDiv = document.createElement("div");
	googleDiv.id = "searchContent";
	var searchDiv = document.createElement("div");
	searchDiv.id = "searchContainer";
	searchDiv.style.marginBottom = '-14px';
	searchDiv.style.marginBottom = '-16px';
	var searchBox = document.getElementById("gSearchForm").cloneNode(true);
	for(i = 0; i < searchBox.childNodes.length; i++)
	{
		if(searchBox.childNodes[i].tagName == "DIV")
		{
			if(searchBox.childNodes[i].id != "")
			{
				searchBox2 = searchBox.childNodes[i];
			}
			else
			{
				searchBox.childNodes[i].style.display = 'block';
			}
		}
	}


	for(i = 0; i < searchBox2.childNodes.length; i++)
	{
		if(searchBox2.childNodes[i].type == "text")
		{
			searchBox2.childNodes[i].style.width = '320px';
			if(searchWord != '')
			{
				searchBox2.childNodes[i].value = searchWord;
				document.getElementById("topSearchTextField").value = searchWord;
			}
			else if(!document.getElementById("topSearchTextField2"))
			{
				searchBox2.childNodes[i].value = document.getElementById("topSearchTextField").value;
			}
			searchBox2.childNodes[i].id = "topSearchTextField2";
		}
		if(searchBox2.childNodes[i].type == "image")
		{
			searchBox2.childNodes[i].src = '/images/google_normal.png';
			searchBox2.childNodes[i].style.styleFloat = 'right';
			searchBox2.childNodes[i].style.cssFloat = 'right';
			searchBox2.childNodes[i].style.marginRight = '10px';
			searchBox2.childNodes[i].onmouseover = function() {this.src="/images/google_mouseover.png"};
			searchBox2.childNodes[i].onmouseout= function () {this.src="/images/google_normal.png"};
		}
	}
	document.getElementById("emptySearch").innerHTML = "";
	document.getElementById("emptySearch").style.display = 'block';
	//searchDiv.appendChild(pageHeader);
	searchDiv.appendChild(searchBox);
	document.getElementById("emptySearch").appendChild(searchDiv);
	document.getElementById("emptySearch").appendChild(googleDiv);


	// create a drawOptions object
	var drawOptions = new google.search.DrawOptions();
	//drawOptions.setSearchFormRoot(document.getElementById("topSearchContainer"));
	drawOptions.setInput(document.getElementById("topSearchTextField2"))
	// tell the searcher to draw itself in linear mode
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_LINEAR);
	// tell the searcher to draw itself and tell it where to attach
	searchControl.draw(document.getElementById("searchContent"), drawOptions);

	// take over the Google keyup event so both search fields are linked to eachother
	document.getElementById("topSearchTextField").onkeyup = function () {
		document.getElementById("topSearchTextField2").value = document.getElementById("topSearchTextField").value;
		searchControl.execute(document.getElementById("topSearchTextField2").value);
	}
	document.getElementById("topSearchTextField2").onkeyup = function () {
		document.getElementById("topSearchTextField").value = document.getElementById("topSearchTextField2").value;
		searchControl.execute(document.getElementById("topSearchTextField2").value);
	}

	// enable to remove the stylesheet that google loads (debugging only)
/*
	for(i = 0; i < document.head.childNodes.length; i++)
	{
		if(document.head.childNodes[i].href && document.head.childNodes[i].href == "http://www.google.com/uds/api/search/1.0/6cc925a597703a43cc65b5d7c705f351/default.css")
		{
			document.head.removeChild(document.head.childNodes[i]);
		}
	}
*/
	resultDraw = true;
	document.getElementById("searchIframe").style.display = 'none';
}

// Pop the function when the script is loaded
google.setOnLoadCallback(LoadGoogleSearch);
