(function($){
  
  var ICastSliderBox = function(element, settings) {
        
    this.options = $.extend({}, $.fn.icastSliderBox.defaults, settings);
        
    this.vars = {
      currentSlide: 0,
      totalSlides: 0,
      firstShownSlide: 0,
      lastShownSlide: 2,
      animationInProgress: false
    }
    
    var that = this;
    
    this.sb = $(element);
    this.sd = this.sb.find('.slide_desc_wrap .slide_desc');
    this.smain = this.sb.find('.slide_wrap');
    this.slides = this.smain.find('.slides');
    this.sthumbs = this.sb.find('.slide_thumbs_wrap .slide_thumbs');
    
    this.vars.totalSlides = this.slides.find('.slide').size();
    
    this.sb
    .data('icastSliderBox:vars', this.vars).addClass('icast_sliderbox');
    
    this.smain.css({ width: this.options.mainWidth, height: this.options.mainHeight });
    this.sb.find('.slide_mask').css({ width: this.options.mainWidth, height: this.options.mainHeight });
    
    this.slides.css({ width: this.vars.totalSlides * this.options.mainWidth, left: 0 });
    
    this.sthumbs.parent().css({ width: this.options.thumbWidth, height: this.options.mainHeight });
    
    this.sthumbs.css({ 'width': this.options.thumbWidth });
    
    this.sd
    .html(this.sthumbs.find('.slide_thumb .slide_hidden_desc').first().html())
    .parent().css({'opacity': '0.8', 'width': this.options.mainWidth}).animate({ height: 80 });
    
    this.sb.find('.slide_thumb').click(function() {
      if ($(this).hasClass('active') || that.vars.animationInProgress) {
        return false;
      }
      
      that.vars.animationInProgress = true;
      
      $nr = $(this).attr('data-nr');
      
      that.sb.find('.slide_thumb').removeClass('active');
      $(this).addClass('active');
      
      that.sd.parent().animate({ height: 0 }, null, function() {
        that.sb.find('.slides').first().animate({
          left: -$nr * that.options.mainWidth
        }, null, function() {
          that.sd
          .html(that.sthumbs.find('.active .slide_hidden_desc').first().html())
          .parent().animate({ height: 80 });
        });
      });
      
      
      if ($nr != 0 && $nr == that.vars.firstShownSlide) {
        that.vars.firstShownSlide--;
        that.vars.lastShownSlide--;
        that.sthumbs.animate({ top: -that.vars.firstShownSlide * that.options.thumbHeight });
      }
      
      if ($nr != that.vars.totalSlides-1 && $nr == that.vars.lastShownSlide) {
        that.vars.firstShownSlide++;
        that.vars.lastShownSlide++;
        that.sthumbs.animate({ top: -that.vars.firstShownSlide * that.options.thumbHeight });
      }
      
      that.vars.animationInProgress = false;
      
      return false;
    });
    
    this.sd.parent().click(function() {
      location = $(this).find('a.readmore').attr('href');
    });
    
  }
  
  
  $.fn.icastSliderBox = function(options) {
    return this.each(function(key, value){
      if ($(this).data('icastSliderBox')) return $(this).data('icastSliderBox');
      $(this).data('icastSliderBox', new ICastSliderBox(this, options));
    });
  };
  
  //Default settings
  $.fn.icastSliderBox.defaults = {
    mainWidth: 660,
    mainHeight: 300,
    thumbWidth: 276,
    thumbHeight: 100
  };
  
})(jQuery);
