(function($){

  $.fn.quickjump = function(settings) {
    
    var config = {
      delay: 5000,
      easing: 'easeOutBack',
      duration: ''
    };
    
    var items = new Array();
    var curr_idx = 0;
    var obj;
    var bt_prev;
    var bt_next;
    var gallery;
    var on_transition = false;
    
    if (settings) $.extend(config, settings);
    
    this.each(function(){
      obj = this;
      
      config.window_width  = ($(this).css('width').replace(/px/,''));
      config.window_height = ($(this).css('height').replace(/px/,''));
      config.item_width = ($($(this).find('.jump_item img')[0]).css('width').replace(/px/,'') );  
      
      $(this).find('.jump_item').width(config.item_width);
      if(config.window_width =='auto')config.window_width = $(this).width();
      if(config.window_height =='auto')config.window_height = $(this).height();      
      

      gallery = $(this).find('.gallery');
      
      // offset the gallery;
      gallery.css({
      'position':'absolute',
      'top':0,
      'left':(config.window_width - config.item_width) /2
      
      });
      
      
      $(this).css('overflow','hidden');  

      // rearange items    
      $(this).find('.gallery .jump_item').each(function(){
        items.push(this);
 
        $(this).css({
          'position':'absolute',
          'top':0 + 'px',
          'left': ((items.length-1) * config.item_width) + 'px' 
          
          });
      });        
      
      // fake items tail
      $(items[0]).clone().appendTo($(this).find('.gallery')).css('left', (items.length * config.item_width) + 'px');
      $((items[1]?items[1]:items[0])).clone().appendTo($(this).find('.gallery')).css('left', ((items.length +1) * config.item_width) + 'px');
      // fake items head
      $(items[items.length-1]).clone().appendTo($(this).find('.gallery')).css('left', -config.item_width);
      $((items[items.length-2]?items[items.length-2]:items[items.length-1])).clone().appendTo($(this).find('.gallery')).css('left', -(2*config.item_width));
      
      // Set navigation position and size
      $(this).find('.prev, .next').css({
 
          'position':'absolute',
          'display':'block',
          'height': config.window_height + 'px',
          'width':((config.window_width - config.item_width)/2),
          'z-index':50      
          });
     
     

      setup_buttons();
     
      
      
      
    });
    
    
    function setup_buttons(){
      
      bt_prev = $(obj).find('.prev');
      bt_next = $(obj).find('.next');
      
      bt_prev.click(prev);
      bt_next.click(next);
      
    }
    
    function prev(){
      if(on_transition)return false;
      
      if(curr_idx == -1){
        curr_idx = items.length-1 ;
      }else{
        curr_idx--;  
      }


      // alert(curr_idx + ' of ' + items.length);
      gallery.children().removeClass('current_item');
      $(gallery.children()[curr_idx]).addClass('current_item') ;
      
      curr_offset = curr_idx * config.item_width ;
      on_transition = true;
       
      if(curr_idx == -1){
        $(gallery.children()[items.length-1]).addClass('current_item') ;
        $(gallery.children()[items.length+2]).addClass('current_item') ;
        
        gallery.animate({'left':-curr_offset + ((config.window_width - config.item_width) /2) },config.duration ,config.easing,function(){
          gallery.css('left', -(config.item_width * (items.length-1)) + ((config.window_width - config.item_width) /2));  
          curr_idx = items.length -1;
          on_transition = false;
        });
        
      }else{
        gallery.animate({'left':-curr_offset + (config.window_width - config.item_width) /2 }, config.duration, config.easing, function(){
          on_transition = false;
        });
      
      }
       
      return false;        
    }
    
    function next(){
      if(on_transition)return false;
            
      if(curr_idx < items.length){
        curr_idx++;
      }else{
        curr_idx = 0;  
      }
      gallery.children().removeClass('current_item');
      $(gallery.children()[curr_idx]).addClass('current_item') ;
      
      
            
      
      on_transition = true;
      //curr_offset = $(items[curr_idx]).css('left').replace(/px/,'');
      curr_offset = curr_idx * config.item_width ;
      if(curr_idx == items.length){
        curr_idx = 0;
        $(gallery.children()[curr_idx]).addClass('current_item') ;
        
        gallery.animate({'left':-curr_offset + (config.window_width - config.item_width) /2 },config.duration,config.easing, function(){
          gallery.css('left',((config.window_width - config.item_width) /2));
          on_transition = false
        });
      }else{
        gallery.animate({'left':-curr_offset + (config.window_width - config.item_width) /2 },config.duration,config.easing,function(){
          on_transition = false;
        });
      }
      return false;        
    }
        
  }
})(jQuery);        
