var OverlayVideo = new Class({
	Implements: [Options, Events, Log],
	Extends: Overlay,
	
	options: {
		eventId: null
	},
	
	// Constants
	AUTO_HIDE_DELAY: 5000,

	// Variables
	form: null,
	explanationMessage: null,
	
	initialize: function(options)
	{
		this.enableLog();
		this.log("OverlayVideo::initialize()");
		
		this.setOptions(options);
		
		this.parent('video', options);
	},

	reset: function()
	{
		this.log("OverlayVideo::reset()");

		// Invoke parent
		this.parent();
		
		// Reset form
		this.form.reset();

		// Success message
		this.successMessage.fx.set(0);
		this.successMessage.hide();
	},

	submit: function()
	{
		this.log("OverlayVideo::submit()");
	},
	
	initilizeWindow: function()
	{
		this.log("OverlayVideo::initilizeWindow()");
		
		// Invoke parent
		this.parent();

		// Window
		this.window = this.element.getElement('.window');

		// Explanation message
		this.explanationMessage = this.element.getElement('.explanation .message');

		// Form
		this.initForm();
	},


	show: function(songId)
	{
		this.log("OverlayLogin::show(" + songId + ")");

		if(!songId)
		{
			return;
		}

		this.form.addValue('event_id', this.options.eventId);
		this.form.addValue('song_id', songId);

		this.parent();
	},

	/*
	 * Init form
	 */
	initForm: function()
	{
		this.log("OverlayVideo::initForm()");

		// Success message
		this.successMessage = this.element.getElement('.success');
		this.successMessage.fx = new Fx.Tween(this.successMessage, {
			duration: 500,
			transition: Fx.Transitions.Quad.easeOut,
			property: 'opacity',
			link: 'cancel'
		});
		this.successMessage.fx.addEvent('start', function() {
			if(this.successMessage.getStyle('opacity').toInt() == 0)
			{
				this.successMessage.show();
			}
		}.bind(this));
		this.successMessage.fx.addEvent('complete', function() {
			if(this.successMessage.getStyle('opacity').toInt() == 0)
			{
				this.successMessage.hide();
			}
		}.bind(this));
		this.successMessage.fx.set(0);

		var requestUrl = this.application.getConfig().requestPath.afterglow.setlist.addVideo;

		var handlers = {
			onRequest: function()
			{
				this.log("onRequest");

			}.bind(this),
			onSuccess: function(response)
			{
				if(response.success)
				{
					this.log("succes!");

					this.close.delay(this.AUTO_HIDE_DELAY, this);

					// Success message
					this.successMessage.fx.start(1);

					this.fireEvent('addVideo', [response]);
				}
				else
				{
					this.log("error!");

					// Error message
					this.errorMessage.fx.start(1);
				}
			}.bind(this),
			onFailure: function(xhr)
			{
				this.log("onFailure");

				this.form.formErrorMessage.clear();
				this.form.formErrorMessage.addMessage('video', 0, "Verzenden mislukt");
				this.form.formErrorMessage.show();
			}.bind(this)
		};

		var options = {
			showLoader: true,
			allowStatus: true,
			formErrorMessage: {
				allow: true,
				container: this.element.getElement('.form-message-container'),
				allowClose: false
			}
		};

		var elementOptions = {
			hint: 'Plak hier de YouTube link'
		};

		var form = new JsonForm($('video-form'), requestUrl, handlers, options);

		var youtubeElement = new FormElement(form, 'video_url', elementOptions);
		youtubeElement.addValidation(Form.REGEX_YOUTUBE_VIDEO, 'Vul de link naar een YouTube video als volgt in:\nhttp://www.youtube.com/watch?v=qDK44rERAXI');
		form.addElement(youtubeElement);

		// Add buttons
		var submitButton = new FormSubmitButton(form, '.button.submit', {showLoader: true});
		form.addButton(submitButton);
		
		// Cancel button
		this.element.getElement('.cancel').addEvent('click', function(event){
			event.preventDefault();
			this.close();
		}.bind(this));

		// Save form
		this.form = form;
	}
	
});
