//word_list_control.class.js

//uses list_control.class.js
//uses Browsers.js
//uses word_list_request.js

function CWordListControl( strBlockId, bHighlightWordForm, bVisible, bFixedPosition)
{
	// Data initialization
	this.ParentConstructor = CListControl;
	this.ParentConstructor( strBlockId, bVisible, bFixedPosition);

	this.m_bHighlightWordForm = bHighlightWordForm;
	if ( IsIE())
		this.m_insideDiv.style.overflow = 'auto';

	// Methods

	this.IsMainWord = function( strType)
	{
		if ( strType != 'MAIN')
			return false;
		return true;
	}

	this.IsAltForm = function( strType)
	{
		if ( strType != 'ALT')
			return false;
		return true;
	}

	this.AddWord = function( strWord, strType, nRootId, nHomog, strMainForm)
	{
		var strOptionValue = nRootId;
		strOptionValue += "_";
		strOptionValue += strType;
		
		var strOptionData = strWord;
		if ( this.IsMainWord( strType))
		{
			if ( nHomog > 0)
				strOptionData += '<sup>'+nHomog+'</sup>';
		}
		else
		if ( this.IsAltForm( strType))
		{
			if ( strWord != strMainForm)
			{
				strOptionData += " ("+strMainForm;
				if ( nHomog > 0)
					strOptionData += '<sup>'+nHomog+'</sup>';
				strOptionData += ")";
			}
		}
		else
		{
			if ( this.m_bHighlightWordForm)
				strOptionData = '<span class="not_main_word">'+strOptionData+'</span>';
		}
		this.AddLine( strOptionValue, strOptionData);	
	}

	this.RootId = function( nLineIndex)
	{
		var nRootId = false;
		var strKey = this.GetKeyByLineId( nLineIndex);
		if ( strKey)
		{
			var arValue = strKey.split( "_");
			nRootId = arValue[0];
		}
		return nRootId;
	}


	this.ResetWordList = function()
	{
		this.ClearControl();
		this.m_hashSelectedLines.Clear();
	  
    if ( IsOpera())
      this.m_insideDiv.scrollTop = 1;
    
    this.CreateUl();
	}

	this.LoadFromTextFormatData = function( strData)
	{
		if ( strData)
		{
			var bUseScrollPositionInData = false;
			this.ResetWordList();
			var arLines = strData.split( "\n");
			if ( arLines.length > 2)
			{
				var i;
				for ( i = 2; i < arLines.length; i++)
				{
					var arFields = arLines[ i].split( "|");
					var j = 0;
					this.AddWord( arFields[ j++] //strWord
											, arFields[ j++] //strType
											, arFields[ j++] // nRootId
											, arFields[ j++] //nHomog
											, RTrim( arFields[ j++]) //strMainForm
											);
				}
				this.LoadRequestInfoFromTextLine( arLines[ 0]);
				// Safari can't change ScrollTop without next line
				this.ScrollBlock();

				bUseScrollPositionInData = this.LoadSelectionFromTextLine( arLines[ 1]);
			}
			this.ShowState( bUseScrollPositionInData);
		}
	}

	this.LoadRequestInfoFromTextLine = function( strLine)
	{
		var arControlInfo = strLine.split( "|");
		if ( arControlInfo.length >= 5)
		{
			var i = 0;
			var strLevel = arControlInfo[ i++];
			var strWord = arControlInfo[ i++];
			var strType = arControlInfo[ i++];
			var strPrevWordKey = arControlInfo[ i++];
			var strNextWordKey = RTrim( arControlInfo[ i++]);
			//var nStartPos = arControlInfo[ i++];
			//var nPageRowCount = arControlInfo[ i++];
			//var bCanNext = arControlInfo[ i++];

			//if ( nStartPos > 0)
			//	this.AddPrevious( "WordListReloadByAjax( '"+this.m_strBlockId+"', '"+strLevel+"', '"+strWord+"', '"+strType+"', "+nStartPos+", true)");
			if ( strPrevWordKey)
				this.AddPrevious( "WordListReloadByAjax( '"+this.m_strBlockId+"', '"+strLevel+"', '"+strWord+"', '"+strType+"', '"+strPrevWordKey+"', true)");

			//if ( bCanNext > 0)
			//	this.AddNext( "WordListReloadByAjax( '"+this.m_strBlockId+"', '"+strLevel+"', '"+strWord+"', '"+strType+"', "+(parseInt( nStartPos)+parseInt( nPageRowCount))+", false)");
			if ( strNextWordKey)
			{
				//alert( 'strNextWordKey = "'+strNextWordKey+'"');
				this.AddNext( "WordListReloadByAjax( '"+this.m_strBlockId+"', '"+strLevel+"', '"+strWord+"', '"+strType+"', '"+strNextWordKey+"', false)");
			}
		}
	}

	this.LoadSelectionFromTextLine = function( strLine)
	{
		var bUseScrollTopPosition = false;
		var arSelectionInfo = strLine.split( "|");
		if ( arSelectionInfo.length > 1)
		{
			var i = 0;
			var nUseScrollTopPosition = arSelectionInfo[ i++];
			var nScrollTopPosition = arSelectionInfo[ i++];
			var nSelectedLineCount = arSelectionInfo[ i++];
			var nSelectedLine = parseInt( arSelectionInfo[ i++]);
			if ( nSelectedLineCount > 0)
			{
				this.SelectLine( nSelectedLine);
				this.ActivateLine( nSelectedLine);
				if ( parseInt( nUseScrollTopPosition) > 0)
				{
					bUseScrollTopPosition = true;
					this.SetScrollTopPosition( parseInt( nScrollTopPosition));
				}
			}
		}
		return bUseScrollTopPosition;
	}

	this.SelectionInfoText = function()
	{
		var nScrollTopPosition = this.GetScrollTopPosition();
		var nActiveLine = this.GetActiveLine();
		var strText = '';
		
		strText += '1';
		strText += '|';
		strText += nScrollTopPosition;
		strText += '|';

		var nCount = 0;
		if ( nActiveLine >= 0)
			nCount = 1;
		strText += nCount;
		strText += '|';
		strText += nActiveLine;
		return strText;
	}
}

function WordListSelectedRootId( strDivId)
{
	var nRootId = false;
	var wordListControl = FindListControl( strDivId);
	if ( wordListControl)
	if ( wordListControl.GetActiveLine())
	{
		nRootId = wordListControl.RootId( wordListControl.GetActiveLine());
	}
	return nRootId;
}