var MemoEvent = new Class( {
	Implements : [ Options, Events ],
	options : {},
	initialize : function(options) {
		options.link.addEvent('click', this.sendEvent.bind(this));
	},
	
	loader: null,

	sendEvent : function(e) {
		e.stop();
		var link = $(e.target);
		while (!link.match('a'))
			link = link.getParent();
		if (!link.hasClass('event-done')) {
			var parent = link.getParent('span.parent-event');
			var message = parent.getElement('span.hidden').get('text');
			parent.empty();
			if (parent.hasClass('pipe'))
				message = '| ' + message;
			parent.set('text', message);
			this.loader = this.loading.periodical(500, parent);
			var request = new Request( {
				url : link.get('href'),
				method : 'post',
				onSuccess : function(data) {
					this.processData(parent, data);
				}.bind(this)
			});
			request.send();
		}
	},
	
	loading: function() {
		var text = this.get('text');
		text = text.contains('...') ? text.replace('...', '') : text + '.';
		this.set('text', text);
	},

	processData : function(parent, data) {
		this.loader = $clear(this.loader);
		var text = JSON.decode(data).text;
		var valid = JSON.decode(data).valid;
		if (parent.hasClass('pipe'))
			text = '| ' + text;
		parent.set('text', text);
		if (valid) {
			parent.addClass('event-done');
			if (parent.getParent().match('li'))
				parent = parent.getParent();
			var dd = parent.getParent('dd');
			if ($defined(dd)) {
				dd.removeClass('event-action');
				var links = [];
				dd.getChildren('a').each(function(link) {
					links.include(link);
				});
				dd.getLast().getChildren('a').each(function(link) {
					links.include(link);
				});
				links.each(function(link) {
					if (link.get('href').contains('tovalidation'))
						link.set('href', link.get('href').slice(0, link.get('href').lastIndexOf('/')));
				});
			}
			this.deleteElem.delay(2000, parent);
		} else {
			parent.addClass('event-fail');
		}
	},

	deleteElem : function() {
		var span = $('nbEventsToValid');
		if (span != null && !this.match('li')) {
			var nbEvents = span.get('text').toInt();
			nbEvents = nbEvents - 1;
			if (nbEvents == 0)
				span.getParent('p').destroy();
			else
				span.set('text', nbEvents);
		}
		this.destroy();
	}
});

