﻿News = function() {
    this.x;
    this.y;
    this.z;
    this.result = $("#result");
    this.items = $(".links a");
    this.thredIndex;
    this.Animate = function() {
        if (this.yVar < 0 | this.yVar > 100) {
            if (this.IsRun == false) {
                return;
            }
            this.y = 0;
            this.z = 2;
            this.zStop = false;
            this.zStopIsAlready = false;
            this.random = Math.round(Math.random() * 1) == 1 ? 1 : -1;
            this.result.css("display", "none");
            if (this.items.length - 1 == this.index) {
                this.index = -1;
            }
            this.index++;
            var itemA = this.items[this.index];
            this.result.empty();
            this.result.append(itemA);
        }
        this.CangeVars();

        this.result.css("top", this.yVar + "%");  // y value
        this.result.css("fontSize", this.zVar + "%"); // font size (illusion of perspective.)
        this.result.css("opacity", this.GetOpacity() / 300); // fade in from the distance.
        this.result.css("display", "block"); // fade in from the distance.
        var that = this;
        if (this.zStopIsAlready == false && this.zStop == true) {
            this.zStopIsAlready = true;
            this.thredIndex = setTimeout(function() { that.Animate(); }, 4000);
        }
        else
            this.thredIndex = setTimeout(function() { that.Animate(); }, 9);
    }
    this.CangeVars = function() {
        this.yVar = 40 + this.y;
        if (this.zStop == false)
            this.zVar = 12 + this.z;
        this.z = this.z + 5;
        if (this.zVar > 200) {
            this.zStop = true
            this.y = this.y + 2 * this.random;
        }
    }
    this.GetOpacity = function() {
        return 2 * this.z;
    }

    this.Run = function() {
        this.index = -1;
        this.random = -1;
        this.xVar = -1;
        this.yVar = -1;
        this.zVar = -1;
        this.zStop = false;
        this.zStopIsAlready = false;
        this.IsRun = true;
        var that = this;
        clearTimeout(this.thredIndex);
        this.result.css("display", "none");
        setTimeout(function() { that.Animate(); }, 800);
    }
    this.Stop = function() {
        this.IsRun = false;
    }
}

Rotator = function (arrayA, arrayRotators) {
    //-----------------------------------------------------------------------
    //
    //-----------------------------------------------------------------------
    this.Scroll = function (objA) {
        var secondRotator;
        var activeRotator = this.array[this.activeIndex].Rotator;
        for (var i = 0; i < this.array.length; i++) {
            var item = this.array[i]
            var obj = $(item.A);
            if (item.A == objA) {
                if (this.activeIndex == i)
                    return;
                this.CheckNews(obj);
                obj.addClass("active");
                this.activeIndex = i;
                secondRotator = item.Rotator;
            }
            else {
                obj.removeClass("active");
            }
        }
        $(secondRotator).css("display", "block");
        $(secondRotator).animate({ "left": "-=1127px" }, "slow");
        var param;
        var rnd = Math.round(Math.random() * 2);

        switch (rnd) {
            case 0:
                param = { "top": "-=460px" };
                break
            case 1:
                param = { "top": "+=460px" };
                break
            case 2:
                param = { "left": "-=1127px" };
                break
            default:
                alert("error");
        }
        $(activeRotator).animate(param, "slow", function () {
            $(this).addClass("second-rotator").removeClass("active-rotator").removeAttr("style");
        });
    }
    //-----------------------------------------------------------------------
    //
    //-----------------------------------------------------------------------
    this.CheckAndChange = function () {
        var hash = document.location.hash;
        if (!hash)
            hash = $(this.array[0].A).attr("href");
        hash = hash.toLowerCase();
        var that = this;
        $.each(this.array, function (i, val) {
            var obj = $(this.A);
            var url = obj.attr("href");

            var index = url.toLowerCase().indexOf(hash);
            if (index >= 0) {
                that.activeIndex = i;
                that.CheckNews.call(that, obj);
                obj.addClass("active");
                $(this.Rotator).addClass("active-rotator");
            }
            else {
                obj.removeClass("active");
                $(this.Rotator).addClass("second-rotator");
            }
        });
    }
    this.CheckNews = function (obj) {
        var url = obj.attr("href");
        var index = url.toLowerCase().indexOf("news");
        if (index >= 0) {
            this.News.Run();
        } else {
            this.News.Stop();
        }
        return url;
    }

    this.array = [];
    this.activeIndex = 0;
    //var hash = document.location.hash;
    //if (arrayA[i].hash == hash)
    //    this.activeIndex = i;
    var that = this;
    for (var i = 0; i < arrayA.length; i++) {
        $(arrayA[i]).click(function () {
            var a = this;
            that.Scroll.call(that, a);
        });
        this.array.push({ A: arrayA[i], Rotator: arrayRotators[i] });
    }
    this.News = new News();
    this.CheckAndChange();
};



$(function() {
    $("#nucleus > div").hover(
        function() {
            $(this).addClass("nucleus-active");
        },
        function() {
            $(this).removeClass("nucleus-active");
        });

});
