/**
 * @author noehmeier-marcel
 */
function Friends(){
	this.friends = [];
	this.url						= rpath+"community/user.php";
	this.content = null;
	
	this.callFriendlist = function(id){
		var _self = this;
		var option = {
			onComplete : function(data){
				_self.friends = data.ret;
				_self.showFriendsOverview();
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'getFriendlist',
			id:		id
		});
	};
	
	this.showFriendsOverview = function(){
		this.content = document.getElementById('friends_content');
		if(this.friends==false){
			this.content.appendChild( document.createTextNode('Keine Freunde vorhanden.') );
		}		
		var _self = this;
		var overlay, container, nickDiv, actionDiv, links, img;
		
		overlay = this.div();
		overlay.className = 'fo_overlay';
		
		for(var i = 0; i < this.friends.length; ++i){
			container = this.div();
			container.className = 'container';
			container.link = rpath+'community/'+_self.friends[i].username;
			container.onclick = function(){
				location.href = this.link;
			}
			
			nickDiv = this.div();
			nickDiv.className = 'nick';
			
			actionDiv = this.div();
			actionDiv.className = 'action';
			
			img = document.createElement('img');
			
			if(this.friends[i].img==null){
				this.friends[i].img = 'nouserpic.jpg';
				//alert(this.friends[i].img);
			}
			img.src = rpath+'bilder/user/s/'+this.friends[i].img;
			nickDiv.appendChild(img);
			nickDiv.appendChild( document.createTextNode(this.friends[i].username) );			
			container.appendChild(nickDiv);
			
			/*
			links = document.createElement('a');
			links.innerHTML = 'chat';
			links.href = rpath+'community/'+this.friends[i].username;
			//actionDiv.appendChild(links);
			
			container.appendChild(actionDiv);
			*/
			overlay.appendChild(container);
		}
		//Für FF
		var br = document.createElement('br');
		br.clear = 'all';
		overlay.appendChild( br );
		//Für IE7
		var br = document.createElement('div');
		br.style.clear = 'all';
		overlay.appendChild( br );
		
		this.content.appendChild(overlay);
	};
	
	this.showAlertMessage = function(msg,reload){
		var overlay, head, body, ok, text, clear;
		head = document.createElement('div');
		head.style.textAlign = 'left';
		head.style.backgroundColor = '#691c23';
		head.style.padding = '2px';
		head.style.color = '#f1f1f1';
		head.style.height = '17px';
		
		body = document.createElement('div');
		body.style.backgroundColor = '#f1f1f1';
		body.style.color = '#333';
		body.style.padding = '2px';
		
		overlay = document.createElement('div');
		overlay.style.position = 'absolute';
		overlay.style.left = '200px';
		overlay.style.top = '100px';
		overlay.style.width = '300px';
		overlay.style.fontSize = '11px';
		overlay.style.border = '1px solid #ddd';
		overlay.style.zIndex = '2222222';

		ok = document.createElement('button');
		ok.className = 'com';
		ok.innerHTML = 'schließen';
		ok = setButton(ok,'button');
		ok.onclick = function(){
			document.body.removeChild(overlay);
			if(typeof(reload)!='undefined' && reload == true){
				location.reload();
			}
		}
		head.appendChild( ok );
		
		text = document.createElement('span');
		text.style.width = '100px';
		text.appendChild( document.createTextNode('Meldung') );
		head.appendChild(text);
		
		if(typeof(msg)=='string'){
			body.appendChild( document.createTextNode( msg ) );
		}else{
			body.appendChild( msg );
		}
				
		overlay.appendChild(head);
		overlay.appendChild(body);
		
		document.body.appendChild(overlay);
	};
	
	this.addFriend = function(id){
		var _self = this;
		var option = {
			onComplete : function(data){
				if(data.ret === 1){
					_self.showAlertMessage('Dieser User ist schon in deinem Freundesbaum. Schau doch mal nach! :)');
				}else if(data.ret === 2){
					_self.showAlertMessage('Dieser User hat dich geblockt!');
				}else{
					_self.showAlertMessage('Anfrage wurde gestellt!',true);
				}
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'addFriend',
			id:			id
		});
	};
	
	this.deleteFriend = function(id){
		var _self = this;
		var option = {
			onComplete : function(data){
				_self.showAlertMessage('Du hast ihm die Freundschaft gekündigt. Nicht sehr nett von dir!',true);
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'delFriend',
			id_from:			id
		});
	};
	
	this.disableFriend = function(id){
		var _self = this;
		var option = {
			onComplete : function(data){
				_self.showAlertMessage('Du hast diese Nervensäge gesperrt!',true);
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'disableFriend',
			id_from:			id
		});
	};
	this.enableFriend = function(id){
		var _self = this;
		var option = {
			onComplete : function(data){
				_self.showAlertMessage('Du hast dem User verziehen!',true);
				
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'enableFriend',
			id_from:			id
		});
	};
	
	this.agreeFriendship = function(id,msg_id,agree){
		var _self = this;
		var option = {
			onComplete : function(data){
				if(agree){
					_self.showAlertMessage('Du hast die Freundschaft akzeptiert!',true);
				}else{
					_self.showAlertMessage('Du hast die Freundschaft abgelehnt! Nicht besonders nett!',true);
				}
			}
		};
		var jSonRequest = new Json.Remote(this.url,option).send({
			session_id:	SID,
			action:		'agreeFriendship',
			id_from:	id,
			msg_id:		msg_id,
			agree:		agree
		});
	};
	
	
	this.div = function(){
		return document.createElement('div');
	};
}
