/* 
	START applesearch object 
	Slightly modified version of:
	http://www.brandspankingnew.net/archive/2005/08/adding_an_os_x.html
	- add a bit more modularity
*/
if (!applesearch)	var applesearch = {};

applesearch.init = function (tgtId, pathToCSS)
{
	// add applesearch css for non-safari, dom-capable browsers
	if ( navigator.userAgent.toLowerCase().indexOf('safari') < 0  && document.getElementById )
	{
		// add style sheet if not safari
		var dummy = document.getElementById(tgtId);
		if (dummy)
		{
			dummy.href = pathToCSS;
		}
	}
}

// clears field
applesearch.onFocus = function ()
{
	if (!this.applesearch.touched)
	{
		this.value = "";
		this.applesearch.touched = false;
	}
}

applesearch.newSearch = function (fldId, btnId, pathToImages)
{
	var fld = document.getElementById( fldId );
	var btn = document.getElementById( btnId );
	
	if (!pathToImages.match(/\/$/))
	{
		pathToImages += "/";
	}

	fld.applesearch = new Object();
	fld.applesearch.clearBth = false;
	fld.applesearch.btn = btn;
	fld.applesearch.pathToImages = pathToImages;
	fld.onkeyup = applesearch.onChange;
	fld.onfocus = applesearch.onFocus;
	fld.applesearch.touched = false;

	btn.applesearch = new Object();
	btn.applesearch.fld= fld;	
}


// clears field
applesearch.clearBtnClick = function ()
{
	this.applesearch.fld.value = "";
	this.applesearch.fld.onkeyup();
}

// called when on user input - toggles clear fld btn
applesearch.onChange = function ()
{
	// check whether to show delete button
	var fld = this;
	var btn = this.applesearch.btn;
	var pathToImages = this.applesearch.pathToImages;

	if (fld.value.length > 0 && !fld.applesearch.clearBth)
	{
		btn.style.background = "white url('"+pathToImages+"srch_r_f2.gif') no-repeat top left";
		btn.onclick = applesearch.clearBtnClick;
		fld.applesearch.clearBth = true;
	}
	else if (fld.value.length == 0 && fld.applesearch.clearBth)
	{
		btn.style.background = "white url('"+pathToImages+"srch_r.gif') no-repeat top left";
		btn.onclick = null;
		fld.applesearch.clearBth = false;
	}
}

/* END applesearch object */
