// Modded for Radostar.
// 
// Dimensions have been hard-coded because for a some reason probably 
// related to the way #overlay is displayed inside a position:fixed; display: none; div
// of 100% height jQuery is finding size calculations impossible inside #overlay.

(function($) {
    function css(el, prop) {
        return parseInt($.css(el[0], prop)) || 0;
    }

    function width(el) {
        return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
    }

    function height(el) {
        return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
    }

    $.fn.carousel = function(o) {
        o = $.extend({
            buttonPrev: null,
            buttonNext: null,

            speed: 200,
            easing: null,

            visible: 5,
            scroll: 2
        }, o || {});

        return this.each(function() {
            var animating = false;
            var div = $(this),
                ul = $("ul", div),
                tLi = $("li", ul),
                tl = tLi.size(),
                v = o.visible;
            var li = $("li", ul),
                itemLength = li.size(),
                curr = 0;

            var liSize = 98;    // width(li);
            var ulSize = liSize * (itemLength+1);
            var divSize = liSize * v;

            div.css("visibility", "visible");
            li.css({
                overflow: "hidden",
                float: "left",
                width: 88+"px", // li.width(),
                height: 94+"px" // li.height()
            });
            ul.css({
                margin: "0",
                padding: "0",
                position: "relative",
                "list-style-type": "none",
                "z-index": "1",
                width: ulSize+"px",
                left: -(curr*liSize)
            });
            div.css({
                position: "relative",
                "z-index": "2",
                left: "0px",
                width: 490+"px" // divSize+"px");
            });

            if(o.buttonPrev) {
                $(o.buttonPrev).click(function() {
                    if (animate(curr-o.scroll)) {
                        $(this).animate({opacity: "hide"}, o.speed);
                    }
                    $(o.buttonNext).animate({opacity: "show"}, o.speed);
                });
            }
            if(o.buttonNext) {
                $(o.buttonNext).click(function() {
                    if (animate(curr+o.scroll)) {
                        $(this).animate({opacity: "hide"}, o.speed);
                    }
                    $(o.buttonPrev).animate({opacity: "show"}, o.speed);
                });
            }

            function animate(to) {
                var end = false;
                if (!animating) {
                    animating = true;
                    if (to <= 0) {
                        curr = 0;
                        end = true;
                    } else if (to > itemLength-v) {
                        curr = itemLength-v;
                        end = true;
                    } else {
                        curr = to;
                    }
                    
                    ul.animate(
                        { left: -(curr*liSize) }, o.speed, o.easing,
                        function() {
                            animating = false;
                        }
                    );
                    
                    $(o.buttonPrev + "," + o.buttonNext);
                     
                    $(
                        (curr == 0 && o.buttonPrev) ||
                        (curr == itemLength-v && o.buttonNext) ||
                        []
                    );
                }
                return end;
            };
        });
    };
})(jQuery);
