var RokStories = new Class({
    version: 2.0,
    options: {
        startElement: 0,
        startWidth: 410,
        startHeight: 200,
        thumbsOpacity: 0.3,
        autorun: true,
        delay: 3000,
        scrollerDuration: 1000,
        scrollerTransition: Fx.Transitions.Expo.easeInOut,
        mousetype: 'click',
        layout: 'layout1',
        linkedImgs: false,
        showThumbs: false,
        fixedThumb: true,
        mask: true,
        descsAnim: 'topdown',
        imgsAnim: 'bottomup',
        thumbLeftOffsets: {
            x: 0,
            y: 0
        },
        thumbRightOffsets: {
            x: 0,
            y: 0
        }
    },
    initialize: function (b, c) {
        this.setOptions(c);
        this.element = $(b) || null;
        this.layout = this.options.layout;
        if (!this.element) return;
        if (this.layout == 'layout7') return new RokStoriesLayout7(b, c);
        this.timer = null;
        this.current = this.options.startElement;
        this.fullParent = this.element.getElements('.image-full')[0];
        if (this.layout == 'layout6') this.fullParent = this.element.getElements('.image-full');
        this.full = RokStoriesImage['rokstories-' + this.options.id];
        this.small = this.element.getElements('.image-small img');
        this.descs = this.element.getElements('.desc-container .description');
        if (this.layout == 'layout2' || this.layout == 'layout5' || this.layout == 'layout6') this.labels = this.element.getElements('.labels-title .feature-block-title');
        if (!this.full.length || !this.small.length || !this.descs.length) return;
        this.images = RokStoriesImage['rokstories-' + this.options.id];
        this.fullFx = [];
        if (this.layout == 'layout2' || this.layout == 'layout5') {
            this.labelsFx = [];
            this.arrowLeft = this.element.getElement('.feature-arrow-l');
            this.arrowRight = this.element.getElement('.feature-arrow-r');
            if (this.arrowLeft && this.arrowRight) this.arrowsEvents();
            (this.labels.length).times(function (i) {
                this.labelsFx.push(new Fx.Styles(this.labels[i], {
                    wait: false,
                    duration: 400
                }).set({
                    'opacity': 0
                }))
            }.bind(this));
            if (this.layout == 'layout5') {
                this.circles = this.element.getElements('.feature-number-sub');
                if (this.circles.length == this.small.length) this.circlesEvents();
                else throw new Error('Circles don\'t match the number of images');
            }
        } else if (this.layout == 'layout3' || this.layout == 'layout4') {
            this.small.setStyle('display', 'none');
            this.element.getElement('.desc-container').inject(this.fullParent).setStyles({
                'position': 'absolute',
                'z-index': 5
            });
            if (this.layout == 'layout4') this.element.getElement('.feature-numbers').inject(this.fullParent);
            this.circles = this.element.getElements('.feature-circles-sub');
            if (this.layout == 'layout4') this.circles = this.element.getElements('.feature-number-sub');
            if (this.circles.length == this.small.length) this.circlesEvents();
            else throw new Error('Circles don\'t match the number of images');
        } else if (this.layout == 'layout6') {
            window.addEvent('load', function () {
                this.loaded = true;
                this.featureContainer.getParent().setStyle('height', this.stories[this.current].getSize().size.y);
                this.stories.setStyle('display', 'block')
            }.bind(this));
            this.circles = this.element.getElements('.feature-circles-sub');
            if (this.circles.length) {
                if (this.circles.length == this.small.length) this.circlesEvents();
                else throw new Error('Circles don\'t match the number of images');
            }
            this.small.setStyle('display', 'none');
            this.stories = this.element.getElements('.feature-story');
            this.labelsFx = [];
            this.arrowLeft = this.element.getElement('.feature-arrow-l');
            this.arrowRight = this.element.getElement('.feature-arrow-r');
            if (this.arrowLeft && this.arrowRight) {
                this.arrowLeft.setProperty('title', 'tip');
                this.arrowRight.setProperty('title', 'tip');
                this.arrowsEvents()
            }(this.labels.length).times(function (i) {
                this.labelsFx.push(new Fx.Styles(this.labels[i], {
                    wait: false,
                    duration: 400
                }).set({
                    'opacity': 0
                }))
            }.bind(this))
        }(this.images.length).times(function (i) {
            this.fullFx.push(null)
        }.bind(this));
        this.smallFx = [];
        this.descsFx = [];
        if (this.full.length != this.small.length && this.full.length != this.descs.length) return;
        this.length = this.full.length;
        if (this.layout != 'layout6') {
            this.smallParent = this.small[0].getParent();
            this.descsParent = this.descs[0].getParent();
            this.fullParent.addClass('rokstories-spinner');
            this.descsParent.addClass('rokstories-spinner');
            this.small.setStyle('opacity', this.options.thumbsOpacity);
            this.descsParentFx = new Fx.Styles(this.descsParent, {
                wait: false,
                duration: 400
            }).set({
                height: 0
            });
            this.fullParentFx = new Fx.Styles(this.fullParent, {
                wait: false,
                duration: 400
            }).set({
                height: 0
            });
            if (!window.webkit && !window.gecko) this.fullParentFx.set({
                'width': (window.ie) ? '100%' : this.options.startWidth
            });
            this.fullParentFx.start({
                height: this.options.startHeight
            })
        } else {
            this.fullParent[this.current].addClass('rokstories-spinner');
            this.featureContainer = this.element.getElement('.feature-container');
            this.scroller = new Fx.Scroll(this.featureContainer.getParent(), {
                wheelStops: false,
                duration: this.options.scrollerDuration,
                transition: this.options.scrollerTransition
            });
            this.scroller.set(0, 0);
            this.fullParentFx = [];
            this.fullParent.each(function (a, i) {
                this.fullParentFx.push(new Fx.Styles(a, {
                    wait: false,
                    duration: 400
                }));
                if (i != this.current && this.layout != 'layout6') this.stories[i].setStyle('display', 'none')
            }, this)
        }
        this.mask = this.element.getElement('.image-mask');
        if (!this.options.mask && this.mask) this.mask.setStyle('display', 'none');
        this.setSizes();
        this.loading = true
    },
    addThumbsEvents: function () {
        var d = this;
        this.small.each(function (c, i) {
            d.smallFx.push(new Fx.Styles(c, {
                wait: false,
                duration: 400
            }).set({
                'opacity': d.options.thumbsOpacity
            }));
            c.addEvents({
                'click': function () {
                    $clear(d.timer);
                    if (d.layout != 'layout6') d.fullParent.addClass('rokstories-spinner');
                    else d.fullParent[i].addClass('rokstories-spinner');
                    if (d.layout != 'layout6') {
                        d.fullFx.each(function (a) {
                            if (a) a.start({
                                'opacity': 0
                            })
                        });
                        if (d.options.layout == 'layout3' || d.options.layout == 'layout4' || d.options.layout == 'layout5') d.circleSwitch(i)
                    } else {
                        d.scroller.stop().toElement(d.stories[i]);
                        if (d.circles.length) d.circleSwitch(i);
                        if (d.loaded) {
                            var b = d.stories[i].getSize().size.y;
                            d.featureContainer.getParent().effect('height', {
                                duration: d.options.scrollerDuration
                            }).start(b)
                        }
                    }
                    d.load(i)
                },
                'mouseenter': function () {
                    if (d.options.mousetype == 'mouseenter') c.fireEvent('click');
                    if (i != d.current) d.smallFx[i].start({
                        'opacity': 1
                    })
                },
                'mouseleave': function () {
                    if (i != d.current) d.smallFx[i].start({
                        'opacity': d.options.thumbsOpacity
                    })
                }
            })
        })
    },
    circlesEvents: function () {
        this.circles.each(function (a, i) {
            a.addEvent('click', function () {
                this.small[i].fireEvent('click');
                this.circleSwitch(i)
            }.bind(this))
        }, this)
    },
    circleSwitch: function (i) {
        if (this.circles) {
            this.circles.removeClass('active');
            this.circles[i].addClass('active')
        }
    },
    arrowsEvents: function () {
        var c = this.arrowLeft,
            right = this.arrowRight,
            self = this;
        c.addEvents({
            'mouseenter': function () {
                c.addClass('arrowleft-hover')
            },
            'mouseleave': function () {
                c.removeClass('arrowleft-hover').removeClass('arrowleft-down')
            },
            'mousedown': function () {
                c.addClass('arrowleft-down')
            },
            'mouseup': function () {
                c.removeClass('arrowleft-down')
            },
            'click': function () {
                self.previous();
                if (self.tipsLeft) {
                    self.tipsLeft.hide();
                    self.tipsLeft.fireEvent('onShow')
                }
            }
        });
        right.addEvents({
            'mouseenter': function () {
                right.addClass('arrowright-hover')
            },
            'mouseleave': function () {
                right.removeClass('arrowright-hover').removeClass('arrowright-down')
            },
            'mousedown': function () {
                right.addClass('arrowright-down')
            },
            'mouseup': function () {
                right.removeClass('arrowright-down')
            },
            'click': function () {
                self.next();
                if (self.tipsRight) {
                    self.tipsRight.hide();
                    self.tipsRight.fireEvent('onShow')
                }
            }
        });
        if (this.options.showThumbs) {
            this.tipsLeft = new Tips(c, {
                className: 'rokstories',
                fixed: self.options.fixedThumb,
                offsets: self.options.thumbLeftOffsets,
                initialize: function () {
                    this.rokTip = new Element('div', {
                        'class': 'tip-wrapper'
                    }).inject(this.toolTip);
                    this.fx = new Fx.Style(this.toolTip, 'opacity', {
                        duration: 300,
                        wait: false
                    }).set(0)
                },
                onShow: function (a) {
                    var b = self.current - 1;
                    if (b < 0) b = self.small.length - 1;
                    self.small[b].clone().inject(this.rokTip.empty());
                    this.fx.start(1)
                },
                onHide: function (a) {
                    this.fx.start(0)
                }
            });
            this.tipsRight = new Tips(right, {
                className: 'rokstories',
                fixed: self.options.fixedThumb,
                offsets: self.options.thumbRightOffsets,
                initialize: function () {
                    this.rokTip = new Element('div', {
                        'class': 'tip-wrapper'
                    }).inject(this.toolTip);
                    this.fx = new Fx.Style(this.toolTip, 'opacity', {
                        duration: 300,
                        wait: false
                    }).set(0)
                },
                onShow: function (a) {
                    var b = self.current + 1;
                    if (b > self.small.length - 1) b = 0;
                    self.small[b].clone().inject(this.rokTip.empty());
                    this.fx.start(1)
                },
                onHide: function (a) {
                    this.fx.start(0)
                }
            })
        }
    },
    load: function (a) {
        var b = this;
        if ($type(this.full[a]) != 'string') {
            b.transition(a, this.full[a]);
            b.loading = false
        } else {
            new Asset.image(this.full[a], {
                onload: function () {
                    $clear(b.timer);
                    if (b.layout == 'layout6' && b.fullParent[a].getElement('img')) {
                        b.transition(a, b.full[a]);
                        b.loading = false;
                        return
                    }
                    if (b.layout != 'layout6') b.full[a] = this.inject(b.fullParent);
                    else b.full[a] = this.inject(b.fullParent[a]);
                    if (b.options.linkedImgs) {
                        this.setStyle('cursor', 'pointer').addEvent('click', function () {
                            window.location = RokStoriesLinks['rokstories-' + b.options.id][a].replace(/&amp;/gi, "&")
                        })
                    };
                    if (b.layout != 'layout6') {
                        b.fullFx[a] = new Fx.Styles(b.full[a], {
                            wait: false,
                            duration: 400
                        }).set({
                            'opacity': 0
                        });
                        b.setDescSizes.delay(70, b)
                    }
                    b.load(a);
                    b.loading = false
                }
            })
        }
    },
    transition: function (b, c) {
        var d = this;
        if (this.layout != 'layout6') {
            this.fullParentFx.stop().set({
                'width': c.width
            }).start({
                height: c.height
            })
        }
        if (this.layout == 'layout5') {
            var e = (this.options.descsAnim == 'bottomup') ? [this.descsHeight, 0] : [-this.descsHeight, 0];
            var f = (this.options.imgsAnim == 'bottomup') ? [this.descsHeight, 0] : [-this.descsHeight, 0]
        }
        if (d.layout != 'layout6') d.fullParent.removeClass('rokstories-spinner');
        else d.fullParent[d.current].removeClass('rokstories-spinner');
        d.descsFx.each(function (a) {
            a.start({
                'opacity': 0
            })
        });
        d.smallFx.each(function (a) {
            a.start({
                'opacity': d.options.thumbsOpacity
            })
        });
        if (d.layout == 'layout2' || d.layout == 'layout5') d.labelsFx.each(function (a) {
            a.start({
                'opacity': 0
            })
        });
        if (d.layout != 'layout6') {
            if (d.options.imgsAnim == 'fade' || d.options.layout != 'layout5') d.fullFx[b].stop().start({
                'opacity': 1
            });
            else d.fullFx[b].stop().start({
                'opacity': 1,
                'top': f
            });
            if ((d.layout == 'layout2' || d.layout == 'layout5') && d.labelsFx.length) d.labelsFx[b].stop().start({
                'opacity': 1
            });
            if (d.options.descsAnim == 'fade' || d.options.layout != 'layout5') d.descsFx[b].stop().start({
                'opacity': 1
            });
            else d.descsFx[b].stop().start({
                'opacity': 1,
                'top': e
            });
            d.smallFx[b].stop().start({
                'opacity': 1
            })
        }
        d.current = b;
        if (d.options.autorun && !d.pause) d.timer = d.next.periodical(d.options.delay, d)
    },
    setSizes: function () {
        var a = this;
        if (this.layout != 'layout6') this.setDescSizes();
        this.addThumbsEvents();
        if (this.options.autorun) {
            this.element.addEvents({
                'mouseenter': function () {
                    $clear(a.timer);
                    a.pause = true
                },
                'mouseleave': function () {
                    $clear(a.timer);
                    a.pause = false;
                    a.timer = a.next.periodical(a.options.delay, a)
                }
            })
        };
        this.next(this.current)
    },
    setDescSizes: function () {
        var c = {
            width: 0,
            height: 0
        };
        this.smallParent.setStyle('width', (this.options.startWidth == 'auto') ? this.fullParent.getStyle('width') : this.options.startWidth);
        this.descs.each(function (a) {
            if (this.descsFx.length < this.length) this.descsFx.push(new Fx.Styles(a, {
                wait: false,
                duration: 400
            }).set({
                'opacity': 0
            }));
            var b = a.getSize().size;
            if (b.x > c.width) c.width = b.x;
            if (b.y > c.height) c.height = b.y
        }, this);
        this.descsParentFx.stop().set('width', c.width).start({
            height: c.height
        });
        this.descsHeight = c.height;
        this.descsParent.removeClass('rokstories-spinner')
    },
    next: function (a) {
        var b = (a != null) ? a : this.current + 1;
        if (b > this.length - 1) b = 0;
        this.current = b;
        this.small[b].fireEvent('click')
    },
    previous: function (a) {
        var b = (a != null) ? a : this.current - 1;
        if (b < 0) b = this.length - 1;
        this.current = b;
        this.small[b].fireEvent('click')
    }
});
RokStories.implement(new Options);
var RokStoriesLayout7 = new Class({
    options: {
        imgWidth: 1200,
        titles: true
    },
    initialize: function (c, d) {
        this.element = $(c);
        this.setOptions(d);
        this.current = 0;
        this.realCurrent = 0;
        this.container = this.element.getElement('.rt-gallery-items');
        this.images = this.container.getChildren();
        this.controls = this.element.getElements('.rt-gallery-controls ul li').filter(function (a) {
            return !a.hasClass('previous') && !a.hasClass('next')
        });
        this.arrows = this.element.getElements('.rt-gallery-controls ul li.arrow');
        this.container.setStyle('left', -this.options.imgWidth);
        this.fixFirstLast();
        if (this.controls.length || this.arrows.length) this.attachEvents();
        this.fx = new Fx.Style(this.container, 'left', {
            duration: this.options.scrollerDuration,
            wait: true,
            transition: this.options.scrollerTransition
        }).set(-this.options.imgWidth);
        this.titlesWrap = this.element.getElement('.rt-gallery-title');
        if (!this.titlesWrap) this.options.titles = false;
        else this.options.titles = true;
        if (this.options.linkedImgs) {
            this.lnkWrap = new Element('div', {
                'class': 'layout7-lnkwrap'
            }).inject(this.element, 'top');
            var e = this.element.getElement('.rt-gallery');
            var f = {
                width: e.getStyle('width') || e.getSize().x,
                height: e.getStyle('height') || e.getSize().y
            };
            var g = window.getSize().size.x - f.width.toInt();
            this.lnkWrap.setStyle('left', g / 2);
            this.lnkWrap.setStyles(f);
            this.lnkWrap.addEvent('click', function () {
                var a = this.images[this.realCurrent].getElement('img').alt;
                document.location = a
            }.bind(this));
            window.addEvent('resize', function () {
                var a = window.getSize().size.x - f.width.toInt();
                this.lnkWrap.setStyle('left', a / 2)
            }.bind(this))
        }
        if (this.options.titles) {
            var h = 0;
            this.titlesFx = [];
            this.titles = this.titlesWrap.getElements('.layout7-title');
            this.titles.each(function (a, i) {
                if (!i) this.titlesWrap.setStyle('width', a.getSize().size.x);
                h = Math.max(h, a.getSize().size.x);
                var b = new Fx.Style(a, 'opacity', {
                    duration: this.options.scrollerDuration,
                    wait: true,
                    transition: this.options.scrollerTransition
                });
                b.set((!i) ? 1 : 0);
                if (!i) a.setStyle('position', 'absolute');
                this.titlesFx.push(b)
            }, this);
            this.titlesWrap.effect('width').start(h + this.titlesWrap.getStyle('margin-right').toInt())
        }
        if (this.options.autorun && !this.pause) this.timer = this.next.periodical(this.options.delay + this.options.scrollerDuration, this)
    },
    attachEvents: function () {
        this.controls.each(function (h, i) {
            var j = this.options.imgWidth * (i + 1) * -1;
            h.addEvent('click', function (e) {
                if (e) new Event(e).stop();
                if (this.fx.timer != null) return;
                var b = this.container.getChildren().indexOf(this.images[i]),
                    j = this.options.imgWidth * (b) * -1,
                    delay = false;
                if (i == this.realCurrent) return;
                if (!b || b == this.images.length - 1) {
                    if (!b) {
                        var c = this.container.getStyle('left').toInt();
                        this.container.getLast().inject(this.container, 'top');
                        this.container.setStyle('left', c - this.options.imgWidth);
                        j = -this.options.imgWidth
                    } else {
                        var c = this.container.getStyle('left').toInt();
                        this.container.getFirst().inject(this.container);
                        this.container.setStyle('left', c + this.options.imgWidth);
                        j = this.options.imgWidth * (b - 1) * -1
                    }
                }
                var d = this.realCurrent;
                this.realCurrent = i;
                this.current = i;
                this.fx.start(j).chain(function () {
                    $$(this.controls).removeClass('active');
                    $(this.controls[this.realCurrent]).addClass('active');
                    var a = this.container.getChildren().indexOf(this.images[this.realCurrent]);
                    (a - 1).times(function () {
                        this.container.getFirst().inject(this.container);
                        this.container.setStyle('left', -this.options.imgWidth)
                    }.bind(this))
                }.bind(this));
                if (this.options.titles) {
                    var f = d;
                    var g = this.realCurrent;
                    if (g >= this.images.length) g = 0;
                    if (window.ie) {
                        this.titlesFx[f].set(0);
                        this.titlesFx[g].set(1)
                    } else {
                        this.titlesFx[f].start(0);
                        this.titlesFx[g].start(1)
                    }
                }
            }.bind(this))
        }, this);
        this.arrows.each(function (a, i) {
            a.addEvent('click', function (e) {
                new Event(e).stop();
                if (this.fx.timer != null) return;
                if (a.hasClass('next')) this.next();
                else this.previous()
            }.bind(this))
        }, this);
        if (this.options.autorun) {
            this.element.addEvents({
                'mouseenter': function () {
                    $clear(this.timer);
                    this.pause = true
                }.bind(this),
                'mouseleave': function () {
                    $clear(this.timer);
                    this.pause = false;
                    this.timer = this.next.periodical(this.options.delay + this.options.scrollerDuration, this)
                }.bind(this)
            })
        }
    },
    fixFirstLast: function () {
        var a = this.images.getLast();
        a.inject(this.container, 'top')
    },
    next: function () {
        var a = this.images[this.realCurrent],
            index = this.container.getChildren().indexOf(a),
            position = this.container.getStyle('left').toInt();
        if (index > 1) {
            (index - 1).times(function () {
                this.container.getFirst().inject(this.container);
                this.container.setStyle('left', -this.options.imgWidth);
                position = this.container.getStyle('left').toInt()
            }.bind(this))
        };
        this.fx.start(position - this.options.imgWidth).chain(function () {
            this.container.getFirst().inject(this.container);
            this.container.setStyle('left', -this.options.imgWidth);
            this.realCurrent += 1;
            if (this.realCurrent >= this.images.length) this.realCurrent = 0;
            if (this.controls) {
                $$(this.controls).removeClass('active');
                $(this.controls[this.realCurrent]).addClass('active')
            }
        }.bind(this));
        if (this.options.titles) {
            var b = this.realCurrent;
            var c = this.realCurrent + 1;
            if (c >= this.images.length) c = 0;
            if (window.ie) {
                this.titlesFx[b].set(0);
                this.titlesFx[c].set(1)
            } else {
                this.titlesFx[b].start(0);
                this.titlesFx[c].start(1)
            }
        }
    },
    previous: function () {
        var a = this.images[this.realCurrent],
            index = this.container.getChildren().indexOf(a),
            position = this.container.getStyle('left').toInt();
        if (index > 1) {
            (index - 1).times(function () {
                this.container.getLast().inject(this.container, 'top');
                this.container.setStyle('left', -this.options.imgWidth);
                position = this.container.getStyle('left').toInt()
            }.bind(this))
        };
        this.container.getLast().inject(this.container, 'top');
        this.container.setStyle('left', -(this.options.imgWidth * 2));
        this.fx.start(position).chain(function () {
            this.realCurrent -= 1;
            if (this.realCurrent < 0) this.realCurrent = this.images.length - 1;
            if (this.controls) {
                $$(this.controls).removeClass('active');
                $(this.controls[this.realCurrent]).addClass('active')
            }
        }.bind(this));
        if (this.options.titles) {
            var b = this.realCurrent;
            var c = this.realCurrent - 1;
            if (c < 0) c = this.images.length - 1;
            if (window.ie) {
                this.titlesFx[b].set(0);
                this.titlesFx[c].set(1)
            } else {
                this.titlesFx[b].start(0);
                this.titlesFx[c].start(1)
            }
        }
    }
});
RokStoriesLayout7.implement(new Options, new Chain);
