// word_list_request.js - CWordListControlData, CWordListRequestInfo, CWordListAjaxSettings clases, s_hashWordListRequest and s_wordListAjaxSettings variables and control functions

// uses array.js
// uses ajax_async.js
// uses param_maker.js
// uses timer.js

// used in word_list_control.class.js

function CWordListControlData( strDivId, strDataInputName, strPositionInputName, strDefaultRequestType)
{
	this.m_strDivId = strDivId;
	this.m_strDataInputName = strDataInputName;
	this.m_strPositionInputName = strPositionInputName;
	this.m_strDefaultRequestType = strDefaultRequestType;

	this.DivId = function()
	{
		return this.m_strDivId;
	}
	this.DataInputName = function()
	{
		return this.m_strDataInputName;
	}
	this.PositionInputName = function()
	{
		return this.m_strPositionInputName;
	}
	this.DefaultRequestType = function()
	{
		return this.m_strDefaultRequestType;
	}
}

/// @param inputIds type is #CWordListInputIds
function CWordListRequestInfo( controlData, hashNotComparableKey)
{
	this.m_controlData = controlData;
	this.m_hashNotComparableKey = hashNotComparableKey;

	this.m_strUrl = false;
	this.m_paramMakerSent = null;
	this.m_paramMakerNeed = null;

	//this.m_bWaitingForStart = false;
	this.m_nCurrentRequestId = 0;

	this.DataInputName = function()
	{
		return this.m_strDataInputName;
	}

	this.SetNewRequest = function( strUrl, paramMaker)
	{
		var bChanged = false;
		if ( this.IsNewRequest( strUrl, paramMaker))
		{
			bChanged = true;
			this.AbortRequest();
			this.m_strUrl = strUrl;
			this.m_paramMakerNeed = paramMaker;
			//this.m_bWaitingForStart = true;
			this.m_nCurrentRequestId = 0;
		}
		return bChanged;
	}

	this.IsNewRequest = function( strUrl, paramMaker)
	{
		var bNew = false;
		if ( paramMaker)
		{
			bNew = true;
			//alert( 'this.m_strUrl = '+this.m_strUrl+'\n strUrl = '+strUrl+'\n this.m_paramMakerNeed = '+this.m_paramMakerNeed+'\n this.m_paramMakerSent = '+this.m_paramMakerSent);
			if ( this.m_strUrl == strUrl)
			{
				if ( this.m_paramMakerNeed)
				{
					if ( this.m_paramMakerNeed.IsEqual( paramMaker, this.m_hashNotComparableKey))
						bNew = false;
				}
				else
				if ( this.m_paramMakerSent)
				{
					if ( this.m_paramMakerSent.IsEqual( paramMaker, this.m_hashNotComparableKey))
						bNew = false;
				}
			}
		}
		return bNew;
	}

	this.AbortRequest = function()
	{
		if ( this.m_nCurrentRequestId)
		{
			AjaxAbortRequest( this.m_nCurrentRequestId);
			this.m_paramMakerSent = null;
			this.m_strUrl = false;
		}

		this.m_nCurrentRequestId = 0;
		this.m_paramMakerNeed = null;
		//this.m_bWaitingForStart = false;
	}

	this.SetReadyRequest = function( strUrl, paramMaker)
	{
		this.AbortRequest();
		this.m_strUrl = strUrl;
		this.m_paramMakerSent = paramMaker;
	}

	this.IsWaitingForStart = function()
	{
		if ( this.m_paramMakerNeed)
		if ( !this.m_paramMakerNeed.IsEqual( this.m_paramMakerSent, this.m_hashNotComparableKey))
			return true;
		return false;
		//return this.m_bWaitingForStart;
	}

	this.StartRequest = function()
	{
		if ( this.m_paramMakerNeed)
		{
			//this.m_bWaitingForStart = false;
			this.m_paramMakerSent = this.m_paramMakerNeed;
			this.m_paramMakerNeed = null;
			this.m_nCurrentRequestId = AjaxSendRequest( this.m_strUrl
																								, true // bPost
																								, this.m_paramMakerSent // can be null or false
																								, this.m_controlData.m_strDivId // userData // can be null or false
																								, "OnWordListDataReady" //strOnDataFnName // can be null or false
																								, null // strOnBadStatusFnName // can be null or false
																								);
		}
	}
	this.IsStarted = function()
	{
		if ( this.m_nCurrentRequestId > 0)
			return true;
		return false;
	}

	this.SetDataReady = function()
	{
		this.m_nCurrentRequestId = 0;
	}

	this.IsRequestDataReady = function()
	{
		if ( !this.IsStarted())
		if ( !this.IsWaitingForStart())
		if ( this.m_paramMakerSent)
			return true;
		return false;
	}
}

var s_hashWordListRequest = new CHashWithKeys();

