/** Javascript for Sliding lists as a group
 * Needs Scriptaculous
 */


//Way to 


//SlideGroup takes a list of elements, and makes it so that whenever one slide element
//is opened, the others are closed.
//slideArray is a list of IDs. curSlide is one of those IDs
//USE: declare, init, then each action takes one slideArrayID as it's entry
function slideGroup(nSlideArray,nCurSlide) {
	//ActiveList - array of arrays, containing:
		//id is id of element, status is status of slide

	this.slideArray = new Array();
	
	this.createActiveList = function(IDArray) {
		// take list accepted, and then attach an 'int' for current movement status

		var sArray = new Array();
		IDArray.each( function(id) {
			var aEntry = new Array();
			aEntry['id']= id;
			
			aEntry['status']= 0;
			sArray.push(aEntry);
		},this);
		this.slideArray = sArray;
		return this.slideArray;
	}

	this.slideArray= this.createActiveList(nSlideArray);

	this.curInstance=nCurSlide;

	//close all slideLists
	this.closeAll= function() {

		this.slideArray.each(function(slideA) {
				$(slideA['id']).hide();
//			new Effect.SlideUp(slideA['id'],{duration:0.0});
		});
	}
	//init, so that only one of the array is currently slid open
	this.init= function() {
		
		this.slideArray.each(function(slideA) {
			if(slideA['id']==this.curInstance) {

				new Effect.SlideDown(slideA['id'],{duration:0.0
					, afterFinish: function() {slideA['status']=1;	
						$(slideA['id']).style.height='auto';
					}
 
				});
			} else {

				new Effect.SlideUp(slideA['id'],{duration:0.0});
			}
		},this);

		return(this.curInstance);
	}
		
	//returns true if succeeded in changing, otherwise, not
	this.slide = function (newSlideID) {
		var result=false;
		if(newSlideID!=this.curInstance){
			this.slideArray.each( function(slideA) {
				if(slideA['id']==newSlideID  
					&& (slideA['status']<=0 ) // || $(slideA['id']).visible() ) 
				) {
						slideA['status']=2;
					new Effect.SlideDown(slideA['id'],{duration:0.2, afterFinish: function(){
						slideA['status']=1;
						//Must reset height, in case interrupted, otherwise height is reset improperly.
						$(slideA['id']).style.height='auto'; 
					}});
					result=true;

				}else if( (slideA['status']>0) ) { // || $(slideA['id']).visible()) ){
						slideA['status']=-1;
					new Effect.SlideUp(slideA['id'],{duration:0.2, afterFinish: function(){
						slideA['status']=0;
						//Must reset height, in case interrupted, otherwise height is reset improperly.
						$(slideA['id']).style.height='auto'; 
							
					}
					});
				} 
			},this);
			this.curInstance=newSlideID;
		}
		return result;	
	}
}


// This function is for toggling the text for an array of elements, such that only one
// has the 'active-prefix text' at any given time.

function updateText(elementIDs, nActivePrefix, nUnactivePrefix, nCurTextId) {
	
	this.textList;
	this.activePrefix = nActivePrefix;
	this.unactivePrefix = nUnactivePrefix;
	this.curTextId = nCurTextId;

	this.createList = function(nElementIDs) {
		var curTextList = new Array();
		nElementIDs.each( function(eID) {
			var textEntry = new Array();
			textEntry['id']=eID;
			textEntry['status']=0;
			curTextList.push(textEntry);
		}, this);	
		this.textList = curTextList;
	}

	this.init = function() {

			this.createList(elementIDs);
		this.textList.each( function(tEntry) {
			$(tEntry['id']).update(this.unactivePrefix+$(tEntry['id']).innerHTML);
		},this);

		this.update(this.curTextId);
	}
	
	this.update = function(activeElementID) {
		this.textList.each( function(tEntry) {
			if(tEntry['id']==activeElementID && tEntry['status']==0) {
			$(tEntry['id']).update($(tEntry['id']).innerHTML.sub(this.unactivePrefix,this.activePrefix));
			tEntry['status']=1;
			this.curTextId = tEntry['id'];
			}
			else if(tEntry['status']!=0) {
			$(tEntry['id']).update($(tEntry['id']).innerHTML.sub(this.activePrefix,this.unactivePrefix));
			tEntry['status']=0;
			}
		}, this);	
	}
}


//Turns a list of elements into arrays.
function elements2Ids(listOfElements) {

	var listOfIds = new Array();
	// expects array of elements
	listOfElements.each( function(element) {
			listOfIds.push(element.identify());
	});
	return listOfIds;
}

