/**
 * Dodanie produktu do oceniania
 * @param int id ID produktu
 * @since 0.7
 * @author Marcin Chylek
 */
var showResult = false;
var showBox    = false;
function searchValue( inputString )
{
    if( inputString.length < 3 )
    {
    	$("#searchCategory").text("");
    	$("#searchCategoryBox").hide();
    	$("#searchProduct").text("");
    	$("#searchProductBox").hide();
    }
    else
    {
        $.ajax({
			type:     "POST",
			url:      "/search/Product",
			data:     { value: inputString },
			dataType: "json",
			success: function( msg )
			{
	        	$("#searchCategory").text("");
	        	$("#searchCategoryBox").hide();
	        	
	        	$("#searchProduct").text("");
	        	$("#searchProductBox").hide();
                
        		if( msg.product != '' )
        		{
        			$("#searchProduct").html( msg.product );
        			$("#searchProductBox").show();
            	
        			$("#searchCategory").html( msg.category );
        			$("#searchCategoryBox").show();
        			
        			showResult = true;
        		}
        		else
        			showResult = false;
			}
        });
    }
}

function hiddenBox()
{
	$("#searchCategoryBox").hide();
	$("#searchProductBox").hide();
}

$(document).ready( function()
{
	$("#searchValue").click( function()
	{
		if( $("#searchValue").val() == 'Szukaj' )
			$("#searchValue").val( "" );
		else if( showResult )
		{
			$("#searchCategoryBox").show();
			$("#searchProductBox").show();
		}
	});
	
	$("#searchValue").blur( function()
	{
		if( $("#searchValue").val() == '' )
			$("#searchValue").val( "Szukaj" );

		if( !showBox )
			hiddenBox();
		else
			setTimeout( "hiddenBox()", 10000 );
	});
	
	$("#searchProductBox").mouseover( function()
	{
		showBox = true;
	});
	
	$("#searchProductBox").mouseout( function()
	{
		showBox = false;
	});
	
	$("#searchCategoryBox").mouseover( function()
	{
		showBox = true;
	});
	
	$("#searchCategoryBox").mouseout( function()
	{
		showBox = false;
	});
	
	$("#searchValue").keyup( function(event)
	{
		searchValue( $("#searchValue").val() );
	});
});