function CWordListAjaxSettings( strUrl
															, strLevelParamName
															, strWordParamName
															, strTypeParamName
															, strPositionParamName
															, strBackwardDirectionParamName
															, strRootIdParamName
															, bUseLevelInParamCompare
															, nWaiting
															)
{
	this.m_strUrl = strUrl;
	this.m_strLevelParamName = strLevelParamName;
	this.m_strWordParamName = strWordParamName;
	this.m_strTypeParamName = strTypeParamName;
	this.m_strPositionParamName = strPositionParamName;
	this.m_strBackwardDirectionParamName = strBackwardDirectionParamName;
	this.m_strRootIdParamName = strRootIdParamName;
	this.m_bUseLevelInParamCompare = bUseLevelInParamCompare;
	this.m_nWaiting = nWaiting;
}
var s_wordListAjaxSettings = null;

function SetWordListAjaxSettings( wordListAjaxSettings)
{
	s_wordListAjaxSettings = wordListAjaxSettings;
}
function GetWordListAjaxSettings()
{
	return s_wordListAjaxSettings;
}

var s_timer = new CTimer();

function OnWordListDataReady( nRequestId)
{
	var strDivId = AjaxGetRequestUserData( nRequestId);
	var requestInfo = s_hashWordListRequest.Item( strDivId);
	if ( requestInfo)
	if ( requestInfo.IsStarted())
	{
		var wordListControl = FindListControl( strDivId);
		if ( wordListControl)
		{
			var ajax = AjaxGetRequestObject( requestInfo.m_nCurrentRequestId);
			wordListControl.LoadFromTextFormatData( ajax.responseText);
			SetItemListValue( GetNamedItems( requestInfo.m_controlData.DataInputName(), null), ajax.responseText);
			requestInfo.SetDataReady();
		}
	}
}

function CreateWordListControl( strDivId, strDataInputName, strPositionInputName, strRequestType, bHighlightWordForm, strOnDblClickFunction, bVisible, bFixedPosition)
{
	var wordListControl = new CWordListControl( strDivId, bHighlightWordForm, bVisible, bFixedPosition);
	wordListControl.SetSingleSelection( true);

	var hashNotComparableKey = false;
	var wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		hashNotComparableKey = new CHash();
		hashNotComparableKey.Set( wordListAjaxSettings.m_strTypeParamName, true); 
		if ( !wordListAjaxSettings.m_bUseLevelInParamCompare)
			hashNotComparableKey.Set( wordListAjaxSettings.m_strLevelParamName, true); 
	}

	s_hashWordListRequest.Set( strDivId
														, new CWordListRequestInfo( new CWordListControlData( strDivId
																																								, strDataInputName
																																								, strPositionInputName
																																								, strRequestType
																																								)
																												, hashNotComparableKey
																												));

	wordListControl.LoadFromTextFormatData( GetItemValue( GetFirstItemByName( strDataInputName, null)));
	//if (nInnerBlockHeight > 0)
	//	wordListControl.SetInnerBlockHeight( nInnerBlockHeight);
	
	wordListControl.SetOnDblClickFunction( strOnDblClickFunction);
	wordListControl.SetOnReturnFunction( strOnDblClickFunction);
}

function AreRequestWaitingForStart()
{
	var bAre = false;
	var arKey = s_hashWordListRequest.Keys();
	var i;
	for ( i = 0; (( !bAre)&&( i < arKey.Count())); i++)
	{
		var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
		bAre = requestInfo.IsWaitingForStart();
	}
	return bAre;
}

function StartAllWaitingForStart()
{
	var arKey = s_hashWordListRequest.Keys();
	var i;
	for ( i = 0; ( i < arKey.Count()); i++)
	{
		var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
		if ( requestInfo.IsWaitingForStart())
			requestInfo.StartRequest();
	}
}

function ClearRequestResultData( strDivId)
{
	var requestInfo = s_hashWordListRequest.Item( strDivId);
	if ( requestInfo)
	{
		requestInfo.AbortRequest();
		// DataInputName data can't be restored by control
		SetItemListValue( GetNamedItems( requestInfo.m_controlData.DataInputName(), null), '');
		//SetItemListValue( GetNamedItems( requestInfo.m_controlData.PositionInputName(), null), '');
	}
}

function StopAllRequests()
{
	var arKey = s_hashWordListRequest.Keys();
	var i;
	s_timer.Stop();
	for ( i = 0; ( i < arKey.Count()); i++)
	{
		var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
		requestInfo.AbortRequest();
	}
}

function CreateParamMaker( strLevel, strWord, strType, nRootId, wordListAjaxSettings)
{
	var paramMaker = new CParamMaker();
	if ( !wordListAjaxSettings)
		wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		paramMaker.Set( wordListAjaxSettings.m_strLevelParamName, strLevel);
		paramMaker.Set( wordListAjaxSettings.m_strWordParamName, strWord);
		paramMaker.Set( wordListAjaxSettings.m_strTypeParamName, strType);
		paramMaker.Set( wordListAjaxSettings.m_strRootIdParamName, nRootId);
	}
	return paramMaker;
}

