$(function () {

    var updateImage = function(direction, o) {
        if (o.fading) { return; }
        o.fading = true;
/*
        if (o.lastdirection) {
           if (o.lastdirection == 1) {
                var next = $("li:first", o.root);
                next.remove();
                // o.ul.css("left", "-711px").append(next);
                o.ul.css("left", "-730px"); //.append(next);
           } else {
                var current = $("li:last", o.root);
                current.remove();
                // o.ul.css("left", "-711px").prepend(current);
                o.ul.css("left", "-730px").prepend(current);
           }
        }
*/
        if (direction == 1) {
            var next = $("li:first", o.root);
            o.ul.append(next.clone());
            o.ul.animate({ left: "-=474px" }, 500, "swing", function() {
                next.remove();
                /* o.ul.css("left", "-711px").append(next); */
                o.ul.css("left", "-730px"); //.append(next);
                o.lastdirection = 1;
                o.fading = false;
            });
        } else {
            var current = $("li:last", o.root);
            o.ul.animate({ left: "+=474px" }, 500, "swing", function() {
                current.remove();
                /* o.ul.css("left", "-711px").prepend(current); */
                o.ul.css("left", "-730px").prepend(current);
                o.lastdirection = -1;
                o.fading = false;
            });
        }
    }

    var updatePicker = function(direction, o) {
            $(".picker li:eq(" + o.pos + ")").removeClass("selected");
            o.pos += direction;
            if (o.pos < 0) o.pos = o.total - 1;
            if (o.pos >= o.total) o.pos = 0;
            $(".picker li:eq(" + o.pos + ")").addClass("selected");
    }

    $(".carousel").each(function() {

        var o = { fading: false, root: $(this), ul: $("ul", $(this)), pos: 0, total: $("li", $(this)).length };

        for (var i = 0; i < o.total; i++)
            $(".picker").append('<li>' + i + '</li>');

        $(".picker li:first").addClass('selected');

        if (o.total == 1) {
            o.ul.css("left", "247px");
            return;
        }


        var n = $("li:last", o.root);
        n.remove();
        o.ul.prepend(n);
        var n = $("li:last", o.root);
        n.remove();
        o.ul.prepend(n);


        if (o.total < 4) {
            o.ul.append($("li", o.ul).clone());
        }

        $(".picker li").bind("click", function(e) {
           var newp = $(e.target).text();
           var direction = 0;
           if (o.pos > newp) {
             direction = -1;
             var c = o.pos - newp;
           } else if (o.pos < newp) {
             direction = 1;
             var c = newp - o.pos;
           }
            if (direction != 0) {
              updatePicker(direction, o);
              updateImage(direction, o);
            }
        });

        $("a", o.root).bind("click", function(e) {
            var direction = $(e.target).parent().hasClass("right") ? 1 : -1;

            updatePicker(direction, o);
            updateImage(direction, o);
            return false;
        });

        /*
        setTimeout(function() {
            updateImage(1, o);
            setTimeout(arguments.callee, 5000);
        }, 5000);
        */

    });

});

