var carousel = Class.create();
carousel.prototype=  {
    
    container: null,
    original_list_width: null,
    multiplied_list_width: null,
    items: [],
    animating: null,
    last_position: 0,
    slides_no: 0,
    items_visible: 1,
    item_width: 620,
    si: null,
    update_interval: 30,
    carousel_width: 620,
    target_offset: 0,
    current_offset: 0,
    speed: 1,
    move_offset: 100,
    easing: false,
    page_type: null,

    initialize: function(container_id, page_type) {
        
        this.page_type = page_type;

        this.container = $(container_id);
        if(!this.container) return;
        this.container_id = container_id;
        
        $(this.container).addClassName('loaded');
        
        this.container.addClassName('loaded');
        this.items = $(this.container).select('.product-category');
        this.items[0].addClassName('first-child');
        
        this.slides_no = this.items.length;
        
        if(page_type == 'home') {
            this.carousel_width = 620;
            this.item_width = 620;
        } else if(page_type == 'category') {
            this.carousel_width = 540;
            this.item_width = 540;
        }
        
        var wrap_element = $('categories-container').wrap();
        wrap_element.setAttribute('id', 'categories-container-wrap');
        
        this.initThumbnails();
        this.initThumbScroller();
        this.setupMinHeight()
        
        //this.photoPosition();
   },
   
   
   photoPosition: function() {
        var photos = $(this.container).select('.photo');
        var il = photos.length;
        
        for(var i = 0; i < il; i++) {
            var current_item = photos[i];
            var current_span = $(photos[i]).select('span')[0];
            
            var diff = $(current_item).getWidth() - $(current_span).getWidth();
            
            current_item.style.width = 184 - Math.floor(diff/2) + 'px';
            current_span.style.marginLeft = Math.floor(diff/2) + 'px';
        }
    
   },
   
   initThumbScroller: function() {
        if(typeof thumbs_scoller == 'function') {
            var all_category_scrollers = $(this.container).select('.other-in-category');
            var il = all_category_scrollers.length;
            for(var i = 0; i < il; i++) {
                thumb_scroller_array[i] = new thumbs_scoller(all_category_scrollers[i], this.page_type, i);
            }    
        }
   },
   
   initThumbnails: function() {
        var thumbs_container = $(this.container).select('.product-category');
        
        var il = thumbs_container.length;
        for(var i = 0; i < il; i++) {
            if(typeof thumbs_handler == 'function') {
                var autoplay = i == 0 ? true : false;
                var max_thumbs = this.page_type == 'home' ? 6 : 5; 
                thumbs_handler_array[i] = new thumbs_handler(thumbs_container[i], autoplay, i, max_thumbs);
            }
        }
   },
   
   setupMinHeight: function() {
    
        if($('page-category-frontpage') && $('page-category-frontpage').hasClassName('section-movies')) {
            var max_height = 235;
        } else {
           var max_height = 225;
        }
        
        var products = $(this.container).select('.product');
        var il = products.length;
        
        for(var i = 0; i < il; i++) {
            var current_item = products[i];
            var current_height = $(current_item).getHeight();
            if(current_height > max_height) {
                max_height = current_height;
            }
        }

        if(navigator.userAgent.indexOf('AppleWebKit/') > -1) {
            //max_height += 20;
        }

        for(var i = 0; i < il; i++) {
            var current_item = products[i];
                current_item.style.height = max_height + 'px';
                current_item.style.minHeight = max_height + 'px';
        }
    
   },
   
   clickListener: function(e) {
        if(window.event) {
            window.event.returnValue = false;
        } else {
            e.preventDefault();
        }

        var event = window.event ? window.event : e;
        var eventTarget = event.srcElement ? event.srcElement :  event.target;
        if(eventTarget.nodeName.toLowerCase() == 'img') {
            eventTarget = eventTarget.parentNode;
        }

        var direction = eventTarget.parentNode.className;
        if(!this.animating) {
            this.prepareScrolling(direction);
        }
   },
   

   prepareScrolling: function(requested_index) {
        var requested_offset = requested_index * this.carousel_width;
        this.target_offset = requested_offset;
        this.initMoving();
   },

   initMoving: function() {
         this.animating = true;
         var o = this;
         this.si = setInterval( function() {o.moveCalculation();}, this.update_interval);
   },
   
   getIndex: function(related_item) {
    
        var position = null;
        var il = this.items.length;
        for(var i = 0; i < il; i++) {
            var current_item = this.items[i];
            if(current_item == related_item) {
                position = i;
                break;
            }
        }
        return position+1;
   },

   moveCalculation: function () {
        var diff = this.target_offset - this.current_offset;

        if(diff > 0) {
            if(!this.easing) {
                var step = this.move_offset;
                if(diff < this.move_offset) {
                    step = diff;
                }
            } else {
                var step = Math.ceil(diff/this.speed);
            }
        } else {
            if(!this.easing) {
                var step = -this.move_offset;
                if(diff > step) {
                    step = diff;
                }
            } else {
                var step = Math.floor(diff/this.speed);
            }
        }

        if(step == 0) {
            clearInterval(this.si);
            this.animating = false;
        }

        this.current_offset += step;
        this.moveItems(step);
   },

   moveItems: function(current_step) {
        var item_to_move = $('categories-container');
        if(item_to_move.style.left) {
            var new_offset = parseInt(item_to_move.style.left) - current_step + 'px';
        } else {
            var new_offset = - current_step + 'px';
        }
        item_to_move.style.left = new_offset;
        item_to_move.style.position = 'relative';
   }
       

}