if (typeof addLoadEvent == 'undefined')
{
function addLoadEvent(func,arg){
  if (!arg){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        oldonload();
        func();
      }
    }
  } else {  
    /*if the onload event has an argument/parameter cater for that*/
    if (arg){
      oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func(arg); 
      }
      window.onload = function() {
        oldonload();
        func(arg); 
      }
    }
  }
}
}

/******
  TIDEWAY SEARCHBOX BEHAVIOUR
******/
var searchControl = null;
var searchSearch = null;
var searchQuery = null;
var searchTime = null;
function executeSearch()
{
	searchSearch = new google.search.WebSearch();
	var options = new google.search.SearcherOptions();
	options.setNoResultsString('Sorry, but nothing was found for your request');
	if(googleSearchCSE)
		searchSearch.setSiteRestriction({cseId: googleSearchCSE});

	searchControl = new google.search.SearchControl();
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	searchControl.addSearcher(searchSearch, options);
	searchControl.setSearchCompleteCallback(null,searchComplete);
	searchControl.setSearchStartingCallback(null,searchStart);
	var drawOptions = new google.search.DrawOptions();
	//drawOptions.setSearchFormRoot(document.getElementById());
	drawOptions.setInput(document.getElementById("q"));
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
	searchControl.draw(document.getElementById(googleSearchIframeName), drawOptions);

	refreshSearchTitle();
	if(googleSearchQuery)
		searchControl.execute(googleSearchQuery);
}
function searchStart(conrtol,search,query)
{
	searchQuery = query;
	searchTime = new Date().getTime();
	if(document.getElementById('searchHeaderStats'))
		document.getElementById('searchHeaderStats').style.display = 'none';
}
function searchComplete(search,control)
{
	refreshSearchTitle();
	if(typeof(control)=='undefined') return;
	searchTime = new Date().getTime() - searchTime;
	searchTime = Math.floor(searchTime/10)/100;

	if(document.getElementById('searchHeaderStats'))
	{
		if(control.results.length > 1)
			document.getElementById('searchHeaderStats').style.display = 'block';
		document.getElementById("searchHeaderStatsStart").innerHTML = parseInt(control.cursor.pages[control.cursor.currentPageIndex].start)+1;
		document.getElementById("searchHeaderStatsEnd").innerHTML = parseInt(control.cursor.pages[control.cursor.currentPageIndex].start)+control.results.length;
		document.getElementById("searchHeaderStatsEst").innerHTML = control.cursor.estimatedResultCount;
		document.getElementById("searchHeaderStatsQ").innerHTML = searchQuery;
		//document.getElementById("searchHeaderTime").innerHTML = searchTime;
	}else
	{
		var s = '<table border=0 cellpadding=0 cellspacing=0 width=100% class="t bb">';
		s += '<tr><td nowrap></td><td align=right nowrap><font size=-1>';
		s += 'Results <b id="searchHeaderStatsStart">'+(parseInt(control.cursor.pages[control.cursor.currentPageIndex].start)+1)+'</b> - ';
		s += '<b id="searchHeaderStatsEnd">'+(parseInt(control.cursor.pages[control.cursor.currentPageIndex].start)+control.results.length)+'</b> of about ';
		s += '<b id="searchHeaderStatsEst">'+control.cursor.estimatedResultCount+'</b> for <b id="searchHeaderStatsQ">'+searchQuery+'</b>';
		s += '.  &nbsp;</font></td></tr></table>';
		//s += '.  (<b id="searchHeaderTime">'+searchTime+'</b> seconds)&nbsp;</font></td></tr></table>';
		var e = document.createElement("DIV");
		e.id = 'searchHeaderStats';
		e.innerHTML = s;
		var p = $$("#"+googleSearchIframeName+" .gsc-control");
		if (typeof (p[0])=='undefined') return;
		p = p[0];
		p.insertBefore(e, p.firstChild);
	}
}
var searchScopeHideTimer = null;
var searchTitleShowTimer = null;
var searchInput = null;
var searchButton = null;
var searchDontSendTimer = null;
var searchInputFocus = false;
function showSearchScope()
{
	if(searchScopeHideTimer != null) {window.clearTimeout(searchScopeHideTimer); searchScopeHideTimer = null;}
	var o = document.getElementById('search_selector_dropdown_list');
	o.style.display = (o.style.display=='block')?'none':'block';
}
function hideSearchScope()
{
	var o = document.getElementById('search_selector_dropdown_list');
	o.style.display = 'none';
}
function setSearchScope(o)
{
	hideSearchScope();
	document.getElementById('search_scope').innerHTML = o.title;
	o = o.getElementsByTagName("IMG")[0];
	var v = o.style.backgroundPosition.replace(/[^\-1234567890 ]/g,'');
	
	if(v == '0 0')
	{
		document.getElementById('search_selector_dropdown').style.backgroundPosition = "0px 0px";
		document.getElementById('search_selector_dropdown_list').className = "sel_whole";
		if(searchInput)
			document.getElementById('q').value = searchInput.value;
		searchInput = document.getElementById('q');
		searchButton = document.getElementById('sa');
		document.getElementById('cse-search-box').style.display = 'block';
		document.getElementById('quick-search').style.display = 'none';
		document.getElementById('forum-search').style.display = 'none';
		searchHandlers();
	}else if(v == '-48 0')
	{
		document.getElementById('search_selector_dropdown_list').className = "sel_forum";
		document.getElementById('search_selector_dropdown').style.backgroundPosition = "0px -78px";
		if(searchInput)
			document.getElementById('forum-search-q').value = searchInput.value;
		searchInput = document.getElementById('forum-search-q');
		searchButton = document.getElementById('forum-search-submit');
		document.getElementById('cse-search-box').style.display = 'none';
		document.getElementById('quick-search').style.display = 'none';
		document.getElementById('forum-search').style.display = 'block';
		searchHandlers();
	}else
	{
		if(searchInput)
			document.getElementById('quick-search-q').value = searchInput.value;
		searchInput = document.getElementById('quick-search-q');
		searchButton = document.getElementById('quick-search-submit');
		document.getElementById('cse-search-box').style.display = 'none';
		document.getElementById('quick-search').style.display = 'block';
		document.getElementById('forum-search').style.display = 'none';
		if(v == '-16 0')
		{
			document.getElementById('search_selector_dropdown_list').className = "sel_docs";
			document.getElementById('search_selector_dropdown').style.backgroundPosition = "0px -26px";
			document.getElementById('quick-search').where.value = '81';
		}else if(v == '-32 0')
		{
			document.getElementById('search_selector_dropdown_list').className = "sel_conf";
			document.getElementById('search_selector_dropdown').style.backgroundPosition = "0px -52px";
			document.getElementById('quick-search').where.value = 'Configipedia';
		}else
		{
			document.getElementById('search_selector_dropdown').style.backgroundPosition = "0px 0px";
			document.getElementById('quick-search').where.value = 'conf_all';
		}
		searchHandlers();
	}
}
function refreshSearchTitle()
{
	if(searchInput)
	if(document.getElementById('search_input_title'))
		document.getElementById('search_input_title').style.display = (searchInput.value.length == 0) ? 'block':'none';
}
function searchHandlers()
{
	if(searchInput == null) return;
	var o = document.getElementById('search_input_title');
	searchInput.onmouseover = o.onmouseover;
	searchInput.onmouseout = o.onmouseout;
	searchInput.onfocus = function(){
		searchInputFocus = true;
		document.getElementById('search_input_title').style.display = 'none';
	}
	searchInput.onblur = searchInput.onchange = function(){
		searchInputFocus = false;
		refreshSearchTitle();
	};
	searchButton.onmousedown = function(){
		searchDontSendTimer = window.setTimeout("searchDontSendTimer = null; showSearchScope();", 400);
	};
	searchButton.onmouseup = function (){
		if(searchDontSendTimer) {window.clearTimeout(searchDontSendTimer); searchDontSendTimer = null;}
	};


}
function searchLoad()
{
	/*if(document.location.pathname.match('^/confluence'))
	{
		searchInput = document.getElementById('quick-search-q');
		document.getElementById('search_selector_dropdown').style.backgroundPosition = "-35px 0px";
		document.getElementById('quick-search').style.display = 'block';
		document.getElementById('cse-search-box').style.display = 'none';
	}else
	{*/
		searchInput = document.getElementById('q');
		searchButton = document.getElementById('sa');
	//}
	refreshSearchTitle();
	if (document.getElementById('search_input_title') == null) return;
	var o = document.getElementById('search_input_title');

	o.onmouseover = function ()
	{

		if(searchTitleShowTimer != null)
		{
			window.clearTimeout(searchTitleShowTimer); searchTitleShowTimer = null;
		};
		document.getElementById('search_input_title').style.display = 'none';
	};
	o.onmouseout = function ()
	{
		if(searchTitleShowTimer == null)
		{
			searchTitleShowTimer = window.setTimeout(function()
			{
				searchTitleShowTimer = null;
				if(!searchInputFocus && searchInput.value.length == 0)
					document.getElementById('search_input_title').style.display = 'block';
			}, 400);
		}
	}
	searchHandlers();
}

addLoadEvent(searchLoad);
