/*
 * Initialize the bannerstory slideshow, when the page has finished loading
 */
$(window).load(function() {
    // Set the height of the topstory container to fit the tallest item
    var topstory_height = 0;
    $(".topstory").each(function(i) {
        topstory_height = ($(this).outerHeight() > topstory_height) ?
            $(this).outerHeight() : topstory_height;
    });
    $("#topstory").animate({height: topstory_height}, 300, 'linear', fadeFirst);
    
    // Fade in the first item and the indicator menu         
    function fadeFirst() {
        $(".topstory:first").fadeIn("def");
        $("#topstory > ul").fadeIn("def", setFirstActive);
    }
    
    
    // fang PBS klik
    
    $("a.pbslink").click(function() {
		
		window.open('https://www.betalingsservice.dk/BS/?id=0&pbs=cd27ec81983b4820de5cbd665c1e1092&dbnr=&dbgr=00001&navn=&adr=&postby=&knmin=&knmax=','BSTilmeld','resizable=no,toolbar=no,scrollbars=no,menubar=no,location=no,status=yes,width=375,height=500');
		return false;
		
	});
});

/*
 * Slideshow of all items
 */
$(function() {
    $("#topstory").everyTime(8000, 'topstorytimer', function () {
        $(".topstory:visible:first").fadeOut("def", setNextActive);
        if($(".topstory:animated").next().is(".topstory"))
            $(".topstory:animated").next().fadeIn("def");
        else
            $(".topstory:first").fadeIn("def");
    });
});

/*
 * Sets first selector item active
 */
function setFirstActive() {
    $("#topstory > ul > li:first").addClass("active");
}

/*
 * Sets next selector item active
 */
function setNextActive() {
	var item = $("#topstory > ul > li.active"); 
    item.removeClass("active");
    if(item.next().is("li"))
        item.next().addClass("active");
    else
        $("#topstory > ul > li:first").addClass("active");
}

/*
 * Function that jumps to a specific item
 */
function showStory(index) {
    // Only change the item if user is not requesting the currently selected item
    if($(".topstory:visible").eq(0).html() != $(".topstory").eq(index).html()) {
        $("#topstory").stopTime('topstorytimer');
        
        if($(".topstory:animated").length > 0) {
            $(".topstory:animated").stop();
            $(".topstory:animated").fadeOut("def");
        } else {
            $(".topstory:visible").fadeOut("def");
        }
        $(".topstory").eq(index).fadeIn("def");
        
        $("#topstory > ul > li.active").removeClass("active");
        $("#topstory > ul > li").eq(index).addClass("active");
    }
}