//===============================================================
/*
	Author: vbrao
	
	JavaScript file containing data structures and functionality
	for page tabs which allow scrolling. This is to prevent the
	user from seeing a horizontal scroll on the page.
*/
//===============================================================

/******************** DATA STRUCTURES & VARIABLES ************************/
/**
 * Stack to hold the page tabs beyond the left scroll button.
 */
if(!_LEFT_STACK){
	var _LEFT_STACK = new Array();
}

/**
 * Stack to hold the page tabs beyond the right scroll button.
 */
if(!_RIGHT_STACK){
	var _RIGHT_STACK = new Array();
}

/**
 * Variable to store the definied value for the minimum number of tabs 
 * to be shown at all the times.
 */
 var _NUM_TABS_IN_ROW;

/**
 * Variable to store the id of the <td> of left scroll image.
 */ 
 var _SCROLL_IMG_ID_LEFT;
 
 /**
 * Variable to store the id of the <td> of right scroll image.
 */ 
 var _SCROLL_IMG_ID_RIGHT;
 
 /**
 * Variable to store the index of the current leftmost shown tab on the UI.
 */ 
 var _FIRST_TAB_IDX;


/************************* METHODS ***************************/

 /**
  *	Method to initialize the setting for scrolling of the page tabs.
  *	This method has to be called before the printing of the page tabs on the UI.
  *	@param idsArr			Array of ids (strings) of each of the <td> for
  * 						each page tab. The Ids have to be sorted
  * 						in ascending order of displayed page tabs. 
  *	@param numTabsInRow		Defined value for the minimum number of tabs 
  * 						to be shown at all the times.
  *	@param leftTabIdx		Index of the current left most page tab on the UI.
  *	@param leftScrollImgId	Id of the <td> for left_scroll.gif image
  *	@param rightScrollImgId	Id of the <td> for right_scroll.gif image
  */
 function initializePageTabs(	idsArr, 
		 						numTabsInRow, 
		 						leftTabIdx, 
		 						leftScrollImgId, 
		 						rightScrollImgId){
 	_NUM_TABS_IN_ROW 		= numTabsInRow;
 	_SCROLL_IMG_ID_LEFT		= leftScrollImgId;
 	_SCROLL_IMG_ID_RIGHT	= rightScrollImgId;
 	_FIRST_TAB_IDX			= leftTabIdx;
 	if( idsArr && (idsArr.length > 0) ){
 		for(var i = 0; i < idsArr.length; i++){
 			var id = idsArr[i];
 			if(i < leftTabIdx){
 				_LEFT_STACK.push(id);
 				changeTDElemDisplay(id, false);
 			} else if(i >= (leftTabIdx + numTabsInRow) ){
 				_RIGHT_STACK.push(id);
 				changeTDElemDisplay(id, false);
 			} 			
 		}//end for
 		if(_LEFT_STACK.length > 0){
 			changeTDElemDisplay(leftScrollImgId, true);
 		} else {
 			changeTDElemDisplay(leftScrollImgId, false);
 		}
 		if(_RIGHT_STACK.length > 0){
 			changeTDElemDisplay(rightScrollImgId, true);
 		} else {
 			changeTDElemDisplay(rightScrollImgId, false);
 		}
 	}//end if
 }
 
 /**
  *	Method to hide/unhide the <td> whose id is specified.
  *	@param id	id of the <td> whose display is to be changed
  *	@param show	boolean to show (true) or hide (false)
  */
 function changeTDElemDisplay(id, show){
 	var tdElem = document.getElementById(id);
 	tdElem.style.display = (show? 'block' : 'none');
 }
 
 /**
  *	Method to scroll the page tabs left once. Called on clicking
  * the left scroll image on the UI.
  * @param idsArr 		Array of ids (strings) of each of the <td> for
  * 					each page tab. The Ids have to be sorted
  * 					in ascending order of displayed page tabs.
  * @param hiddenField	Optional param for hidden field to hold the value
  *						for current first left tab index.
  */
 function scrollLeft(idsArr, hiddenField){
 	if(_LEFT_STACK.length > 0){
	 	var leftElemId 	= _LEFT_STACK.pop();
	 	var leftElemIdx	= indexOfArr(idsArr, leftElemId);
	 	changeTDElemDisplay(leftElemId, true);
	 	_FIRST_TAB_IDX 	= leftElemIdx;
	 	
	 	if(_LEFT_STACK.length > 0){
			changeTDElemDisplay(_SCROLL_IMG_ID_LEFT, true);
	 	} else {
			changeTDElemDisplay(_SCROLL_IMG_ID_LEFT, false);
	 	}
		
		var rightElemIdx = (leftElemIdx + _NUM_TABS_IN_ROW) ;
		if(isValidArrIndex(idsArr, rightElemIdx)){
			var rightElemId	 = idsArr[rightElemIdx];
			_RIGHT_STACK.unshift(rightElemId);
			changeTDElemDisplay(rightElemId, false);
			changeTDElemDisplay(_SCROLL_IMG_ID_RIGHT, true);
		}
		if(hiddenField){
			hiddenField.value = leftElemIdx;
		}
	}//end outer if
 }
 
 /**
  *	Method to scroll the page tabs right once. Called on clicking
  * the right scroll image on the UI.
  * @param idsArr 		Array of ids (strings) of each of the <td> for
  * 					each page tab. The Ids have to be sorted
  * 					in ascending order of displayed page tabs.
  * @param hiddenField	Optional param for hidden field to hold the value
  *						for current first left tab index.
  */
 function scrollRight(idsArr, hiddenField){
 	if(_RIGHT_STACK.length > 0){
 		var rightElemId 	= _RIGHT_STACK.shift();
 		var rightElemIdx	= indexOfArr(idsArr, rightElemId);
	 	changeTDElemDisplay(rightElemId, true);
	 	if(_RIGHT_STACK.length > 0){
			changeTDElemDisplay(_SCROLL_IMG_ID_RIGHT, true);
	 	} else {
			changeTDElemDisplay(_SCROLL_IMG_ID_RIGHT, false);
	 	}
	 	
	 	var leftElemIdx = rightElemIdx - _NUM_TABS_IN_ROW;
	 	if(isValidArrIndex(idsArr, leftElemIdx)){
	 		var leftElemId	 = idsArr[leftElemIdx];
			_LEFT_STACK.push(leftElemId);
			changeTDElemDisplay(leftElemId, false);
			changeTDElemDisplay(_SCROLL_IMG_ID_LEFT, true);
			_FIRST_TAB_IDX 	= (leftElemIdx + 1);
	 	}
	 	if(hiddenField){
			hiddenField.value = leftElemIdx;
		}
	 }//end outer if
 }
 
 /**
  *	Method to get the value of current first page tab index.
  */
 function getCurrentFirstTabIdx(){
 	return _FIRST_TAB_IDX;
 }
 
 /*
  *	Method to get the first index of the specified value for the specified array.
  *	@param arr		Input array
  *	@param value	Input value whose index in the given array has to be computed
  */
 function indexOfArr(arr, value){
 	if(arr && (arr.length > 0) ){
 		for(var i = 0; i < arr.length; i++){
 			if(value == arr[i]){
 				return i;
 			}
 		}
 	}
 	return -1;
 }

 /*
  *	Method to compute if the specified index is a valid index for the specified array.
  *	@param arr		Input array
  *	@param value	Input index value
  */ 
 function isValidArrIndex(arr, idx){
 	if(arr){
	 	return ( (idx >= 0) && (idx < arr.length) );
	} else {
		return false;
	}
 }
 
