﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('OnLineExt.Questions');

OnLineExt.Questions.QuestionsNotificatorBehavior = function(element) {
    OnLineExt.Questions.QuestionsNotificatorBehavior.initializeBase(this, [element]);
    this._ColorAcc = 25;
    this._ColorMin = 0 + this._ColorAcc;
    this._ColorMax = 255 - this._ColorAcc;
    this._Color = this._ColorMin;

    this._IsAnimated = false;
    this._TimeInterval = 30000; // через какое время опрашивать сервис. По умолчанию 30 сек.
}

OnLineExt.Questions.QuestionsNotificatorBehavior.prototype = {
    initialize: function() {
        OnLineExt.Questions.QuestionsNotificatorBehavior.callBaseMethod(this, 'initialize');

        this._OnInitHandler = Function.createDelegate(this, this._init);
        this._OnAnimationHandler = Function.createDelegate(this, this._animation);
        this._OnPopulateHandler = Function.createDelegate(this, this._onPopulate);
        this._OnFailedHandler = Function.createDelegate(this, this._onFailed);


        this._MessageText = FindShablon(this._element, "MessageText");

        this._init();
    },

    dispose: function() {
        OnLineExt.Questions.QuestionsNotificatorBehavior.callBaseMethod(this, 'dispose');
    },

    _init: function() {
        try {
            EmEx_Services.GetQuestionsStates(this._OnPopulateHandler, this._OnFailedHandler);
        }
        catch (e) { }
    },

    _animation: function() {
        if (this._element && this._MessageText && this._MessageText.innerHTML.length > 0) {
            this._Color += this._ColorAcc;
            if (this._Color >= this._ColorMax || this._Color <= this._ColorMin)
                this._ColorAcc = -this._ColorAcc;
            else
                this._element.style.color = "rgb(255," + this._Color + "," + this._Color + ")";
            setTimeout(this._OnAnimationHandler, 150);
            this._IsAnimated = true;
        }
        else
            this._IsAnimated = false; // Останавливаем анимацию 
    },

    _onPopulate: function(qState) {
        if (this._element) {
            if (this._MessageText) {
                if (qState.QuestionId > 0) {
                    this._element.style.display = '';
                    if (qState.IsNotify)
                        this._MessageText.innerHTML = "Напоминание (" + qState.QuestionsCount + ")";
                    else
                        this._MessageText.innerHTML = "Новые сообщения (" + qState.QuestionsCount + ")";
                }
                else {
                    this._element.style.display = 'none';
                    this._MessageText.innerHTML = '';
                }
            }
            if (this._element.href.indexOf('?qid=') == -1 && qState.QuestionId > 0)
                this._element.href = this._element.href + '?qid=' + qState.QuestionId;
            if (!this._IsAnimated) // Если анимация еще не запущена, то запустить
                this._animation();
            setTimeout(this._OnInitHandler, this._TimeInterval);
        }

        var c = Sys.Application.getComponents();
        for (var i = 0; i < c.length; i++) {
            var tpVar = Object.getType(c[i]);
            if (tpVar.implementsInterface(OnLineExt.Common.ISetUsersStat)) {
                c[i].set_UsersCount(qState.CountUsers);
            }
        }
    },

    _onFailed: function(e) {
        //alert(e._message);
    },

    get_TimeInterval: function() { return this._TimeInterval; },
    set_TimeInterval: function(value) { this._TimeInterval = value; }

}
OnLineExt.Questions.QuestionsNotificatorBehavior.registerClass('OnLineExt.Questions.QuestionsNotificatorBehavior', AjaxControlToolkit.BehaviorBase);
