var elements = "";
var counter = 0;
var widthElement = -445;
var animateSpeed = 3000;
var timeForNext = 5000;
var timer = "";
var scrollClick = false;

function rotator() {
    elements = jQuery('#scrollcontent .articlelistblock ul li');
    
    if (elements.length > 0) {
        timer = setTimeout("moveRotator(false)", timeForNext);
    }
}

function turnIt(position) {
    if (scrollClick == false) {
        scrollClick = true;
        clearTimeout(timer);
        if (position == 1) {
            moveRotator(true);
        } else {
            moveBackRotator(true);
        }

        timer = setTimeout("moveRotator(false)", timeForNext);
        setTimeout(function() { scrollClick = false; }, 3000);
    }
}

function moveRotator(click) {
    scrollClick = true;
    jQuery(elements[counter]).animate({ marginLeft: widthElement }, animateSpeed, function() {
        jQuery(elements[counter].parentNode).append(jQuery(elements[counter]));
        elements = jQuery('#scrollcontent .articlelistblock ul li');
        jQuery(elements[elements.length - 1]).css('marginLeft', '0px');
        if (click == false) {
            timer = setTimeout("moveRotator(false)", timeForNext);
        }
        scrollClick = false;
    });
}

function moveBackRotator(click) {
    scrollClick = true;
    jQuery(elements[elements.length - 1]).css("marginLeft", widthElement+ "px");
    jQuery(elements[counter].parentNode).prepend(jQuery(elements[elements.length - 1]));
    elements = jQuery('#scrollcontent .articlelistblock ul li');

    jQuery(elements[counter]).animate({ marginLeft: '0px' }, animateSpeed, function() {
        if (click == false) {
            timer=setTimeout("moveBackRotator(false)", timeForNext);
        }
        scrollClick = false;
    });
}

jQuery(function() {
    rotator();
});