(function($){
  
  $.fn.jslide = function(options) {
    
    var arg = arguments;
    
    if(typeof arg[0] != 'object'){
      return this.data('jslide_api')[arg[0]](arg[1]);
    };
    
    var o = $.extend({
      direction: 'horizontal',
      duration: 5000,
      easing: 'swing',
      loop: true,
      page: 0
    }, options);
    
    o.direction = o.direction.toLowerCase();
    
    return this.each(function(){
      
      // ================== Initialization
      var that = $(this),
          page = o.page,
          api = {},
          filmstrip = that.children('ul'),
          items = filmstrip.children('li'),
          blocks = Math.ceil(items.length / o.items)-1,
          fwidth = (o.direction==='horizontal')? (blocks+1) * o.width : o.width,
          fheight = (o.direction==='horizontal')? o.height : (blocks+1) * o.height;
      
      that.css({
        overflow: 'hidden',
        width: o.width,
        height: o.height,
        position: 'relative'
      });
      
      filmstrip.css({
        position: 'absolute',
        top: (o.direction==='horizontal')? 0 : -(page*o.height),
        left: (o.direction==='horizontal')? -(page*o.width) :0,
        listStyle: 'none',
        margin: 0,
        padding: 0,
        display: 'block',
        width: fwidth,
        height: fheight
      });
      
      items.css({float: 'left'});
      
      if(o.loop===false && page===0){
        $(o.back).css({visibility: 'hidden'});
      } else if(o.loop===false && page===blocks) {
        $(o.next).css({visibility: 'hidden'});
      };
      
      // ================== Events
      var anim = function(){
        if(o.transition ==='slide'){
          if(o.direction ==='horizontal'){
            filmstrip.stop(true).animate({
              left: -(page * o.width)
            }, o.duration, o.easing);
          } else {
            filmstrip.stop(true).animate({
              top: -(page * o.height)
            }, o.duration, o.easing);
          };
        } else if(o.transition==='fade') {
          if(o.direction==='horizontal'){
            filmstrip.stop(true).animate({
              opacity: 0,
            }, o.duration/2, o.easing, function(){
              filmstrip
              .css({left: -(page*o.width)})
              .animate({opacity: 1}, o.duration/2, o.easing);
            });
          } else {
            filmstrip.stop(true).animate({
              opacity: 0,
            }, o.duration/2, o.easing, function(){
              filmstrip
              .css({top: -(page*o.height)})
              .animate({opacity: 1}, o.duration/2, o.easing);
            });
          };
        };
      };
      
      var backfn = function(e){
        if(page > 0){
          page--;
          if(o.loop===false && page===0){
            $(o.back).css({visibility: 'hidden'});
          };
        } else {
          if(o.loop===true){
            page = blocks;
          } else {
            page = 0;
          };
        };
        
        if(o.loop===false){
          $(o.next).css({visibility: 'visible'});
        };
        
        anim();
        if(e){
          e.preventDefault();
        };
      };
      
      var nextfn = function(e){
		console.log('click');
        if(page < blocks){
          page++;
          if(o.loop===false && page===blocks){
            $(o.next).css({visibility: 'hidden'});
          };
        } else {
          if(o.loop===true){
            page = 0;
          } else {
            page = blocks;
          };
        };
        
        if(o.loop===false){
          $(o.back).css({visibility: 'visible'});
        };
        
        anim();
        if(e){
          e.preventDefault();
        };
      };
      
      $(o.back).bind('click', backfn);
      $(o.next).bind('click', nextfn);
      
      // ================== Slideshow
      
      if(o.slideshow){
        var ss = {};
        ss.o = o.slideshow;
        
        ss.start = function(){
          ss.c = 0;
          ss.ticks = o.duration + ss.o.speed;
        
          ss.interval = setInterval(function(){
            ss.c += ss.ticks;
          
            if(ss.o.direction === 'next'){
              nextfn();
            } else {
              backfn();
            };
          
            if(ss.c > ss.o.duration && ss.o.duration !== 0){
              clearInterval(ss.interval);
              delete ss.interval;
            };
          }, ss.ticks);
        };
        
        ss.stop = function(){
          clearInterval(ss.interval);
          delete ss.interval;
        };
        
        if(ss.o.autoplay===true){
          ss.start();
        };
      };
      
            
      // ================== Public API
      api.back = backfn;
      api.next = nextfn;
      
      api.get = function(){
        return {
          page: page,
          max: blocks,
          options: o,
          fwidth: fwidth,
          fheight: fheight
        };
      };
      
      api.go = function(p){
        if(p > page){
          page = p - 1;
          nextfn();
        } else if(p < page){
          page = p + 1;
          backfn();
        };
      };
      
      if(o.slideshow){
        api.start = ss.start;
        api.stop = ss.stop;
      };
      
      that.data('jslide_api', api);
      
      
    });
    
  };
  
})(jQuery);
