var Energiakademin = (function($) {
	// Public API.
	var that = {};

	that.initAddThis = function(options) {
		if (typeof addthis === 'undefined')	{
			return;
		}
		var settings = jQuery.extend({}, { username: '' }, options);
		addthis_config = { data_use_flash: false, 
						   ui_click: true, 
						   ui_language: 'sv', 
						   username: settings.username }; // Global variable :(, somehow it doesn't work if the object is passed as a parameter to `addthis.button()`.
	};
		
	that.showAddThis = function() {
		if (typeof addthis === 'undefined')	{
			return;
		}
		addthis.button($('#share a').get(0));
		$('#share').show();
	};

	// Misc. fixes to make certain browsers behave.
	var makeDOMFixes = function() {
		// Add class 'first-child' to all list-items (li elements) that are the first child of its list.
		$('li:first-child').addClass('first-child');
	};
	
	var initContent = function() {
		$('#content:has(#aside .relatedinfo)').addClass('relatedinfo');
		$('#aside .relatedinfo').css('background-image', 'none');
	};
	
	var initPrintButton = function(options) {
		$('#print').wrapInner('<a href="?print">').show().click(function()	{
			window.print();
			return false;
		});
		
		if (window.location.search.substring() === '?print') {
			// Display print dialog if the link created above was actually followed
			window.print();
		}
	};

	// Initializes the sub menu, sets a slide up/down animation for items with class 'disabled'.
	var initSubnav = function() {
		var slideToggle = function(elem) {
			elem.parent().children('ul').slideToggle(200);
		}
		$('#subnav li.disabled:not(.selected):has(ul) span.disabled').click(function() {
			var $this = $(this);
			var $disabledUnselectedSiblings = $this.parent().siblings('li.disabled:not(.selected):has( > ul)').find(' > ul');
			if ($disabledUnselectedSiblings.length) {
				$disabledUnselectedSiblings.slideUp(100);
				slideToggle($this);				
			} else {
				slideToggle($this);
			}
		});
	};
	
	// Makes clickable links out of items.
	that.createLink = function(options) {
		$(options.target).each(function() {
			var $this = $(this);
			var $link = $this.find(options.source);
			if ($link.length) {
				if (options.addTargetClass) {
					$this.addClass(options.addTargetClass);
				}
				$this.click(function() {
					document.location = $link.attr('href');
					return false;
				});
			}
		});
	};
	
	that.initStartPage = function() {
		Energiakademin.createLink({ addTargetClass: 'link', 
									source: 'a:first', 
									target: '#forumlist ul.posts > li, #newslist ul.news > li' });
	};
	
	that.initListPage = function() {
		Energiakademin.createLink({ addTargetClass: 'link',
									source: 'a:first',
									target: 'ul.pages > li' });
	};

	that.initFAQItemPage = function() {
		$('#faqs .pagination a').attr('href', function() {
			return this.href + '#faqs';
		});
	};
	
	that.initProjectPage = function() {
		$('#projects .pagination a').attr('href', function() {
			return this.href + '#projects';
		});
	};	

	that.initProcedurePage = function(options) {
		var settings = jQuery.extend({}, { moreElementSelector: '#more' }, options);
		var $readmore = $('a#readmore');
		var $more = $(settings.moreElementSelector);

		if ($readmore.length) {
			$readmore.click(function() {
				$readmore.hide();
				$more.slideToggle(200);
				return false;
			});
		} else {
			$more.show();
		}				
	};

	// Constructor.
	(function() {
		$(document).ready(function() {
			makeDOMFixes();
			initContent();
			initSubnav();
			initPrintButton();
		});
	})();

	return that;
	
})(jQuery);
