﻿function ChangeNewsBlock(type) {
    var spanLastNews = document.getElementById('spanLastNews');
    var newsTopRLinck = document.getElementById('newsTopRLinck');
    if (type == 'top' && newsTopRLinck.className != 'curr') {
        spanLastNews.className = '';
        newsTopRLinck.className = 'curr';
        document.getElementById('divTopNews').style.display = '';
        document.getElementById('divLastNews').style.display = 'none';
    }
    if (type == 'lastnews' && spanLastNews.className != 'curr') {
        newsTopRLinck.className = '';
        spanLastNews.className = 'curr';
        document.getElementById('divTopNews').style.display = 'none';
        document.getElementById('divLastNews').style.display = '';
    }
}

function addHandler(object, event, handler, useCapture) {
    // Кроссбраузерная функция для добавления хендлера.
    if (object.addEventListener) // W3C
        object.addEventListener(event, handler, useCapture);
    else if (object.attachEvent) { // IE
        object.attachEvent('on' + event, handler);
    }
    else object['on' + event] = handler; // Other
}

function removeHandler(object, event, handler, useCapture) {
    // Кроссбраузерная функция для удаления хендлера.
    if (object.removeEventListener) // W3C
        object.removeEventListener(event, handler, useCapture);
    else if (object.detachEvent) // IE
        object.detachEvent('on' + event, handler);
    else if (object['on' + event]) object['on' + event] = null; // Other
}

function IYOpacityManager() { }

IYOpacityManager.prototype = {
    SetOpacity: function(elem, nOpacity) {
        var opacityProp = this.GetOpacityProperty();

        if (!elem || !opacityProp) return;

        if (opacityProp == "filter") {
            nOpacity *= 100;
            try { var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha; }
            catch (e) { }
            if (oAlpha) oAlpha.opacity = nOpacity;
            else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
        }
        else elem.style[opacityProp] = nOpacity;
    },

    GetOpacityProperty: function() {
        if (typeof document.body.style.opacity == 'string')
            return 'opacity';
        else if (typeof document.body.style.MozOpacity == 'string')
            return 'MozOpacity';
        else if (typeof document.body.style.KhtmlOpacity == 'string')
            return 'KhtmlOpacity';
        else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1] >= 5.5)
            return 'filter';
        else return false;
    }
}

var OpacityManager = new IYOpacityManager();

function IYFader(timeout, opacityRate) {
    this.timeout = timeout;
    this.opacityRate = opacityRate;
    this.fadeInTimerID = null;
    this.fadeOutTimerID = null;
    this.fadeInOpacity = 0;
    this.fadeOutOpacity = 0;
    this.fadeInElement = null;
    this.fadeOutElement = null;
    this.callBack = null;

    this.init = function() { }
}

IYFader.prototype = {
    FadeIn: function(element, callBack) {
        if (!element) return false;
        else {
            this.callBack = callBack;
            this.fadeInElement = element;
            this.fadeInOpacity = 0;
            function StartFadeIn(fader) {
                if (!fader || fader.fadeInTimerID || !fader.fadeInElement) return false;
                else {
                    var handler = function() {
                        OpacityManager.SetOpacity(fader.fadeInElement, fader.fadeInOpacity);
                        fader.fadeInOpacity += fader.opacityRate;
                        if (fader.fadeInOpacity > 1) fader._resetFadeInTimer();
                    }
                    fader.fadeInTimerID = setInterval(handler, fader.timeout);
                }
            }
            StartFadeIn(this);
        }
    },

    FadeOut: function(element) {
        if (!element) return false;
        else {
            this.fadeOutElement = element;
            this.fadeOutOpacity = 1;
            function StartFadeOut(fader) {
                if (!fader || fader.fadeOutTimerID || !fader.fadeOutElement) return false;
                else {
                    var handler = function() {
                        OpacityManager.SetOpacity(fader.fadeInElement, fader.fadeOutOpacity);
                        fader.fadeOutOpacity -= fader.opacityRate;
                        if (fader.fadeOutOpacity < 0) fader._resetFadeOutTimer();
                    }
                    fader.fadeOutTimerID = setInterval(handler, fader.timeout);
                }
            }
            StartFadeOut(this);
        }
    },

    _resetFadeInTimer: function() {
        clearInterval(this.fadeInTimerID);
        this.fadeInTimerID = null;
        this._tryToCallBack();
    },

    _resetFadeOutTimer: function() {
        clearInterval(this.fadeOutTimerID);
        this.fadeOutTimerID = null;
        this._tryToCallBack();
    },

    _tryToCallBack: function() {
        if ((typeof this.callBack) == "function" && !this.fadeInTimerID && !this.fadeOutTimerID)
            this.callBack();
    }
}

