if (typeof MooTools == 'object') {

	/**
	 * Replaces the tt_news back link with history.back() and changes the according link text
	 */
	function replaceBackLink(){
		$$('.back a').each(function(el) { el.setProperty('href', 'javascript:history.back();'); });
	}
	
	function bodyJs() {
		$$('body').addClass('js');
	}

	/**
	 * Adds the google analytics tracking code to all download links
	 */
	function addGaDownloadTracking() {
		$$('.tx_ttnews_downloads-filelink a').each(function(el){
			el.addEvent('click', function(){
				pageTracker._trackPageview('/download_files/' + escape(el.getText()));
			});
		});
	}
	
	/**
	 * Adds the google analytics tracking code to all outgoing links
	 */
	function addExternalPageTracking(){
		$$('a').each(function(el){
			var href = el.getProperty('href');
			
			if (href && (href.match(/^http/)) && (!href.match(document.domain)) && (!href.match(/http:\/\/www.health-and-beauty.com\/(ads|phpmv2)\//))) {
				el.addEvent('click', function(){
					href = escape(href.replace('http://', ''));
					pageTracker._trackPageview('/external/' + href);
				});
			}
		});
	}
	
	/**
	 * Searches for the registrationforms or the thankyou page and then tracks the state of the
	 * registration process in google analytics
	 */
	function trackRegistrationProcess() {
		if ($$('.registrationform').length > 0) {
			pageTracker._trackPageview('/registration_start/' + document.title);
		} else if ($$('.previewform').length > 0) {
			pageTracker._trackPageview('/registration_preview/' + document.title);
		} else if ($defined($('thankyou'))) {
			pageTracker._trackPageview('/registration_thankyou/' + document.title);
		}
	}
	
	/**
	 * Tracks clicks on each lightbox image with google analytics
	 */
	function trackLightboxClick(){
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightbox/i)) {
				var href = el.href;
				href = escape(href.replace('http://' + document.domain + '/', ''));
				el.addEvent('click', function(){
					pageTracker._trackPageview('/lightbox/' + href);
				});
			}
		});
	}
	
	window.addEvent('domready', function(){
		try {
			replaceBackLink();
			bodyJs();
			if (typeof pageTracker != 'undefined') {
				trackLightboxClick();
				trackRegistrationProcess();
				addExternalPageTracking();
				addGaDownloadTracking();
			}
		} catch (e) {}
	});
}