(function ($) {

    if ($("#adBox").length) {

        var int = null,
            panelWidth = parseInt($(".panels").children(":first").width()),
            ui = $("#ui"),
            li = $("<li></li>"),
            a = $("<a></a>"),
            current = 1;

        //build nav links
        $.each($(".panels").children(), function (i, val) {
            var newLi = li.clone();

            a.clone().attr({
                href: "#panel-" + (i + 1),
                "class": (i === 0) ? "on" : ""
            }).text(i + 1).appendTo(newLi);

            newLi.appendTo(ui);
        });

        //set slider width
        $(".panels").width(panelWidth * $(".panels").children().length);

        //auto rotate
        function automata() {

            if (current < $(".panels").children().length) {
                $("#adBox").find(".panels").animate({
                    "left": "-=" + panelWidth + "px"
                }, "slow", function () {
                    current++;
                    ui.find(".on").removeClass("on").parent().next().find("a").addClass("on");
                });
            } else {
                //move last panel to front
                $(".panels").children(":last").prependTo(".panels");
                $(".panels").css("left", 0);

                //animate to 'first' panel
                $("#adBox").find(".panels").animate({
                    "left": "-=" + panelWidth + "px"
                }, "slow", function () {

                    //put last panel back to end
                    $(".panels").children(":first").appendTo(".panels");
                    $(".panels").css("left", 0);

                    current = 1;
                    ui.find(".on").removeClass("on");
                    ui.children(":first").find("a").addClass("on")
                });
            }
        }

        //go auto rotate!
        int = setInterval(function () { automata(); }, 10000);

        //click handler
        ui.find("a").click(function (e) {
            e.preventDefault();

            if (!$(".panels").is(":animated") && !$(this).hasClass(".on")) {
                clearInterval(int);

                ui.find(".on").removeClass("on");

                var target = parseInt($(this).text());

                if (current < target) {

                    var distance = (target - current) * panelWidth;

                    $(".panels").animate({
                        left: "-=" + distance + "px"
                    }, "slow");

                } else {

                    var distance = (current - target) * panelWidth;

                    $(".panels").animate({
                        left: "+=" + distance + "px"
                    }, "slow");
                }

                ui.find("a").eq(target - 1).addClass("on");
                current = target;

                //restart auto rotate
                int = setInterval(function () { automata(); }, 10000);
            }
        });
    }
})(jQuery);
