

var Classes = {}

Classes.EventsScroller = Class.create({
	
	init: function(args) {
		var self = this;
		
		var defaults = {
			'idButton': null,
			'url': null,
			'urlparams': {},
			'imageLoading': null,
			'limit': 10,
			'offset': 0
		};
		
		self.args = $.extend(true, defaults, args);
		
		if ((self.args['url'] == null) || (self.args['idButton']) == null) {
			return;
		}
		
		self.bind();
	},
	
	bind: function() {
		var self = this;
		
		var button = $('#' + self.args['idButton']);
		if (button) {
			button.bind('click', {'self':self}, self.onButtonClick);
		}
		
		page_bind_events(GLOBAL_PAGE_VARS);
	},
	
	onButtonClick: function(event) {
		var self = event.data.self;
		$(this).replaceWith('<div style="width:100%;text-align:center;" id="'+self.args['idButton']+'"><img src="'+self.args['imageLoading']+'" style="margin:10px auto;" /></div>');
		
		
		self.args['offset'] = self.args['offset'] + self.args['limit'];

		var urlparams = $.extend(true, {}, self.args['urlparams']);
		urlparams['offset'] = self.args['offset'];
		urlparams['limit'] = self.args['limit'];
		
		$.get(self.args['url'], urlparams, function(data) {
			$('#' + self.args['idButton']).replaceWith(data);
			self.bind();
		});
	}
	
});
