/*
Copyright (c) 2009 Victor Stanciu - http://www.victorstanciu.ro

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Carousel=Class.create(Abstract,{initialize:function(c,d,e,f){this.scrolling=false;this.scroller=$(c);this.slides=d;this.controls=e;this.options=Object.extend({duration:1,auto:false,frequency:3,visibleSlides:1,controlClassName:'carousel-control',jumperClassName:'carousel-jumper',disabledClassName:'carousel-disabled',selectedClassName:'carousel-selected',circular:false,wheel:true,effect:'scroll',transition:'sinoidal'},f||{});if(this.options.effect=='fade'){this.options.circular=true}this.slides.each(function(a,b){a._index=b});if(this.controls){this.controls.invoke('observe','click',this.click.bind(this))}if(this.options.wheel){this.scroller.observe('mousewheel',this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll',this.wheel.bindAsEventListener(this))}if(this.options.auto){this.start()}if(this.options.initial){var g=this.slides.indexOf($(this.options.initial));if(g>(this.options.visibleSlides-1)&&this.options.visibleSlides>1){if(g>this.slides.length-(this.options.visibleSlides+1)){g=this.slides.length-this.options.visibleSlides}}this.moveTo(this.slides[g])}},click:function(a){this.stop();var b=a.findElement('a');if(!b.hasClassName(this.options.disabledClassName)){if(b.hasClassName(this.options.controlClassName)){eval("this."+b.rel+"()")}else if(b.hasClassName(this.options.jumperClassName)){this.moveTo(b.rel);if(this.options.selectedClassName){this.controls.invoke('removeClassName',this.options.selectedClassName);b.addClassName(this.options.selectedClassName)}}}this.deactivateControls();a.stop()},moveTo:function(a){if(this.options.beforeMove&&(typeof this.options.beforeMove=='function')){this.options.beforeMove()}this.previous=this.current?this.current:this.slides[0];this.current=$(a);var b=this.scroller.cumulativeOffset();var c=this.current.cumulativeOffset();if(this.scrolling){this.scrolling.cancel()}switch(this.options.effect){case'fade':this.scrolling=new Effect.Opacity(this.scroller,{from:1.0,to:0,duration:this.options.duration,afterFinish:(function(){this.scroller.scrollLeft=c[0]-b[0];this.scroller.scrollTop=c[1]-b[1];new Effect.Opacity(this.scroller,{from:0,to:1.0,duration:this.options.duration,afterFinish:(function(){if(this.controls){this.activateControls()}if(this.options.afterMove&&(typeof this.options.afterMove=='function')){this.options.afterMove()}}).bind(this)})}).bind(this)});break;case'scroll':default:var d;switch(this.options.transition){case'spring':d=Effect.Transitions.spring;break;case'sinoidal':default:d=Effect.Transitions.sinoidal;break}this.scrolling=new Effect.SmoothScroll(this.scroller,{duration:this.options.duration,x:(c[0]-b[0]),y:(c[1]-b[1]),transition:d,afterFinish:(function(){if(this.controls){this.activateControls()}if(this.options.afterMove&&(typeof this.options.afterMove=='function')){this.options.afterMove()}this.scrolling=false}).bind(this)});break}return false},prev:function(){if(this.current){var a=this.current._index;var b=(a==0)?(this.options.circular?this.slides.length-1:0):a-1}else{var b=(this.options.circular?this.slides.length-1:0)}if(b==(this.slides.length-1)&&this.options.circular&&this.options.effect!='fade'){this.scroller.scrollLeft=(this.slides.length-1)*this.slides.first().getWidth();this.scroller.scrollTop=(this.slides.length-1)*this.slides.first().getHeight();b=this.slides.length-2}this.moveTo(this.slides[b])},next:function(){if(this.current){var a=this.current._index;var b=(this.slides.length-1==a)?(this.options.circular?0:a):a+1}else{var b=1}if(b==0&&this.options.circular&&this.options.effect!='fade'){this.scroller.scrollLeft=0;this.scroller.scrollTop=0;b=1}if(b>this.slides.length-(this.options.visibleSlides+1)){b=this.slides.length-this.options.visibleSlides}this.moveTo(this.slides[b])},first:function(){this.moveTo(this.slides[0])},last:function(){this.moveTo(this.slides[this.slides.length-1])},toggle:function(){if(this.previous){this.moveTo(this.slides[this.previous._index])}else{return false}},stop:function(){if(this.timer){clearTimeout(this.timer)}},start:function(){this.periodicallyUpdate()},pause:function(){this.stop();this.activateControls()},resume:function(b){if(b){var c=b.relatedTarget||b.toElement;if(!c||(!this.slides.include(c)&&!this.slides.any(function(a){return c.descendantOf(a)}))){this.start()}}else{this.start()}},periodicallyUpdate:function(){if(this.timer!=null){clearTimeout(this.timer);this.next()}this.timer=setTimeout(this.periodicallyUpdate.bind(this),this.options.frequency*1000)},wheel:function(a){a.cancelBubble=true;a.stop();var b=0;if(!a){a=window.event}if(a.wheelDelta){b=a.wheelDelta/120}else if(a.detail){b=-a.detail/3}if(!this.scrolling){this.deactivateControls();if(b>0){this.prev()}else{this.next()}}return Math.round(b)},deactivateControls:function(){this.controls.invoke('addClassName',this.options.disabledClassName)},activateControls:function(){this.controls.invoke('removeClassName',this.options.disabledClassName)}});Effect.SmoothScroll=Class.create();Object.extend(Object.extend(Effect.SmoothScroll.prototype,Effect.Base.prototype),{initialize:function(a){this.element=$(a);var b=Object.extend({x:0,y:0,mode:'absolute'},arguments[1]||{});this.start(b)},setup:function(){if(this.options.continuous&&!this.element._ext){this.element.cleanWhitespace();this.element._ext=true;this.element.appendChild(this.element.firstChild)}this.originalLeft=this.element.scrollLeft;this.originalTop=this.element.scrollTop;if(this.options.mode=='absolute'){this.options.x-=this.originalLeft;this.options.y-=this.originalTop}},update:function(a){this.element.scrollLeft=this.options.x*a+this.originalLeft;this.element.scrollTop=this.options.y*a+this.originalTop}}); 

*/


