(function($){
	
	$.fn.jFilterBox = function(o){
		return this.each(function(){
			$(this).data('jFilterBox', new $fb(this, o));
		});
	}
	
	var defaults = {
		onChange : null,
		onInit : null
	};
	
	jFilterBox = function(el, o){
		this.options = $.extend({}, defaults, o || {});
		this.list = $(el);
		this.current = null;
		
		this.setup();
	};
	
	var $fb = jFilterBox;
	
	$fb.fn = jFilterBox.prototype = {
		jFilterBox : 'version 0.1.0'
	};
	
	$fb.fn.extend = $fb.extend = $.extend;
	
	$fb.fn.extend({
		
		setup : function(){
			var self = this;
			
			self.list.find('a.js-filter-column').each(function(){
				var arr = $(this).attr('href').split('#');
				if (!arr[1]) return;
				$(this).data('sort', {'column': arr[1], 'dir': ''});
			}).click(function(){
				self.set(this);
				if (typeof self.options.onChange == 'function') 
					self.options.onChange.call(self, self.current.data('sort'));
				return false;
			});
			
			self.list.find('a.js-desc:first').each(function(){
				self.set($(this).data('sort').column, 'desc');
			});
			self.list.find('a.js-asc:first').each(function(){
				self.set($(this).data('sort').column, 'asc');
			});
			
			if (typeof self.options.onInit == 'function') self.options.onInit.call(self);
		},
		
		get : function(){
			return this.current.data('sort');
		},
		
		set : function(n, d){
			var self = this;
			if (typeof n == 'object') n = $(n).data('sort').column;
			self.list.find('a.js-filter-column[href$="#'+n+'"]').each(function(){
				if (self.current) self.current.removeClass('sort-asc sort-desc');
				var $this = $(this), sort = $this.data('sort');
				if (!sort) return;
				$this.removeClass('sort-asc sort-desc');
				if (!d) d = sort.dir == 'asc' ? 'desc' : 'asc';
				sort.dir = d;
				if (sort.dir == 'asc') $this.addClass('sort-asc'); else $this.addClass('sort-desc');
				$this.data('sort',sort);
				self.current = $this;
			});
		},
		
		foo : function(){
			
		}
		
	});
	
})(jQuery);

(function($){
	
	$.fn.jPagesBox = function(o){
		return this.each(function(){
			$(this).data('jPagesBox', new $pb(this, o));
		});
	}
	
	var defaults = {
		onChange : null
	};
	
	jPagesBox = function(el, o){
		var self = this;
		
		self.options = $.extend({}, defaults, o || {});
		self.list = $(el);
		self.current = self.options.currentPage || 1;
		self.count = self.options.pagesCount || 1;
		
		self.list.find('.js-pages-current').each(function(){
			var n = parseInt($(this).text());
			if (n > 0) self.current = n;
		});
		
		self.list.find('.js-pages-count').each(function(){
			var n = parseInt($(this).text());
			if (n > 0) self.count = n;
		});
		
		self.setup();
	}
	
	var $pb = jPagesBox;
	
	$pb.fn = $pb.prototype = {
		jPagesBox : 'version 0.1.0'
	};
	
	$pb.fn.extend = $pb.extend = $.extend;
	
	$pb.fn.extend({
		
		setup : function(){
			var self = this;
			
			self.list.find('a[href$="#first"]').unbind('click').click(function(){
				self.go(1);
				return false;
			});
			self.list.find('a[href$="#last"]').unbind('click').click(function(){
				self.go(self.count);
				return false;
			});
			self.list.find('a[href$="#prev"]').unbind('click').click(function(){
				self.go(self.current-1);
				return false;
			});
			self.list.find('a[href$="#next"]').unbind('click').click(function(){
				self.go(self.current+1);
				return false;
			});
			
			self.list.find('li a[href$="#"]').click(function(){
				var page = $(this).parents('li:first').attr('value');
				if (parseInt(page)<=0){
					
				} else self.go(page);
				return false;
			});
			
			self.set(self.current);
		},
		
		go : function(n){
			var self = this;
			if (!self.set(n)) return false;
			if (typeof self.options.onChange == 'function'){
				self.options.onChange.call(self, n);
			}
		},
		
		set : function(n){
			var self = this;
			if (n < 1 || n > self.count) return false;
			var $first = self.list.find('ul.pages a[href$="#first"]');
			var $prev = self.list.find('ul.pages a[href$="#prev"]');
			var $next = self.list.find('ul.pages a[href$="#next"]');
			var $last = self.list.find('ul.pages a[href$="#last"]');
			if (n == 1){
				$first.removeClass('first').addClass('first-inactive');
				$prev.removeClass('prev').addClass('prev-inactive');
			} else {
				$first.removeClass('first-inactive').addClass('first');
				$prev.removeClass('prev-inactive').addClass('prev');
			}
			if (n == self.count){
				$next.removeClass('next').addClass('next-inactive');
				$last.removeClass('last').addClass('last-inactive');
			} else {
				$next.removeClass('next-inactive').addClass('next');
				$last.removeClass('last-inactive').addClass('last');
			}
			self.current = n;
			return true;
		},
		
		foo : function(){
			
		}
		
	});
	
})(jQuery);

