﻿// Prepares page elements.
$j(document).ready(function() {
});

// Scrolls the rug scroller to the left or right depending on the scroll amount specified (negative numbers for left, positive for right).
function scrollRugs(scrollerId, scrollAmount) {
    var scroller = $j("#" + scrollerId);
    var scrollerItems = scroller.children();
    var scrollLeft = scrollAmount < 0;
    var scrollValue = 0;
    
    // Get the scroller items that need to be moved.
    for(var i = 0; i < Math.abs(scrollAmount); i++) {
        scrollValue += $j(scrollerItems[scrollLeft ? scrollerItems.size() - 1 - i : i]).width();
    }

    // If scrolling left, move items on the end to the beginning before scrolling.
    if (scrollLeft) {
        for (var i = 0; i < Math.abs(scrollAmount); i++) {
            scroller.prepend($j(scrollerItems[scrollerItems.size() - 1]).remove());
        }
        
        scroller.css("left", scrollAmount * scrollValue);
    }
    
    // Scroll the rug scroller.
    if (scrollerItems.size() > 0) {
        scroller.animate({
            left: (scrollLeft ? 0 : -scrollAmount * scrollValue)
        }, 400, function() {
            if (!scrollLeft) {
                for (var i = 0; i < Math.abs(scrollAmount); i++) {
                    scroller.append($j(scrollerItems[0]).remove());
                }

                scroller.css("left", 0);
            }
        });
    }
}

// Moves the ad that slid off the slider to the end of the slider queue.
function slideBigAdsComplete() {
    var slider = $j("#big_ads_slider");

    slider.append($j(slider.children()[0]).remove());
    slider.css("top", 0);
}
