(function($) {
    $(function() {
        var entry_ids = [];
        
            entry_ids.push(195);
        
            entry_ids.push(153);
        
            entry_ids.push(7);
        
            entry_ids.push(8);
        
            entry_ids.push(32);
        

        var current_entry = 0;

        $("#feature").attr({role: "complementary", "aria-live": "polite"});

        $("#feature_container").append(
            $("<a href='#' class='change_feature' id='link-prev' role='button' aria-controls='feature'>Previous</a>")
            .click(function(e) {
                current_entry = (current_entry == 0) ? current_entry = 5 % entry_ids.length : current_entry = current_entry - 1;
                clearInterval(switch_int);
                switch_content();
                e.preventDefault();
            })
        )
        .append(
            $("<a href='#' class='change_feature' id='link-next' role='button' aria-controls='feature'>Next</a>")
            .click(function(e) {
                current_entry = (current_entry + 1) % entry_ids.length;
                clearInterval(switch_int);
                switch_content();
                e.preventDefault();
            })
        );

        var features = [];

        var switch_content = function() {
            $("#feature").attr("aria-busy", "true");
            if (features[current_entry]) {
                insert_content(features[current_entry]);
            } else {
                $.get("/includes/hf_content/" + entry_ids[current_entry], null, function(data) {
                    features[current_entry] = data;
                    insert_content(data);
                });
            }
        };

        var insert_content = function(data) {
            $("#feature").attr("aria-busy", "true");
            $("#feature_container").prepend($(data));
            $('.feature_contents').eq(1).fadeOut(250, function(){
                $(this).remove();
                $("#feature").attr("aria-busy", "false");
            });
        };

        var switcher = $("<ul class='switcher'></ul>");
        for (var i = 1; i <= 5; i++) {
            var switch_button = $("<li><a href='#' role='button' aria-controls='feature'>" + i + "</a></li>")
            .click(function(e) {
                current_entry = ($(this).text() - 1) % entry_ids.length;
                switch_content();
                clearInterval(switch_int);
                e.preventDefault();
            })
            .appendTo(switcher);
         }
         $("#feature").prepend(switcher);

        var switch_int = setInterval(function(){
            current_entry = (current_entry + 1) % entry_ids.length;
            switch_content();
        }, 10000);

    });
})(jQuery);