/**
* @requires jQuery
* Return or set the minimum height of an element
*/
jQuery.fn.minHeight = function(size) {
	// Explorer versions prior to IE7 needs to have height instead of min-height
	var type = (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)  ? "height" : "min-height";
	if (size == undefined) {
		// Get min-height for the first element
		return this.css(type);
	} else {
		// Set the min-height on all elements (default to pixels if value is unitless)
		this.css(type, size.toString().match(/^\d+$/) ? size + "px" : size);
		return this;
	}
};

/**
* @requires jQuery
* Justify columns
*/
jQuery.fn.justifyTeaserGroup = function() {
	jQuery(this).each(function(){
		var maxHeight = 0;
		// Get the height of the highest element
		jQuery(".m-c", this).each(function(){
			var height;
			jQuery(this).minHeight(0);
			if ($(this).siblings('.m-image').length){
				height = jQuery(this).outerHeight() + jQuery(this).siblings(".m-h").outerHeight() + $(this).siblings('.m-image').outerHeight();
			} else {
				height = jQuery(this).outerHeight() + jQuery(this).siblings(".m-h").outerHeight();
			}
			if (height > maxHeight) {
				maxHeight = height;
			}
		});
		// Set min-height for all elements
		jQuery(".m-c", this).each(function(){
			if ($(this).siblings('.m-image').length){
				jQuery(this).minHeight(maxHeight - jQuery(this).siblings(".m-h").outerHeight() - $(this).siblings('.m-image').outerHeight());
			} else {
				jQuery(this).minHeight(maxHeight - jQuery(this).siblings(".m-h").outerHeight());
			}
		});
	});
	return this;
};

/**
* @requires jQuery
* Add article functions, e.g., 'Print' or 'Tell a friend'.
*/
document.articleFunctions = function() {
	var ul;
	function getUl() {
		if (!ul) {
			var funcs = $('#content-primary');
			if (funcs.length) {
				ul = $('<ul class=\"article-functions\"></ul>').appendTo(funcs);
			}
		}
		return ul;
	}
	/**
	* Add an item
	* @param  options  Object  An object with options
	*/
	function addItem(options) {
		var li, a;
		options = jQuery.extend({
			// Link text
			text: '',
			// Link (or li) class
			className: '',
			// Link (or li) id
			id: '',
			// Callback to execute on click
			callback: null,
			// An element to append the link to, bypassing the regular ul
			insertInto: null
		}, options || {});

		if (getUl() || options.insertInto) {
			// Create link
			a = $('<a href="#"></a>');
			a.text(options.text);
			a.click(options.callback);
			if (!options.insertInto) {
				// If no insertInto-element is specified,
				// append the link to an li and add it to the ul
				li = $('<li>');
				// Add class and id to the li
				li.attr({
					'class': options.className,
					'id': options.id
				});
				a.appendTo(li);
				li.appendTo(ul);
			} else {
				// Add class and id to the link
				a.attr({
					'class': options.className,
					'id': options.id
				});
				a.appendTo($(options.insertInto));
			}
		}
	}
	return {
		addItem: addItem,
		setupShare: setupShare,
		displayShare: displayShare
	};

	function setupShare() {
		
			var doYouFeelgood = ($('.feelgood').length) ? true : false;
			
			// Hide the share
			var shareObject = {};
			shareObject.wrapper = $('#share-wrapper');
			shareObject.mail = shareObject.wrapper.children('.emailPage');
			shareObject.mailTrigger = shareObject.wrapper.find('a.mail-trigger');
			shareObject.trigger = $('#article-links').children('.share');
	
			if (location.hash != "#emailpage") {
				if (!doYouFeelgood) {
					shareObject.wrapper.hide();
				}
				shareObject.mail.hide();
			}
	
			// Add a click event
			if (!doYouFeelgood) {
				shareObject.trigger.click(function(e) {
					e.preventDefault();
					displayShare(shareObject.wrapper);
				});
			}
	
			shareObject.mailTrigger.click(function(e) {
				e.preventDefault();
				displayShare(shareObject.mail);
			});

	}

	function displayShare(el) {
			
			// Toggle the share
			el.slideToggle();

	}

}();

