jQuery.fn.pbHero = function(options){
    return this.each(function(){
        var defaults = {
                hero : $(this),
                feats : $(this).children('ul').children('li'),
                nav : $('#hero_nav')
            },
            $hero,
            $feats,
            $nav,
            length,
            pos = 0,
            r;

        options = $.extend(defaults,options);

        $hero = options.hero;
        $feats = options.feats;
        $nav = options.nav;
        length = $feats.length;

        $feats.hide();
        $feats.css({position:'absolute',top:'0',left:'0'});

        $feats.each(function(i){
            return new Feature($(this),i);
        });

	    function Feature($el,position){
            var thmb = $el.find('img').clone();
            this.$cont = $el;
            this.$link = $('<a href="#"></a>');

            this.$link.append(thmb);
            $nav.append(this.$link);

            this.$link.click(function(){
                if(pos != position){
                    rotate(position);
                }
                return false;
            });
	    }

	    function rotate(x){
			var old = pos;

			//Advance the counter or set pos to a new position
			pos = (x != null) ? x : pos + 1;

			if(pos > length - 1){
				pos = 0;
			}

			//Hide feats
			if(old > -1){
				$feats.eq(old).fadeOut('slow');
			}
				//Show the current one
			$feats.eq(pos).fadeIn('slow');
			startTimer(7000);
	    }

	    function stop(){
            clearTimeout(r);
	    }

	    function startTimer(time){
            clearTimeout(r);
            r = setTimeout(function(){
                rotate();
            },time);
	    }

	    $('#feature').hover(function(){
            stop();
	    },
	    function(){
            startTimer(1000);
	    });

	    rotate(0);
    });
};
