/*
	USAGE:
		actions[0] = new Array();
		actions[0]["Title"] = "Titel des Listen-Eintrags";
		actions[0]["SubTitle"] = "Untertitel des Eintrages";
		actions[0]["IconPath"] = false; // Oder Pfad zu Icon
			
		list = new ListClass();
			
		list.id = this.id + "actionList";
		list.title = "Funktionen";
		list.name = 'actionList';
		list.limit = 5;
		list.listEntries = actions;
		list.eventController = this;
		list.init();
		list.displayList();
		
	EVENTHANDLER:
		handler.listEntryClicked(listname, entryId);
		handler.listEntryHover(listname, entryId);
		handler.listEntryUnhover(lsitname, entryId);

	TODO:
		+ eventController ?? eventHandler
*/

function ListClass() {
	var displayedEntries = new Array;
	var displayedEntriesCounter = 0;
	var selected = -1;
	var page = 0;
	var pageCounter = 0;
	
	this.init = function() {
		if(!this.limit) {
			this.limit = this.listEntries.length;
		}
		
		pageCounter = Math.ceil(this.listEntries.length / this.limit);
	}
		
	this.displayList = function() {
		displayedEntriesCounter = 0;
		displayedEntries = new Array;
		selected = -1;
		
		document.getElementById(this.id).innerHTML = '';
		
		ListContainer = document.createElement("DIV");
		ListContainer.id = this.id + 'ListContainer';
		ListContainer.className = 'ListContainer';
		
		if(this.title!=false || this.limit < this.listEntries.length) {
			ListHeader = document.createElement("DIV");
			ListHeader.className = 'listHeader';
			ListHeader.innerHTML = this.title;
			
			if(this.limit < this.listEntries.length) {
				// Prepare right ScrollButton
				var topScrollRightButton = document.createElement("IMG");
				if(pageCounter <= page + 1) {
					topScrollRightButton.className = 'scrollRightButton scrollButtonInactive';
				} else {
					eval("topScrollRightButton.onclick = function() { " + this.name + ".nextPage(); }");
					topScrollRightButton.className = 'scrollRightButton';			
				}
				topScrollRightButton.src = '/images/style/arrows/greyArrowRightActive.gif';			
				
				// Prepare left ScrollButton
				var topScrollLeftButton = document.createElement("IMG");
				if(page == 0) {
					topScrollLeftButton.className = 'scrollLeftButton scrollButtonInactive';
				} else {
					eval("topScrollLeftButton.onclick = function() { " + this.name + ".prevPage(); }");
					topScrollLeftButton.className = 'scrollLeftButton';			
				}
				topScrollLeftButton.src = '/images/style/arrows/greyArrowLeftActive.gif';			
				
				if(!this.title) {
					ListHeader.innerHTML = 'Seite ' + Math.abs(page + 1) + ' von ' + pageCounter;
				}
				
				// Add ScrollButtons to listHeader
				ListHeader.appendChild(topScrollRightButton);		
				ListHeader.appendChild(topScrollLeftButton);			
			}
			
			ListContainer.appendChild(ListHeader);
		}
		
		List = document.createElement("DIV");
		List.id = this.id + 'List';
		List.className = 'List';
					
		if(this.listEntries) {									
			for(z1 = page * this.limit; z1 < (page + 1) * this.limit && z1 < this.listEntries.length; z1++) {
				if(this.listEntries[z1]["Title"] != false) {
					displayedEntries[displayedEntriesCounter] = z1;
					ListEntry = document.createElement("DIV");
					ListEntry.id = this.id + "ListEntry" + z1;
					ListEntry.className = 'listEntry';
					
					if(this.listEntries[z1]["IconPath"]!=false) {						
						ListEntryIcon = document.createElement("DIV");
						ListEntryIcon.className = 'listEntryIconContainer';

						ListEntryIconFile = document.createElement("IMG");
						ListEntryIconFile.className = 'listEntryIcon';
						ListEntryIconFile.src = this.listEntries[z1]["IconPath"];
						
						ListEntryIcon.appendChild(ListEntryIconFile);
						ListEntry.appendChild(ListEntryIcon);
					} else {
						ListEntry.className = 'listEntry noIcon';	
					}
					
					ListEntryTitle = document.createElement("DIV");
					ListEntryTitle.id = this.id + 'ListEntryTitle' + z1;	
					ListEntryTitle.className = "listEntryTitle";					
					ListEntryTitle.innerHTML = this.listEntries[z1]["Title"];
					
					if(typeof(this.listEntries[z1]["RightBox"])!="undefined") {
						ListEntryRightBox = document.createElement("DIV");
						ListEntryRightBox.innerHTML = this.listEntries[z1]["RightBox"];
						ListEntry.appendChild(ListEntryRightBox);
					}
	
					if(typeof(this.listEntries[z1]["SubTitle"])!="undefined") {
						ListEntrySubTitle = document.createElement("DIV");
						ListEntrySubTitle.className = 'listEntrySubTitle';
						ListEntrySubTitle.innerHTML = this.listEntries[z1]["SubTitle"];
						ListEntry.appendChild(ListEntrySubTitle);
					}
					
					ListEntry.appendChild(ListEntryTitle);
									
					eval("ListEntry.onclick = function() { " + this.eventController.name + ".listEntryClicked('" + this.listName + "', " + z1 + "); };");
					eval("ListEntry.onmouseover = function() { " + this.name + ".highlightListEntry(" + z1 + ",'" + ListEntry.className + " selected'); };");
					eval("ListEntry.onmouseout = function() { " + this.name + ".deHighlightListEntry(" + z1 + ",'" + ListEntry.className + "'); };");
					
					List.appendChild(ListEntry);
					
					displayedEntriesCounter++;
				}
				this.listEntries[z1]["scrolling"] = false;
				this.listEntries[z1]["className"] = ListEntry.className;
			}
		}

		ListContainer.appendChild(List);

		document.getElementById(this.id).appendChild(ListContainer);				

		if(displayedEntriesCounter == 1 && false) {
			selected = -1;
			this.selectNext();
		}
	}

	this.highlightListEntry = function(id, className) {
		document.getElementById(this.id + "ListEntry" + id).className = className;
		
		if(this.reportMouseOver && this.eventController) {
			this.eventController.listEntryHovered(this.listName, id);
		}		
	}
	
	this.deHighlightListEntry = function(id, className) {
		document.getElementById(this.id + "ListEntry" + id).className = className;
		if(this.reportMouseOver && this.eventController) {
			this.eventController.listEntryUnhovered(this.listName, id);
		}
	}	

	this.selectNext = function() {
		var currentSelected = selected;
		var nextSelected = currentSelected + 1;
		
		if(nextSelected <= displayedEntriesCounter - 1) {
			if(selected > -1) {
				id = displayedEntries[selected];
				document.getElementById(this.id + "ListEntry" + id).className = this.listEntries[id]["className"];
			}
			selected = nextSelected;

			id = displayedEntries[selected];		
			document.getElementById(this.id + "ListEntry" + id).className = this.listEntries[id]["className"] + ' selected';
		}
	}
	
	this.selectPrevious = function() {
		var currentSelected = selected;
		var nextSelected = currentSelected - 1;
		
		if(nextSelected >= 0) {
			if(selected > -1) {
				id = displayedEntries[selected];
				document.getElementById(this.id + "ListEntry" + id).className = this.listEntries[id]["className"];
			}
			selected = nextSelected;
			id = displayedEntries[selected];			
			document.getElementById(this.id + "ListEntry" + id).className = this.listEntries[id]["className"] + ' selected';
		}	
	}
	
	this.nextPage = function() {
		page++;
		this.displayList();
	}
	
	this.prevPage = function() {
		page--;
		this.displayList();
	}
	
	this.getSelected = function() {
		if(selected >= 0) {
			id = displayedEntries[selected];	
			this.eventController.listEntryClicked(this.name, id);
		} else {
			this.eventController.listEntryClicked(false, false);			
		}
	}
	
	this.selectNull = function() {
		selected = -1;
	}
}

function guiListsjs() {}
