
/* classes/base_carousel.js */

 
/* classes/base_carousel.js */

 BaseCarousel = Class.create();
Object.extend(BaseCarousel.prototype, {
    initialize: function(carousel_id, params, callbacks) {
        if (typeof carousel_id == 'undefined') return;   //this hack makes object.extend work for inheritance in prototype1.5
        this.carousel_id = carousel_id;
        this.disabled_class = 'js_disabled';
        var config = Object.extend({}, {
            slide_by      : 1,
            window_size   : 1,
            loop          : false,
            duration      : 1,
            add_to_ul_width : 0,
            window_overflow: 0
        });

        this.config = Object.extend(config, params || {});

        this.setCallbacks(callbacks);
    },

    customize: function() {

    },

    toggleControls: function(){
      if (this.dom.previous_button) {
        if(this.last_current_item == this.dom.li_items.length){
          this.dom.next_button.hide();
        } else {
          this.dom.next_button.show();
        }
      }

      if (this.dom.previous_button) {
        if(this.first_current_item== 1){
          this.dom.previous_button.hide();
        } else {
          this.dom.previous_button.show();
        }
      }
    },

    setCallbacks: function(callbacks) {
        var default_callbacks = Object.extend({}, {
            beforeSlide:function() {
            },
            afterSlide:function() {
            }
        });

        this.callbacks = Object.extend(default_callbacks, callbacks);
    },

    setUp: function() {
        var carousel = $(this.carousel_id);
        this.dom = this.setDOM(carousel);
        this.total_items = this.dom.li_items.length;
        this.first_current_item = 1;
        var itemViewCount = this.total_items;
        if (this.total_items > this.config.window_size) {
            itemViewCount = this.config.window_size;
        }
        this.last_current_item = this.first_current_item + itemViewCount - 1;


        this.dimensions = this.setDimensions();
        var scroll = new Scroll(this.first_current_item, this.first_current_item, this.last_current_item, this.total_items, this.config, this.dom, this.callbacks);
        scroll.setPositionInfo();
        this.setupUI();
        this.attachListeners();
        this.customize();
        this.toggleControls();
    },

    itemsSmallerThanWindow: function(total_items) {
        return total_items < this.config.window_size;
    },

    setDOM: function(carousel) {
        return {
            carousel:           carousel,
            window:             carousel.down('.carousel_window'),
            ul_item:            carousel.down('.carousel_items'),
            li_items:           carousel.getElementsBySelector('.carousel_item'),
            next_button:        carousel.down('.carousel_next'),
            previous_button:    carousel.down('.carousel_previous'),
            first_current_item: carousel.down('.carousel_first_current_item'),
            last_current_item:  carousel.down('.carousel_last_current_item'),
            total_items:        carousel.down('.carousel_total'),
            section_items:      carousel.getElementsBySelector('.carousel_section')
        };
    },

    setDimensions: function() {
        dimensions = {};
        dimensions.ul_width = this.calculateWidth(this.total_items, this.config.add_to_ul_width);
        dimensions.window_width = this.calculateWidth(this.config.window_size, this.config.window_overflow);
        return dimensions;
    },

    setupUI: function() {
        this.dom.li_items.first().addClassName('first-child');
        this.dom.li_items.last().addClassName('last-child');
        this.dom.carousel.addClassName('horizontal_carousel');
        this.dom.window.setStyle({
            width:PixelMath.toPx(this.dimensions.window_width)
        });
        this.dom.ul_item.setStyle({
            width:PixelMath.toPx(this.dimensions.ul_width)
        });
    },

    calculateWidth: function(no_of_items, margin) {
        var total_width = 0;
        for (i = 0; i < no_of_items; i++) {
            var element = this.dom.li_items[i];
            if (element) total_width += PixelMath.getCompleteElementWidth(element);
        }

        return total_width + margin;
    },

    next: function() {
        var scroll = new Scroll(this.first_current_item, this.first_current_item, this.last_current_item, this.total_items, this.config, this.dom, this.callbacks);
        items = scroll.next();
        this.first_current_item = items[0];
        this.last_current_item = items[1];
        this.toggleControls();
        return true;
    },

    previous: function() {
        var scroll = new Scroll(this.first_current_item, this.first_current_item, this.last_current_item, this.total_items, this.config, this.dom, this.callbacks);
        items = scroll.previous();
        this.first_current_item = items[0];
        this.last_current_item = items[1];
        this.toggleControls();
        return true;
    },

    attachListeners: function() {
        if (!this.attached) {
            this.attached = true;
            if (this.dom.next_button) {
                this.dom.next_button.observe('click', function(ev) {
                    ev.preventDefault();
                    Event.stop(ev);
                    this.next();
                }.bindAsEventListener(this));
            }
            if (this.dom.previous_button) {
                this.dom.previous_button.observe('click', function(ev) {
                    ev.preventDefault();
                    Event.stop(ev);
                    this.previous();
                }.bindAsEventListener(this));
            }
        }
    },
    register: function(observer) {
        this.observer = observer;
    }
});
