jQuery.fn.flide = function(options) {
  
  var defaults = {
    slide_time      : 400,
    ease_type       : 'easeInOutExpo',
    flide_width     : 648,
    auto_play       : true,
    slide_interval  : 5000
  };
  
  var settings = $.extend({}, defaults, options);
  
  return this.each(function(){
    
    // From the jquery.easing plugin
    // http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
    $.fn.extend( $.easing, {
      easeInOutExpo: function (x, t, b, c, d) {
        if (t===0) return b;
        if (t===d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
      }
    });
    
    var container = $(this);
    var li_length = container.find('li').length;
    var flide_w = settings.flide_width * li_length;
    var current_slide = 0;
    var append_pages = true;
    
    container.wrap('<div class="flide_wrapper">').find('ul').css('width', flide_w).end().after('<div class="flide_nav"><ul></ul></div>').find('li').each(function(i) {
      
      // Make sure that pagination doesn't run more than once
      append_pages ? $('.flide_nav ul').append('<li><a href="#">'+ (i+1) +'</a></li>') : false;
      (i+1) === li_length ? append_pages = false : false;
      
    });
    
    
    // Hide nav if its only one slide
    li_length  === 1 ? $('.flide_nav').hide() : false;
    
    // Adjust heading if it wraps
    try {
      owc_heading_adjuster.init();
    }catch(ex){}
    
    $('.flide_nav ul li:first').addClass('active');
    
    $('.flide_nav li').live('click', function(){
      $('.flide_nav li').removeClass('active');
      $(this).addClass('active');
      current_slide = $(this).index();
      container.find('ul').animate({
        left: -(settings.flide_width*current_slide)
      }, {
        'duration': settings.slide_time,
        'queue'   : false,
        'easing'  : settings.ease_type
      });
      change_headline();
      return false;
    });
    
    $('.flide_nav li a').live('click', function(){
      remove_countdown();
    });
    
    function next_slide(){
      if(current_slide+1 == li_length){
        $('.flide_nav ul li:eq(0)').trigger('click');
      }else{
        $('.flide_nav ul li:eq('+(current_slide+1)+')').trigger('click');
      }
    }
    
    function prev_slide(){
      if(current_slide === 0){
        $('.flide_nav ul li:eq('+(li_length-1)+')').trigger('click');      
      }else{
        $('.flide_nav ul li:eq('+(current_slide-1)+')').trigger('click');
      }
    }
    
    function change_headline(){
      container.parent().siblings('h1').html( container.find('li').eq(current_slide).find('.headline').val() );
    }
    
    $('.flide li').click(function(){
      if( $(this).children('#map').length > 0 ){
        return false;
      }
      next_slide();
      if( settings.auto_play ){
        remove_countdown();
      }
    });
    
    function check_key(e){
      switch (e.keyCode) {
        case 37: prev_slide(); remove_countdown(); break; // Left
        case 39: next_slide(); remove_countdown(); break; // Right
      }      
    }
    
    $(document).keydown(check_key);
    
    if( settings.auto_play ){
      
      var auto_slide = window.setInterval(function(){
        next_slide();
        change_headline();
      }, settings.slide_interval);
      
    }
    
    function remove_countdown(){
      clearInterval(auto_slide);
      auto_slide = false;
    }
    
  });
};
