﻿/* Article tools */
(function ($) {
    $.fn.addTools = function (customOptions) {
        var options = $.extend({}, $.fn.addTools.defaultOptions, customOptions);

        return this.each(function () {
            $(this).prepend('<span class="' + options.printClass + '"><a href="">' + options.printText + '</a></span><span class="' + options.shareClass + '"><a href="">' + options.shareText + '</a></span>');


            $('span.' + options.shareClass + ' a').toggle(function (event) {
                $('div.sharethisbox').slideDown();
                $('li.' + options.shareClass + ' a').html(options.closeText);
                return false;
            }, function () {
                $('div.sharethisbox').slideUp();
                $('li.' + options.shareClass + ' a').html(options.shareText);
                return false;
            });

            $('fieldset.sharethisboxright a').click(function () {
                $('div.sharethisbox').slideUp();
                $('li.' + options.shareClass + ' a').html(options.shareText);
                window.open(this.href);
                return false;
            });

            $('span.print a').click(function () {
                window.print();
                return false;
            });
        });
    };

    $.fn.addTools.defaultOptions = {
        printClass: 'print',
        printText: 'Print',
        shareClass: 'share',
        shareText: 'Share content',
        closeText: 'Lukk'
    };

    $.fn.addCaption = function () {
        return this.each(function () {
            var title = $(this).attr("title");
            if (title.length) {
                $caption = $("<p/>");
                var imageClass = $(this).attr("class");
                var width = $(this).width() ? $(this).width() : $(this).attr("width");
                var cssWidth = $(this).css("width");

                if ($(this).hasClass("ImageWithTxt")) {
                    // do nothing 
                } else if (!width) {
                    $caption.css("width", $(this).css("width"));
                } else {
                    $caption.css("width", width + "px");
                }
                $caption.addClass(imageClass + " ImageCaptionText").html(title);
                $(this).after($caption);
            }
        });
    };


})(jQuery);
jQuery(document).ready(function () {
    // show search for js enabled
    $("div.google-cse").show();
    /* Article tools */
    $('#ShareLink').addTools({
        printText: 'Skriv ut',
        shareText: 'Del innhold'
    });

    /* image captions */
    $("img.ImageRightWithTxt, img.ImageLeftWithTxt, img.ImageWithTxt").addCaption();

});

