var searchFieldId = "search_field";

// path to the script that retreives results
var scriptName = "/handlers/smartSearch/smartSearch.php";

//var http_req = null; // it will be the ajax-processing object
var http_req = new JsHttpRequest();
http_req.onreadystatechange = SmartSearchCallback; // set callback function
http_req.caching = false; // disallow caching

var needRefresh =false;
var awaitingAnswer = false;

// Global vars to pass data between functnions
var searchBoxId = false; // object id that will contain search results
var searchText = '';     // search text
var inputFieldId = false;// search field object id
var artistId = false;    // artist id to generate request url
var pageNum = false;     // results page number

var searchBoxVisible = false;


window.setInterval( 'OnInterval();', 400 );

function OnInterval()
{
    if ( needRefresh && !awaitingAnswer )
        SendRequest();
}


// Callback function which will be called after ajax object retreives requiest
function SmartSearchCallback()
{
	//console.log( 'ReadyState: %d', http_req.readyState );
	//alert('ReasyState: ' + http_req.readyState );
	if( http_req.readyState == 4 )
	{
		if( http_req.responseJS )
		{
			if ( searchBoxVisible ) {
			    // if searchBox object exists then show received content
            	var searchBox = document.getElementById( searchBoxId );
				if ( searchBox )
                	searchBox.innerHTML = http_req.responseJS.text;
			}

            //needRefresh = false;
            awaitingAnswer = false;
            return true;
		}
	}
}

// Hide block with results
function HideSuggestList( objectId ) {
	object = document.getElementById( objectId );
    if ( object )
    {
		object.style.visibility = "hidden";
		searchBoxVisible = false;
	}
}

// Show block with results
function ShowSuggestList( objectId ) {
	object = document.getElementById( objectId );
    if ( object )
    {
		object.style.visibility = "visible";
		searchBoxVisible = true;
	}
}

// Show text 'Loading' when awaiting answer
function ShowLoadingText( objectId ) {
    object = document.getElementById( objectId );
    if ( object )
    {
            object.innerHTML = 'Loading...'
            //searchBoxVisible = true;
    }
}

function SmartSuggest( inputId, boxId, artID, page )
{
    // Check if search input field is correct
    srch_field = document.getElementById( inputId );
    if ( !srch_field )
        return false;

    searchText = srch_field.value;

    // Hide list, if no text is entered
    if (searchText == '')
	{
		needRefresh = false;
        HideSuggestList( searchBoxId );
	}
	else
	{
        // set page = 1 by default
    	if ( !pageNum )
        	pageNum = 1;

    	searchBoxId = boxId; // object id that will contain search results
    	inputFieldId = inputId;     // search field object id
    	artistId = artID;
    	pageNum = page;
    	needRefresh = true;
	}
}



// Function sends a request to php-script, which should retreive formatted html
// with search results. This function is called when interval function
// decide that it's necessary to refresh results.
// Parameters are taken from global variables:
//   searchText - a text for search
//   artistId   - ID of artist which albums should be searched through
//   pageNum    - number of page with results which should be retreived
function SendRequest() {

	ShowLoadingText( searchBoxId ); // show text
    ShowSuggestList( searchBoxId );

    awaitingAnswer = true;  // from now we're awaiting answer
    needRefresh = false;
	// make request
    http_req.open( 'GET', scriptName + '?search=' + escape( searchText )
				       + '&aid=' + artistId
        			   + '&pg=' + pageNum );
    http_req.send( {} ); // send request
}