var PageEvent = new Class( {
	implements : [ Events ],

	page : 1,
	toValid : false,
	nexts : null,
	previouz : null,
	div : null,
	allBtn : null,
	validBtn : null,
	xhr : null,

	initialize : function(tovalid) {
		this.nexts = $$('.next');
		this.previouz = $$('.previous');
		this.div = $('events-div');
		this.allBtn = $('events-all');
		this.validBtn = $('events-tovalid');
		
		this.clean();
		
		if($defined(tovalid)) {
			tovalid ? this.getToValid() : this.getAll();
		} else {
			this.manageBtns();
		}
	},

	getToValid : function(e) {
		this.request(1, true);
	},

	getAll : function(e) {
		this.request(1, false);
	},

	getNext : function(e) {
		this.request(this.page + 1, this.toValid);
	},

	getPrevious : function(e) {
		this.request(this.page - 1, this.toValid);
	},

	loader : function() {
		this.div.empty();
		var img = new Element('img').set('src', '/images/misc/loader-event.gif').addClass('loader-event');
		img.injectInside(this.div);
	},

	request : function(page, toValid) {
		if (this.xhr == null) {
			this.xhr = new Request( {
				url : '/dashboard?ajax',
				method : 'get',
				onRequest : function() {
					this.loader();
					this.clean();
				}.bind(this),
				onSuccess : function(data) {
					try {
						window.location.href = JSON.decode(data).url;
					} catch (e) {
						this.processData(data, page, toValid);
					}
				}.bind(this)
			}).send('page=' + page + '&toValid=' + toValid + '&nocache=' + Math.random());
		}
	},

	processData : function(data, page, toValid) {
		this.page = page;
		this.toValid = toValid;

		this.div.set('html', data);

		var links = this.div.getElements('a.event');
		links.each(function(item) {
			new MemoEvent( {
				link : item
			});
		});
		
		this.manageBtns();
		
		this.xhr = null;
	},
	
	// remove events on all buttons and disable them
	clean: function() {
		// clean all events
		this.allBtn.removeEvents();
		this.validBtn.removeEvents();
		this.nexts.removeEvents();
		this.previouz.removeEvents();
		
		// disable all btns
		this.disableBtn(this.allBtn);
		this.disableBtn(this.validBtn);
		this.nexts.each(this.disableBtn);
		this.previouz.each(this.disableBtn);
	},
	
	// add events on buttons and enable them
	manageBtns: function() {
		if(this.hasNext()) {
			this.nexts.addEvent('click', this.getNext.bind(this));
			this.nexts.each(this.enableBtn);
		}
		if(this.hasPrevious()) {
			this.previouz.addEvent('click', this.getPrevious.bind(this));
			this.previouz.each(this.enableBtn);
		}
		if(this.toValid) {
			this.enableBtn(this.allBtn);
			this.allBtn.addEvent('click', this.getAll.bind(this));
		} else if(this.hasEventToValid()) {
			this.enableBtn(this.validBtn);
			this.validBtn.addEvent('click', this.getToValid.bind(this));
			$('nbEventsToValid').getParent().addEvent('click', this.getToValid.bind(this));
		} else {
			this.enableBtn(this.allBtn);
		}
		this.switchOnOff();
	},

	// enable button by adding class 'disable' and 'off', and removing class 'on'
	disableBtn: function(btn) {
		if(!btn.hasClass('disable')) {
			btn.addClass('disable');
		}
	},
	
	// enable button by removing class 'disable' and 'off', and adding class 'on'
	enableBtn: function(btn) {
		btn.removeClass('disable');
	},
	
	switchOnOff: function() {
		this.validBtn.removeClass(this.toValid ? 'off' : 'on');
		this.validBtn.addClass(this.toValid ? 'on' : 'off');
		this.allBtn.removeClass(this.toValid ? 'on' : 'off');
		this.allBtn.addClass(this.toValid ? 'off' : 'on');
		if(this.toValid || this.hasEventToValid()) {
			this.validBtn.removeClass('disable');
			this.allBtn.removeClass('disable');
		} else {
			this.allBtn.removeClass('disable');
		}
	},

	// check if there is previous events
	hasPrevious : function() {
		return this.page > 1;
	},

	// check if there is next events
	hasNext : function() {
		return $('hasNext') != null && $('hasNext').get('text') == 'true';
	},

	// check if there is events to valid
	hasEventToValid : function() {
		return $defined($('nbEventsToValid'));
	}
});

var MemoryEvent = new Class( {
	implements : [ Events ],

	page : 1,
	next : null,
	previous : null,
	div : null,
	idMemory : null,
	xhr : null,

	initialize : function() {
		this.next = $('next');
		this.previous = $('previous');
		this.div = $('div-events');
		this.idMemory = $('idMemory').get('text');

		if (this.hasNext()) {
			this.next.removeClass("hidden");
			this.next.addEvent('click', this.getNext.bind(this));
		}
	},

	getNext : function(e) {
		e.stop();
		this.next.removeEvents('click');
		this.request(this.page + 1);
	},

	getPrevious : function(e) {
		e.stop();
		this.previous.removeEvents('click');
		this.request(this.page - 1);
	},

	loader : function() {
		this.div.empty();
		var img = new Element('img').set('src', '/images/misc/loader-event.gif').addClass('loader-event');
		;
		img.injectInside(this.div);
	},

	request : function(page) {
		if (this.xhr == null) {
			this.xhr = new Request( {
				url : '/ajax/memory/1/events',
				method : 'get',
				onRequest : function() {
					this.loader();
				}.bind(this),
				onSuccess : function(data) {
					this.page = page;
					this.processData(data);
					this.xhr = null;
				}.bind(this)
			}).send('page=' + page + '&idMemory=' + this.idMemory + '&nocache=' + Math.random());
		}
	},

	processData : function(data) {
		this.div.set('html', data);

		if (this.hasPrevious()) {
			this.previous.removeClass('hidden');
			this.previous.addEvent('click', this.getPrevious.bind(this));
		} else {
			this.previous.addClass('hidden');
			this.previous.removeEvents('click');
		}
		if (!this.hasNext()) {
			this.next.addClass('hidden');
			this.next.removeEvents('click');
		} else {
			this.next.removeClass('hidden');
			this.next.addEvent('click', this.getNext.bind(this));
		}
	},

	hasPrevious : function() {
		return this.page > 1;
	},

	hasNext : function() {
		return $defined($('hasNext')) && $('hasNext').get('text') == 'true';
	}
});