﻿/*
Title: Google Site Search
Author: Ian Zajdel
*/

google.load('search', '1');
google.setOnLoadCallback(
function()
{
	var SearchControl = new google.search.CustomSearchControl('001169599044286445054:efs4ef-qrj8');
	SearchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
	SearchControl.draw('SearchResults');
	UpdateResults();
}
, true);

function UpdateResults()
{
	var Divs = document.getElementsByTagName("div");
	var Inputs = document.getElementsByTagName("input");
	var Forms = document.getElementsByTagName("form");

	var ResultsDiv;
	var CloseDiv;
	var SearchText;
	var SearchButton;

	for(var i = 0; i < Divs.length; i++)
	{
		if(Divs[i].className == "gsc-wrapper")
		{
			ResultsDiv = Divs[i];
		}
		else if(Divs[i].className == "gsc-clear-button")
		{
			CloseDiv = Divs[i];
		}
		else if(Divs[i].className == "gsc-adBlock" || Divs[i].className == "gsc-adBlockVertical" || Divs[i].className == "gsc-adBlockVerticalInvisible" || Divs[i].className == "adBlock" || Divs[i].className == "ad")
		{
			Divs[i].style.display = "none";
			Divs[i].style.visibility = "hidden";
		}
	}
	
	for(var i = 0; i < Inputs.length; i++)
	{
		if(Inputs[i].className == "gsc-search-button")
		{
			SearchButton = Inputs[i];
		}
		else if(Inputs[i].className == " gsc-input")
		{
			SearchText = Inputs[i];
		}
	}
	
	for(var i = 0; i < Forms.length; i++)
	{
		if(Forms[i].className == "gsc-search-box")
		{
			SearchForm = Forms[i];
		}
	}
	
	if(ResultsDiv != null)
	{
		ResultsDiv.style.backgroundColor = "#ffffff";
		ResultsDiv.style.border = "1px solid #999999";
		ResultsDiv.style.display = "block";
		ResultsDiv.style.maxHeight = "250px";
		ResultsDiv.style.minHeight = "120px";
		ResultsDiv.style.left = "50%";
		ResultsDiv.style.right = "10%";
		ResultsDiv.style.overflow = "auto";
		ResultsDiv.style.padding = "4px";
		ResultsDiv.style.position = "absolute";
		ResultsDiv.style.visibility = "hidden";
		ResultsDiv.style.zIndex = "100";
		
		if(SearchButton != null)
		{
			SearchButton.value = "";
		
			SearchButton.onclick = function()
			{
				if(SearchText != null && SearchText.value != null && SearchText.value != "")
				{
					ResultsDiv.style.backgroundColor = "#ffffff";
					ResultsDiv.style.border = "1px solid #999999";
					ResultsDiv.style.display = "block";
					ResultsDiv.style.maxHeight = "250px";
					ResultsDiv.style.minHeight = "120px";
					ResultsDiv.style.left = "50%";
					ResultsDiv.style.right = "10%";
					ResultsDiv.style.overflow = "auto";
					ResultsDiv.style.padding = "4px";
					ResultsDiv.style.position = "absolute";
					ResultsDiv.style.visibility = "visible";
					ResultsDiv.style.zIndex = "100";
				}
			};
		}
		
		if(SearchText != null)
		{
			SearchText.onkeypress = function(event)
			{
				if(!event)
				{
					event = window.event;
				}
				
				var key = event.charCode ? event.charCode : event.keyCode;
				
				if(key == "13" && SearchText.value != null && SearchText.value != "")
				{
					ResultsDiv.style.display = "block";
					ResultsDiv.style.backgroundColor = "#ffffff";
					ResultsDiv.style.border = "1px solid #999999";
					ResultsDiv.style.maxHeight = "250px";
					ResultsDiv.style.minHeight = "120px";
					ResultsDiv.style.left = "50%";
					ResultsDiv.style.right = "10%";
					ResultsDiv.style.overflow = "auto";
					ResultsDiv.style.padding = "4px";
					ResultsDiv.style.position = "absolute";
					ResultsDiv.style.visibility = "visible";
					ResultsDiv.style.zIndex = "100";
					
					return true;
				}
			};
			
			SearchText.onblur = function()
			{
				SearchText.style.background = "none";
			};
			
			SearchText.style.background = "none";
		}
		
		if(CloseDiv != null)
		{
			CloseDiv.onclick = function()
			{
				ResultsDiv.style.display = "none";
				ResultsDiv.style.visibility = "hidden";
				
				if(SearchText != null && SearchText.value != null)
				{
					SearchText.value = "";
					SearchText.focus();
				}
			};
		}
	}
}

