jQuery.noConflict();
jQuery(document).ready(function()
{
	var mySearch = jQuery("#qsearch");
	var defaultTxt = "Quick Search Artists, Art, Mediums, Series, and Locations ... ";
	var myDiv =  jQuery("#results");
	var myHover = false;
	var myBlur = false;
	var baseUrl = "/index.php/search/qsearch/";
	
	
	/*********** auto update after keypress    ***********/
	
	mySearch.keyup(function()
		{
			if((mySearch.val().length)>=3)
			{
				myPost(mySearch.val());
				if(myDiv.is(":hidden"))
				{
					myDiv.show();
				}
			} 
			
			
			else if((mySearch.val().length)<3)
			{
				if(myDiv.is(":visible"))
				{
					myDiv.hide();
				}
			}
			
		}
		
		
	)
	
	
	/******   gets the data results   ******/
	
	function myPost(gr)
	{
	
		var myStr = baseUrl + gr + "/";
		jQuery.post(myStr, function(returns)
			{
				myDiv.html(returns);
			}
			);
	}
	
	
	
	
	/*****         takes care of displaying results    *****/
	
	mySearch.val(defaultTxt).toggleClass('defaultQS');
	
	
	mySearch.focus(function()
		{
			myBlur = true;
			if(mySearch.val() == defaultTxt)//erases the input if it's the default text
			{
				mySearch.val("").toggleClass('defaultQS');
			}
			
			if((mySearch.val().length)>=3 && !(mySearch.val() == defaultTxt))//shows the results once it's appropriate
			{
				myDiv.show();
			}
		}
	)
	
	mySearch.blur(function()
		{
			myBlur = false;
			if(!mySearch.val())//puts default text back in if it's blank
			{
				mySearch.val(defaultTxt).addClass('defaultQS');
			}
			
			//sets the hover state so that if hovering, it's true
			
			if(!myHover)//hides results only if not hovering
			{
				myDiv.hide();
			}
		}
	)
	
	/**** once unhovered, if it's blurred, will hide the results****/
	myDiv.hover(function(){ myHover = true;},function()
						  {
							  if(!myBlur)
							  {
								  myDiv.hide();
							  }
							  myHover = false;
						  }
						  );
})
