/**
 * @author noehmeier-marcel
 */
function User(sid){
	this.session_id					= sid;
	this.logged						= false;
	this.username					= '';
	this.password					= '';
	this.url						= rpath+"community/user.php";
	this.data						= {username:''};
	
	this.htmlObj					= {
		login_box : 		document.getElementById('login_box'),
		top5_menu : 		document.getElementById('top5_menu'),
		top5_content : 		document.getElementById('top5_content'),
		top5_header : 		document.getElementById('top5_header'),
		dropdown_list :		document.getElementById('dropdown_list'),
		right_user_menu : 	document.getElementById('right_user_menu'),
		message_content : 	document.getElementById('message_content'),
		messageContent : {
			content : document.getElementById('cdWindow'),
			header : document.getElementById('cdWindowheader'),
			body : document.getElementById('cdWindow_body')
		}
	}
	
	this.messages					= {
		inbox: {
			title: 'Eingang',
			count: 00,
			messages: []
		},
		outbox: {
			title: 'Ausgang',
			count: 00,
			messages: []
		},
		archiv: {
			title: 'Archiv',
			count: 0,
			messages: []
		}
	};
	
	
	this.top5						= {
		active_header : 'Who is Online',
		active_content : 'loading'
	};
	
	this.top5Active					= function(){
		var user = this;
		var jSonRequest = new Json.Remote(this.url,{
			onComplete : function(l){
				switch(l.ret)
				{
					case "whoisonline":
					user.whoIsOnline();
					break;
					
					case "lastlogin":
					user.lastLogins('5');
					break;
					
					case "lastregister":
					user.lastRegister('5');
					break;
					
					case "lastnews":
					user.lastNews('5');
					break;
					
					case "lastPics":
					user.lastPics('5');
					break;
					
					case "lastClicks":
					user.lastClicks();
					break;
					
					default:
					user.whoIsOnline();
					break;
				}
			}
		}).send({
				session_id:	this.session_id,
				action:		'gettop5active'
			});
	}
	
	
	
	
	this.checkValues				= function(regex,value){
		return regex.test(value);
	}
	this.validateUser				= function(){
		return this.checkValues(/^[0-9a-zA-z]{5,12}$/,this.data.username);
	}
	this.validatePassword			= function(){
		return this.checkValues(/^[0-9a-zA-z]{5,15}$/,this.data.username);
	}
	
	this.login						= function(username,password){
		this.data.username = username.value;
		this.data.password = password.value;
		if( this.validateUser() && this.validatePassword() ){
			var user = this;
			document.getElementById('login_box').innerHTML = 'Lade Daten...';
			var jSonRequest = new Json.Remote(this.url,{
				autoCancel: true,
				onComplete : function(login){
					if( login.ret == true ){
						location.href = rpath+"community/"+user.data.username;
					}else{
						alert('Benutzername oder Passwort falsch!');
						user.render();
					}
				}
			}).send({
					session_id:	this.session_id,
					username:	this.data.username,
					password:	this.data.password,
					action:		'login'
				});
		}else{
			e = 'Folgende Eingaben sind nicht korrekt:\n';
			if( !this.validateUser() )
				e += '\nUsername ('+this.username+')';
			if( !this.validatePassword() )
				e += '\nPassword ('+this.password+')';
				
			alert(e);
		}
	}
	
	this.checkLogin					= function(){
		var user = this;
		var jSonRequest = new Json.Remote(this.url,{
			onComplete : function(l){
				if( l.ret ){
					user.getUserData();
					setInterval('cdUser.top5Active()',RLT4);
					setInterval('cdUser.getMessageCount()',RLT4);						
				}else{
					user.loggedIn(l.ret);
					user.render();
				}
			}
		}).send({
				session_id:	this.session_id,
				action:		'checklogin'
			});
	}
	
	this.logout						= function(){
		if(confirm('Wirklich ausloggen?')){
			var user = this;
			var jSonRequest = new Json.Remote(this.url,{
				onComplete : function(l){
					if( l.ret ){
						user.getUserData();				
					}else{
						user.loggedIn(l.ret);
						location.href = rpath;
					}
				}
			}).send({
					session_id:	this.session_id,
					action:		'logout'
				});
		}
	}
	
	this.getUserData 				= function(){
		var user = this;
		var jSonRequest = new Json.Remote(this.url,{
			onComplete : function(data){
				user.loggedIn(true);
				user.setUserData(data);
				user.top5Active();
				user.getMessageCount();	
				user.render();			
			}
		}).send({
				session_id:	this.session_id,
				action:		'getuserdata'
			});
	}
	
	this.setUserData				= function(data){
		this.data = data;
	}
	
	this.loggedIn					= function(l){
		this.logged = l;
	}
	
	this.whoIsOnline				= function(){
		var user = this;
		option = {
			onComplete : function(data){
				user.top5.active_header = 'Who is Online';
				user.top5.active_content = '';
				for(var i = 0; i < data.ret.length; ++i){
					user.top5.active_content += user.getUserProfileLink(data.ret[i].username,data.ret[i].id);
				}
				user.renderTop5(user.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				action:		'whoisonline'
			});
	}
	
	this.lastLogins					= function(maxm){
		var user = this;
		var option = {
			onComplete : function(data){
				user.top5.active_header = 'Last 5 Logins';
				user.top5.active_content = '';
				for(var i = 0; i < data.ret.length; ++i){
					user.top5.active_content += user.getUserProfileLink(data.ret[i].username,data.ret[i].id);
				}
				user.renderTop5(this.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				max:		maxm,
				action:		'lastlogin'
			});
	}
	
	this.lastRegister				= function(maxm){
		var user = this;
		var option = {
			onComplete : function(data){
				user.top5.active_header = 'Last 5 Register';
				user.top5.active_content = '';
				for(var i = 0; i < data.ret.length; ++i){
					user.top5.active_content += user.getUserProfileLink(data.ret[i].username,data.ret[i].id);
				}
				user.renderTop5(this.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				max:		maxm,
				action:		'lastregister'
			});
	}
	
	this.lastNews				= function(maxm){
		var user = this;
		var option = {
			onComplete : function(data){
				user.top5.active_header = 'Last 5 News';
				user.top5.active_content = '';
				for(var i = 0; i < data.ret.length; ++i){
					user.top5.active_content += '<li><a style="font-size:9px;" href="'+rpath+'news/ausgabe_'+data.ret[i].id+'.htm"> - '+data.ret[i].title+'</a></li>\n<br />';
				}
				user.renderTop5(this.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				max:		maxm,
				action:		'lastnews'
			});
	}
	
	
	this.lastPics				= function(maxm){
		var user = this;
		var option = {
			onComplete : function(data){
				user.top5.active_header = 'Last 5 Pics';
				user.top5.active_content = '';
				for(var i = 0; i < data.ret.length; ++i){
					user.top5.active_content += '<div align="center"><a rel="lightbox" href="'+rpath+'bilder/user/xl/'+data.ret[i].image_name+'" target="_blank"> <img border="0" src="'+rpath+'bilder/user/s/'+data.ret[i].image_name+'" /></a><br />'+data.ret[i].username+'</div><br />\n';
				}
				user.renderTop5(this.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				max:		maxm,
				action:		'lastPics'
			});
	}
	
	this.lastClicks				= function(maxm){
		var user = this;
		var option = {
			onComplete : function(data){
				user.top5.active_header = 'Last visits';
				user.top5.active_content = '';
				if(data.ret.length>0){
					for(var i = 0; i < data.ret.length; ++i){
						user.top5.active_content += user.getUserProfileLink(data.ret[i]);
					}
				}else{
					user.top5.active_content = "Dich hat schon länger keiner mehr angeklickt!<br />:(<br /><br />Klick doch einfach mal rum und lerne neue Leute kennen<br />:D";
				}				
				user.renderTop5(this.logged);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				max:		maxm,
				action:		'lastClicks'
			});
	}
	
	this.getProfileData				= function(id){
		var user = this;
		var option = {
			onComplete : function(data){
				user.showProfile(data);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				userid:		id,
				action:		'getuserprofiledata'
			});
	}
	
	this.getUserProfileLink		= function(username){
		return '<li><a href="'+rpath+'community/'+username+'">'+username+'</a></li>\n';
	}
	this.showMyProfile				= function(){
		location.href=rpath+'community/'+this.data.username;
	}
	
	
	/******************************************************************************
	MessageSection
	********************************************************************************/
	this.showSendMessageDialog			= function(id,name){
		
		this.htmlObj.messageContent.content.style.display = 'block';
		this.htmlObj.messageContent.header.innerHTML = 'Nachricht an '+name;
		this.htmlObj.messageContent.body.innerHTML = ''+
		'<form action="#" onsubmit="cdUser.sendMessage(\''+id+'\',this); return false;">'+
		'	<table class="nachricht">'+
		'		<tr>'+
		'			<td class="left">Bereff:</td>'+
		'			<td class="right"><input name="betreff" type="text" value="" /></td>'+
		'		</tr>'+
		'		<tr>'+
		'			<td class="left">Nachricht:</td>'+
		'			<td class="right">'+
		'				<textarea name="nachricht"></textarea>'+
		'			</td>'+
		'		</tr>'+
		'		<tr>'+
		'			<td colspan="2" class="right"><button type="submit">absenden</button></td>'+
		'		</tr>'+
		'	</table>'+
		'</form>';
	}
	this.sendMessage				=function(user,form){
		var betreff = form.betreff.value;
		var nachricht = form.nachricht.value;
		var t = this;
		var option = {
			onComplete : function(data){
				if(data.ret){
					t.htmlObj.messageContent.body.innerHTML = '<span>gesendet...</span>';
				}else{
					t.htmlObj.messageContent.body.innerHTML = '<span>Fehler beim connecten auf Host</span>';
				}
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				action:		'savemessage',
				body:		nachricht.split('\n'),
				header:		betreff,
				user_id_to:	user
			});
	}
	
	
	
	this.sendGbMessage				=function(user,uid){
		var message = document.gb_form.gb_input.value;
		if(message.length<5){
			alert('Bitte geben sie mehr als 5 Zeichen ein!');
			return false;
		}
		document.gb_form.gb_input.value = 'speichere...';
		var option = {
			onComplete : function(data){
				if(data.ret){
					document.gb_form.gb_input.value = 'gepeichert';
					location.href=rpath+'community/'+user+'#gb';
					location.reload();
				}else{
					document.gb_form.gb_input.value = message;
					alert('Fehler beim connecten auf Host');
				}
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				action:		'savegbmessage',
				msg:		message.split('\n'),
				user_id:	uid
			});
	}
	
	
	
	this.getMessageCount			= function(){
		var user = this;
		var option = {
			onComplete : function(data){
				user.messages.inbox.count = data.ret;
				user.rightUserMenu();
				if(typeof(messages)!='undefined'){
					messages.callMessages();
				}
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				action:		'getreadmessagecount'
			});
	}
	
	this.deleteMessage			= function(msg_id,postCat){
		var user = this;
		var option = {
			onComplete : function(data){
				user.renderMessages(postCat);
			}
		}
		var jSonRequest = new Json.Remote(this.url,option).send({
				session_id:	this.session_id,
				action:		'deleteMessage',
				msgid:		msg_id
			});
	}
	
	this.getMessageContainer	= function(msg,postCat){
		var _new = '';
		if( msg.status == 0 ){ _new = 'msg_unread' }
		return '<div id="msg'+msg.id+'" class="msg '+_new+'" onclick="cdUser.showMessage(\''+msg.id+'\',\''+postCat+'\');">'+
				'	<div class="msg_from"><span>Von:</span> '+msg.user_from+'</div>'+
				'	<div class="msg_time"><span>Uhrzeit:</span> '+msg.ts_send.time+' - <span>Datum:</span> '+msg.ts_send.date+'</div>'+
				'	<div class="msg_action"><img onclick="cdUser.deleteMessage(\''+msg.id+'\');" src="'+rpath+'bilder/x.gif" /></div>'+
				'	<div class="msg_header"><span>Betreff:</span> '+msg.header+'</div>'+
				'	<div id="msg_content'+msg.id+'" class="msg_content"><span>Nachricht:</span><div>'+msg.body+'</div></div>'+
				'</div>\n';
	}
	
	this.getMsgObj				= function(o){
		//alert(o);
		switch(o)
		{
			case "inbox":return this.messages.inbox;
			case "outbox":return this.messages.outbox;
			case "archiv":return this.messages.archiv;
		}
	}
	
	this.getMessages			= function(postCat){
		this.messageObj = new Message();
		this.messageObj.callMessages(postCat);
	}
	
	this.showMessage			= function(msgid){
		obj = document.getElementById('msg_content'+msgid);
		if(obj.style.display == 'none'){
			obj.style.display = 'block';
			var user = this;
			var option = {
				onComplete : function(data){
					document.getElementById('msg'+msgid).className = 'msg';
				}
			}
			var jSonRequest = new Json.Remote(this.url,option).send({
					session_id:	this.session_id,
					order:		'asc',
					msg_id:		msgid,
					action:		'readmessage'
				});
		}else{
			obj.style.display = 'none';
		}
	}
	
	
	
	/******************************************************************************
	MessageSection END
	********************************************************************************/
	
	/******************************************************************************
	Images
	********************************************************************************/
	this.showImageDialog			= function(){
		imgadd = document.getElementById('user_image_add');
		imgadd.innerHTML = ''+
						'<div>'+
						'	<form action="#images" method="post" enctype="multipart/form-data">'+
						'		<table>'+
						'			<tr>'+
						'				<td align="left">'+
						'					Bild:'+
						'				</td>'+
						'			</tr>'+
						'			<tr>'+
						'				<td align="left">'+
						'					<input type="file" name="image" />'+
						'				</td>'+
						'			</tr>'+
						'			<tr>'+
						'				<td align="left">'+
						'					Kommentar:'+
						'				</td>'+
						'			</tr>'+
						'			<tr>'+
						'				<td align="left">'+
						'					<input type="text" name="kommentar" maxlength="30" size="40" />'+
						'				</td>'+
						'			</tr>'+
						'			<tr>'+
						'				<td colspan="2" align="right">'+
						'					<button type="submit">Hochladen</button>'+
						'				</td>'+
						'			</tr>'+
						'		</table>'+
						'	</form>'+
						'</div>'+
						'';

	}
	/******************************************************************************
	Images END
	********************************************************************************/
	
	
	this.inviteFriend				= function(){
		
	}
	
	this.killFriend					= function(){
		
	}
	
	this.blockUser					= function(){
		
	}
	
	this.onlineStatusFriends		= function(){
		
	}
	
	this.uploadPic					= function(){
		
	}
	
	
	
	this.render						= function(){
		this.getLoginBoxContent();
		this.rightUserMenu();
		this.renderTop5();
	}
	
	this.renderMessages				= function(postCat){
		this.htmlObj.message_content.innerHTML = '';
		var msgObj = this.getMsgObj(postCat);
		for(var i = 0; i < msgObj.messages.length; ++i){
			this.htmlObj.message_content.innerHTML += this.getMessageContainer(msgObj.messages[i],postCat);
		}
	}
	
	
	this.getLoginBoxContent			= function(e){		
		if( !this.logged ){
			this.htmlObj.login_box.innerHTML =	'<form name="login" action="#" method="post" onsubmit="cdUser.login(this.username,this.password); return false;">'+
							'<div>Username:</div> <input tabindex="1" type="text" name="username" /><br />'+
							'<div>Passwort: <a style="color:#000;" href="'+rpath+'community/pw.html">?</a></div> <input tabindex="2" type="password" name="password" /><br />'+
							'<button tabindex="3" type="submit">anmelden</button>'+
							'</form>';
		}else{
			this.htmlObj.login_box.innerHTML =	'<div>Angemeldet:</div> '+this.data['username']+'<br /><br />'+
							'<button onclick="cdUser.logout();">logout</button>';
		}
	}
	
	this.rightUserMenu				= function(){
		if(!this.logged){
			this.htmlObj.right_user_menu.style.display = 'none';
			this.htmlObj.top5_menu.style.display = 'none';
		}else{
			this.renderTop5();
			
			var rumContent =	'<div id="nav_left">'+
								'	<div id="navigation_left">'+
								'		<div class="nav_header" style="text-align:left;">MEIN CYBERDAY</div>'+
								'		<div id="right_user_content">'+
								'			<a class="myLink" '+((this.messages.inbox.count>0) ? 'style="font-weight:bold;"':'')+' href="'+rpath+'community/nachrichten.html">Nachrichten ('+this.messages.inbox.count+')</a><br /><br />'+
								'			<a class="myLink" href="'+rpath+'community/user.html#">Mein Profil</a><br />'+
								'			<a class="myLink" href="'+rpath+'community/user.html#images">Meine Bilder</a><br />'+
								'			<a class="myLink" href="'+rpath+'community/user.html#gb">Mein G&auml;stebuch</a><br />'+
								'			<a class="myLink" href="'+rpath+'community/user.html#friends">Meine Freunde</a><br />'+
								'		</div>'+
								'	</div>'+
								'</div>';
			
			this.htmlObj.right_user_menu.innerHTML = rumContent;
			this.htmlObj.right_user_menu.style.display = 'block';
			this.htmlObj.top5_menu.style.display = 'block';
		}
	}
	
	this.renderTop5					= function(){
		this.htmlObj.dropdown_list.style.display = 'none';
		this.htmlObj.top5_header.innerHTML = this.top5.active_header;
		this.htmlObj.top5_content.innerHTML = this.top5.active_content;
	}
}
