var CKTscroller = new Class({ 
    Implements: [Options, Events],
    options: {
        contentdiv_id:'content',
        wrapperdiv_id:'slider',
        direction:'vertical',
        type:'slider' 
    },
    initialize: function(options){
        this.setOptions(options);
        this.contentdiv = document.id(this.options.contentdiv_id); 
        this.wrapperdiv=document.id(this.options.wrapperdiv_id); 
        this.scrollerdirection=this.options.direction;
        this.scrollertype=this.options.type;
        if (!this.contentdiv) {
            return;
        }
        this.divsize = this.contentdiv.getSize();
        this.divscrollsize= this.contentdiv.getScrollSize();
        this.divscrollsizedim = (this.scrollerdirection=='vertical')?(this.divscrollsize.y):(this.divscrollsize.x);
        this.divsizedim = (this.scrollerdirection=='vertical')?(this.divsize.y):(this.divsize.x);
        this.scrollpotion = (this.divscrollsizedim-this.divsizedim);
        if (this.scrollpotion<10) {
            return;
        }           
        this.curstep=0;
        this.contentdiv.setStyle('overflow', 'hidden');
        (this.wrapperdiv.getParent()).setStyle('display', 'block');
        if (this.scrollertype=='slider') {
            this.makemyslider();
            this.makemyscrollerbuttons();
        } else {
            this.makemybuttons();
        }
        this.calcwheel();
        if ($('guestnext')) {            
            this.movetoitem();
        }
        $(document.body).addEvent('mouseleave',function(){
            this.mySlider.drag.stop()
        }.bind(this));
    },    
    makemyslider: function(){
        this.mySlider = new Slider(this.wrapperdiv, this.wrapperdiv.getElement('.knob'), {
            mode: this.scrollerdirection,
            wheel: true,
            snap: true,
            steps: (this.scrollpotion+50),	
            range: [0,(this.scrollpotion+50)],	
            onChange: function(step){
                this.curstep = step;
                if (this.scrollerdirection=='vertical') {
                    this.contentdiv.scrollTo(0,this.curstep);
                }
                else {
                    this.contentdiv.scrollTo(this.curstep,0);
                }
            }.bind(this)
        });        
    },
    makemybuttons: function(){
        if (this.scrollerdirection=='vertical') {
            this.uparrow=this.wrapperdiv.getElement('.uparrow');
            this.downarrow=this.wrapperdiv.getElement('.downarrow');
            this.uparrow.addEvents({
                mousedown: function() {
                    this.upfx = new Fx.Scroll(this.contentdiv).start(0, 0);
                },
                mouseup: function() {
                    this.upfx.cancel();  
                }
            });
            this.downarrow.addEvents({
                mousedown: function() {
                    this.downfx = new Fx.Scroll(this.contentdiv).start(0, this.scrollpotion);
                },
                mouseup: function() {
                    this.downfx.cancel();
                }
            });
        } else {
            this.leftarrow=this.wrapperdiv.getElement('.leftarrow')
            this.rightarrow=this.wrapperdiv.getElement('.rightarrow')
            this.leftarrow.addEvents({
                mousedown: function() {
                    this.leftfx = new Fx.Scroll(this.contentdiv).start(0, 0);
                },
                mouseup: function() {
                    this.leftfx.cancel();  
                }
            });
            this.rightarrow.addEvents({
                mousedown: function() {
                    this.rightfx = new Fx.Scroll(this.contentdiv).start(this.scrollpotion, 0);
                },
                mouseup: function() {
                    this.rightfx.cancel();
                }
            });
        }
    },    
    movemycontent:function(){
        if (this.scrollertype=='slider') {
            this.mySlider.set(this.curstep);
        } 
        else {
            if (this.scrollerdirection=='vertical') {
                this.contentdiv.scrollTo(0,this.curstep);
            }
            else {       
                this.contentdiv.scrollTo(this.curstep,0);
            }
        }
    },    
    makemyscrollerbuttons: function(){
        if (this.scrollerdirection=='vertical') {
            this.uparrow = this.wrapperdiv.getSiblings('.uparrow');
            this.downarrow =this.wrapperdiv.getSiblings('.downarrow');
            this.uparrow.addEvents({
                click: function() {
                    if (this.curstep>0) {
                        this.curstep-=30;
                    }
                    this.movemycontent(this.curstep);
                }.bind(this)
            });
            this.downarrow.addEvents({
                click: function() {
                    if (this.curstep<(this.scrollpotion+50)) {
                        this.curstep+=30;
                    }
                    this.movemycontent(this.curstep);
                }.bind(this)
            });
        }
    },
    calcwheel: function (){
        this.contentdiv.addEvents({
            mousewheel: function(event) {
                event.preventDefault();
                if (event.wheel > 0) {
                    if (this.curstep>0) {
                        this.curstep-=30;
                    }
                    this.movemycontent(this.curstep);
                }
                /* Mousewheel DOWN*/
                else if (event.wheel < 0) {
                    if (this.curstep<(this.scrollpotion+50)) {
                        this.curstep+=30;
                    }
                    this.movemycontent(this.curstep);
                }
            }.bind(this)
        });        
    },
    movetoitem: function (){
        this.theliitems=$$('#content li');
        this.ov=[];
        this.curnumber=0;
        this.maxidnex=0;
        this.theliitems.each(
            function(item, index){
                this.ov[index]=item.getPosition(this.contentdiv).y;
                this.maxindex=index;
            }.bind(this)
            );
        $('guestnext').addEvents({
            click: function() {
                if (this.curstep<this.ov[this.curnumber]) {
                    while (this.curstep<this.ov[this.curnumber]) {
                        this.curnumber--;
                    }
                }
                else {
                    while (this.curstep>this.ov[this.curnumber]) {
                        this.curnumber++;
                    }
                }
                if (this.curnumber<(this.maxindex-1)) {
                    this.curnumber++;
                }
                this.curstep = (this.ov[this.curnumber]);
                this.movemycontent();
            }.bind(this)
        });
        $('guestprev').addEvents({
            click: function() {
                if (this.curstep<this.ov[this.curnumber]) {
                    while (this.curstep<this.ov[this.curnumber]) {
                        this.curnumber--;
                    }
                }
                else {
                    while (this.curstep>this.ov[this.curnumber]) {
                        this.curnumber++;
                    }
                }
                if (this.curnumber>0) {
                    this.curnumber--;
                }
                this.curstep = (this.ov[this.curnumber]);
                this.movemycontent();
            }.bind(this)        
        });
    }
});
