/**
 * YJYouplus News Slider - content slider
 * @version		1.0.0
 * @MooTools version 1.1
 * Copyright Youjoomla LLC
 * @author	Constantin Boiangiu
 */


var YJYouplus = new Class({
    initialize: function (A) {
        this.options = Object.extend({
            outerContainer: null,
            innerContainer: null,
            elements: null,
            navigation: {
                forward: null,
                back: null,
                container: null,
                elements: null,
                outer: null,
                visibleItems: 0
            },
            slideType: 0,
            orientation: 1,
            slideTime: 3000,
            duration: 600,
            tooltips: 0,
            autoslide: 1,
            navInfo: null,
            navLinks: null,
			startElem: null
        }, A || {});
        this.navElements = $(this.options.navigation.container).getElements(this.options.navigation.elements);
        this.navScroll = new Fx.Scroll(this.options.navigation.outer, {
            wait: false,
            duration: 800,
            transition: Fx.Transitions.Quad.easeInOut
        });
        this.correction = Math.round(this.options.navigation.visibleItems / 2.00001);
        this.start()
    },
    start: function () {
        this.currentElement = this.options.startElem;
        this.direction = 1;
        this.elements = $(this.options.innerContainer).getElements(this.options.elements);
        this.showEffect = {};
        this.hideEffect = {};
        this.firstRun = {};
        if (this.options.slideType !== 0) {
            if (this.options.orientation == 1) {
                this.showEffect.left = [1200, 0];
                this.hideEffect.left = [0, 1200];
                this.firstRun.left = 1200
            } else {
                this.showEffect.top = [400, 0];
                this.hideEffect.top = [0, 400];
                this.firstRun.top = 400
            }
        }
        if (this.options.slideType !== 1) {
            this.showEffect.opacity = [0, 1];
            this.hideEffect.opacity = [1, 0];
            this.firstRun.opacity = 0
        }
        this.elements.each(function (B, A) {
            B.setStyles({
                display: "block",
                position: "absolute",
                top: 0,
                left: 0,
                "z-index": (100 - A)
            });
            if (this.options.slideType !== 1 && A !== this.currentElement) {
                B.setStyle("opacity", 0)
            }
            this.elements[A]["fx"] = new Fx.Styles(B, {
                wait: false,
                duration: this.options.duration
            });
            if (A !== this.currentElement) {
                this.elements[A]["fx"].set(this.firstRun)
            }
            B.addEvent("mouseover", function (C) {
                if ($defined(this.period)) {
                    $clear(this.period)
                }
            }.bind(this));
            B.addEvent("mouseout", function (C) {
                if (this.options.autoslide == 0) {
                    this.period = this.rotateSlides.periodical(this.options.slideTime, this)
                }
            }.bind(this))
        }.bind(this));
        if (!this.options.tooltips) {
            new Tips($$(".YJYouplus_link"), {
                className: "YJYouplus_tips"
            })
        }
        if (!this.options.autoslide) {
            this.period = this.rotateSlides.periodical(this.options.slideTime, this)
        }
        this.setNavigation();
        if (this.options.navLinks) {
            this.secondNavigation();
            $(this.options.navigation.container).addEvent("mousewheel", function (B) {
                B = new Event(B);
                B.stop();
				var dir = B.wheel > 0 ? 1 : -1;
				var A = this.currentElement - dir;
               // var A = this.currentElement - B.wheel;
                if (B.wheel > 0 && A < 0) {
                    A = this.navElements.length - 1
                }
                if (B.wheel < 0 && A > this.navElements.length - 1) {
                    A = 0
                }
                this.resetAutoslide();
                this.period = this.rotateSlides.bind(this).periodical(this.options.autoSlide);
                this.nextSlide(A)
            }.bind(this))
        }
    },
    rotateSlides: function () {
        var A = this.currentElement + this.direction;
        if (A < 0) {
            A = this.elements.length - 1
        }
        if (A > this.elements.length - 1) {
            A = 0
        }
        this.nextSlide(A)
    },
    nextSlide: function (A) {
        if (A == this.currentElement) {
            return
        }
        this.elements[this.currentElement]["fx"].start(this.hideEffect);
        this.elements[A]["fx"].start(this.showEffect);
        this.currentElement = A;
        if ($(this.options.navInfo)) {
            $(this.options.navInfo).setHTML("Link " + (A + 1) + " of " + this.elements.length)
        }
        if ($defined(this.navElements)) {
            this.navElements.removeClass("selected");
            this.navElements[A].addClass("selected");
            var B = A - this.correction < 0 ? 0 : A - this.correction;
            if (B + this.correction >= this.navElements.length - this.correction) {
                B = (this.navElements.length - 1) - this.correction * 2
            }
            this.navScroll.toElement(this.navElements[B])
        }
    },
    setNavigation: function () {
        if (!$(this.options.navigation.forward)) {
            return
        }
        $(this.options.navigation.forward).addEvent("click", function (A) {
            new Event(A).stop();
            this.direction = 1;
            this.resetAutoslide();
            this.rotateSlides()
        }.bind(this));
        $(this.options.navigation.back).addEvent("click", function (A) {
            new Event(A).stop();
            this.direction = -1;
            this.resetAutoslide();
            this.rotateSlides()
        }.bind(this))
    },
    resetAutoslide: function () {
        if ($defined(this.period)) {
            $clear(this.period);
            this.period = this.rotateSlides.periodical(this.options.slideTime, this)
        }
    },
    secondNavigation: function () {
        this.navElements = $$(this.options.navLinks);
        this.navElements.each(function (B, A) {
            if (A == this.currentElement) {
                this.navScroll.toElement(B);
                B.addClass("selected")
            }
            B.addEvent("click", function (C) {
                new Event(C).stop();
                this.resetAutoslide();
                this.nextSlide(A)
            }.bind(this))
        }.bind(this));
        if (!this.options.tooltips) {
            new Tips(this.navElements, {
                className: "YJYouplus_tips"
            })
        }
    }
});


