$.fn.partCollapsibleList = function(options) {

    var defaults = {
        maximumItems: 5,
        tolerance: 3,
        showText: "Vis flere",
        hideText: "Vis færre"
    };

    var options = $.extend(defaults, options);

    return this.each(function(index, el) {

        var $listItems = $(el).children("li"),
            $toggleableItems,
            $button;
        
        if ($listItems.length > (options.maximumItems + options.tolerance)) {

            $toggleableItems = $listItems.slice(options.maximumItems);

            $toggleableItems.hide();
            $button = $('<a href="#" class="linkButton">' +
                    options.showText + '</a>');
            $button.insertBefore(
                    $toggleableItems.eq(0)).wrap('<li class="toggler"></li>');

            $button.click(function (ev) {

                if ($button.text() == options.showText) {
                    $toggleableItems.show();
                    $button.parent().addClass("expanded");
                    $button.text(options.hideText);
                } else {
                    $toggleableItems.hide();
                    $button.parent().removeClass("expanded");
                    $button.text(options.showText);
                }
                return false;
            });
        }
    });
};



$(document).ready(function(){

        
        Shadowbox.init(
        {
            players:['img', 'iframe']
           , continuous: true
           ,overlayColor:"#404040"
           ,overlayOpacity: 1.0
        });	
	
$('div#moretxt').hide()
$('div#detailInfo').toggle(function(event){$('div#moretxt').fadeIn();$('img#arrow').attr({src:"/@@/resources/moreArrowDown.gif"})},function(event){$('div#moretxt').fadeOut();$('img#arrow').attr({src:"/@@/resources/moreArrow.gif"})})

//console.log($('div#detailInfo'))

$('div> ul> li> a#publicationlink').toggle(function(event){$("ul#publicationlist> li.hidden").fadeIn()},function(event){$("ul#publicationlist> li.hidden").fadeOut();});

$('div> ul> li> a#articlelink').toggle(function(event){$("ul#testlistarticle> li.hidden").fadeIn()},function(event){$("ul#testlistarticle> li.hidden").fadeOut();});

$('div> ul> li> a#newslink').toggle(function(event){$("ul#testlistnews> li.hidden").fadeIn()},function(event){$("ul#testlistnews> li.hidden").fadeOut();});

$('div> ul> li> a#filmlink').toggle(function(event){$("ul#testlistfilm> li.hidden").fadeIn()},function(event){$("ul#testlistfilm> li.hidden").fadeOut();});

$('div> ul> li> a#personlink').toggle(function(event){$("ul.relatedlist> li.hidden").fadeIn()},function(event){$("ul.relatedlist> li.hidden").fadeOut();});

$('div> ul> li> a#morelinknews').toggle(function(event){$("ul#newslist> li.hidden").fadeIn()},function(event){$("ul#newslist> li.hidden").fadeOut();});
$('div> ul> li> a#morelinkfilm').toggle(function(event){$("ul#filmlist> li.hidden").fadeIn()},function(event){$("ul#filmlist> li.hidden").fadeOut();});

//script to open external links in new window
//console.log($('div.mainTxt> p> a').attr("rel"));
$('div.mainTxt> p> a[rel="external"]').click(function(){
  window.open(this.getAttribute('href'), '_blank');
  return false;
});



// Toogle the tip-a-friend form
 $("a.tip-a-friend").click(function () {
     //$("#tip-a-friend").slideToggle("fast");
	 $("#tip-a-friend").animate({ height: "toggle", opacity: "toggle"}, "fast" )
     return false;
   });


//script for A-Z page

$('ul#letterList> li> a').click(function() {
  $holder = $(this).attr("class");
  $("div#A-A> div").css("background", "#ffffff")
            .filter(function (index) {
                  return $(this).attr("class") != $holder;
                })
            .css("display", "none");
  $("div#A-A> div")
            .filter(function (index) {
                  return ($(this).css("display", "none")) && $(this).attr("class") == $holder;
                })
            .css("display", "block");

});
    //quick way to handle labels for the norwegian and english site
    options = {
        showText: "Vis flere",
        hideText: "Vis færre"
    }
    if(location.pathname.indexOf("/en/") > -1){
        //english part of the site
        options['showText'] = "Show more";
        options['hideText'] = "Show less";
    }
    $("#relatedContent .relatedBox ul, .collapsible").partCollapsibleList(options);
});

