var Slogan = {
	init: function () {
		if ($('other-slogan'))
			$('other-slogan').addEvent('click', Slogan.getRandom);

		if ($('own-slogan'))
			$('own-slogan').addEvent('click', Slogan.showForm);

		if ($('slogan') && $('slogan').getFirst('form'))
			$('slogan').getFirst('form').addClass('hide');

		if ($('send-slogan'))
			$('send-slogan').addEvent('click', Slogan.send);

		if ($('cancel-slogan'))
			$('cancel-slogan').addEvent('click', Slogan.hideForm);
	},

	getRandom: function (e) {
		var ajaxRequest = new Request({
			url: '/slogan/random/',
			method: 'get',
			onComplete: Slogan.showRandom,
			onStart: Slogan.showLoading
		});
		ajaxRequest.send();
		
		e.stop();
		return false;
	},

	showLoading: function () {
		$('slogan').addClass('loading');
	},

	showRandom: function (responseText, responseXML) {
		$('slogantext').set('html', responseText);
		$('slogan').removeClass('loading');
	},

	showForm: function (e) {
		$('slogantext').addClass('hide');
		$('slogantext').getNext().addClass('hide');
		$('slogantext').getNext('form').removeClass('hide');

		$('new-slogan').set('value', 'Driebit,');

		if (e) e.stop();
		return false;
	},

	hideForm: function (e) {
		$('slogantext').removeClass('hide');
		$('slogantext').getNext().removeClass('hide');
		$('slogantext').getNext('form').addClass('hide');
		
		if (e) e.stop();
		return false;
	},

	send: function (e) {
		if ($('new-slogan').get('value') == '')
			return;

		var ajaxRequest = new Request({
			url: '/slogan/add/',
			method: 'post',
			onSuccess: Slogan.sendSuccess,
			onFailure: Slogan.sendFailure
		});
		ajaxRequest.send('slogan=' + $('new-slogan').get('value') + '&name=Driebit');

		e.stop();
		return false;
	},

	sendSuccess: function () {
		$('slogantext').set('html', '<q>' + $('new-slogan').get('value') + '</q>');
		Slogan.hideForm();
	},

	sendFailure: function () {
		$('slogantext').set('html', '<p>Je slogan is niet opgeslagen :(</p>');
	}
}

window.addEvent('domready', Slogan.init);