var Form = new Class
({
	__handleFieldBlur: function(event)
	{
		this.__hideError(event.target);
		this.__showPrompt(event.target);
	},
	
	__handleFieldFocus: function(event)
	{
		this.__hideError(event.target);
		this.__hidePrompt(event.target);
	},
	
	__showPrompt: function(field)
	{
		var prompt = field.get('prompt');

		if (this.__getValue(field) != '')
			return;

		field.addClass('prompt');
		field.set('value', prompt);
	},
	
	__hidePrompt: function(field)
	{
		if (this.__getValue(field) != '')
			return;

		field.removeClass('prompt');
		field.set('value', '');
	},
	
	__showError: function(field, message)
	{
		field.addClass('error');
		field.getNext('.message').set('html', '* ' + message);
	},
	
	__hideError: function(field)
	{
		field.removeClass('error');
		field.getNext('.message').set('html', '');
	},
	
	__getValue: function(field)
	{
		return field.get('value').toString() != field.get('prompt').toString() ? field.get('value') : '';
	}
});

var AddTopicForm = new Class
({
	Extends: Form,
	
	__form: null,
	__title: null,
	__content: null,
	__titleMessage: null,
	__contentMessage: null,
	__cancel: null,
	__submit: null,
	
	initialize: function()
	{
		try
		{
			this.__form = $$('form.addTopic');
			this.__title = this.__form.getElement('#topicTitle');
			this.__content = this.__form.getElement('#topicContent');
			this.__submit = this.__form.getElement('input.submit');
			this.__cancel = this.__form.getElement('input.cancel');
			
			this.__title.set('prompt', 'Wpisz temat...');
			this.__content.set('prompt', 'Zacznij wpisywać treść postu...');
		}
		catch (xcpt)
		{
			return false;
		}
		
		this.assignEvents();

		this.__showPrompt(this.__title);
		this.__showPrompt(this.__content);
	},
	
	assignEvents: function()
	{
		this.__form.addEvent('submit', this.verify.bind(this));
		this.__cancel.addEvent('click', this.cancel.bind(this));

		this.__title.addEvent('blur', this.__handleFieldBlur.bind(this));
		this.__title.addEvent('focus', this.__handleFieldFocus.bind(this));
		this.__content.addEvent('blur', this.__handleFieldBlur.bind(this));
		this.__content.addEvent('focus', this.__handleFieldFocus.bind(this));
	
	},
	
	verify: function()
	{
		var valid = true;
		
		if (!this.__getValue(this.__title))
		{
			valid = false;
			this.__showError(this.__title, 'pole wymagane');
		}
		
		if (!this.__getValue(this.__content))
		{
			valid = false;
			this.__showError(this.__content, 'pole wymagane');
		}

		return valid;
	},
	
	cancel: function()
	{
		document.location.href = '/forum/';
	}
});

var AddPostForm = new Class
({
	Extends: Form,

	__form: null,
	__content: null,
	__contentMessage: null,
	__cancel: null,
	__submit: null,
	__actuators: null,
	
	initialize: function()
	{
		try
		{
			this.__form = $$('form.addPost');
			this.__content = this.__form.getElement('#postContent');
			this.__submit = this.__form.getElement('input.submit');
			this.__cancel = this.__form.getElement('input.cancel');
			
			this.__actuators = $$('div.addPost a');
			
			this.__content.set('prompt', 'Zacznij wpisywać treść odpowiedzi na temat...');
		}
		catch (xcpt)
		{
			return false;
		}
		
		this.assignEvents();

		this.__showPrompt(this.__content);
	},
	
	assignEvents: function()
	{
		this.__form.addEvent('submit', this.verify.bind(this));
		this.__cancel.addEvent('click', this.cancel.bind(this));

		this.__content.addEvent('blur', this.__handleFieldBlur.bind(this));
		this.__content.addEvent('focus', this.__handleFieldFocus.bind(this));
		
		this.__actuators.addEvent('click', this.__handleActivation.bind(this));
	},
	
	verify: function()
	{
		var valid = true;
		
		if (!this.__getValue(this.__content))
		{
			valid = false;
			this.__showError(this.__content, 'pole wymagane');
		}

		return valid;
	},
	
	cancel: function()
	{
		this.__form.addClass('hide');
		this.__actuators.removeClass('hide');
	},
	
	__handleActivation: function(event)
	{
		this.__form.removeClass('hide');
		this.__actuators.addClass('hide');
		
		event.stop();
	}
});

window.addEvent
(
	'domready',
	function()
	{
		var addTopicForm = new AddTopicForm();
		var addPostForm = new AddPostForm();
	}
)