window.addEvent('load', function() {
	$$('.videoCont').each(function(el) {
		el.getElement('span').set('styles', {
			height: el.getSize().y,
			width: el.getSize().x
		});
	});
	$$('.monthBase').middle('img');
});

var POM = new Class({
	Implements: [Options],
	options: {},

	/* initialize needed methods
	 ======================================================================= */
	initialize: function(options) {
		this.setOptions(options);
		this.pomNavEl = $('pom-nav');
		this.commentForm = $('pom-add-comment')
		this.overlay = $$('#pom-add-comment .overlay')
		this.start();
	},

	/* attach events and add the initially fired methods
	 ======================================================================= */
	start: function() {	
		if($chk(this.pomNavEl)) this.pomNav();
		this.preloadImage();
		this.addComment();		
	},
	
	pomNav: function() {
		if($chk(this.pomNavEl.getElement('span'))) this.pomNavEl.getElements('span').fade(0.3);
	},
	
	preloadImage: function() {
		if(!$('pom-main-image')) return false;
		
		this.portraitPicture = $$('#pom-main-image img')[0];
		this.portraitPictureSrc = this.portraitPicture.getProperty('src');
		this.portraitPicture.set('styles', {'opacity': 0, 'visibility': 'visible'});
		
		new Asset.image(this.portraitPictureSrc, {
			onload: function() {
				this.portraitPicture.fade('in');
			}.bind(this)
		});
	},
	
	validateForm: function() {
		var commentInput 	= this.commentForm.getElement('#comment');
		var tacInput 		= this.commentForm.getElement('#tac');
		var submitInput 	= this.commentForm.getElement('.submitAlt input');
		
		submitInput.set('disabled', true);
		this.overlay.fade('hide');
		
		commentInput.addEvent('keyup', function(e) {
			if(this.get('value').length > 0 && tacInput.checked) {
				submitInput.set('disabled', false);
			} else if(this.get('value').length > 0 && !tacInput.checked) {
				submitInput.set('disabled', true);
			} else {
				submitInput.set('disabled', true);
			}
		});
		
		tacInput.addEvent('click', function(e) {
			if(commentInput.get('value').length > 0 && this.checked) {
				submitInput.set('disabled', false);
			} else if(!this.checked) {
				submitInput.set('disabled', true);
			} else {
				submitInput.set('disabled', true);
			}
		});
	},
	
	addComment: function() {
		if(!this.commentForm) return false;

		this.validateForm();
		
		this.commentForm.addEvent('submit', function(e) {
			e.stop();
			
			new Request.HTML({
				url: this.options.baseUrl+'collection/insert_portrait_of_the_month_message',
				method: 'post',
				onRequest: function() {
					this.overlay.set('class', 'overlay adding');
					this.overlay.fade(0.7);
				}.bind(this),
				onSuccess: function() {
					this.overlay.set('class', 'overlay added');
					this.commentForm.getElements('p input, p textarea').set('value', '');
					this.commentForm.getElement('.submitAlt input').set('disabled', true);
					(function() {
						this.overlay.fade('out');
					}).delay(2000, this);
				}.bind(this)
			}).send(this.commentForm);
		}.bind(this));
	}
});
