/* HelloACY namespace */
HelloACY = Class.create({
	reloadCommentForm: function() {
		$('comments').insert({
			top: $('comment_form')
		});
		$('comment_form').down('input[name="parent"]').setValue('');
		$('comment_form').show();
	},
	
	moveCommentForm: function(container) {
		var id = parseInt(container.id.replace('comment-', ''));
		container.insert($('comment_form'));
		$('comment_form').down('input[name="parent"]').setValue(id);
		$('comment_form').show();
	},
	validateCommentFormInput: function() {
		var filled = true;
		$$('#comment_form input[type="text"], #comment_form textarea').each(function(el) {
			if (($F(el).strip().length == 0) || $F(el) == el.readAttribute('placeholder')) {
				filled = false;
			}
		});
		if (filled == true)
			$('comment_form').down('input[type="submit"]').enable();
		else
			$('comment_form').down('input[type="submit"]').disable();
	},
});

/* Global rules and actions */
document.observe("dom:loaded", function() {
	var helloacy = new HelloACY();
	
	$('rss').observe('click', function(event) {
		event.stop();
		$('rss-full').toggle();
	});
	
	$$('#comments a.reply').each(function(el) {
		el.observe('click', function(event) {
			event.stop();
			helloacy.moveCommentForm(this.up('li'));
		});
	});
	$$('#comments input, #comments textarea').each(function(el) {
		Form.Element.Methods.setDefaultValue(el);
	});
	$('toolbar-comment').observe('click', function(event) {
		event.stop();
		$('embed').hide();
		helloacy.reloadCommentForm();
	});
	$('toolbar-embed').observe('click', function(event) {
		event.stop();
		if ($$('#comments > #comment_form').length > 0) {
			$('comment_form').hide();
		}
		$('embed').toggle();
	});
	$('comment_form').down('input[type="submit"]').disable();
	$$('#comment_form input[type="text"], #comment_form textarea').each(function(el) {
		el.observe('keyup', helloacy.validateCommentFormInput);
		el.observe('keypress', function(event) {
			if (event.ctrlKey && event.keyCode == 13) {
				var submit = $('comment_form').down('input[type="submit"]');
				submit.enable();
				submit.click();
				submit.disable();
			}
		});
	});
	$('comics-fall').observe('change', function() {
		window.location = '/'+this.getValue()+'/';
	});

	$$('a').each(function(el) {
		var href = el.readAttribute('href');
		if ((href.split(/\/+/g)[1].replace('www.', '') != 'helloacy.ru') && ( href.substr(0, 1) != '/' ) && (href.substr(0, 1) != '#')) {
			pageTracker._trackPageview('/outgoing/'+href.split(/\/+/g)[1]);
		}
	});
});