;(function($) {
// What does the tv2paginator plugin do?
$.fn.tv2paginator = function(options) {
    var opts = $.extend({}, $.fn.tv2paginator.defaults, options);

    return this.each(function() {
        var $this = $(this), entries = [], currentItem = { 'num': 0 },
        scroller, navigation = {},

        updateNavigation = function(cur){
            cur = cur || 0;
            navigation.label.text($(entries[cur]).find('h3').text());

            /* Diasble / Enable previous button */
            if(cur==0) {
                navigation.previous
                    .addClass('disabled')
                    .filter('button').attr('disabled','disabled');
            }
            else {
                navigation.previous
                    .removeClass('disabled')
                    .filter('button').removeAttr('disabled');
            }
            /* Diasble / Enable next button */
            if(cur==entries.length-1) {
                navigation.next
                    .addClass('disabled')
                    .filter('button').attr('disabled','disabled');
            }
            else {
                navigation.next
                    .removeClass('disabled')
                    .filter('button').removeAttr('disabled');
            }
        };

        /* find the scroller and find references to the entries */
        scroller = $this.find('ol.scroller:eq(0)');
        entries = $this.find('ol.scroller > li');

        if(entries.length){
            pos = parseInt(scroller.css('marginLeft'),10)
                - (scroller.offset().left - $(entries[0]).offset().left);

            scroller.css({'marginLeft':pos * -1});
        }

        /* if there's only one panel, do not set the pagination stuff up */
        if(entries.length < 2) return;

        /* build and append the pagination navigation to the box */
        navigation.wrapper = $('<p class="nav"></p>');
        navigation.next = $([
            '<span class="pagination next">',
                '<button>Gå en dag tilbage</button>',
            '</span>'
        ].join(''));

        navigation.previous = $([
            '<span class="pagination previous">',
                '<button>Gå en dag frem</button>',
            '</span>'
        ].join(''));

        navigation.label = $('<span class="label"></span>');
        navigation.wrapper
            .append(navigation.next)
            .append(navigation.label)
            .append(navigation.previous)
            .insertAfter(scroller);

        var widest = 0;
        entries.find('h3').each(function(key,value){
            navigation.label.text($(this).text());
            var width = navigation.label.width();
            if(key == 0 || width > widest) widest = width;
        });
        navigation.label.css('width', widest);

        updateNavigation();

        $this.click(function(event){
            var $target = $(event.target);

            if($target.is('button') && $target.parent('span').hasClass('pagination')){
                event.preventDefault();

                /* the following makes it possible to paginate through the
                 * related news items */
                var pos = parseInt(scroller.css('marginLeft'),10),
                    smallest, cur,

                elmOffset = scroller.offset().left;

                entries.each(function(key, value){
                    var a = Math.abs(pos - (elmOffset-$(this).offset().left));
                    if(key == 0 || a < smallest){ smallest = a; cur = key; }
                });

                var old = cur;

                /* forward pagination */
                if($target.parent('.pagination').hasClass('next')){
                    cur = cur+1 < entries.length ? cur+1 : entries.length-1;
                    pos = $(entries[cur]).position().left;
                }
                /* backwards pagination */
                else if($target.parent('.pagination').hasClass('previous')){
                    cur = cur > 0 ? cur-1 : 0;
                    pos = $(entries[cur]).position().left;
                }

                if(cur != old){
                    if(opts.enableAnimation)
                        scroller.animate(
                            { 'marginLeft': pos * -1 },
                            opts.animationSpeed,
                            null,
                            function(){ updateNavigation(cur); }
                        );
                    else {
                        scroller.css('marginLeft', pos * -1);
                        updateNavigation(cur);
                    }
                }
            }
        });
    });
};

// default options
$.fn.tv2paginator.defaults = {
    enableAnimation : true,
    animationSpeed: 'fast'
};

})(jQuery);

