$(document).ready(function(){
        
        /* SCROLL */
        $('.foto-box .in').each(function(){
            var $this = $(this)
            var scroll = {
                el: $('ul', this)
                ,elWidth: function(){return scroll.el.width();}
                ,elScroll: function(){return scroll.el[0].scrollWidth;}
                ,elScrollL: function(){return scroll.el[0].scrollLeft;}
                ,itemWidth: function(){return $('li', scroll.el).width();}
                ,coef: 1
                ,hover: function(e){
                    e = e || window.event;
                    if(e.type == 'mouseenter'){
                        $(e.target).addClass('hover');
                    }
                    else if(e.type == 'mouseleave'){
                        $(e.target).removeClass('hover');
                    }   
                }
                ,controlN: function(){
                      if(left + scroll.elWidth() >= scroll.elScroll()){
                          $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                      }
                      if($('.prev', $this).is('.disable') && left > 0){
                          $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                      }
                }
                ,controlP: function(){
                    if(left <= 0){
                        $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                    }
                    if($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()){
                        $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                    }
                }
                ,next: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left + length;
                    scroll.controlN();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
                ,prev: function(){
                    scroll.el.stop();
                    var length = scroll.itemWidth() * scroll.coef;
                    left = left - length;
                    scroll.controlP();
                    scroll.el.animate({
                        scrollLeft: left +'px'
                    }, 300*scroll.coef);        
                }
            };
            if(scroll.elWidth() < scroll.elScroll() ){
                $this.append('<span class="prev-wrap"><span class="prev"></span></span><span class="next-wrap"><span class="next"></span></span>');
                var left = scroll.elScrollL();
                scroll.el.css('overflow-x', 'hidden');
                scroll.controlN();
                scroll.controlP();
                $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
            }
            else{
                $('.product-list-mini-scroll').addClass('no-scroll').removeClass('product-list-mini-scroll');
            }
        });
});
