(function($){

    var slider = $("#dig-top-region .slider"),
        creg   = $("#dig-top-region .content-region"),
        ctrls  = $(".controls", creg),
        numbox = $(".numbers", ctrls),
        next_b = $("a.next", ctrls),
        prev_b = $("a.prev", ctrls),
        play_b = $("a.play-pause", ctrls),
        imgs   = $("img", slider),
        total  = imgs.length,
        cur    = 0,
        pad    = 10,
        img_width = 340,
        animating = false,
        autoplay  = false, // starts false, but gets set fairly immediately below this
        autoplay_interval = 5000,
        autoplay_ref;

    function autoswitch_pages() {
        if (autoplay) { go_to_page(cur+1); }
    }
    
    function autoplay_stop() {
        if (autoplay) {
            autoplay = false;
            window.clearInterval(autoplay_ref);
            play_b.addClass('pause').removeClass('play');
        }
    }
    function autoplay_start() {
        if (! autoplay) {
            autoplay = true;
            autoplay_ref = window.setInterval(autoswitch_pages, autoplay_interval);
            play_b.addClass('play').removeClass('pause');
            autoswitch_pages();
        }
    }
    
    function go_to_page(page_number) {
        if (animating) { return; }
        animating = true;
        if (page_number < 0) {
            page_number = total - 1;
        }
        else if (page_number >= total) {
            page_number = 0;
        }
        var old_page = ".page:eq("+cur+")";
        cur = page_number;
        var new_page = ".page:eq("+cur+")";
        $('a:eq('+page_number+')', numbox).addClass('cur').siblings().removeClass('cur');
        new_pos = 0 - (cur * (img_width + pad) );
        slider.animate({left: new_pos}, 500);
        $(old_page, creg).animate({opacity:'hide'}, 250, 'linear', function(){
            $(new_page, creg).animate({opacity:'show'}, 250, 'linear');
            animating = false;
        });
    }

    // we don't use autoplay_start() here because we don't want to trigger the initial switch right away. 
    autoplay = true;
    autoplay_ref = window.setInterval(autoswitch_pages, autoplay_interval);
    play_b.addClass('play').removeClass('pause');

    next_b.click(function(e){
        autoplay_stop();
        go_to_page(cur+1);
        e.preventDefault();
    });

    prev_b.click(function(e){
        autoplay_stop();
        go_to_page(cur-1);
        e.preventDefault();
    });

    play_b.click(function(e){
        if (autoplay) {
            autoplay_stop();
        } else {
            autoplay_start();
        }
        e.preventDefault();
    });

    function mk_goto_callback(i) {
        return function(e){
            autoplay_stop();
            go_to_page(i);
            e.preventDefault();
        }
    }

    for (var i=0; i < total; i++) {
        var lnk = $("<a>").attr('href', '#').text(i+1);
        if (i==0) { lnk.addClass('cur'); }
        lnk.click( mk_goto_callback(i) );
        numbox.append(lnk);
    }

})(jQuery);

(function($){
    var feed      = $("#dig-main-page-news-feed"),
        frame     = $(".frame", feed),
        slider    = $(".slider", frame),
        cframe    = $(".controls-frame", feed),
        controls  = $(".controls", cframe),
        numbox    = $(".numbers", controls),
        entries   = $(".news-entry", slider),
        per_page  = 3,
        page_max  = 15,
        page_ct   = Math.ceil(entries.length / per_page),
        entry_max = per_page * page_max,
        cur_page  = 0,
        animating = false;
    /*
     * This block of code is so that we can provide a "read older news" when
     * we get more than the max (currently 45, but the value of entry_max above)
     * amount of news entries we can create a news archives page later, but for
     * now it is commented because no /news-archives/ yet exists.
     */
    /*
    if (entries.length > entry_max-1) {
        $(".news-entry:gt("+ (entry_max-2) + ")", slider).remove();
        $("<div/>")
        .addClass( 'news-entry' )
        .append( "<a class='full-size-link' href='/news-archives/'>Read Older News</a>" )
        .appendTo( slider );
    }
    */
    
    function go_to_page(page_number) {
        if (animating) { return; }
        animating = true;
        if (page_number < 0) {
            page_number = page_ct - 1;
        }
        else if (page_number >= page_ct) {
            page_number = 0;
        }
        cur_page = page_number;
        $('a:eq('+page_number+')', numbox).addClass('cur').siblings().removeClass('cur');
        var new_offset = -(cur_page * 246);
        slider.animate({top: new_offset}, 500);
        animating = false;
    }

    function mk_goto_callback(i) {
        return function(e){
            go_to_page(i);
            e.preventDefault();
        }
    }
    
    $(".news-entry:odd", slider).css('background-color', '#fff');
    $(".news-entry", slider)
    .hover(
        function(e){ $(this).addClass('hovered');    },
        function(e){ $(this).removeClass('hovered'); }
    )
    ;
    for (var i=0; i < page_max; i++) {
        if (i < page_ct) {
            var lnk = $("<a>").attr('href', '#').text(i+1);
            if (i==0) { lnk.addClass('cur'); }
            lnk.click( mk_goto_callback(i) );
            numbox.append(lnk);
        } else {
            $("<span>").addClass('no-link').text(i+1).appendTo(numbox);
        }
    }
    
   $("a.next", controls).click(function(e){
        go_to_page(cur_page + 1);
        e.preventDefault();
    });
    $("a.prev", controls).click(function(e){
        go_to_page(cur_page - 1);
        e.preventDefault();
    });
})(jQuery);
