var Hemmavinsten = (function($) {
	// Public API.
	var that = {};

	// Misc. fixes to make certain browsers behave.
	var doDOMFixes = 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');
		// Add class 'last-child' to the last list-item in the #nav.
		$('#nav ul li:last').addClass('last-child');		
		// Add class 'first-child' to all h1 elements that are the first child of its parent element.
		$('h1:first-child').addClass('first-child');
	};

	// Make ads open in a new tab/window.
	var initAds = function() {
		$('#ads a').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	};
	
	var initFieldsetDefaultSubmit = function() {	
		$('#login,#content').fieldsetsubmit();
	};
	
	var doIEFixes = function() {
		if (jQuery.browser.msie) {
			$('.box3').wrapInner('<div class="b1"><div class="b2"/></div>');
			$('.box1').each(function() {
				var c1 = $(document.createElement('div'));
				var c2 = $(document.createElement('div'));
				var c3 = $(document.createElement('div'));
				var c4 = $(document.createElement('div'));
				c1.attr('class', 'c1');
				c2.attr('class', 'c2');
				c3.attr('class', 'c3');
				c4.attr('class', 'c4');
				$(this).append(c1).append(c2).append(c3).append(c4);
			});

			if (parseInt(jQuery.browser.version) < 7) {
				DD_belatedPNG.fix('#home img, #header, #body, #footer, #nav, #nav ul, #nav li');
				$('ul.plain>li,ol.plain>li').addClass('plain');
			}
		}
	};

	// 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;
				});
			}
		});
	};

	// Constructor.
	(function() {
		$(document).ready(function() {
			doDOMFixes();
			initAds();
			doIEFixes();
			initFieldsetDefaultSubmit();
		});
	})();

	return that;

})(jQuery);
