// Filter Box
(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': ''});
			}).unbind('click').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(){
			if (!this.current) return {'column' : '', 'dir' : ''};
			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);

// Pages Box
(function($){
	
	$.fn.jPagesBox = function(o){
		return this.each(function(){
			$(this).data('jPagesBox', new $pb(this, o));
		});
	}
	
	var defaults = {
		onChange : null,
		onInit : null
	};
	
	jPagesBox = function(el, o){
		var self = this;
		
		self.options = $.extend({}, defaults, o || {});
		self.list = $(el);
		self.current = self.options.currentPage || 0;
		self.count = self.options.pagesCount || 1;
		self.limit = self.options.limit || 10;
		
		self.list.find('.js-pages-current').each(function(){
			var n = parseInt(this.tagName == 'INPUT' ? $(this).val() : $(this).text());
			if (n > 0) self.current = n;
		});
		if (!self.current){
			self.list.find('li.js-checked').each(function(){
				self.current = $(this).attr('value');
			});
		}
		if (!self.current) self.current = 1;
		
		self.list.find('.js-pages-count').each(function(){
			var n = parseInt(this.tagName == 'INPUT' ? $(this).val() : $(this).text());
			if (n > 0) self.count = n;
		});
		
		self.list.find('input.js-limit, select.js-limit').each(function(){
			self.limit = $(this).val();
		});
		
		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('input.js-limit, select.js-limit').change(function(){
				self.limit = $(this).val();
				self.go(1);
			});
			
			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);
			
			if (typeof self.options.onInit == 'function') self.options.onInit.call(self);
		},
		
		go : function(n){
			var self = this;
			if (!self.set(n)) return false;
			if (typeof self.options.onChange == 'function'){
				self.options.onChange.call(self, n, self.limit);
			}
		},
		
		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.list.find('ul.pages li').removeClass('js-checked');
			self.list.find('ul.pages li[value="'+n+'"]:first').addClass('js-checked');
			self.current = n;
			return true;
		},
		
		get : function(){
			return { "current" : this.current, "count" : this.count, "limit" : this.limit };
		},
		
		foo : function(){
			
		}
		
	});
	
})(jQuery);

// Search Box
(function($){
	
	$.fn.jSearchBox = function(o){
		return this.each(function(){
			$(this).data('jSearchBox', new $sb(this, o));
		});
	}
	
	var defaults = {
		onChange : null,
		onInit : null,
		onSubmit : null
	};
	
	jSearchBox = function(el, o){
		var self = this;
		
		self.options = $.extend({}, defaults, o || {});
		self.list = $(el);
		
		self.setup();
	}
	
	var $sb = jSearchBox;
	
	$sb.fn = $sb.prototype = {
		jSearchBox : 'version 0.1.0'
	};
	
	$sb.fn.extend = $sb.extend = $.extend;
	
	$sb.fn.extend({
		
		setup : function(){
			var self = this;
			
			self.list.find('input, textarea, select').change(function(){
				self.change(this);
			});
			self.list.find('.js-submit').click(function(){
				self.submit(this);
				return false;
			});
			
			if (typeof self.options.onInit == 'function') self.options.onInit.call(self);
		},
		
		change : function(el){
			var self = this;
			
			if (typeof self.options.onChange == 'function') self.options.onChange.call(self, el);
			if ($(el).hasClass('js-urgent')) self.submit(el);
		},
		
		submit : function(el){
			var self = this;
			if (typeof self.options.onSubmit == 'function') 
				self.options.onSubmit.call(self, el, self.get());
		},
		
		get : function(){
			return this.list.getFields();
		},
		
		foo : function(){
			
		}
		
	});
	
})(jQuery);

window.onHashChangeBinded = false;

