var bProfileWall = {
	uid:0,
	page:1,
	container:null,
	area:null,
	thread:null,
	parent:null,
	editor:null,
	cancel:null,
	button:null,
	text:null,
	remote:null,
	
	pagetool:null,
		
	init: function(uid) {
		this.uid = uid;
		this.construct();
		this.update();
	},
	
	construct: function() {
		
		var div;
		var text;
		var button;
		var cancel;
		var thread;
		var parent;
		
		this.container = jQuery('#bprofilewall');
		this.pagetool = new bPageTool;
		
		//. create basic input.
		this.editor = div = jQuery.create('div',{'id':'bprofilewall-editor'}).css({'clear':'both','margin':'0px 0px 24px 0px'});
		this.text = text = jQuery.create('input',{'id':'bprofilewall-editor-input'}).css({'width':'80%'});
		this.button = button = jQuery.create('input',{'type':'button','value':'Post'}).css({'float':'right'}).click(function(){ bProfileWall.save(); });
		this.cancel = cancel = jQuery.create('input',{'id':'bprofile-wall-cancel','type':'button','value':'Cancel'}).css({'float':'right','display':'none'}).click(function(){ bProfileWall.mount(0,0); });
		this.thread = thread = jQuery.create('input',{'id':'bprofilewall-thread','type':'hidden','value':0});
		this.parent = parent = jQuery.create('input',{'id':'bprofilewall-parent','type':'hidden','value':0});
		div.append(thread);
		div.append(parent);
		div.append(text);
		div.append(cancel);
		div.append(button);
		this.container.append(div);
		
		//. create input area.
		this.area = div = jQuery.create('div',{'id':'bprofilewall-area'}).css({'clear':'both','padding-top':'10px'});
		this.container.append(div);
		
		//. create remote
		this.remote = div = jQuery.create('div',{'id':'bprofilewall-remote'});
		this.container.append(div);
	
		return;
	},
	
	update: function(page) {

		if(!page) page = bProfileWall.page;
	
		jQuery.get(
			'/ai/user-comments.api/' + getTime(),
			{ 'user':this.uid,'page':page },
			function(json) {
			
				var html = new String;
				var style = new String;
				var odd = true;
				var that = bProfileWall;
				
				if(json.list.length > 0) {
					jQuery(json.list).each(function(){
					
						if(this.commentID != this.threadID) style = 'clear:both;margin-left:32px';
						else style = 'clear:both;';

						html += ''+
'<div id="bprofilewall-post-' + this.commentID + '" style="'  + ((this.parentID != this.commentID)?('margin-left:42px;'):('')) +  '">'+
'<div class="commentbox ' + ((this.parentID != this.commentID)?('commentboxr'):('')) + '">'+
'	<div class="content">'+
'		<div class="wrapper">'+
'			<div class="t"></div>'+
'			<div class="bd">'+
'				<div class="round">'+
'					<div class="comment-thumb"><img src="' + this.userAvatarURL + '" alt="" width="34" height="34" style="margin-right:3px" /></div>'+
'					<div class="comment-txt">' + this.text + '</div>'+
'					<div class="comment-brk">&nbsp;</div>'+
'				</div>'+
'			</div>'+
'		</div>'+
'	</div>'+
'	<div class="b"><div></div></div>'+
'</div>'+
'<div class="comment-info">'+
'	<div class="lfloat"><a href="' + this.userProfileURL + '"><strong>' + this.username + '</strong></a> on ' + this.date + '</div>'+
'	<div class="rfloat"><a href="javascript:bProfileWall.mount(\'' + this.threadID + '\',\'' + this.commentID + '\');"><strong>Reply</strong></a> &nbsp; <a class="delete" href="javascript:bProfileWall.deletePost(\'' + this.id + '\');"><strong>Delete</strong></a></div>'+
'</div>'+
'<div style="clear:both;margin-left:16px;" class="reply-mount"></div>'+
'</div>'+
							'';


/*
						html += ''+
							'<div id="bprofilewall-post-' + this.commentID + '" class="' + ((this.parentID != this.commentID)?('comment-reply'):('comment-parent')) + '">'+
							'<div class="commentbox" style="">' +
							'<div class="round"><div class="t"><div class="r"></div></div><div class="c"><div class="br"></div>'+
							'<div class="comment-thumb"><img src="' + this.userAvatarURL + '" alt="" style="margin-right: 3px;" height="34" width="34"></div>'+
							'<div class="comment-txt">' + this.text + '</div>'+
							'<div class="comment-brk">&nbsp;</div>'+
							'<div class="br"></div></div><div class="b"><div class="r"><!----></div></div></div>'+
							'</div>'+
							'<div class="comment-info">'+
							'<div class="lfloat"><a href="' + this.userProfileURL + '"><strong>' + this.username + '</strong></a> on ' + this.date + '</div>'+
							'<div class="rfloat"><a href="javascript:bProfileWall.mount(\'' + this.threadID + '\',\'' + this.commentID + '\');">reply</a> &bull; <a class="delete" href="javascript:bProfileWall.deletePost(\'' + this.id + '\');">delete</a></div>'+
							'</div><div style="clear:both;" class="reply-mount"></div>'+							
							'</div>'+
							'';
*/
/*					
						html += ''+
							'<div id="bprofilewall-post-' + this.commentID + '" class="blog-box' + ((this.commentID == this.threadID)?(1):(2)) + '" style="' + style + '">'+
							'<a href="' + this.userProfileURL + '"><img class="bprofilewall-icon" title="" alt="' + this.username + '" src="' + this.userAvatarURL + '" style="float:left;width:32px;height:32px;margin-right:8px;margin-bottom:8px;" /></a>'+
							'<div style="float:right;margin-left:8px;margin-bottom:4px;font-size:0.8em;font-style:italic;text-align:right">'+
								'<a href="javascript:bProfileWall.mount(\'' + this.threadID + '\',\'' + this.commentID + '\');">reply</a><br />'+
								'<a class="delete" href="javascript:bProfileWall.deletePost(\'' + this.id + '\');">delete</a></div>'+
							''+this.text+'<div style="font-size:0.8em;font-style:italic;">' + this.date + ' (' + this.timeAgo + ')</div>'+
							'<div style="clear:both"></div>'+
							'</div>';
*/
						
						odd = !odd;
					});
				}
				
				//. update the page tool.
				that.page = that.pagetool.page = json.page;
				that.pagetool.pageCount = json.pageCount;
				that.pagetool.pageCallback = function(page){
					that.update(page);
					return;
				}; that.remote.html(that.pagetool.get());
				
				that.area.html(html);
				
				that.area.find('img.bprofilewall-icon').each(function(){
					jQuery(this).qtip({
						content:this.alt,
						position: { corner: { target:'leftMiddle',tooltip:'rightMiddle' } },
						style: { name:'dark',tip:{corner:'rightMiddle',x:8,y:8},border:{ width:3,radius:6 }  }
					});
				});
				
				
				return;
			},'json'
		);
	
		return;
	},
	
	deletePost: function(pid) {
		if(confirm('This will delete the comment and all replies to it. Are you sure?'))
		jQuery.post(
			'/ai/user-comment-delete.api/' + getTime(),
			{ 'pid':pid },
			function(json) {
				bProfileWall.update();
				return;
			},'json'
		);
		
		return;
	},
	
	mount: function(thread,parent) {
	
		this.thread.val(thread);
		this.parent.val(parent);
		
		if(parent) {
			jQuery('#bprofilewall-post-' + parent + ' div.reply-mount').append(this.editor);
			this.cancel.show()
			this.text.focus().css({'width':'70%'}).val('');
		} else {
			this.container.prepend(this.editor);
			this.text.focus().css({'width':'80%'}).val('');
			this.cancel.hide();
		}
			
		return;
	},
	
	save: function() {
	
		var data = {
			'user':this.uid,
			'thread':this.thread.val(),
			'parent':this.parent.val(),
			'content':this.text.val()
		};
		
		jQuery.post(
			'/ai/user-comment-post.api/' + getTime(),
			data,
			function(json) {
			
				bProfileWall.text.val('');
				bProfileWall.mount(0,0);
				bProfileWall.update();
			
				return;
			},'json'
		);
	
		return;
	}
};