function IYFadeManager() {
    this.container = null;
    this.fader = null;
    this.fadeTimeout = 40;
    this.fadeRate = 0.01;
    this.argsImgUrls = [];
    this.curImgIndex = 0;
    this.delay = 500; // im ms
    this.firstImg = null;
    this.secondImg = null;
    this.initialized = false;
    this.timerID = null;
    this.size = { width: 300, height: 200 }

    this.init = function(Settings) {
        this.container = document.getElementById(Settings.ContainerId);
        if (this.container && Settings.ArgsImgUrls) {
            this.fader = new IYFader(this.fadeTimeout, this.fadeRate);
            this.fader.init();
            this.argsImgUrls = Settings.ArgsImgUrls;
            if (Settings.Delay) this.delay = Settings.Delay;
            if (Settings.Width) this.size.width = Settings.Width;
            if (Settings.Height) this.size.height = Settings.Height;
         
            this.container.style.width = this.size.width + "px";
            this.container.style.height = this.size.height + "px";

            this.firstImg = this.container.getElementsByTagName("img")[0]; // container must have only one image with abs position
            this.firstImg.style.position = "absolute";
            this.firstImg.style.top = "0px";
            this.firstImg.style.left = "0px";
            this.initialized = true;
            this.Action();
        }
    }
}

IYFadeManager.prototype = {
    CreateSecondImg: function() {
        var secondImg = document.createElement("img");
        if (++this.curImgIndex > (this.argsImgUrls.length - 1)) this.curImgIndex = 0;
        secondImg.src = this.argsImgUrls[this.curImgIndex];
        secondImg.style.position = "absolute";
        secondImg.style.top = "0px";
        secondImg.style.left = "0px";
        secondImg.style.width = this.size.width + "px";
        secondImg.style.height = this.size.height + "px";
        secondImg.style.visibility = "hidden";
        return secondImg;
    },

    Action: function() {
        if (this.initialized) {
            var handleFunction = function(fadeManager) {
                fadeManager.secondImg = fadeManager.container.appendChild(fadeManager.CreateSecondImg());
                addHandler(fadeManager.secondImg, "load", function() { fadeManager._startFade(fadeManager); });
                //fadeManager._startFade(fadeManager);
            }
            handleFunction(this);
        }
    },

    _startFade: function(fadeManager) {
        fadeManager.secondImg.style.visibility = "visible";
        OpacityManager.SetOpacity(fadeManager.secondImg, 0);
        fadeManager.fader.FadeIn(fadeManager.secondImg, function() { fadeManager._endFade(fadeManager); });
    },

    _endFade: function(fadeManager) {
        fadeManager.container.removeChild(fadeManager.firstImg);
        fadeManager.firstImg = fadeManager.secondImg;
        fadeManager.Delay();
    },

    Delay: function() {
        function startDelay(fadeManager) {
            var handler = function() {
                clearTimeout(fadeManager.timerID);
                fadeManager.timerID = null;
                fadeManager.Action();
            }
            fadeManager.timerID = setTimeout(handler, fadeManager.delay);
        }
        startDelay(this);
    }
}




