function initialiseFriendslist(startParam) {
	containerId = profilePage.getContainerForApp(startParam);
	if(containerId) {
		var friendslist = new FriendslistClass();
		appManagerId = appManager.runApp(startParam, friendslist);
		friendslist.appId = startParam;
		friendslist.name = 'appManager.runningApps["' + startParam + '"]';
		friendslist.id = containerId;
		document.getElementById(containerId).className = document.getElementById(friendslist.id).className + ' contentDoubleBox';
		friendslist.limit = 12;
		friendslist.title = '<span class="darkerGreen">Meine</span> Freunde...';
		friendslist.type = 'app';
		friendslist.init();
	}
}

function FriendslistClass() {
	var page = 0;
	var pageCounter = 0;
	var pages = new Array;
	var sameFriends = false;
	
	this.init = function() {
		this.box = document.getElementById(this.id);
		
		if(typeof(displaySameFriends)!='undefined') { 
			if(displaySameFriends) {
				sameFriends = true;
				this.title = '<span class="darkerGreen">Gemeinsame</span> Freunde...';
			}
		}

		this.buildContainer();		
		
		this.loadFriendslist();
		
		if(!this.appId) {
			this.appId = 0;
		}
	}
	
	this.loadFriendslist = function() {
		if(sameFriends) {
			jsonGetData('json/friends/getFriendslist.php?appId=' + this.appId + '&limit=' + this.limit + '&sameFriends=true&page=' + page, this.name + '.loadFriendslistInt');
		} else {
			jsonGetData('json/friends/getFriendslist.php?appId=' + this.appId + '&limit=' + this.limit + '&sameFriends=false&page=' + page, this.name + '.loadFriendslistInt');			
		}
	}
	
	this.loadFriendslistInt = function(Object) {		
		if(Object["status"] && Object["friends"] && Object["page"]) {
			document.getElementById(this.id + 'topScrollBar').style.display = 'block';
			if(this.type != 'mini') {
				document.getElementById(this.id + 'bottomScrollBar').style.display = 'block';
			}
			if(Object["status"] == 'ok' && Object["friends"].length > 0 && Object["page"] == page) {

				if(Object["count"] > this.limit) {
					pageCounter = Math.ceil(Object["count"] / this.limit);
				}
								
				if(pageCounter == 0) {
					pageCounter = 1;
				}
				
				pages[page] = new Array;
				pages[page]["friends"] = new Array;
				pages[page]["loaded"] = true;
				for(z1=0; z1 < Object["friends"].length; z1++) {
					pages[page]["friends"][z1] = new Array;
					pages[page]["friends"][z1]["publicId"] = Object["friends"][z1]["PublicID"];			
					pages[page]["friends"][z1]["userName"] = Object["friends"][z1]["PublicID"];	
					pages[page]["friends"][z1]["urlName"] = Object["friends"][z1]["PublicID"];		
					pages[page]["friends"][z1]["name"] = Object["friends"][z1]["Name"];
					pages[page]["friends"][z1]["imagePath"] = Object["friends"][z1]["ThumbnailPath"];
					pages[page]["friends"][z1]["type"] = Object["friends"][z1]["Type"];
					pages[page]["friends"][z1]["status"] = Object["friends"][z1]["Status"];
				}
				this.displayFriendslist();			
			}
		} else {
			document.getElementById(this.id + "friendslist").innerHTML = '';
			document.getElementById(this.id + 'topScrollBar').style.display = 'none';
			if(this.type != 'mini') {			
				document.getElementById(this.id + 'bottomScrollBar').style.display = 'none';
			}
			var noFriendsIcon = document.createElement("IMG");
			noFriendsIcon.src = '/images/style/icons/friendsOnBrightSmall.gif';
			noFriendsIcon.style.marginTop = '20px';
			noFriendsIcon.className = 'noFriendsIcon';
					
			var noFriendsMessage = document.createElement("DIV");
			noFriendsMessage.className = 'noFriendsMessage';
			noFriendsMessage.style.marginTop = '15px';
			noFriendsMessage.style.marginBottom = '20px';
			noFriendsMessage.style.color = '#888888';
			noFriendsMessage.style.fontSize = '11px';
			noFriendsMessage.style.textTransform = 'uppercase';
			if(sameFriends) {
				noFriendsMessage.innerHTML = 'Ihr habt bisher<br />keine gemeinsamen Freunde.';
			} else {
				noFriendsMessage.innerHTML = 'Es wurden keine<br />Freunde gefunden.';				
			}
			document.getElementById(this.id + "friendslist").style.textAlign = "center";
			document.getElementById(this.id + "friendslist").appendChild(noFriendsIcon);			
			document.getElementById(this.id + "friendslist").appendChild(noFriendsMessage);
		}
	}
	
	this.setContainerClass = function(id, className) {
		document.getElementById(this.id + 'friendContainer' + id).className = className;
	}
	
	this.displayFriendslist = function() {
		var containerClass = 'friendContainer';
		
		if(this.type == 'mini') {
			document.getElementById(this.id + 'topScrollBar').innerHTML = this.title;			
		} else {
			document.getElementById(this.id + 'topScrollBar').innerHTML = 'Seite ' + Math.round(page + 1) + ' von ' + pageCounter;
		}
		
		var topScrollRightButton = document.createElement("IMG");
		if(pageCounter <= page + 1) {
			topScrollRightButton.className = 'scrollRightButton scrollButtonInactive';
		} else {
			eval("topScrollRightButton.onclick = function() { " + this.name + ".nextPage(); }");
			topScrollRightButton.className = 'scrollRightButton';			
		}
		
		if(this.type != 'mini') {
			topScrollRightButton.src = '/images/style/arrows/greyArrowRightActive.gif';
		} else {
			topScrollRightButton.src = '/images/style/arrows/greyArrowRightActive.gif';			
		}
		
		var topScrollLeftButton = document.createElement("IMG");
		if(page == 0) {
			topScrollLeftButton.className = 'scrollLeftButton scrollButtonInactive';
		} else {
			eval("topScrollLeftButton.onclick = function() { " + this.name + ".prevPage(); }");
			topScrollLeftButton.className = 'scrollLeftButton';			
		}

		if(this.type != 'mini') {
			topScrollLeftButton.src = '/images/style/arrows/greyArrowLeftActive.gif';
		} else {
			topScrollLeftButton.src = '/images/style/arrows/greyArrowLeftActive.gif';			
		}

		document.getElementById(this.id + 'topScrollBar').appendChild(topScrollRightButton);		
		document.getElementById(this.id + 'topScrollBar').appendChild(topScrollLeftButton);
		
		document.getElementById(this.id + 'friendslist').innerHTML = '';

		if(pages[page]["friends"].length > 0) {
	
			for(z1=0; z1 < pages[page]["friends"].length; z1++) {

				friendContainer = document.createElement("DIV");
				friendContainer.id = this.id + 'friendContainer' + z1;
				friendContainer.className = containerClass;
				eval("friendContainer.onmouseover = function() { this.className ='friendContainer hovered'; };");		
				eval("friendContainer.onmouseout = function() { this.className ='friendContainer'; };");		
				eval("friendContainer.onclick=function() { " + this.name + ".clickedOnFriend(" + z1 + "); };");		
				
				friendName = document.createElement("DIV");
				friendName.className = 'friendName';
				friendName.innerHTML = pages[page]["friends"][z1]['name'];
		
				if(this.type != 'mini') {
					friendType = document.createElement("DIV");
					friendType.className = 'friendType';
					friendType.innerHTML = pages[page]["friends"][z1]['type'];		
					
					friendStatus = document.createElement("DIV");
					friendStatus.className = 'friendStatus';
					friendStatus.innerHTML = '<b>Letzter Status -</b> ' + pages[page]["friends"][z1]['status'];
				}

				if(pages[page]["friends"][z1]['imagePath']) {				
					friendImage = document.createElement("IMG");
					friendImage.src = pages[page]["friends"][z1]['imagePath'];
					friendImage.className = 'friendImage';
				} else {
					friendImage = document.createElement("DIV");
					friendImage.className = 'friendImageContainer';
				}
				
				friendLink = document.createElement("A");
				friendLink.href = '/user/' + pages[page]["friends"][z1]['urlName'];
				friendLink.className = 'friendLink';
				friendLink.innerHTML = '&raquo;';
				
				friendContainer.appendChild(friendName);
				if(this.type != 'mini') {
					if(pages[page]["friends"][z1]['type'] && pages[page]["friends"][z1]['type']!=null) {
						friendContainer.appendChild(friendType);
					}
					if(pages[page]["friends"][z1]['status']) {
						friendContainer.appendChild(friendStatus);
					}
				}
				
				friendContainer.appendChild(friendImage);

				friendContainer.appendChild(friendLink);
		
				document.getElementById(this.id + 'friendslist').appendChild(friendContainer);
				
				if(containerClass == 'friendContainer') {
					containerClass = 'friendContainer friendContainerUneven';
				} else {
					containerClass = 'friendContainer';
				}
			}
			
			if(this.type != 'mini') {
			
				document.getElementById(this.id + 'bottomScrollBar').innerHTML = 'Seite ' + Math.round(page + 1) + ' von ' + pageCounter;
				
				var bottomScrollRightButton = document.createElement("IMG");
				
				if(pageCounter <= page + 1) {
					bottomScrollRightButton.className = 'scrollRightButton scrollButtonInactive';
				} else {
					bottomScrollRightButton.className = 'scrollRightButton';			
					eval("bottomScrollRightButton.onclick = function() { " + this.name + ".nextPage(); }");
				}
				
				bottomScrollRightButton.src = '/images/style/arrows/greyArrowRightActive.gif';
							
				var bottomScrollLeftButton = document.createElement("IMG");
				
				if(page == 0) {
					bottomScrollLeftButton.className = 'scrollLeftButton scrollButtonInactive';
				} else {
					eval("bottomScrollLeftButton.onclick = function() { " + this.name + ".prevPage(); }");
					bottomScrollLeftButton.className = 'scrollLeftButton';			
				}
				
				bottomScrollLeftButton.src = '/images/style/arrows/greyArrowLeftActive.gif';
	
				document.getElementById(this.id + 'bottomScrollBar').appendChild(bottomScrollRightButton);			
				document.getElementById(this.id + 'bottomScrollBar').appendChild(bottomScrollLeftButton);
			}
		}
	}
	
	this.prevPage = function() {
		if(page > 0) {
			page--;
			if(pages[page]) {
				this.displayFriendslist();
			} else {
				this.loadFriendslist();
			}
		}
	}
	
	this.nextPage = function() {
		if(Math.round(page + 1) < pageCounter) {
			page++;
			if(pages[page]) {
				this.displayFriendslist();		
			} else {
				this.loadFriendslist();
			}
		}
	}
	
	this.clickedOnFriend = function(id) {

		if(this.eventController) {
			this.eventController.contactClicked(pages[page]["friends"][id]);
		} else {
			location.href = '/user/' + pages[page]["friends"][id]['urlName'];
		}
	}
	
	this.changeListType = function(listType) {
		if(listType == 1) {
			page = 0;
			pageCounter = 0;
			pages = new Array;
			
			sameFriends = true;
			document.getElementById(this.id + 'header').innerHTML = '<span class="darkerGreen">Gemeinsame</span> Freunde...';
			
			var headerOptions = document.createElement("DIV");
			headerOptions.className = 'headerOptions';
			headerOptions.id = this.id + 'headerOptions';
			headerOptions.innerHTML = '<li><a href="JavaScript:' + escape(this.name) + '.changeListType(0);">Alle Freunde anzeigen</a></li>';
			
			document.getElementById(this.id + 'header').appendChild(headerOptions);		

			this.loadFriendslist();		
		} else {
			page = 0;
			pageCounter = 0;
			pages = new Array;
			
			sameFriends = false;
			document.getElementById(this.id + 'header').innerHTML = '<span class="darkerGreen">Alle</span> Freunde...';		

			var headerOptions = document.createElement("DIV");
			headerOptions.className = 'headerOptions';
			headerOptions.id = this.id + 'headerOptions';
			headerOptions.innerHTML = '<li><a href="JavaScript:' + escape(this.name) + '.changeListType(1);">Gemeinsame Freunde anzeigen</a></li>';
			
			document.getElementById(this.id + 'header').appendChild(headerOptions);				
			
			this.loadFriendslist();
		}
	}
	
	this.buildContainer = function() {
		this.box.className = this.box.className + ' friendslistContainer';
		
		
		var containerHeader = document.createElement("DIV");
		containerHeader.id = this.id + 'header';
		containerHeader.className = 'header';
		containerHeader.innerHTML = this.title;
		
		var headerOptions = document.createElement("DIV");
		headerOptions.className = 'headerOptions';
		headerOptions.id = this.id + 'headerOptions';
		if(sameFriends) {
			headerOptions.innerHTML = '<li><a href="JavaScript:' + escape(this.name) + '.changeListType(0);">Alle Freunde anzeigen</a></li>';			
		} else {
			headerOptions.innerHTML = '<li><a href="JavaScript:' + escape(this.name) + '.changeListType(1);">Gemeinsame Freunde anzeigen</a></li>';			
		}
		
		containerHeader.appendChild(headerOptions);

		var topScrollBar = document.createElement("DIV");
		topScrollBar.id = this.id + 'topScrollBar';
		topScrollBar.className = 'friendlistScrollBar top';
		
		var friendslist = document.createElement("DIV");
		friendslist.id = this.id + "friendslist";

		var bottomScrollBar = document.createElement("DIV");
		bottomScrollBar.id = this.id + 'bottomScrollBar';
		bottomScrollBar.className = 'friendlistScrollBar bottom';		
		
		if(this.type != 'mini') {
			this.box.appendChild(containerHeader);
		}
		
		this.box.appendChild(topScrollBar);		
		this.box.appendChild(friendslist);
		
		if(this.type != 'mini') {
			this.box.appendChild(bottomScrollBar);
		}
	}
	
	this.sendContacts = function() {
		
	}
}

function friendsfriendslistjs() {}