MarkItUp = {
bbCodeSettings: {
	previewParserPath: '', // path to your BBCode parser
	markupSet: [
		{ name: 'Betoning', className: "italic", key: 'I', openWith: '[i]', closeWith: '[/i]' },
		{ name: 'Stark betoning', className: "bold", key: 'B', openWith: '[b]', closeWith: '[/b]' },
		{ separator: '---------------' },
		{ name: 'Bild', key: 'P', className: "picture", replaceWith: '[img][![Adress]!][/img]' },
		{ name: 'L&auml;nk', key: 'L', className: "link", openWith: '[url=[![Adress]!]]', closeWith: '[/url]', placeHolder: 'Text...' },
		{ separator: '---------------' },
		{ name: 'Lista', className: "bulletlist", openWith: '[list]\n', closeWith: '\n[/list]' },
		{ name: 'Numrerad lista', className: "numericlist", openWith: '[list=[![Startnummer]!]]\n', closeWith: '\n[/list]' },
		{ name: 'Listobjekt', className: "listitem", openWith: '[*] ' },
		{ separator: '---------------' },
		{ name: 'Citat', className: "quote", openWith: '[quote]', closeWith: '[/quote]' }
	]
}
}

$(function() {
	// Rounded corners!
	$('.rounded-corners, .rounded-corners-starbackground, .rounded-corners-border, .rounded-corners-border-filled').addCorners();

	// Equal height boxes!
	$('.teaser-group').justifyTeaserGroup();

	// Show the MarkItUp-editor
	$('.bbcode').markItUp(MarkItUp.bbCodeSettings);

	var bdy = $('body');
	if (!bdy.hasClass('layout-1') || (bdy.hasClass('layout-1') && bdy.hasClass('forum'))) {
		document.articleFunctions.addItem({
			text: 'Skriv ut sidan',
			className: 'print',
			insertInto: $('#article-links'),
			callback: function(e) {
				e.preventDefault();
				window.print();
			}
		});
		if (!$('.feelgood').length) {
			document.articleFunctions.addItem({
				text: 'Dela med dig',
				className: 'share',
				insertInto: $('#article-links')
			});
		}
		document.articleFunctions.setupShare();
	}

	var allLinks = $('a');
	if (allLinks.length) {
		allLinks.each(function() {
			var t = $(this);
			if (t.isExternal()) {
				t.wrap('<span class="ext-link-wrapper"></span>');
				t.after('<span class="external-link"></span>');
			}
		});
	}

	//Video carousel
	var videoList = $('.video-list');
	if (videoList.length) {
		var videoCarousel = new NetR.VideoCarousel(videoList);
	}
	
	//Lightbox images
	var thumbnailLinks = $('.feelgood #content-primary a img').not('.feelgood.layout-2 #content-primary a img');
	if (thumbnailLinks.length) {
		thumbnailLinks.each(function() {
			var t = this;

			this.parentLink = $(this).parent('a');

			this.url = this.parentLink.attr('href');
			this.parentLink.addClass('lightbox-link');

			this.magnifyingGlass = $('<img src="/i/magnifying-glass.png" alt="Zooma" />');
			this.parentLink.addClass($(this).hasClass('right') ? 'lightbox-right' : 'lightbox-left');
			this.magnifyingGlass.addClass($(this).hasClass('right') ? 'magnifying-glass-right' : 'magnifying-glass-left');
			$(this).after(this.magnifyingGlass);

			if (!this.url.match(/(\.jpg|\.png|\.gif\.jpeg)$/gi)) {
				thumbnailLinks = thumbnailLinks.not(this);
			}
		});
		thumbnailLinks.parent('a').slimbox({
			counterText: 'Bild {x} av {y}'
		});
	}
	
	// Set width for image captions based on image width
	$('span.caption').each(function () {
		var caption = $(this);
		var img = caption.find('img').not('.magnifying-glass-left, .magnifying-glass-right');
		var src;
		if (img.length && !caption.hasClass('fullwidth') && !caption.hasClass('fullwidth-dec')) {
			// Save src
			src = img.attr('src');
			// Reset src, otherwise the load event will fire instantly
			img.attr('src', '');
			// Set load event observer
			img.bind('load', function (e) {
				caption.css('width', img.outerWidth());
			});
			// Reset src
			img.attr('src', src);
		}
	});
	
	

});