(function($){
	
	$.fn.jAjaxDatalist = function(o){
		return this.each(function(){
			$(this).data('jAjaxDatalist', new $dl(this, o));
		});
	}
	
	var defaults = {
		editable : false,
		sort : null,
		loadOnInit : false,
		dataType : 'html',
		url : null,
		urlData : null,
		onLoad : null,
		onReload : null
	};
	
	jAjaxDatalist = function(el, o){
		this.origOptions = o;
		this.options = $.extend({}, defaults, o || {});
		this.list = $(el);
		this.form = this.list.find('form.js-data-form:first');
		this.ul = this.list.find('ul.js-data-list:first');
		this.filter = this.list.find('.js-filter-box');
		this.pages = this.list.find('.js-pages-box');
		this.currentSort = { column: 'id', dir: 'desc' };
		this.currentPage = 1;
		
		this.setup();
	}
	
	var $dl = jAjaxDatalist;
	
	$dl.fn = $dl.prototype = {
		jAjaxDatalist : 'version 0.1.0'
	};
	
	$dl.fn.extend = $dl.extend = $.extend;
	
	$dl.fn.extend({
		
		setup : function(){
			var self = this;
			
			if (self.options.sort) self.currentSort = self.options.sort;
			
			self.filter.jFilterBox({
				onChange : function(o){
					self.filterChanged.call(self, o);
				},
				onInit : function(){
					self.currentSort = this.get();
				}
			});
			
			self.pages.jPagesBox({
				onChange : function(n){
					self.pageChanged.call(self, n);
				}
			});
			
			if (self.options.loadOnInit) self.load();
		},
		
		filterChanged : function(o){
			this.currentSort = o;
			this.currentPage = 1;
			this.load();
		},
		
		pageChanged : function(n){
			this.currentPage = n;
			this.load();
		},
		
		getDataToPost : function(o){
			var res  = o || {};
			res['ajax']			= 1;
			this.form.find('input').each(function(){
				res[ $(this).attr('name') ] = $(this).val();
			});
			res['sort_column']	= this.currentSort.column;
			res['sort_dir']		= this.currentSort.dir;
			res['page']			= this.currentPage;
			return res;
		},
		
		load : function(){
			var self = this;
			var url = self.options.url || self.form.attr('action');
			var dataType = self.options.dataType || 'html';
			
			$.post(url, self.getDataToPost(), function(response){
				if (dataType == 'json'){
				} else {
					self.list.html(response);
					self.list.jAjaxDatalist(self.origOptions);
					if (typeof self.options.onReload == 'function')
						self.options.onReload.call(self.list);
				}
			},dataType);
		},
		
		loadData : function(){
		},
		
		foo : function(){
		}
		
	});
	
})(jQuery);