// AjaxDataList
(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',
		loadingStyle : "default",
		url : null,
		urlData : null,
		onInit : null,
		onReload : null,
		getExtraData : null
	};
	
	jAjaxDatalist = function(el, o){
		var self = this;
		
		self.options = $.extend({}, defaults, o || {});
		self.origOptions = $.extend(o, {
			loadOnInit : false
		});
		self.list = $(el);
		
		self.setup();

		if (!window.onHashChangeBinded){
			window.onHashChangeBinded = true;
			$(window).bind('hashchange', function(e){
				if ($.bbq.pushingState){
					$.bbq.pushingState = false;
					return;
				}
				self.load(e.getState(), true);
			});
		}
	}
	
	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;
			
			self.form = self.list.find('form.js-data-form:first');
			self.ul = self.list.find('ul.js-data-list:first');
			
			self.filter		= self.list.find('.js-filter-box');
			self.pages		= self.list.find('.js-pages-box');
			self.search		= self.list.find('.js-search-box');
			
			self.currentSort = { column: 'id', dir: 'desc' };
			self.currentPage = 1;
			self.limit = 10;
			
			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, l){
					self.pageChanged.call(self, n, l);
				},
				onInit : function(){
					self.limit = this.get().limit;
				}
			});
			
			self.search.jSearchBox({
				onSubmit : function(el, data){
					self.load();
				}
			});
			
			if (self.options.loadOnInit) {
				var values = null;
				if (typeof self.options.loadOnInit == 'object'){
					values = self.options.loadOnInit;
				} else if (typeof self.options.loadOnInit == 'function'){
					values = self.options.loadOnInit();
				} else if (self.options.loadOnInit == 'navigation'){
					if (typeof $.param.fragment == 'function'){
						values = $.deparam($.param.fragment());
					} else {
						values = null;
					}
				}
				self.options.loadOnInit = false;
				self.load(values, true);
			}
			
			if (typeof self.options.onInit == 'function')
				self.options.onInit.call(self, self.list);
		},
		
		filterChanged : function(o){
			this.currentSort = o;
			this.currentPage = 1;
			this.load();
		},
		
		pageChanged : function(n, l){
			this.currentPage = n;
			if (l > 0) this.limit = l;
			this.load();
		},
		
		getDataToPost : function(o, navigate){
			if (!navigate) navigate = false;
			var res  = o || {}, self = this;
			res['ajax']			= 1;
			if (typeof self.options.getExtraData == 'function'){
				var o = self.options.getExtraData.call(self, self.list);
				if (typeof o == 'object') res = $.extend(res, o);
			}
			if (navigate) return res;
			self.form.find(':input').each(function(){
				res[ $(this).attr('name') ] = $(this).val();
			});
			if (self.search.size()){
				res = $.extend(self.search.data('jSearchBox').get(), res);
			}
			res['sort_column']	= self.currentSort.column;
			res['sort_dir']		= self.currentSort.dir;
			res['page']			= self.currentPage;
			res['limit']		= self.limit;
			return res;
		},
		
		load : function(values, navigate){
			if (!navigate) navigate = false;
			var self = this;
			var url = self.options.url || self.form.attr('action');
			var dataType = self.options.dataType || 'html';
			var $box = self.list.find('.loading-box');
			if (!$box.size()) $box = self.list;
			
			if (typeof $.fn.showLoading == 'function') {
				$box.showLoading({style : self.options.loadingStyle});
			}
			if (!values || typeof values == 'object' && isEmpty(values)) {
				values = self.getDataToPost({}, navigate);
			}
			$.post(url, values, function(response){
				if (typeof $.fn.hideLoading == 'function') $box.hideLoading();
				if (!navigate) self.setNav(url, values);
				if (dataType == 'json'){
				} else {
					self.list.html(response);
					self.setup();
					//self.list.jAjaxDatalist(self.origOptions);
					if (typeof self.options.onReload == 'function' 
						&& typeof self.options.onInit != 'function')
						self.options.onReload.call(self.list, values);
				}
			}, dataType);
		},
		
		loadData : function(){
		},
		
		setNav : function(url, values){
			if (typeof $.bbq.pushState != 'function') return false;
			$.bbq.pushingState = true;
			$.bbq.pushState(values);
		},
		
		foo : function(){
		}
		
	});
	
})(jQuery);


