function WallEvents(){
	if(!WallEvents.instance){
		WallEvents.instance = this;
	}else if(WallEvents.instance.properties.singleton){
		return WallEvents.instance;
	}
	

	this.properties = {
		id: 'wall_events',
		id_user: 0,
		is_guest: 0,
		singleton: 1,
		url: 'ajax_backend.php',
		view_button_title: 'View more...',
		send_button_title: 'Send',
		placeholder: '',
		onInit: function(){},
		load_on_scroll: 1,
		all_loaded: 0,
		max_id: 0,
		min_id: 0,
		type: 'homepage'
	}
	
	var init_uniq_id = 0;
	var loading_status = 0;
	var deleting_status = 0;
	var posting_status = 0;

	var _self = this;

	
	this.init = function(options){
		$.extend(_self.properties, options);
		$('#'+_self.properties.id).wrap('<div id="'+_self.properties.id+'_wrapper">')
		$('#'+_self.properties.id+'_wrapper')
			.append('<div class="wall_events_button_cont"><input type="button" value="'+_self.properties.view_button_title+'" /></div>')
			.on('click', '.wall_events_button_cont input[type="button"]', function(){_self.loadEvents();});
		if(!_self.properties.is_guest){
			$('#'+_self.properties.id+'_wrapper')
				.prepend('<div class="wall_events_form_cont"><table><tr><td><textarea id="'+_self.properties.id+'_message" class="wall_post_text" placeholder="'+_self.properties.placeholder+'" onkeyup="BadWords(\''+_self.properties.id+'_message\')"></textarea><div id="bad_div_'+_self.properties.id+'_message" class="bad_div"></div></td><td><input type="button" value="'+_self.properties.send_button_title+'" /></td></tr></div>')
				.on('click', '.wall_events_form_cont input[type="button"]', function(){if(!BadWords(_self.properties.id+'_message')) _self.newPost();})
				.on('focusin', 'textarea.wall_post_text', function(){$(this).css('height', '90px');})
				.on('focusout', 'textarea.wall_post_text', function(){$(this).css('height', '32px');});
		}
		if(_self.properties.all_loaded){
			$('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont input[type="button"]').hide();
		}
		if(_self.properties.load_on_scroll){
			setTimeout(function(){_self.setLoadOnScroll(1, 1);}, 500);
		}
		init_uniq_id++;
		_self.properties.onInit();
		return this;
	}
	
	
	this.reload = function(){
		_self.setLoadOnScroll(0);
		loading_status = deleting_status = posting_status = 0;
		_self.properties.min_id = _self.properties.max_id = _self.properties.all_loaded = 0;
		init_uniq_id++;
		$('#'+_self.properties.id).html('');
		_self.loadEvents();
		_self.setLoadOnScroll(1,1);
	}
	
	
	this.setLoadOnScroll = function(load_on_scroll, is_init){
		is_init = is_init || 0;
		if(load_on_scroll && (!_self.properties.load_on_scroll || is_init)){
			_self.properties.load_on_scroll = 1;
			/*if($(window).scrollTop() > $('#'+_self.properties.id).offset().top){
				$(window).scrollTop($('#'+_self.properties.id).offset().top);
			}*/
			$(window).scrollTop(0);
			$(window).bind('scroll', function(){
				var offset = $('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont').offset();
				var window_height = window.innerHeight ? window.innerHeight : $(window).height();
				if(offset && $(window).scrollTop() >= offset.top - window_height + 100){
					_self.loadEvents();
				}
			});
		}else{
			_self.properties.load_on_scroll = 0;
			$(window).unbind('scroll');
		}
	}
	
	
	this.loadEvents = function(){
		if(_self.properties.all_loaded || loading_status){
			return this;
		}
		loading_status = 1;
		$('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont input[type="button"]').hide();
		
		$.ajax({
			type: 'POST',
			url: _self.properties.url,
			data: {
				sel: 'wall', 
				data: {wall_events:{
					min_id: _self.properties.min_id,
					max_id: _self.properties.max_id,
					type: _self.properties.type,
					id_user: _self.properties.id_user
				}}
			},
			beforeSend: function(jqXHR, settings){
				jqXHR.uniq_id = init_uniq_id;
			},
			success: function(data, textStatus, jqXHR){
				if(data.wall_events){
					resp = data.wall_events;
					_self.properties.all_loaded = resp.all_loaded;
					if(resp.min_id < _self.properties.min_id || _self.properties.min_id == 0){
						_self.properties.min_id = resp.min_id;
					}
					if(resp.max_id > _self.properties.max_id || _self.properties.max_id == 0){
						_self.properties.max_id = resp.max_id;
					}
					
					if(resp.all_loaded){
						$('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont input[type="button"]').hide();
					}else{
						$('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont input[type="button"]').show();
					}
					$('#'+_self.properties.id).append($(resp.html));
				}
				loading_status = 0;
			},
			error: function(jqXHR, textStatus, errorThrown){
				loading_status = 0;
				if(typeof(console)!=='undefined'){
					console.log(errorThrown);
				}
				$('#'+_self.properties.id+'_wrapper').find('.wall_events_button_cont input[type="button"]').show();
			},
			dataType: 'json',
			backend: 0
		});
	}
	
	
	this.newPost = function(){
		var text = _self.trimStr($('#'+_self.properties.id+'_wrapper').find('textarea').val());
		if(posting_status || !text){
			return this;
		}
		posting_status = 1;
		
		$.ajax({
			type: 'POST',
			url: _self.properties.url,
			data: {
				sel: 'wall', 
				data: {wall_post:{
					text: text,
					type: _self.properties.type,
					id_user: _self.properties.id_user,
					max_id: _self.properties.max_id
				}}
			},
			beforeSend: function(jqXHR, settings){
				jqXHR.uniq_id = init_uniq_id;
			},
			success: function(data, textStatus, jqXHR){
				if(data.wall_post){
					resp = data.wall_post;
					if(resp.status == 1 && resp.html){
						$('#'+_self.properties.id+'_wrapper').find('textarea').val('');
						$('#'+_self.properties.id).prepend($(resp.html));
					}
				}
				posting_status = 0;
			},
			error: function(jqXHR, textStatus, errorThrown){
				posting_status = 0;
				if(typeof(console)!=='undefined'){
					console.log(errorThrown);
				}
			},
			dataType: 'json',
			backend: 0
		});
	}
	
	
	this.deleteWallPost = function(id_post){
		id_post = id_post || 0;
		if(deleting_status || !id_post){
			return this;
		}
		deleting_status = 1;
		
		$.ajax({
			type: 'POST',
			url: _self.properties.url,
			data: {
				sel: 'wall', 
				data: {wall_del_post:{
					id_post: id_post,
					type: _self.properties.type,
					id_user: _self.properties.id_user
				}}
			},
			beforeSend: function(jqXHR, settings){
				jqXHR.uniq_id = init_uniq_id;
			},
			success: function(data, textStatus, jqXHR){
				if(data.wall_del_post){
					resp = data.wall_del_post;
					if(resp.status == 1){
						$('#wall_events_item_'+id_post).remove();
					}
				}
				deleting_status = 0;
			},
			error: function(jqXHR, textStatus, errorThrown){
				deleting_status = 0;
				if(typeof(console)!=='undefined'){
					console.log(errorThrown);
				}
			},
			dataType: 'json',
			backend: 0
		});
	}
	
	
	this.showComments = function(id_post, gid){
		id_post = id_post || 0;
		gid = gid || 0;
		if(!id_post || !gid){
			return this;
		}
		
		var comments_cont = $('#wall_events_item_comments_'+id_post).find('.comments_cont')
		if(comments_cont.size()){
			comments_cont.slideToggle(100);
		}else{
			$.ajax({
				type: 'POST',
				url: _self.properties.url,
				data: {
					sel: 'get_ajax_comments', 
					id: id_post,
					gid: gid
				},
				beforeSend: function(jqXHR, settings){
					jqXHR.uniq_id = init_uniq_id;
				},
				success: function(resp, textStatus, jqXHR){
					if(resp){
						$('#wall_events_item_comments_'+id_post).html(resp);
					}
				},
				error: function(jqXHR, textStatus, errorThrown){
					if(typeof(console)!=='undefined'){
						console.log(errorThrown);
					}
				},
				dataType: 'html',
				backend: 0
			});
		}
	}
	
	
	this.trimStr = function(s) {
		s = s.replace( /^\s+/g, '');
		return s.replace( /\s+$/g, '');
	}
	
	return this;
};