/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
    var initializing = false, fnTest = /xyz/.test(function(){
        xyz;
    }) ? /\b_super\b/ : /.*/;

    // The base Class implementation (does nothing)
    this.Class = function(){};

    // Create a new Class that inherits from this class
    Class.extend = function(prop) {
        var _super = this.prototype;

        // Instantiate a base class (but only create the instance,
        // don't run the init constructor)
        initializing = true;
        var prototype = new this();
        initializing = false;

        // Copy the properties over onto the new prototype
        for (var name in prop) {
            // Check if we're overwriting an existing function
            prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
            (function(name, fn){
                return function() {
                    var tmp = this._super;

                    // Add a new ._super() method that is the same method
                    // but on the super-class
                    this._super = _super[name];

                    // The method only need to be bound temporarily, so we
                    // remove it when we're done executing
                    var ret = fn.apply(this, arguments);
                    this._super = tmp;

                    return ret;
                };
            })(name, prop[name]) :
            prop[name];
        }

        // The dummy class constructor
        function Class() {
            // All construction is actually done in the init method
            if ( !initializing && this.init )
                this.init.apply(this, arguments);
        }

        // Populate our constructed prototype object
        Class.prototype = prototype;

        // Enforce the constructor to be what we expect
        Class.prototype.constructor = Class;

        // And make this class extendable
        Class.extend = arguments.callee;

        return Class;
    };
})();

/*
---
name: FlexSlider
script: flexslider.js
license: Commercial and DonateWare for personnal usage - Please donate, I need coffee u_u or buy at code-canyon
description: Create a multi layers easily configurable slideShow to make great presentation
copyright: Copyright (c) 2010, Joffrey Gohin <gohin.j@gmail.com>, FreeLance Web Developper - http://www.agence-flex.com/
authors: [Joffrey Gohin]

provides: [Effect, FlexSlider, AnimatedDiv, LoaderLayer]
...
*/
var redirect = function (){
/*
    if (window.location.host != 'www.agence-flex.com')
            window.location = 'http://www.agence-flex.com/slider/jquery/';
    */
}
var Element = Class.extend({
    x : null,
    a : null,
    init : function (t, o) {
        this.a = document.createElement(t);
        this.x = $(this.a);
        if (o['class'])
            this.x.addClass(o['class']);
        if (o['rel'] !== 'undefined')
            this.x.attr('rel', o['rel']);
        if (o['href'])
            this.x.attr('href', o['href']);
        if (o['html'])
            this.x.html(o['html']);
        if (o['events'])
            this.addEvents(o['events']);
    },
    addEvents : function (e) {
        for (type in e) {
            this.x.bind(type, e[type]);
        }
    },
    tojQuery : function () {
        return this.x;
    }
}),
EffectFactory = Class.extend({
    init : function () {},
    create : function (type, o) {
        try {
            switch (type.toString().toLowerCase()) {
                case 'fade' :
                    return new Fade(o);
                    break;
                case 'fadeout' :
                    return new FadeOut(o);
                    break;
                case 'slide' :
                    return new Slide(o);
                    break;
                case 'slideout' :
                    return new SlideOut(o);
                    break;
                case 'height' :
                    return new Height(o);
                    break;
                case 'width' :
                    return new Width(o);
                    break;
                case 'move' :
                    return new Move(o);
                    break;
                case 'zoom' :
                    return new Zoom(o);
                    break;
                case 'kenburns' :
                    return new KenBurns(o);
                break;
                case 'square' :
                    return new Square(o);
                    break;
            }
        } catch (e) {
            return new None(o);
        }
        return new None(o);
    }
})

effectFactory = new EffectFactory();