function PrepareTimerForRequests( wordListAjaxSettings)
{
	if ( !wordListAjaxSettings)
		wordListAjaxSettings = GetWordListAjaxSettings();
	if ( AreRequestWaitingForStart())
		s_timer.StartTimeout( "OnRequestTimerProc()", wordListAjaxSettings.m_nWaiting);
	else
		s_timer.Stop();
}

function SetNewRequest( strDivId, strLevel, strWord, strType, nRootId)
{
	var wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		var requestInfo = s_hashWordListRequest.Item( strDivId);
		var paramMaker = CreateParamMaker( strLevel, strWord, strType, nRootId, wordListAjaxSettings);
		requestInfo.SetNewRequest( wordListAjaxSettings.m_strUrl, paramMaker);
	}
}

function SetReadyRequest( strDivId, strLevel, strWord, strType)
{
	var wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		var requestInfo = s_hashWordListRequest.Item( strDivId);
		var paramMaker = CreateParamMaker( strLevel, strWord, strType, false, wordListAjaxSettings);
		requestInfo.SetReadyRequest( wordListAjaxSettings.m_strUrl, paramMaker);
	}
}

function SetNewRequestByWord( strLevel, strWord)
{
	var wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		var arKey = s_hashWordListRequest.Keys();
		var i;
		for ( i = 0; i < arKey.Count(); i++)
		{
			var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
			var paramMaker = CreateParamMaker( strLevel, strWord, requestInfo.m_controlData.DefaultRequestType(), false, wordListAjaxSettings);
			//paramMaker.Set( wordListAjaxSettings.m_strLevelParamName, strLevel);
			//paramMaker.Set( wordListAjaxSettings.m_strWordParamName, strWord);
			//paramMaker.Set( wordListAjaxSettings.m_strTypeParamName, requestInfo.m_controlData.DefaultRequestType());
			requestInfo.SetNewRequest( wordListAjaxSettings.m_strUrl, paramMaker);
		}
		PrepareTimerForRequests( wordListAjaxSettings);
		//if ( AreRequestWaitingForStart())
		//	s_timer.StartTimeout( "OnRequestTimerProc()", wordListAjaxSettings.m_nWaiting);
		//else
		//	s_timer.Stop();
		//	//setTimeout( "OnRequestTimerProc()", wordListAjaxSettings.m_nWaiting);
	}
}

function OnRequestTimerProc()
{
	if ( AreRequestWaitingForStart())
		StartAllWaitingForStart();
	s_timer.SetTimerProcessed();
}

function PutAllRequestDataAndPositionsToHidden()
{
	var arKey = s_hashWordListRequest.Keys();
	var i;
	for ( i = 0; ( i < arKey.Count()); i++)
	{
		var strPosition = '';
		var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
		if ( requestInfo.IsRequestDataReady())
		{
			//alert( "requestInfo.IsRequestDataReady()");
			var wordListControl = FindListControl( arKey.Item( i));
			if ( wordListControl)
			{
				strPosition = wordListControl.SelectionInfoText();
				//alert( "strPosition = "+strPosition);
			}
		}
		else
		{
			SetItemListValue( GetNamedItems( requestInfo.m_controlData.DataInputName(), null), '');
		}
		SetItemListValue( GetNamedItems( requestInfo.m_controlData.PositionInputName(), null), strPosition);
	}
}

//function ClearAllPositionsInHidden()
//{
//	var arKey = s_hashWordListRequest.Keys();
//	var i;
//	for ( i = 0; ( i < arKey.Count()); i++)
//	{
//		var requestInfo = s_hashWordListRequest.Item( arKey.Item( i));
//		SetItemListValue( GetNamedItems( requestInfo.m_controlData.PositionInputName(), null), '');
//	}
//}

function WordListReloadByAjax( strBlockId, strLevel, strWord, strType, strStartWordKey, bBackward)
{
	var wordListAjaxSettings = GetWordListAjaxSettings();
	if ( wordListAjaxSettings)
	{
		var requestInfo = s_hashWordListRequest.Item( strBlockId);

		var paramMaker = CreateParamMaker( strLevel, strWord, strType, false, wordListAjaxSettings);

		//paramMaker.Set( wordListAjaxSettings.m_strLevelParamName, strLevel);
		//paramMaker.Set( wordListAjaxSettings.m_strWordParamName, strWord);
		//paramMaker.Set( wordListAjaxSettings.m_strTypeParamName, strType);
		//paramMaker.Set( wordListAjaxSettings.m_strPositionParamName, nStartPos);
		paramMaker.Set( wordListAjaxSettings.m_strPositionParamName, strStartWordKey);
		if ( bBackward)
			paramMaker.Set( wordListAjaxSettings.m_strBackwardDirectionParamName, 1);
		requestInfo.SetNewRequest( wordListAjaxSettings.m_strUrl, paramMaker);
		requestInfo.StartRequest();
	}
}
