/*$(document).ready(function() {
    if ($('#LogoutPanel').length > 0) {
        $('Body').css('background-position', '0 36px');
    }
});
*/

(function($) {
    $.fn.ssnSameHeight = function() {
        var maxHeight = 0;
        return this.each(function() {
            var height = $(this).height();
            if (height > maxHeight) maxHeight = height;
        }).height(maxHeight);
    }
})(jQuery);


/* Special Menu */
$(document).ready(function() {
    var menuBg = $('div.SubMenuBg');
    var menufix = $("#menugfx_left, #menugfx_right");
    var tagfix = $(".tag");

    $('ul.SubMenu li').ssnMenu({
        BindData: $('ul.Menu.Main a'),
        TargetFilter: function(target) {
            if (!$(target).is('a'))
                target = $(target).parents('a').get(0);
            return target;
        },
        ItemsEffect: function(target, hideItems, showItems) {
            if (showItems.size() == 0) {
                menuBg.hide();
                menufix.removeClass('withsub');
                tagfix.show();
                kvinna_ForecastWidget.start();
            } else {
                menuBg.show();
                menufix.addClass('withsub');
                tagfix.hide();
                kvinna_ForecastWidget.stop();
            }

            target.parent().siblings().removeClass('Selected').end().addClass('Selected');
            hideItems.hide();
            showItems.show();
        },
        NoItemEffect: function(items, options) {
            if (SSNContext.Page.IsFrontPage)
                menuBg.hide();
            else
                kvinna_ForecastWidget.stop();
        },
        StartUpWithSelectedItem: $('ul.Menu.Main li.Selected a'),
        NoItemSelectedGoBackToStartupItem: true,
        LockedArea: menuBg
    });
});

/* TopNews */
$(document).ready(function() {
    var selectlist = $('ul.TopNews a > span.Switch').ssnSelectList({
        hideEffect: function(obj) { obj.parent().removeClass('selected'); obj.fadeOut(200); },
        showEffect: function(obj) { obj.parent().addClass('selected'); obj.fadeIn(200); }
    });
    selectlist.goto(0);
    var current = null;
    selectlist.list.each(function(i) {
        $(this).parent().mouseover(function() {
            if (current != i) {
                selectlist.goto(i);
                clearTimeout(rotateTimer);
            }
            current = i;
        });
    });

    /* Rotate */
    var rotateTimer;
    var Rotate = function(list, time) {
        rotateTimer = setTimeout(function() {
            list.next();
            Rotate(list, time);
        }, time);
    }
    Rotate(selectlist, 3000);
});




/* Bloggarar */
$(document).ready(function() {
    var itemsOnPage = 8;

    var startlink = $('.NewsSnippet.Blog.Pager .controls .start');
    var prevlink = $('.NewsSnippet.Blog.Pager .controls .previous');
    var counter = $('.NewsSnippet.Blog.Pager .controls .counter');
    var morelink = $('.NewsSnippet.Blog.Pager .controls .next');
    var items = $('.NewsSnippet.Blog.Pager .items .item');
    items.slice(0, itemsOnPage).show();

    $.fn.setHide = function(enabled) { if (enabled) this.hide(); else this.show(); }

    var updateCounter = function(showItems) {
        var pages = Math.ceil(items.size() / itemsOnPage);
        var lastShowItem = showItems.eq(showItems.size() - 1);
        var pagePosition = 0;
        var currentPage = items.each(function(i) {
            if ($(this).not(lastShowItem).size() == 0) pagePosition = Math.ceil((i + 1) / itemsOnPage);
        });
        counter.html(pagePosition + ' / ' + pages);
        morelink.setHide(pagePosition == pages);
        prevlink.setHide(pagePosition == 1);
        startlink.setHide(pagePosition == 1);
        counter.setHide(pages == 1);
    }

    var showItems = function(showItems) {
        if (showItems.size() != 0) {
            showItems.show();
            items.not(showItems).hide();
            updateCounter(showItems);
        }
    }

    morelink.click(function() {
        var show = items.filter(':visible:last').nextAll('.item:lt(' + itemsOnPage + ')');
        showItems(show);
    });
    prevlink.click(function() {
        var show = items.filter(':visible:first').prevAll('.item:lt(' + itemsOnPage + ')');
        showItems(show);
    });
    startlink.click(function() {
        var show = items.filter(':lt(' + itemsOnPage + ')');
        showItems(show);
    });

    updateCounter(items.filter(':lt(4)'));
});

/* UseBelow container */
$(document).ready(function() {
    $('div.Container.UseBelow').each(function() {
        var moveTo = $(this).nextAll('div.Container:first').find('.Content');
        $(this).prependTo(moveTo);
    });
});