jQuery.fn.hero_rotator = function(options) {

    var settings = jQuery.extend({}, jQuery.fn.hero_rotator.defaults, options);

    var hero = {

            stage : Object,
            paperclip : Object,
            courses : Object,
            timing : Object,
            
            init :      function(settings){


                            this.stage.box = $("#stage");
                            this.stage.frame = $("#stage_inner");
                            this.stage.content = $("#stage_content");
                            this.stage.content.title = $(this.stage.content).find("h1").find("strong");
                            this.stage.content.summary = $(this.stage.content).find("p");
                            this.stage.content.link = $(this.stage.content).find("a");

                            this.paperclip = "<div class='paperclip' style='position: relative;'></div>";
                            this.courses = settings.courses_list.courses;

                            this.timing.interval = settings.interval;
                            this.timing.fade_out = settings.fade_out_time;
                            this.timing.fade_in = settings.fade_in_time;
                            
                            this.run();

                        },
                        
            radio :     {
                            generate :  function(){
                                            var radio_buttons = "";
                
                                            $(hero.courses).each(function(i){
                                                radio_buttons += "<li id='slide_" + i + "'><a href='#' rel='" + i + "'></a></li>";
                                            });
                                            
                                            radio_buttons = "<ul class='radios'>" + radio_buttons + "</ul>";
                                            
                                            $(hero.stage.frame).prepend(radio_buttons);
                                        },

                            off:        function(){
                                            $(hero.stage.frame).find("ul").find("li").removeClass();
                                        },
                                        
                            on :        function(i){
                                            $("#slide_" + i).addClass("on");
                                        }
                        },
                        
            show_clip : function(paperclip, stage){
                            $(stage).prepend(paperclip);
                        },
            
            populate :  function(i){        
                            $(this.stage.content.title).html(this.courses[i].title);
                            $(this.stage.content.summary).html(this.courses[i].summary);
                            $(this.stage.content.link).attr("href", this.courses[i].link);
                        },
                        
            images :    function(){
                            var base = this;
                            var image_stack = [];
                            var images = "";
                            
                            $(base.stage.box).find("img").remove();
                            
                            $(base.courses).each(function(i){
                                image_stack[i] = "<img src='" + this.image + "' id='img_" + i +"'/>";
                            });
                            
                            image_stack.reverse();
                            
                            $(image_stack).each(function(){
                                images += this;
                            });
                            
                            $(base.stage.box).prepend(images);
                        },
                        
            rotate :    function(i){            
                            this.populate(i);
                            this.radio.on(i);

                            var base = this;
                            var c = (this.courses.length) - 1;

                            var rotator = function(){    

                                i++;
                                                                
                                if(i > c){
                                    i = 0;
                                };
                                
                                animate(i);
                                
                            };

                            z = 1000;

                                                        
                            var animate = function(i){
                                                                
                                $("#img_" + i).animate({ left: "-300px" }, base.timing.fade_out, function(){
                                    $(this).css({ zIndex: z }).animate({ left: "20px" }, base.timing.fade_in);
                                    $(".paperclip").css({ zIndex: z+1 });
                                    z++;
                                });

                                $(base.stage.content).animate({ opacity: 0 }, base.timing.fade_out, function(){
                                    base.radio.off();
                                    base.populate(i);
                                    base.radio.on(i);
                                }).animate({ opacity: 1 }, base.timing.fade_in);

                            };

                            var timer = setInterval(rotator, base.timing.interval);
                            
                            $(this.stage.frame).hover(function(){

                                $(this).stop(true, true);
                                window.clearInterval(timer);

                            }, function(){

                                timer = setInterval(rotator, base.timing.interval);

                            });
                            
                            $("ul.radios li").not("on").find("a").click(function(){

                                window.clearInterval(timer);

                                $(this).parent("li").addClass("on").siblings().removeClass();

                                i = $(this).attr("rel");
                                animate(i);

                                return false;
                            });
                            
                        },

            run :       function(){
                            this.radio.generate();
                            this.images();
                            this.show_clip(this.paperclip, this.stage.box);
                            
                            var i = 0;
    
                            this.rotate(i);
                        }

    };

    $.getJSON(settings.json_path, function(json){
        
        settings.courses_list = json;
        hero.init(settings);

    });

    jQuery.fn.hero_rotator.defaults = {
        json_path: "courses.json",
        interval: 10000,
        fade_in_time: 300,
        fade_out_time: 300
    };

};