(function($){
	EYE.blogArchive = function() {
		var click = function(ev) {
			var trg = $(ev.target).closest('a.collapsable');
			if (trg.size() == 1) {
				trg.blur().parent().toggleClass('collapsed');
				return false;
			}
		};
		return {
			init: function() {
				$('#blogArchive ul:first').bind('click', click);
			}
		};
	}();
	EYE.register(EYE.blogArchive.init, 'init');

	EYE.formMessage = function() {
		var click = function() {
			$(this).css('display', 'none');
		};
		return {
			init: function(){
				$('form div.formMessage').each(function(){
					$(this).show().css({
						left: (this.offsetParent.offsetWidth - this.offsetWidth)/2,
						top: (this.offsetParent.offsetHeight - this.offsetHeight)/2
					});
				}).bind('click', click);
			}
		};
	}();

	EYE.register(EYE.formMessage.init, 'init');

	EYE.letterCount = function() {
		var max = 200;
		var setCount = function() {
			var current = max - $('#commentComment').val().length;
			$('#lettersCount').text(current).addClass(function() {
				return current >= 0 ? false : 'overCount';
			}).removeClass(function() {
				return current < 0 ? false : 'overCount';
			});
		}
		return {
			init: function() {
				if (document.getElementById('lettersCount')) {
					setCount();
					$('#commentComment')
						.bind('change', setCount)
						.bind('keyup', setCount);
				}
			}
		};
	}();
	EYE.register(EYE.letterCount.init, 'init');

	EYE.register(function(){
		$("a[href^='http://']").bind('click keypress', function(ev){
			var code=ev.charCode || ev.keyCode;
			if(!code || (code && code == 13)) {
				if(pageTracker){
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;
					pageTracker._trackPageview(fixedLink);
				};
			};
		});
		$('form').bind('submit', function(){
			if(pageTracker){
				pageTracker._trackEvent(
					this.id,
					$('button', this).text(),
					$('h3', this).text()
				);
			}
		});
	}, 'init');
})(jQuery);