Function.prototype.bind = function() {
  var fn = this, args = jQuery.makeArray(arguments), scope = args.shift();
  return function() {
    return fn.apply(scope, args);
  }
}

var Slider = {
  
  links: [],
  posts: [],
  current: -1,
  options: { duration: 5000, autoStart: true },
	
  init: function(posts, links, options) {
  
		this.links = jQuery(links); //  $('div.featured_nav ul li')
		this.posts = jQuery(posts);
		this.options = jQuery.extend(this.options, options || {});
		
		var ref = this;
		this.links.each(function(index) {
      jQuery(this).children('a').click(ref.show.bind(ref, index));
		});

		this.show(0, false);

		if(this.options.autoStart) {
  		this.timer = setInterval(this.next.bind(this), this.options.duration);
		}

  },
  
  show: function(index, pause) {
     if(this.current == index) return;

     this.hideCurrent();
    
    this.current = index;
    var link = jQuery(this.links[this.current]);
    var post = jQuery(this.posts[this.current]);
  
    link.addClass('active af');
    post.fadeIn('slow');
	
  	if(pause != false) {
			clearTimeout(this.timer);
		}
		
  },

  next: function() {
		var nextIndex = parseInt(this.current + 1);
	    
		if(nextIndex >= jQuery(this.posts).length) {
		  nextIndex = 0;
		}
		
		this.show(nextIndex, false);
	},
	
  hideCurrent: function() {

    if(this.current >= 0 && this.current < this.posts.length) {
      var link = jQuery(this.links[this.current]);
      var post = jQuery(this.posts[this.current]);
      
      link.removeClass('active');
      post.fadeOut();
    }
    
  }

};
