/**
* @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);
			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(){
			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() {
        // Hide the share
        var shareObject = {
            wrapper: $('#share-wrapper'),
            mail: $('#share-wrapper').children('.emailPage'),
            mailTrigger: $('#share-wrapper').find('a.mail-trigger'),
            trigger: $('#article-links').children('.share')
        };


        if (location.hash != "#emailpage") {
            shareObject.wrapper.hide();
            shareObject.mail.hide();
        }


        // Add a click event		
        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();

    }

} ();

$(function() {
    // Rounded corners!
    $('.rounded-corners, .rounded-corners-starbackground, .rounded-corners-border, .rounded-corners-border-filled').addCorners();

    // Equal height boxes!
    $('.teaser-group').justifyTeaserGroup();

    if (!$('body').hasClass('layout-1')) {
        document.articleFunctions.addItem({
            text: 'Skriv ut sidan',
            className: 'print',
            insertInto: $('#article-links'),
            callback: function(e) {
                e.preventDefault();
                window.print();
            }
        });
        document.articleFunctions.addItem({
            text: 'Dela med dig',
            className: 'share',
            insertInto: $('#article-links')
        });
        document.articleFunctions.setupShare();
    }
});