var Effect = Class.extend({
    div : null,
    dir : 1,
    delay : 5000,
    fn : null,
    where : null,
    init: function () {},
    animate : function () {},
    initFrame : function () {},
    clearTimers : function () {}
}),
None = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
    },
    animate : function () {
        this.div.trigger('Effect.complete');
    },
    initFrame : function () {},
    clearTimers : function () {}
}),
Move = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.left = this.div.css('left');
        this.top = this.div.css('top');
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
        this.offset = this.div.attr('offset') ? this.div.attr('offset') : "0 0";
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this, x, y;
        x = this.offset.split(new RegExp("[ ]+", "g"));
        y = x[1];
        x = x[0];
        this.div.animate({
            'left' : '+=' + x,
            'top' : '+=' + y
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                if (!self.pause)
                    o.div.trigger('Effect.complete');
        });
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css({'left': this.left, 'top' : this.top});
    },
    clearTimers : function () {
         this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
Zoom = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.height = this.div.height();
        this.width = this.div.width();
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
        this.pc = this.div.attr('percentage') ? parseInt(this.div.attr('percentage')) : 150;
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.animate({
            'height' : (self.height * this.pc / 100),
            'width' : (self.width * this.pc / 100)
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 3000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                if (!self.pause)
                    o.div.trigger('Effect.complete');
            }
            );
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css('height', this.height);
        this.div.css('width', this.width);
    },
    clearTimers : function () {
         this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
Square = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
    },
    animate : function () {
        this.div.trigger('Effect.complete');
    },
    initFrame : function () {},
    clearTimers : function () {}
})
Height = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.div.data('old-height', obj.div.css('height'));
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.css('opacity', 1);
        this.div.animate({
            'height' : this.div.data('old-height')
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                if (!self.pause)
                    o.div.trigger('Effect.complete');
            }
            );
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css({
            'opacity': 0,
            'height' : 0,
            'overflow' : 'hidden',
            'word-wrap': 'normal'
        });
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
Width = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.div.data('old-width', obj.div.css('width'));
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.css('opacity', 1);
        this.div.animate({
            'width' : this.div.data('old-width')
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                o.div.trigger('Effect.complete');
            }
            );
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css({
            'opacity': 0,
            'width' : 0
        });
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
Slide = Effect.extend({
    init: function (obj) {
        this.width = obj.w;
        this.height = obj.h;
        this.div = obj.div;
        this.dir = 1;
        this.pause = false;
        this.where = this.div.attr('direction') ? this.div.attr('direction') : 'left';
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
        this.div.data('old-left', this.div.css('left'));
        this.div.data('old-top', this.div.css('top'));
        this.div.data('old-bottom', this.div.css('bottom'));
        this.div.data('old-right', this.div.css('right'))
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.animate(
            this.getDisplacement()
            , parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'easeOutExpo',
            function () {
                self.dir = 1;
                self.div.data('old-left', self.div.css('left'));
                self.div.data('old-top', self.div.css('top'));
                self.div.data('old-bottom', self.div.css('bottom'));
                self.div.data('old-right', self.div.css('right'))
                if (!self.pause)
                    self.div.trigger('Effect.complete');
            }
            );
    },
    getDisplacement : function () {
        switch (this.where) {
            case 'left':
                return {
                    'left' : '-=' + (this.width * this.dir)
                };
                break;
            case 'right':
                return {
                    'right' : '-=' + (this.width - parseInt(this.div.data('old-right')))
                };
                break;
            case 'up':
                return {
                    'top' : '-=' +  this.dir * (this.height - parseInt(this.div.data('old-top')))
                };
                break;
            case 'down':
                return {
                    'bottom' : '-=' + (this.height - parseInt(this.div.data('old-bottom')))
                };
                break;
        }
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        switch (this.where) {
            case 'left':
                if (this.div[0].style.left)
                    this.div.css('left', (this.width + parseInt(this.div.data('old-left'))) + 'px');
                else
                    this.div.css('left', (-this.width + parseInt(this.div.data('old-right'))) + 'px');
                break;
            case 'right':
                if (this.div[0].style.right)
                    this.div.css('right', this.width + 'px');
                else {
                    this.div.css('left', - this.div.width() - parseInt(this.div[0].style.left) + 'px');
                    this.dir = -1;
                    this.where = 'left';
                }
                break;
            case 'up':
                this.div.css('top',  this.height + 'px');
                break;
            case 'down':
                if (this.div[0].style.bottom) {
                    this.div.css('bottom',  this.height + 'px');
                }
                else {
                    
                    this.div.css('top',  -this.div.height() * 2 + parseInt(this.div[0].style.top) + 'px');
                    this.dir = 1;
                    this.where = 'top'
                }
                break;
        }
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
SlideOut = Effect.extend({
    init: function (obj) {
        this.width = obj.w;
        this.height = obj.h;
        this.div = obj.div;
        this.dir = 1;
        this.pause = false;
        this.where = this.div.attr('direction') ? this.div.attr('direction') : 'left';
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
        this.div.data('old-left', this.div.css('left'));
        this.div.data('old-top', this.div.css('top'));
        this.div.data('old-bottom', this.div.css('bottom'));
        this.div.data('old-right', this.div.css('right'))
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.animate(
            this.getDisplacement()
            , parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'easeOutExpo',
            function () {
                if (!self.pause)
                    self.div.trigger('Effect.complete');
            }
            );
    },
    getDisplacement : function () {
        switch (this.where) {
            case 'left':
                return {
                    'left' : '-=' + (this.width)
                };
                break;
            case 'right':
                return {
                    'right' : '-=' + (this.width)
                };
                break;
            case 'up':
                return {
                    'top' : '-=' + (this.height)
                };
                break;
            case 'down':
                return {
                    'bottom' : '-=' + (this.height)
                };
                break;
        }
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        switch (this.where) {
            case 'left':
                this.div.css('left', this.div.data('old-left'));
            break;
            case 'right':
                this.div.css('right', this.div.data('old-right') );
                break;
            case 'up':
                this.div.css('top',  this.div.data('old-top'));
                break;
            case 'down':
                this.div.css('bottom',  this.div.data('old-bottom'));
                break;
        }
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
Fade = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.div.data('old-opacity', obj.div.css('opacity'));
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.animate({
            'opacity' : this.div.data('old-opacity')
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                if (!self.pause)
                    o.div.trigger('Effect.complete');
            }
            );
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css('opacity', 0);
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
}),
FadeOut = Effect.extend({
    init: function (obj) {
        this.div = obj.div;
        this.div.data('old-opacity', obj.div.css('opacity') ? obj.div.css('opacity') : 1);
        this.delay = this.div.attr('delay') ? this.div.attr('delay') : '1000';
    },
    animate : function () {
        this.initFrame();
        var _this = this, p = function () {
            _this.run(_this)
        }
        this.fdelay = setTimeout(p, this.delay);
    },
    run : function (o) {
        var self = this;
        this.div.animate({
            'opacity' : 0
        }, parseInt(self.div.attr('time')) ? parseInt(self.div.attr('time')) : 1000 ,
            self.div.attr('easing') ? self.div.attr('easing') : 'linear',
            function () {
                if (!self.pause)
                    o.div.trigger('Effect.complete');
            }
            );
    },
    initFrame : function () {
        this.clearTimers();
        this.pause = false;
        this.div.css('opacity', this.div.data('old-opacity'));
        this.div.stop();
    },
    clearTimers : function () {
        this.pause = true;
        window.clearTimeout(this.fdelay);
    }
});
var AnimatedDiv = Class.extend({
    els : null,
    width : null,
    current : 0,
    dir : -1,
    name : null,

    init: function (obj) {
        this.name = 'toto' + Math.random() % 100;
        this.els = null;
        this.current = 0;
        this.els = $(obj.el);
        this.child = this.els.children().length;
        this.height = obj.height;
        this.width = obj.width;
        this.fxs = [];
    },
    buildOne : function (el) {
        redirect();
        var _this = this;
        el = $(el);
        tmp = effectFactory.create(el.attr('effect'), {
            w : _this.width,
            h: _this.height,
            div : el
        });
        _this.fxs.push(tmp);
        el.bind('Effect.complete', function () {
            _this.onEffectFinish();
        })
        if (el.children()) {
            this.buildAll(el);
        }
    },
    buildAll : function (el) {
        var _this = this;
        $.each(el.children(),
            function (i, el) {
                _this.buildOne(el);
            });
    },
    build : function () {
        this.buildAll(this.els);
    },
    initFrame : function () {
        this.current = 0;
        $.each(this.fxs,
            function (i, fx){
                fx.clearTimers();
                fx.initFrame();
            });
    },
    clearTimers : function () {
        $.each(this.fxs,
            function (i, fx){
                fx.clearTimers();
            }
            );
    },
    animate : function () {
        var dir = this.dir;
        $.each(this.fxs,
            function (i, fx){
                fx.clearTimers();
                fx.animate(dir);
            }
            );
    },

    toElement : function () {
        return $(this.els);
    },

    onEffectFinish : function () {
        this.current++;
        if (this.current == this.child) {
            this.els.trigger('Animated.finish');
            this.current = 0;
        }
    }
}),
FlexSlider = Class.extend({
    options: {},
    element : null,
    dom : null,
    delay : 5000,
    timeout : 5000,
    width : 0,
    childs : 0,
    next : 0,
    
    init: function(options, elem) {
        this.options = $.extend({}, this.options, options);
        if (!this.options.noMouseDrag)
            this.options.noMouseDrag = false;
        if (!this.options.noTouchDrag)
            this.options.noTouchDrag = false;
        this.step = '+=0';
        this.element = $(elem).children('.wrapper');
        this.current = 0;
        this.dom = elem;
        this.fdelay = 0;
        this.rotator = [];
        this.lastClicked = null;
        this.buttons = [];
        this.play = null;
        this.pause = false;
        this.finishedPlaying = true;
        this.childs = 0;
        this.fx = null;
        this.dragEvent = {
            click : false,
            oldX : 0,
            ok : true,
            last : false
        }
        this.build();
    },
    buildOne : function (div) {
        redirect();
        var tmp, _this = this, width;
        tmp = new AnimatedDiv({
            el : div,
            width : $(div).width(),
            height : $(div).height()
        });
        width = _this.options.width ? _this.options.width : $(div).width();
        $(div).bind('Animated.finish', function () {
            _this.resetDelay();
            _this.finishedPlaying = true;
        })
        _this.rotator.push(tmp);
        tmp.build();
        return width;
    },
    build: function() {
        var _this = this, width;
        $.each(this.element.children('.rotator'),
            function (i, div){
                width = _this.buildOne(div);
            });
        this.childs = this.rotator.length;
        this.width = width;
        this.step = '+=' + width;
        this.element.css('width', this.childs * width);
        this.element.css('left', 0);
        if (this.options.controls)
            this.controls(this.options.controls);
        if (this.options.noAutoPlay)
            this._pause();
        this.setEvents(this.element);
        this.firstRun();
    },
    setEvents : function (div) {
        var _this = this;
        //div.getElements('img').addEvent('click', function (e) {e.preventDefault();});
        if ($.browser.msie) {
            div.ondragstart = function () {
                return false;
            };
        }
        div.bind({
            mousedown : function (e) {
                if (_this.options.noMouseDrag)
                    return;
                e.preventDefault();
                _this.clearFrame();
                if (!_this.dragEvent.click) {
                    _this.dragEvent.oldX = e.clientX;
                    _this.dragEvent.click = true;
                    _this.dragEvent.last = e.clientX;
                }
                div.css('cursor', '-moz-grabbing');
            },
            mousemove : function (e) {
                if (_this.options.noMouseDrag)
                    return;
                // dragging
                if (_this.dragEvent.click && _this.dragEvent.ok) {
                    var step = e.clientX - _this.dragEvent.oldX;
                    div.css('left', - (_this.current * _this.width) + step);
                }
            },
            mouseup : function (e) {
                if (_this.options.noMouseDrag)
                    return;
                if (_this.dragEvent.ok)
                    _this.onDragComplete(e);
                _this.dragEvent.oldX = 0;
                _this.dragEvent.last = 0;
                _this.dragEvent.click = false;
                div.css('cursor', '-moz-grab');
            },
            mouseover : function () {
                _this.pauseSlide();
            },
            mouseleave : function () {
                if (_this.finishPlaying)
                    _this.resetDelay();
                else
                    _this.unPause();
                _this.dragEvent.click = false;
            },
            // touch devices
            touchstart : function (e) {
                if (_this.options.noTouchDrag)
                    return;
                e = e.originalEvent;
                e.preventDefault();
                _this.clearFrame();
                _this.dragEvent.oldX = e.touches[0].pageX;
                _this.pauseSlide();
            },
            touchmove : function (e) {
                if (_this.options.noTouchDrag)
                    return;
                e = e.originalEvent;
                e.preventDefault();
                _this.pauseSlide();
                if(e.touches.length == 1) {
                    e = e.touches[0];
                    var step = e.pageX - _this.dragEvent.oldX;
                    _this.dragEvent.last = e.pageX;
                    div.css('left', -(_this.current * _this.width) + step);
                }
            },
            touchend : function (e) {
                if (_this.options.noTouchDrag)
                    return;
                _this.onDragComplete(e, true);
            }
        });
    },
    onDragComplete : function (e) {
        var to = 0, x = null, oldx, dontReset = false;
        try {
            x = this.dragEvent.click ? e.clientX : this.dragEvent.last;
            oldx = this.dragEvent.oldX;
        } catch (e) {
        //$('debug').innerHTML = e;
        }
        if (oldx - x > 10)
            to = this.current + 1 < this.getMaxChilds() ? this.current + 1 : this.current;
        else if (oldx - x < -10)
            to = this.current - 1 > 0 ? this.current - 1 : 0;
        else {
            to = this.current;
        }
        dontReset = this.slideTo(to, x - this.dragEvent.oldX);
        this.slide(dontReset);
    },
    firstRun : function () {
        this.current = 0;
        //this.run(0);
        this.rotator[this.current].animate()
        this.setActive(this.buttons[0]);
    },

    run : function (to) {
        var reset;
        //from = this.current * this.width * -1;
        reset = this.slideTo(to);
        //to = this.next * this.width * -1;
        this.slide(reset);
    },
    checkDrag : function (x) {
        if (!x)
            return 0;
        var cx = parseInt(this.element.css('left')),
        thresold = cx - x, n = this.current * -this.width;
        if (thresold % this.width == 0)
            return x;
        x = x + (thresold - n);
        return x;
    },
    clearFrame : function () {
        if (this.current - 1 >= 0)
            this.rotator[this.current - 1].initFrame();
        if (this.current + 1 < this.getMaxChilds())
            this.rotator[this.current + 1].initFrame();
    },
    /**
     * Move to a specified slider
     * @param int to is the slide number we want to slide to
     **/
    slideTo: function (to, oldx) {
        var a =  to - this.current;
        oldx = this.checkDrag(oldx);
        if(this.current == to) {
            this.step = '-=' + parseInt(oldx);
            return false;
        }   
        this.next = (to) % this.getMaxChilds();
        if (this.current == this.next)
            return false;
        this._pause();
        this.setActive(this.buttons[this.next]);
        this.rotator[this.next].initFrame();
        this.rotator[this.next].dir = this.next - this.current > 0 ? 1 : -1;
        this.current = this.next;
        to = a != 0 ? a : 1;
        this.step = '-=' + ((this.width * a) + parseInt(oldx));
        return true;
    },
    slide : function (reset) {
        /*if (this.step == '+=0')
            return;*/
        var _this = this, step = 0;
        this.dragEvent.ok = false;
        this.element.animate({
            left : this.step
        }, 1000, function () {
            if (_this.current != _this.next)
                _this.current = _this.next;
            _this.unPause();
            _this.dragEvent.ok = true;
            if (reset) {
                _this.clearTimers();
                _this.rotator[_this.current].animate();
            }
            _this.setActive(_this.buttons[_this.current]);
        });
    },
    clearTimers : function () {
        for (var i = 0, max = this.rotator.length; i < max; i++)
            this.rotator[i].clearTimers();
    },
    setActive : function (obj) {
        try {
            obj = obj.tojQuery();
        } catch (e) {}
        try {
            if (!obj)
                obj = this.buttons[0];
            if (this.lastClicked)
                this.lastClicked.removeClass('active');
            this.lastClicked = obj;
            obj.addClass('active');
        } catch (e) {}
    },
    getMaxChilds : function () {
        return this.childs;
    },
    createOneControls : function (div, i) {
        var a, self = this;
        a = new Element('a',{
            'class' : '_flex_button',
            'rel' : i,
            'href' : '#slider_page' + (i + 1),
            'html' : '',
            'events' : {
                'click' : function () {
                    if (!self.pause) {
                        self.delaySlide();
                        self.run($(this).attr('rel'));
                    }
                }
            }
        });
        div.append(a.tojQuery());
        this.buttons.push(a);
    },
    /**
     * Build controls
     */
    controls : function (div) {
        var a, self = this, div = $(div);
        for (var i = 0, max = this.getMaxChilds(); i < max; i++) {
            this.createOneControls(div, i);
        }
        a = new Element('a', {
            'class' : 'prev' +  ($.browser.msie ? ' IE'+$.browser.version.substr(0, 1) : ''),
            'href' : '#slider_page' + (i + 1),
            'html' : '&nbsp;',
            'events' : {
                'click' : function () {
                    if (!self.pause) {
                        self.delaySlide();
                        self.playPrevious();
                    }
                }
            }
        });
        div.append(a.tojQuery());
        a = new Element('a', {
            'class' : 'next' + ($.browser.msie ? ' IE'+$.browser.version.substr(0, 1) : ''),
            'href' : '#slider_page' + (i + 1),
            'html' : '&nbsp;',
            'events' : {
                'click' : function () {
                    if (!self.pause) {
                        self.delaySlide();
                        self.playNext();
                    }
                }
            }
        });
        div.append(a.tojQuery());
    },
    /**
     *  API FUNCTIONS
     */
    addSlide : function (div) {
        var self = this;
        tmp = new AnimatedDiv({
            el : div,
            width : this.width ? this.width : div.width()
        });
        tmp.addEvent('Animated.finish', function () {
            self.resetDelay();
            self.finishedPlaying = true;
        });
        if (this.options.controls)
            this.createOneControls(this.options.controls, this.childs);
        this.childs += 1;
        div.addClass('rotator');
        this.element[0].css('width', this.childs * this.width);
        this.element[0].adopt(div.toJquery());
        this.rotator.push(tmp);
        this.run(this.childs - 1);
    },
    setControls : function (controls) {
        var self = this;
        this.buttons = controls;
        this.lastClicked = this.buttons[0];
        controls.bind(
            'click', function (e) {
                self.buttons.each(function () {
                    $(this).removeClass('active');
                })
                if (!self.pause) {
                    $(this).addClass('active');
                    self.delaySlide();
                    self.run($(this).attr('rel'));
                }
            })
    },
    disallowDrag : function() {
        this.dragEvent.ok = false;
    },
    allowDrag : function() {
        this.dragEvent.ok = true;
    },
    autoPlay : function () {
        if (!this.pause && !this.options.noAutoPlay)
            this.run(this.current + 1 < this.getMaxChilds() ? this.current + 1 : 0);
    },
    delaySlide : function () {
        window.clearTimeout(this.fdelay);
        this.fdelay = null;
    //this.fdelay = this._delay();
    },
    _pause : function () {
        this.finishedPlaying = true;
        this.pause = true;
    },
    unPause : function () {
        this.pause = false;
    },
    pauseSlide : function () {
        this._pause();
        window.clearTimeout(this.fdelay);
    },
    playNext : function () {
        this.autoPlay();
    },
    playPrevious : function () {
        this.run(this.current - 1 < 0 ? this.getMaxChilds() - 1 : this.current - 1);
    },
    nextSlideDelay : function () {
        var delay = null;
        if (this.current > 0)
            delay = this.rotator[this.current].toElement().attr('time')
        else
            delay = this.rotator[0].toElement().attr('time');
        if (!delay)
            delay = this.delay;
        return delay;
    },
    resetDelay : function () {
        var _this = this;
        window.clearTimeout(this.fdelay);
        this.fdelay = this._delay();
        return 1;
    },
    _delay : function () {
        var _this = this;
        window.clearTimeout(this.fdelay);
        return window.setTimeout(function ()  {
            _this.autoPlay();
        }, this.nextSlideDelay());
    }
});

