/*
	make a pop up to search text in the page

	Created: 2008/10/4 - Olivier Zyngier
*/


function searchInPage(str , eltIdToFocus)
{
	try
	{
		if (window.find)
		{
			window.find(str, false, false);
		} else {
			searchInPageIe(str);
			if (eltIdToFocus)
			{
				var eltToFocus = document.getElementById(eltIdToFocus);
				if (eltToFocus)
				{
					setTimeout(function() { eltToFocus.focus(); }, 0);
				}
			}
		}
	}
	catch(exc)
	{
		alert("Sorry, this search is not supported in this browser. Please use the browser's search function.");			
	}	
	return false;
}


var y1 = -15;   // change the # on the left to adjust the Y co-ordinate

function createSearchPopup(eltId)
{
	if (document.all)
	{
		document.all[eltId].style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight - (document.documentElement.clientHeight - y1)) + "px";
	} else {
		document.getElementById(eltId).style.top = window.pageYOffset + (window.innerHeight - (window.innerHeight - y1)) + "px";
	}
		
	window.setTimeout("createSearchPopup('" + eltId + "')", 10);
	window.searchPopupCreated = true;
}

function hideSearchPopup(eltId , eltIdToFocus)
{
	document.getElementById(eltId).style.visibility = 'hidden';
}

function showSearchPopup(eltId , eltIdToFocus)
{
	// window.find() works well only in Firefox (not well Safari)
	if (window.find && ! $.browser.safari)
	{
		try
		{
			//window.find(str, false, false);
			window.find('' , false , false);
			return false;
		}
		catch(exc)
		{
			// ignore the error, we'll just use the DHTML popup
		}
	}

	try
	{
		if (! window.searchPopupCreated)
		{
			if (! window.find)
			{
				// try to create a text range. If that fails, fall back o the browser's native search
				document.body.createTextRange();
			}
			createSearchPopup(eltId);
		}	    

		document.getElementById(eltId).style.visibility = 'visible';
		if (eltIdToFocus)
		{
			var eltToFocus = document.getElementById(eltIdToFocus);
			if (eltToFocus)
			{
				setTimeout(function() { eltToFocus.focus(); }, 0);
			}

		}
	}
	catch (exc)
	{
		alert("Sorry, this search is not supported in this browser. Please use the browser's search function.");			
	}
	return false;
}

function searchInPageIe(str)
{
	if (! window.searchInPageIeTextRange)
	{
		window.searchInPageIeTextRange = document.body.createTextRange();
	}
	if (window.searchInPageIeTextRange != null)
	{
		window.searchInPageIeTextRange.collapse(false);
		doSearchInPageIe(str);
	}
	var strFound = window.searchInPageIeTextRange.findText(str);

	if (window.searchInPageIeTextRange == null || ! strFound)
	{
		window.searchInPageIeTextRange = window.document.body.createTextRange();
		doSearchInPageIe(str);
	}
	
}

function doSearchInPageIe(str , fromMarkerForTesting)
{
	try
	{
		var strFound = window.searchInPageIeTextRange.findText(str);
		if (strFound)
		{
			window.searchInPageIeTextRange.select();
			document.execCommand("BackColor","false","yellow") ;
	//		document.execCommand("Bold","false") ;
			window.searchInPageIeTextRange.collapse(false);
			window.searchInPageIeTextRange.select();
		}
	}
	catch (exc)
	{
		// if the found text is hidden an exception is thrown, so start the search again
		searchInPageIe(str);
	}
}






