/**
* @depend ../jsclass/core.js
* @depend ../innershiv.min.js
* @depend display.js
* @depend scroller.js
* @depend navigation.js
*/
var ShinyScroll;
(function($){
  ShinyScroll= new JS.Class({

    initialize: function(conf) {
      if(conf.total_slices > 1) {
        this.slices_container= $(conf.first_slice)
        var total_width= conf.slice_width * conf.total_slices + conf.margin_between_slices * (conf.total_slices - 1)
        this.slices_container.width(total_width).addClass('ss-scrolled')
        this.slices_container.find('> div').addClass('ss-slice')
        this.ajax_source= conf.ajax_source
        this.slice_width= conf.slice_width
        this.margin_between_slices= conf.margin_between_slices
        this.footer_nav= conf.footer_nav
        this.current_slice= 0;
        this.loaded_slices= [true, true];
        this.total_slices= conf.total_slices;
        this.conf= conf;

        this.create_mask()
        this.create_wrapper()
        this.create_nav(conf.total_slices, conf.track_width)
      }
    },

    create_mask: function() {
      this.slices_container.wrap('<div class="ss-mask"/>')
      this.mask= this.slices_container.parent()
      this.mask.css({ overflow: 'hidden' })
    },

    create_wrapper: function() {
      this.mask.wrap('<div class="shiny-scroll ss-wrapper"/>')
      this.wrapper= this.mask.parent()
    },

    create_nav: function(total_slices, track_width) {
      this.nav= new Navigation(this, total_slices, track_width)
      this.nav.prev.bind('prev_slice', this.prev_slice)
      this.nav.next.bind('next_slice', this.next_slice)
      this.wrapper.prepend(this.nav.html)

      if(this.footer_nav) {
        this.footer_nav= new Navigation(this, total_slices, track_width)
        this.footer_nav.prev.bind('prev_slice', this.prev_slice)
        this.footer_nav.next.bind('next_slice', this.next_slice)
        this.wrapper.append(this.footer_nav.html)
      }
    },

    prev_slice: function(event) {
      that=  $(this).data('this');
      var slices_container= that.slices_container;
      var x= slices_container.css('margin-left').to_i()
      var target_x= x + that.slice_width + that.margin_between_slices;

      if(target_x <= 0) { // if still can go to left
        slices_container.animate({marginLeft:  target_x + "px"}, function(){
          that.nav.prev.enable('prev_slice', that.prev_slice)
          if(that.footer_nav)
            that.footer_nav.prev.enable('prev_slice', that.prev_slice)
        })

        that.nav.prev.disable('prev_slice')
        that.nav.trigger('prev_slice')
        that.nav.display.prev_slice()
        if(that.footer_nav) {
          that.footer_nav.prev.disable('prev_slice')
          that.footer_nav.trigger('prev_slice')
          that.footer_nav.display.prev_slice()
        }

        that.current_slice -= 1;
      }
      return false
    },

    next_slice: function(event) {

      that=  $(this).data('this');
      var slice_width = that.slice_width * -1;
      var slices_container= that.slices_container;
      var x= slices_container.css('margin-left').to_i()
      var target_x= x + slice_width - that.margin_between_slices;

      if(target_x > slices_container.width() * -1) { // if still can go to right

        slices_container.animate({marginLeft:  target_x + "px"}, function(){
          that.nav.next.enable('next_slice', that.next_slice)
          if(that.footer_nav)
            that.footer_nav.next.enable('next_slice', that.next_slice)
        })

        that.nav.next.disable('next_slice')
        that.nav.trigger('next_slice')
        that.nav.display.next_slice()
        if(that.footer_nav) {
          that.footer_nav.next.disable('next_slice')
          that.footer_nav.trigger('next_slice')
          that.footer_nav.display.next_slice()
        }

        that.current_slice += 1;
        var next= that.current_slice + 1;

        if (!that.loaded_slices[next] && next != that.total_slices && that.conf.ajax)
          that.load_slice(next);

      }

      return false
    },

    load_slice: function( slice_num ) {
      var slice= $('<div/>').attr('class', this.slices_container.find(':first').attr('class'));
      var that= this;
      $.get(this.conf.ajax_page, {
        'category_name': this.conf.category_name,
        'page': slice_num + 1,
        'posts_per_page': this.conf.posts_per_page,
        'blog': this.conf.blog
      }, function(data) {
         $(data).addClass('ss-slice').appendTo(that.slices_container)
         that.loaded_slices[slice_num]= true;
      });
    }

  });


  Number.prototype.nice= function() {
    return this < 10 ? '0'+this : this;
  };

  function to_i(obj) {
   var obj= this || obj;
   return parseInt(obj, 10);
  }

  String.prototype.to_i= to_i;
  Number.prototype.to_i= to_i;

  String.prototype.to_f= function() {
   return parseFloat(this);
  };
}(jQuery));
