//4954882
var $_MARGIN = 30; // заданная дизайном константа xD
var $_SCROLLER_WIDTH=140;
var $SCROLL={
    _container:null,
    _scroll_left:null,
    _scroll_right:null,
    position:0,
    _count:0,
    width:4,
    recalc:function(){
        var self=this;
        var el=$$('#ja-slider-center div.vm_element')[0];
        // бляццкий ие, здесь будет ручное вычисление ширины
        this._container.style.width=(document.body.offsetWidth-$_SCROLLER_WIDTH*2)+"px";
        var width_cont_px=self._container.getSize().size.x;
        var num=parseInt(width_cont_px/self.div_width,10);
        self.width=num;
        self._container.style.width=(num*self.div_width)+"px";
    },
    initCss:function(){
        this._cnt=$$('#ja-slider-cont')[0];
        this._container=$$('#ja-slider-center')[0];
        this._cnt.style.display="block";
        this._container.style.display="block";

        var _el_size=$$('#ja-slider-center div.vm_element')[0].getSize();
        this._viewport=$$('#ja-slider-viewport')[0];
        this.div_width=_el_size.size.x+$_MARGIN*2;
        this._viewport.style.width=(this.div_width*this.total_count)+"px"
        this._container.style.overflow="hidden";
        this._container.style.overflowX="hidden";
        // this._container.style.border="1px solid red";
        this._container.style.height=(_el_size.size.y)+"px";

        var _inc=function(el,val){
            el.style.opacity=val;
            window.setTimeout(function(){

                    _inc(el,val+0.05);
                },50);
        }
        _inc(this._cnt,0);
    },
    init:function(){
        var self=this;
        $$('#ja-slider-left')[0].onclick=function(){
            self.move(-1);
            return false;
        }
        $$('#ja-slider-right')[0].onclick=function(){
            self.move(1);
            return false;
        }
        this.position=0;
        this.total_count=$$('#ja-slider-center div.vm_element').length;
        this.initCss();
        self.recalc();
        self.move(-1);
    },
    move:function(direction){
        direction=direction<0?-1:1;
        var new_pos=this.position + (direction* this.width);
        if(new_pos < 0){
            new_pos=0;
        }else if(new_pos>(this.total_count - this.width)){
            new_pos=this.total_count - this.width;
        }
        $$("#ja-slider-left")[0].className=(new_pos==0)?"inactiv":"";
        $$("#ja-slider-right")[0].className=(new_pos>=(this.total_count-this.width))?"inactiv":"";
        $$("#ja-slider-left a")[0].className=(new_pos==0)?"inactiv":"";
        $$("#ja-slider-right a")[0].className=(new_pos>=(this.total_count-this.width))?"inactiv":"";
        this.position=new_pos;
        this._moveto();
    },
    _moveto:function(){
        var new_item=$$("#ja-slider-center div.vm_element")[this.position];
        var rel_pos=this._container.getPosition();
        var new_pos=new_item.getPosition();
        // console.log(new_pos, $_MARGIN);
        //        alert(new_size);
        new Fx.Scroll('ja-slider-center').scrollTo((new_pos.x-rel_pos.x)-$_MARGIN,0);//toElement(new_item);
    },
    onResize:function(ev){
        this.recalc();
        //        this._moveto();
    },
    __pad__:false
};

window.onload=function(){
    $SCROLL.init();
}
window.onresize=function(){
    $SCROLL.onResize();
}

