(function($, undefined) {
	$(document).ready(function() {
		//auto fancybox on links to images
		try {
			$('a').each(function(){
				if ($(this).attr('href') != undefined) {
					if ($(this).attr('href').match(new RegExp(/\.(jpg|jpeg|png|gif)$/i))) {
						if (!$(this).attr('rel').length) {
							$(this).attr('rel', 'fancybox');
						}
						$(this).attr('title', $(this).find('img').first().attr('alt'));
						$(this).fancybox({'titlePosition': 'inside'});
					}
				}
			});
		} catch(e) {}
		
		
		//adjust menu to full width
		try {
			var initW = 0,
				offsetW = 0,
				adjustW = 0,
				remainderW = 0,
				$items = $('#menu-hovedmeny').children();

			if ($items.length) {
				$items.each(function() {
					initW += $(this).width();
				});
				offsetW = $('#menu-hovedmeny').parent().width() - initW;
				adjustW = Math.floor(offsetW / $items.length);
				remainderW = offsetW - adjustW * $items.length;
				$items.css({width: function(index, value) {
					return parseFloat(value) + adjustW + (index < remainderW ? 1 : 0);
				}});
			}
		} catch(e) {}
	});
})(jQuery);
