/**
 * Author directory related javascript module.  It module is responsible for 
 * Author Directory component.  It has function for genereting Author directory 
 * component and two char substring of author names and getting the author list 
 * from the server.
 *
 * @author Veeresh D.
 * @date March 2008
 */

var authorCurId, authorCurColor, authorPreId, authorCTab, authorTempEle
var authorFontColor, authorBackColor, authorPrevColor, authordefColor
var tempHTML;
var searchKeyString;

var smallChars = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

String.prototype.replaceAll = function(s1, s2) 
{
    return this.split(s1).join(s2)
}

/**
Replaces all occurences of a string with given
*/
var tabAuthDir =
{

/**
Populates the author directory tab component and initializes it with 
first tab as a default and displays author results starting with "Aa" string

@param tabNo Contains the tab no of the author directorie's upper tabs(A,B,C..)
@param keySubString Contains the author's sub-search string(Ax, Ab, Ac..)
@return null It disables the redirection/link action of anchor tab
*/
getAuthorDirTab: function(tabNo, keySubString)
{
    var authorDirData;
    searchKeyString = keySubString;
  	
	authorDirData = "<DIV style='' class=searchResultPageTitle>Author Directory<HR style='height: 1px'></DIV>";
	authorDirData += components.getAuthorDirTabHTMLComponent();
	
	document.getElementById("queryResultsDivId").innerHTML = authorDirData + "<BR>";
	printer.printerPageType = 9;	
	printer.showPrinterPageLink();	
	
	tabAuthDir.initAuthorTabPanel(tabNo);
	searchResults.hideAllPanels();
	components.penel.hideBanner();	
	return null;
},

/**
Initializes the author tab component with default tab.
By default, it shows the results of first tab's author(Aa, Ra, Va)
*/
initAuthorTabPanel: function(tabNo)
{
	authorFontColor = "BLACK";
	authorBackColor = "LIGHTSTEELBLUE";
	authorPrevColor = authorFontColor;
	authordefColor	= "gray"
	authorCTab 	= 0;	
	
	tabAuthDir.setAuthorTabFocus("authorTabId" + tabNo);
	tabAuthDir.setAuthorTabDivdata("authorTabId" + tabNo, tabNo);	
},

/**
Sets the current tab focus based on onmouseover over particular author name's tab

@param tid Id of focused tab
*/
setAuthorTabFocus: function(tid)    //Set Focus, On mouseover
{
	authorCurId = tid;
	authorTempEle = document.getElementById(tid);   	
	authorTempEle.style.background = authorBackColor;
	authorTempEle.style.color = authorFontColor;
	authorCurColor = authorTempEle.style.background;	
},

/**
Resets the current author name's tab focus based on onmouseout event over previously visited tab

@param tid Id of the tab, its focus gets reset to default focus styles
*/
resetAuthorTabFocus: function(tid)  //Delete Focus , On mouseout
{
	if ( authorPreId == tid )		//To retain the previous color
	{
		authorTempEle = document.getElementById(authorCurId);
		authorTempEle.style.background = authorPrevColor;		
	}
	else
	{
		authorTempEle = document.getElementById(tid);
		authorTempEle.style.background= authordefColor;		
	}
},

/**
Sets the current tab focus based on onclick event over particular tab. 
It also generates author name's second character set and lets search based on second letter

@param tid Id of the tab, its focus gets set
@param tn Id of the current focussed tab
*/
setAuthorTabDivdata: function(tid, tn)
{
	authorCTab = tn;
	if (authorPreId != null && authorPreId != tid)	//To set back the default color of previously clicked tab
	{
		authorTempEle = document.getElementById(authorPreId);
		authorTempEle.style.color = authorFontColor;
		authorTempEle.style.background= authordefColor;
	}	
	tempHTML = document.getElementById("" + tid).innerHTML;
	
	tabAuthDir.generateAuthorSecondCharacters(tempHTML);
	
	if( searchKeyString != null )
	    tabAuthDir.getAuthorList(searchKeyString);
	else
	    tabAuthDir.getAuthorList(tempHTML + smallChars[0]);
	
	authorPrevColor = authorCurColor;
	authorPreId = tid;	
},

/**
Generates all small alphabetic characters and suffixes them with author's first character(Aa, Ab..)

@param firstChar Starting characters of the author's name
*/
generateAuthorSecondCharacters: function(firstChar)
{
	var firstCharHTML = "";
	for(var i = 0; i < 26; i++)
	{
	    firstCharHTML = "<A href=# id=" + firstChar + smallChars[i];
		firstCharHTML += " onclick=tabAuthDir.getAuthorList('" + firstChar + smallChars[i] + "') >";
		firstCharHTML += firstChar + smallChars[i];
		firstCharHTML += "</A>";
		document.getElementById("authorTabSecondCharId" + (i+1)).innerHTML = firstCharHTML;
	}	
},

/**
Gets the list of authors from the server

@param authorSubstring Author name's first two charcters
*/
getAuthorList: function(authorSubstring)
{
	var ajaxRequest = zXmlHttp.createRequest();
	showTopNotification();//showNotification('authorTabComponentResults', 'Loading...', 0);
	
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if (ajaxRequest.status == 200)
			{
				hideTopNotification();
				tabAuthDir.renderAuthorList(ajaxRequest.responseText, authorSubstring);
			}
			else
			{
				hideTopNotification();//hideNotification('authorTabComponentResults');
				document.getElementById("authorTabComponentResults").innerHTML = "<BR>" + showAlertInformation(2, "Unable to get author directory response.  Please try again!") + "<BR>";				
			}
		}
	}
	//ajaxRequest.open("GET", "http://192.168.10.25:8080/comsocdl/authorDir.action?prefix=" + authorSubstring.substr(0,1) , true);	//Server URL
	ajaxRequest.open("GET", ROOT_DB_URL + "authorDirectory.action?authorPrefix=" + authorSubstring.substr(0, 2) + ((loginStatus == false) ? "" : "&nullParam=" + new Date()) , true);	//Server URL
	ajaxRequest.send(null);
},