Carousel = Class.create(Abstract, {
    initialize: function (c, d, e, f) {
        this.scrolling = false;
        this.scroller = $(c);
        this.slides = d;
        this.controls = e;
        this.options = Object.extend({
            duration: 1,
            auto: false,
            frequency: 3,
            visibleSlides: 1,
            controlClassName: 'carousel-control',
            jumperClassName: 'carousel-jumper',
            disabledClassName: 'carousel-disabled',
            selectedClassName: 'carousel-selected',
            circular: false,
            wheel: true,
            effect: 'scroll',
            transition: 'sinoidal'
        }, f || {});
        if (this.options.effect == 'fade') {
            this.options.circular = true
        }
        this.slides.each(function (a, b) {
            a._index = b
        });
        if (this.controls) {
            this.controls.invoke('observe', 'click', this.click.bind(this))
        }
        if (this.options.wheel) {
            this.scroller.observe('mousewheel', this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll', this.wheel.bindAsEventListener(this))
        }
        if (this.options.auto) {
            this.start()
        }
        if (this.options.initial) {
            var g = this.slides.indexOf($(this.options.initial));
            if (g > (this.options.visibleSlides - 1) && this.options.visibleSlides > 1) {
                if (g > this.slides.length - (this.options.visibleSlides + 1)) {
                    g = this.slides.length - this.options.visibleSlides
                }
            }
            this.moveTo(this.slides[g])
        }
    },
    click: function (a) {
        this.stop();
        var b = a.findElement('a');
        if (!b.hasClassName(this.options.disabledClassName)) {
            if (b.hasClassName(this.options.controlClassName)) {
                eval("this." + b.rel + "()")
            } else if (b.hasClassName(this.options.jumperClassName)) {
                this.moveTo(b.rel);
                if (this.options.selectedClassName) {
                    this.controls.invoke('removeClassName', this.options.selectedClassName);
                    b.addClassName(this.options.selectedClassName)
                }
            }
        }
        this.deactivateControls();
        a.stop()
    },
    moveTo: function (a) {
        if (this.options.beforeMove && (typeof this.options.beforeMove == 'function')) {
            this.options.beforeMove()
        }
        this.previous = this.current ? this.current : this.slides[0];
        this.current = $(a);
        var b = this.scroller.cumulativeOffset();
        var c = this.current.cumulativeOffset();
        if (this.scrolling) {
            this.scrolling.cancel()
        }
        switch (this.options.effect) {
        case 'fade':
            this.scrolling = new Effect.Opacity(this.scroller, {
                from: 1.0,
                to: 0,
                duration: this.options.duration,
                afterFinish: (function () {
                    this.scroller.scrollLeft = c[0] - b[0];
                    this.scroller.scrollTop = c[1] - b[1];
                    new Effect.Opacity(this.scroller, {
                        from: 0,
                        to: 1.0,
                        duration: this.options.duration,
                        afterFinish: (function () {
                            if (this.controls) {
                                this.activateControls()
                            }
                            if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
                                this.options.afterMove()
                            }
                        }).bind(this)
                    })
                }).bind(this)
            });
            break;
        case 'scroll':
        default:
            var d;
            switch (this.options.transition) {
            case 'spring':
                d = Effect.Transitions.spring;
                break;
            case 'sinoidal':
            default:
                d = Effect.Transitions.sinoidal;
                break
            }
            this.scrolling = new Effect.SmoothScroll(this.scroller, {
                duration: this.options.duration,
                x: (c[0] - b[0]),
                y: (c[1] - b[1]),
                transition: d,
                afterFinish: (function () {
                    if (this.controls) {
                        this.activateControls()
                    }
                    if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
                        this.options.afterMove()
                    }
                    this.scrolling = false
                }).bind(this)
            });
            break
        }
        return false
    },
    prev: function () {
        if (this.current) {
            var a = this.current._index;
            var b = (a == 0) ? (this.options.circular ? this.slides.length - 1 : 0) : a - 1
        } else {
            var b = (this.options.circular ? this.slides.length - 1 : 0)
        }
        if (b == (this.slides.length - 1) && this.options.circular && this.options.effect != 'fade') {
            this.scroller.scrollLeft = (this.slides.length - 1) * this.slides.first().getWidth();
            this.scroller.scrollTop = (this.slides.length - 1) * this.slides.first().getHeight();
            b = this.slides.length - 2
        }
        this.moveTo(this.slides[b])
    },
    next: function () {
        if (this.current) {
            var a = this.current._index;
            var b = (this.slides.length - 1 == a) ? (this.options.circular ? 0 : a) : a + 1
        } else {
            var b = 1
        }
        if (b == 0 && this.options.circular && this.options.effect != 'fade') {
            this.scroller.scrollLeft = 0;
            this.scroller.scrollTop = 0;
            b = 1
        }
        if (b > this.slides.length - (this.options.visibleSlides + 1)) {
            b = this.slides.length - this.options.visibleSlides
        }
        this.moveTo(this.slides[b])
    },
    first: function () {
        this.moveTo(this.slides[0])
    },
    last: function () {
        this.moveTo(this.slides[this.slides.length - 1])
    },
    toggle: function () {
        if (this.previous) {
            this.moveTo(this.slides[this.previous._index])
        } else {
            return false
        }
    },
    stop: function () {
        if (this.timer) {
            clearTimeout(this.timer)
        }
    },
    start: function () {
        this.periodicallyUpdate()
    },
    pause: function () {
        this.stop();
        this.activateControls()
    },
    resume: function (b) {
        if (b) {
            var c = b.relatedTarget || b.toElement;
            if (!c || (!this.slides.include(c) && !this.slides.any(function (a) {
                return c.descendantOf(a)
            }))) {
                this.start()
            }
        } else {
            this.start()
        }
    },
    periodicallyUpdate: function () {
        if (this.timer != null) {
            clearTimeout(this.timer);
            this.next()
        }
		/*
        this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000)
		*/
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), 5 * 1000)
		
		
    },
    wheel: function (a) {
        a.cancelBubble = true;
        a.stop();
        var b = 0;
        if (!a) {
            a = window.event
        }
        if (a.wheelDelta) {
            b = a.wheelDelta / 120
        } else if (a.detail) {
            b = -a.detail / 3
        }
        if (!this.scrolling) {
            this.deactivateControls();
            if (b > 0) {
                this.prev()
            } else {
                this.next()
            }
        }
        return Math.round(b)
    },
    deactivateControls: function () {
        this.controls.invoke('addClassName', this.options.disabledClassName)
    },
    activateControls: function () {
        this.controls.invoke('removeClassName', this.options.disabledClassName)
    }
});
Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
    initialize: function (a) {
        this.element = $(a);
        var b = Object.extend({
            x: 0,
            y: 0,
            mode: 'absolute'
        }, arguments[1] || {});
        this.start(b)
    },
    setup: function () {

        if (this.options.continuous && !this.element._ext) {
            this.element.cleanWhitespace();
            this.element._ext = true;
            this.element.appendChild(this.element.firstChild)
        }
        this.originalLeft = this.element.scrollLeft;
        this.originalTop = this.element.scrollTop;
        if (this.options.mode == 'absolute') {
            this.options.x -= this.originalLeft;
            this.options.y -= this.originalTop
        }
    },
    update: function (a) {
        this.element.scrollLeft = this.options.x * a + this.originalLeft;
        this.element.scrollTop = this.options.y * a + this.originalTop
    }
});

