
var Slider = {
    position: 0,
    width: 0,
    imageIndex: 0,
    stopped: false,
    intervalHandle: null,
    conf: {},
    slideElement: null,

    initialize: function(conf) {
        this.conf = conf;

        if (conf.cookie) {
            cookie = (parseInt(this.getCookie('sliderPos'))) ? parseInt(this.getCookie('sliderPos')) : 0;
            if (cookie) {
                this.position = cookie;
            }
        }
        if (conf.rememberStop) {
            this.stopped = (this.getCookie('sliderStopped') == 1) ? true : false;
        }
        
        containerEl = $(conf.containerElement);

        containerEl.setStyle({ whiteSpace: 'nowrap' });

        this.slideElement = $(conf.slideElement);
        this.width = this.slideElement.clientWidth;
        if (this.conf.width != undefined) {
            this.width = this.conf.width;
        }

        var html = $(this.slideElement).innerHTML.strip();
        this.slideElement.update(html + html + html);

        this.move(true);
        containerEl.setStyle({ visibility: 'visible' });

        if (!this.stopped) {
            window.setTimeout(function() { Slider.start(); }, conf.delay);
        }
    },

    start: function() {
        if (this.intervalHandle) {
            window.clearInterval(this.intervalHandle);
        }
        this.intervalHandle = window.setInterval(function() { Slider.move(); }, this.conf.interval);

        this.stopped = false;
        if (this.conf.rememberStop) {
            this.setCookie('sliderStopped', (this.stopped) ? 1 : 0, 100);
        }
    },

    stop: function() {
        if (this.intervalHandle) {
            window.clearInterval(this.intervalHandle);
        }

        this.stopped = true;
        if (this.conf.rememberStop) {
            this.setCookie('sliderStopped', (this.stopped) ? 1 : 0, 100);
        }
    },

    switchMove: function() {
        if (this.stopped) {
            this.start();
        } else {
            this.stop();
        }
    },

    move: function(noMove) {
        if (!noMove || noMove == false) {
            this.position += this.conf.step;
        }
        if (this.position > this.width) {
            this.position = 1;
        }
        if (this.position < 0) {
            this.position = this.width - 1;
        }
        this.slideElement.style.marginLeft = (this.position - this.width) + 'px';

        if (this.conf.cookie) {
            this.setCookie('sliderPos', this.position, 100);
        }
    },

    setCookie: function(sName, sValue, nDays) {
        var expires = "";
        if (nDays) {
            var d = new Date();
            d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
            expires = "; expires=" + d.toGMTString();
        }

        document.cookie = sName + "=" + sValue + expires + "; path=/";
    },

    getCookie: function(sName) {
        var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
        var res = re.exec( document.cookie );
        return res != null ? res[3] : null;
    }

};