/**
Renders the list of authors and displays in sorted order vertically

@param authorList List of Author names as a JSON object
@param authorSubstring Author name's first two charcters
*/
renderAuthorList: function(authorList, authorSubstring)
{
	var authorListObject = eval('(' + authorList + ')');
	
	tempHTML = "<B><U><font color=STEELBLUE size=5 font-family: 'Verdana'>" + authorSubstring + "</font> </U></B><BR><BR>";
	tempHTML += "<TABLE width=99%>";
	tempHTML += "<TR>";	
	
	if( authorListObject.authors.length > 0)
	{	
	    var noOfColumns = 4;
	    var colIndex = 0;
	    var rowIndex = 0;
	    var cellIndex;
	    var totalCells;
	    var authorList = new Array();
	    
	    totalCells = authorListObject.authors.length;
	    if ( (totalCells % 4) != 0 )
	        totalCells = totalCells + (4 - totalCells % 4);
	    
	    for( i = 0; i < totalCells/4; i++)
	    {
	        tempHTML += "<TR>";
	        for( j = 0; j < 4; j++)
	        {
	            cellIndex = i + j*(totalCells/4);
	            tempHTML += "<TD class=searchResultItem>";	
			    if ( cellIndex < authorListObject.authors.length )
	            {
	                //tempHTML += "<A href=# onclick=\"searchResults.getAuthorData(" + authorListObject.authors[cellIndex].authorID +")\">";					
			        tempHTML += "<A href=?author=" + authorListObject.authors[cellIndex].authorID +" onclick=\"return searchResults.getAuthorData(" + authorListObject.authors[cellIndex].authorID +")\">";
		            tempHTML += authorListObject.authors[cellIndex].lastName + " ";
			        tempHTML += authorListObject.authors[cellIndex].firstName;
			        tempHTML += "</A>";		        
	            }
	            else
	                tempHTML += "";
	            
	            tempHTML += "</TD>";	            
	        }	               
            tempHTML += "</TR>";	        
	    }
	}
	else
	{
		tempHTML += "<TD class=searchResultItem>" + showAlertInformation(1, "Authors not found") + "</TD>"
	    tempHTML += "</TR>";
	}	
	tempHTML += "</TABLE><BR>";
	document.getElementById("authorTabComponentResults").innerHTML = tempHTML;
	//document.getElementById("authorTabComponentResults").style.border = "2px solid LIGHTSTEELBLUE";
	//document.getElementById("authorTabComponentResults").style.width = "100%";
	
	historyIsItResultDetails = 0;
	parityHistory.setInHistory();
}
}
