var _most_news_last = null;
$(document).ready(function () {

    $(".jcarusel").jCarouselLite({
        visible: 3,
        start: 0,
        scroll: 1,
        btnNext: "#wrapper .next",
        btnPrev: "#wrapper .prev",
        btnGo:
        [".select-news .1", ".select-news .2", ".select-news .3", ".select-news .4", ".select-news .5", ".select-news .6",
        ".select-news .7", ".select-news .8", ".select-news .9", ".select-news .10", ".select-news .11", ".select-news .12"]
    });

    featuredcontentglider.init({
        gliderid: "feat-col",       // ID of main glider container
        contentclass: "columnist",  // Shared CSS class name of each glider content
        togglerid: "row",           // ID of toggler container
        remotecontent: "",          // Get gliding contents from external file on server? "filename" or "" to disable
        selected: 0,                // Default selected content index (0=1st)
        persiststate: false,        // Remember last content shown within browser session (true/false)?
        speed: 500,                 // Glide animation duration (in milliseconds)
        direction: "leftright",     // set direction of glide: "updown", "downup", "leftright", or "rightleft"
        autorotate: false,          // Auto rotate contents (true/false)?
        autorotateconfig: [3000, 2] // if auto rotate enabled, set [milliseconds_btw_rotations, cycles_before_stopping]
    })

    $(".footer-slider").jCarouselLite({
        btnNext: ".rotator-container .forward",
        btnPrev: ".rotator-container .back",
        scroll: 5,
        visible: 5
    });

    /**
     * Functions for the set of "most X" boxes.
     *
     * TODO: God, clean this up. Srsly.
     */
    function closeMostBox (box_el, link_el) {
        $(box_el).hide();
        $(link_el).removeClass('open');
        $(link_el).addClass('close');
    }
    function openMostBox (box_el, link_el) {
        $(box_el).show();
        $(link_el).removeClass('close');
        $(link_el).addClass('open');
    }
    // Bind functions to anchors.
    $('#most_popular').click(function () {
        openMostBox($('#mn1-container'), this);
        closeMostBox($('#mn2-container'), $('.most-view'));
        closeMostBox($('#mn3-container'), $('.most-email'));
    });
    $('.most-view').click(function () {
        closeMostBox($('#mn1-container'), $('#most_popular'));
        openMostBox($('#mn2-container'), this);
        closeMostBox($('#mn3-container'), $('.most-email'));
    });
    $('.most-email').click(function () {
        closeMostBox($('#mn1-container'), $('#most_popular'));
        closeMostBox($('#mn2-container'), $('.most-view'));
        openMostBox($('#mn3-container'), this);
    });

    // Top story of main news.
    function animateCarousel (els) {
        // Where are we?
        var cur = $('.jcarusel .selected').prevAll().length;
        // Where to next?
        var next = (cur == $('.jcarusel li').length - 1) ? 0 : cur + 1;

        $(els[cur]).removeClass('selected');
        $(els[next]).addClass('selected');
        var el = $(els[next]).find('div.new-banner').html();
        $('#banner-rotator').html(el);
    }

    // Start by "select"ing 4th item (since that's the first "big picture" that shows up)
    $('.jcarusel li:nth-child(4)').addClass('selected');
    var carusel_timer = setInterval(animateCarousel, 6000, $('.jcarusel li'));
    $('a.view-banner').click(function () {
        $('.jcarusel').find('.selected').removeClass('selected');
        $(this).parent().addClass('selected');
        var el = $(this).parent().find('div.new-banner').html();
        $('#banner-rotator').html(el);
        clearInterval(carusel_timer); // stop timer on first click
    });

});

var show = 0;
function displayColum () {
    if (show == 0) {
        show = 1;
        $(".colunist-more").show();
    } else {
        show = 0;
        $(".colunist-more").hide();
    }
}

function resizeFont (size) {
    $(".entry-content").css('font-size',size +'px');
}

function emailPage() {
    // patched using addthis
    addthis_sendto('email');
    //window.open( '/share/email/?url='+escape(top.location)+'&title='+escape(document.title), 'emailthis', 'location=0,status=0,scrollbars=0,width=300,height=200');
}

function signIn () {
    var o = document.getElementById('sign-form');
//    o.elements['return_to'].value = "http://beta.newamericamedia.org/redirect.php?url=" + urlEncode( top.location );
    o.elements['return_to'].value = top.location;
    o.submit();
}

function urlEncode (str) {
    return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function showEditEntryLink (is_logged_in, dom_id) {
    if (is_logged_in) {
        document.getElementById(dom_id).style.display   = 'block';
        document.getElementById(dom_id).style.textAlign = 'right';
    } else {
        // code to execute if user is not logged in.
        // Right now, that's nothing.
    }
}

