
/*
    Flowen
    Javascript Object Library
    Slider
*/

var CXSlider = new Class({
    initialize: function(slobj) {

        var _active = false;
        var _prevIndex = 0;        
        var _selectedIndex = 0;
        var _slides = new Array();
        var _slides_xpos = new Array();
        var _slides_ypos = new Array();
        var _timer = null;
        var _running = false;

        // Public Properties
        this.SelectedIndex = _selectedIndex;
        this.SlideCount = _slides.length;
        this.Direction = "horizontal";
        this.Interval = 1000;
        
        // Public Methods
        this.AddSlide = function (obj) {
            _slides[_slides.length] = obj;
        }
        
        this.Init = function()
        {
            for(var i = 0; i < _slides.length; i++)
            {
                _slides_xpos[i] = $(_slides[i]).getPosition(slobj).x;
                _slides_ypos[i] = $(_slides[i]).getPosition(slobj).y;
            }
            _active = true;
            this.GoToSelected();
        }

        this.Next = function () {
            if(!_active) return;
            if(_selectedIndex<_slides.length - 1)
            {
                _selectedIndex++;
                this.GoToSelected();
            } else {
                this.Stop();
            }
        }

        this.Previous = function () {
            if(!_active) return;
            if(_selectedIndex > 0)
            {
                _selectedIndex--;
                this.GoToSelected();
            }
        }

        this.GoToSelected = function () {
            if(!_active) return;
            switch(this.Direction)
            {            
                case "horizontal":
                    var _startx = $(slobj).getPosition().x;
                    var _endx = (-1)*(_slides_xpos[_selectedIndex]);
                    $(slobj).set('tween', {duration: 1000, transition:'linear'});
                    $(slobj).tween('left', _endx + 'px');
                    break;
                case "vertical":
                    $(slobj).tween('top', 
                        $(slobj).getPosition().y + 'px', 
                        (-1)*(_slides_ypos[_selectedIndex]) + 'px');
                    break;
            }
        }

        this.GoToNumber = function (slideIndex) {
            if(!_active) return;
            _prevIndex = _selectedIndex;
            _selectedIndex = slideIndex;
             this.GoToSelected();
        }
        
        this.Play = function () {
            if(!_active) return;
            _running = true;
            if(_timer == null)
                _timer = this.Next().delay(this.Interval);
        }
        
        this.Stop = function () {
            if(!_active) return;
            _running = false;
            if(_timer)
            {
                window.clearInterval(_timer);
                _timer = null;
                this.GoToNumber(0);
            }
        }
        
        this.Pause = function () {
            if(!_active) return;
            _running = false;
            if(_timer)
            {
                window.clearInterval(_timer);
                _timer = null;
            }
        }
    }
});
