﻿Event.observe(window, 'load', function()
{


});



ClientData = function(controlId, instanceName)
{
    this.controlId = controlId;
    this.instanceName = instanceName;


    this.fadeTime = 0.3;
    this.clientRange;
    this.clientRange2; //for vertical
    this.movedWidth = 0;
    this.movedLine1;
    this.movedLine1X = 0;
    this.movedLine1Left = 0;
    this.movedLine2;
    this.movedLine2X = 0;
    this.movedLine2Left = 0;
    this.movedLineVelocity = -1;

    this.acceleration = 0;
    this.arrowVelocity = 0;
    this.currentVertClient = 0;

    this.clients = null;

    if (Sys.Browser.agent == Sys.Browser.Firefox) this.isFF = true;
    else this.isFF = false;

    this.isVertMenu = false;

    this.ChangeVerticalTimer = null;
    this.isVertChanging = false;

    this.vertChangeTime = 7000;
    this.vertActiveClientRange = 1;
    this.vertPos = 0;

    this.vertWidth = 214;

}

ClientData.prototype =
{

    getControlId: function()
    {
        return this.controlId;
    },

    InitHorizontal: function()
    {

        this.isVertMenu = false;
        var element = $(this.controlId);


        if (element != null)
        {

            var leftArrowButton = $(this.controlId + "_left");
            var rightArrowButton = $(this.controlId + "_right");

            leftArrowButton.onclick = Function.createDelegate(this, this.ArrowClickLeft);
            rightArrowButton.onclick = Function.createDelegate(this, this.ArrowClickRight);

            var clients = element.select("div.Client");

            if (clients.length > 0)
            {
                this.clientRange = $(this.controlId + "_ClientRange");

                var lineContainer = $(this.controlId + "_LineContainer");

                lineContainer.parentNode.onmouseover = Function.createDelegate(this, this.StopMove);
                lineContainer.parentNode.onmouseout = Function.createDelegate(this, this.StartMove);
                var tempWidth = element.style.width.substr(0, element.style.width.length - 2);
                tempWidth = tempWidth - 24;
                lineContainer.style.width = tempWidth.toString() + "px";

                var lineImgContainer1 = $(this.controlId + "_lineImgContainer1");
                var lineImgContainer2 = $(this.controlId + "_lineImgContainer2");

                this.movedLine1 = $(this.controlId + "_movedLine1");
                this.movedLine2 = $(this.controlId + "_movedLine2");

                this.movedWidth = 0;
                for (var i = 0; i < clients.length; i++)
                {
                    clients[i].id = "ClientData_" + i.toString();

                    var td1 = document.createElement("TD");
                    td1.className = "LogoTD";

                    var src = clients[i].getAttribute('src');
                    var isShowLogo = Boolean(clients[i].getAttribute('isShowLogo'));


                    if (isShowLogo)
                    {
                        var img1 = document.createElement("img");
                        img1.src = clients[i].getAttribute('src');

                    }
                    else
                    {

                        var img1 = document.createElement("span");
                        img1.innerHTML = src;
                        img1.className = "LogoText";
                    }
                    img1.id = "LogoImg1_" + i.toString();
                    img1.onclickid = clients[i].id;
                    lineImgContainer1.appendChild(td1);
                    td1.appendChild(img1);

                    Effect.Fade(img1.id, { duration: 0.01, from: 0.4, to: 0.4 });
                    img1.isOvering = false;
                    img1.isOuting = false;
                    img1.onmouseover = function()
                    {
                        if (this.isOvering == false && this.isOuting == false)
                        {
                            this.isOvering = true;
                            Effect.Fade(this.id, { duration: 0.3, from: 0.4, to: 1.0 });

                        }
                    }
                    img1.onmouseout = function()
                    {
                        if (this.isOuting == false)
                        {
                            this.isOuting = true;
                            if (this.isOvering == false)
                            {
                                window.setTimeout("$('" + this.id + "').isOuting = false;", 300);
                                Effect.Fade(this.id, { duration: 0.3, from: 1.0, to: 0.4 });
                            }
                            else
                            {
                                window.setTimeout("$('" + this.id + "').isOvering = false;", 300);
                                window.setTimeout("$('" + this.id + "').isOuting = false;", 600);
                                window.setTimeout("Effect.Fade('" + this.id + "', { duration: 0.3, from: 1.0, to: 0.4 });", 300);
                            }
                        }

                    }

                    img1.ClientRangeControl = this;
                    img1.onclick = function()
                    {
                        Effect.Fade(this.ClientRangeControl.clientRange.id, { duration: this.ClientRangeControl.fadeTime, from: 1.0, to: 0.01 });
                        window.setTimeout(this.ClientRangeControl.instanceName + ".clientRange.innerHTML = $('" + this.onclickid + "').innerHTML;", this.ClientRangeControl.fadeTime * 1000);
                        window.setTimeout("Effect.Fade('" + this.ClientRangeControl.clientRange.id + "', { duration: " + this.ClientRangeControl.fadeTime + ", from: 0.001, to: 1.0 });", this.ClientRangeControl.fadeTime * 2 * 1000);
                    }


                    var td2 = document.createElement("TD");
                    td2.className = "LogoTD";

                    if (isShowLogo)
                    {
                        var img2 = document.createElement("img");
                        img2.src = clients[i].getAttribute('src');
                    }
                    else
                    {
                        var img2 = document.createElement("span");
                        img2.innerHTML = src;
                        img2.className = "LogoText";
                    }
                    img2.id = "LogoImg2_" + i.toString();
                    img2.onclickid = clients[i].id;
                    lineImgContainer2.appendChild(td2);
                    td2.appendChild(img2);

                    Effect.Fade(img2.id, { duration: 0.01, from: 0.4, to: 0.4 });
                    img2.isOvering = false;
                    img2.isOuting = false;
                    img2.onmouseover = function()
                    {
                        if (this.isOvering == false && this.isOuting == false)
                        {
                            this.isOvering = true;
                            Effect.Fade(this.id, { duration: 0.3, from: 0.4, to: 1.0 });
                        }
                    }
                    img2.onmouseout = function()
                    {
                        if (this.isOuting == false)
                        {
                            this.isOuting = true;
                            if (this.isOvering == false)
                            {
                                window.setTimeout("$('" + this.id + "').isOuting = false;", 300);
                                Effect.Fade(this.id, { duration: 0.3, from: 1.0, to: 0.4 });
                            }
                            else
                            {
                                window.setTimeout("$('" + this.id + "').isOvering = false;", 300);
                                window.setTimeout("$('" + this.id + "').isOuting = false;", 600);
                                window.setTimeout("Effect.Fade('" + this.id + "', { duration: 0.3, from: 1.0, to: 0.4 });", 300);
                            }
                        }
                    }

                    img2.ClientRangeControl = this;

                    img2.onclick = function()
                    {
                        Effect.Fade(this.ClientRangeControl.clientRange.id, { duration: this.ClientRangeControl.fadeTime, from: 1.0, to: 0.01 });
                        window.setTimeout(this.ClientRangeControl.instanceName + ".clientRange.innerHTML = $('" + this.onclickid + "').innerHTML;", this.ClientRangeControl.fadeTime * 1000);
                        window.setTimeout("Effect.Fade('" + this.ClientRangeControl.clientRange.id + "', { duration: " + this.ClientRangeControl.fadeTime + ", from: 0.001, to: 1.0 });", this.ClientRangeControl.fadeTime * 2 * 1000);
                    }

                }

                setTimeout(Function.createDelegate(this, this.SetWidth), 400);

                var randomClient = Math.ceil(clients.length * Math.random()) - 1;
                this.clientRange.innerHTML = $(clients[randomClient].id).innerHTML;

                this.movedLine1X = this.movedLine1.offsetLeft;

                window.setTimeout(Function.createDelegate(this, this.MoveLine), 420);
            }
        }
    },

    SetWidth: function()
    {
        this.movedWidth = this.movedLine1.offsetWidth;
        this.movedLine2X = this.movedLine1.offsetWidth + this.movedLine1.offsetLeft;
    },

    MoveLine: function()
    {
        if (this.movedWidth == 0)
        {
            this.movedWidth = this.movedLine1.offsetWidth;
            this.movedLine2X = this.movedLine1.offsetWidth + this.movedLine1.offsetLeft;
        }
        if (this.arrowVelocity == 0)
        {
            if (this.acceleration == 0)
            {
                this.movedLine1X += this.movedLineVelocity;
                this.movedLine2X += this.movedLineVelocity;
            }
            else if (this.acceleration < 0)
            {
                this.acceleration++;
                if (this.acceleration <= -8 || this.acceleration == -7 || this.acceleration == -5 || this.acceleration == -3 || this.acceleration == -1)
                {
                    this.movedLine1X -= 1;
                    this.movedLine2X -= 1;
                }
                if (this.acceleration >= 0) this.movedLineVelocity = 0;
            }
        }
        else
        {
            if (this.arrowVelocity < 0)
            {
                this.arrowVelocity++;
                this.movedLine1X -= 20;
                this.movedLine2X -= 20;
            }
            else
            {
                this.arrowVelocity--;
                this.movedLine1X += 18;
                this.movedLine2X += 18;
            }
        }


        if (this.movedLine1X < -this.movedWidth)
        {
            this.movedLine1X += this.movedWidth * 2;
        }
        if (this.movedLine2X < -this.movedWidth)
        {
            this.movedLine2X += this.movedWidth * 2;
        }

        if (this.movedLine1X > this.movedWidth)
        {
            this.movedLine1X -= this.movedWidth * 2;
        }
        if (this.movedLine2X > this.movedWidth)
        {
            this.movedLine2X -= this.movedWidth * 2;
        }

        if (this.movedLineVelocity != 1 && this.movedLineVelocity != -1) this.movedLineVelocity = 0;


        this.movedLine1.style.left = this.movedLine1X + "px";
        this.movedLine2.style.left = this.movedLine2X + "px";

        var velSlow = 40;
        if (this.isFF) velSlow = 55;

        if (this.arrowVelocity != 0)
        {
            velSlow = 20;
            if (this.isFF) velSlow = 30;
        }


        window.setTimeout(Function.createDelegate(this, this.MoveLine), velSlow);


    },

    ArrowClickLeft: function(direction)
    {
        if (this.isVertMenu)
        {
            this.MoveVertical(-1);
        }
        else
        {
            this.arrowVelocity = 14;
        }

    },
    ArrowClickRight: function(direction)
    {
        if (this.isVertMenu)
        {
            this.MoveVertical(1);
        }
        else
        {
            this.arrowVelocity = -14;
        }
    },

    StartMove: function()
    {
        this.movedLineVelocity = -1;
        this.acceleration = 0;
    },

    StopMove: function()
    {
        this.movedLineVelocity = 0;
    },

    FadeVertical: function(direction)
    {
        if (!this.isVertChanging)
        {
            this.SetChangingFlag();

            if (direction == -1)
            {
                this.currentVertClient--;
                if (this.currentVertClient < 0) this.currentVertClient = this.clients.length - 1;
                if (this.ChangeVerticalTimer != null)
                {
                    window.clearTimeout(this.ChangeVerticalTimer);
                    this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), 5000);
                }
            }
            else if (direction == 1)
            {
                this.currentVertClient++;
                if (this.currentVertClient >= this.clients.length) this.currentVertClient = 0;
                if (this.ChangeVerticalTimer != null)
                {
                    window.clearTimeout(this.ChangeVerticalTimer);
                    this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), 5000);
                }
            }
            var cuurentId = "";
            if (this.vertActiveClientRange == 1) cuurentId = this.clientRange.id;
            else cuurentId = this.clientRange2.id;

            Effect.Fade(cuurentId, { duration: this.fadeTime, from: 1.0, to: 0.01 });
            window.setTimeout(this.instanceName + ".clientRange.innerHTML = $('" + this.clients[this.currentVertClient].id + "').innerHTML;", this.fadeTime * 1000);
            window.setTimeout("Effect.Fade('" + cuurentId + "', { duration: " + this.fadeTime + ", from: 0.001, to: 1.0 });", this.fadeTime * 2 * 1000);
            window.setTimeout(Function.createDelegate(this, this.SetChangingFlag), this.fadeTime * 2 * 1000);
        }
    },

    MoveVertical: function(direction)
    {
        if (!this.isVertChanging)
        {
            this.SetChangingFlag();

            if (direction == -1)
            {
                this.currentVertClient--;
                if (this.currentVertClient < 0) this.currentVertClient = this.clients.length - 1;
                if (this.ChangeVerticalTimer != null)
                {
                    window.clearTimeout(this.ChangeVerticalTimer);
                    this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), this.vertChangeTime);
                }

                if (this.vertActiveClientRange == 1)
                {
                    $(this.clientRange2.id).style.left = (this.vertPos.x - this.vertWidth) + "px";
                    $(this.clientRange2.id).innerHTML = $(this.clients[this.currentVertClient].id).innerHTML;
                }
                else
                {
                    $(this.clientRange.id).style.left = (this.vertPos.x - this.vertWidth) + "px";
                    $(this.clientRange.id).innerHTML = $(this.clients[this.currentVertClient].id).innerHTML;
                }
                new Effect.Move($(this.clientRange.id), { x: this.vertWidth, y: 0, duration: 1.0 });
                new Effect.Move($(this.clientRange2.id), { x: this.vertWidth, y: 0, duration: 1.0 });

            }
            else if (direction == 1)
            {
                this.currentVertClient++;
                if (this.currentVertClient >= this.clients.length) this.currentVertClient = 0;
                if (this.ChangeVerticalTimer != null)
                {
                    window.clearTimeout(this.ChangeVerticalTimer);
                    this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), this.vertChangeTime);
                }

                if (this.vertActiveClientRange == 1)
                {
                    $(this.clientRange2.id).style.left = (this.vertPos.x + this.vertWidth) + "px";
                    $(this.clientRange2.id).innerHTML = $(this.clients[this.currentVertClient].id).innerHTML;
                }
                else
                {
                    $(this.clientRange.id).style.left = (this.vertPos.x + this.vertWidth) + "px";
                    $(this.clientRange.id).innerHTML = $(this.clients[this.currentVertClient].id).innerHTML;
                }

                new Effect.Move($(this.clientRange.id), { x: -this.vertWidth, y: 0, duration: 1.0 });
                new Effect.Move($(this.clientRange2.id), { x: -this.vertWidth, y: 0, duration: 1.0 });
            }

            this.vertActiveClientRange = -this.vertActiveClientRange + 3;

            window.setTimeout(Function.createDelegate(this, this.SetChangingFlag), this.fadeTime * 2100);
        }
    },

    ChangeVertical: function()
    {
    //Случайный выбор после временной задержки.
//        var i = Math.ceil(this.clients.length * Math.random()) - 1;
//        while (this.currentVertClient == i)
//            i = Math.ceil(this.clients.length * Math.random()) - 1;
//        this.currentVertClient = i;

        this.MoveVertical(1);
        this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), this.vertChangeTime);
        
    },

    SetChangingFlag: function()
    {
        this.isVertChanging = !this.isVertChanging;
    },

    InitVertical: function()
    {
        this.fadeTime = 0.5;
        this.isVertMenu = true;

        var element = $(this.controlId);

        var arPos = 1;
        var clPos = 2;

        if (element != null)
        {

            var leftArrowButton = $(this.controlId + "_left");
            var rightArrowButton = $(this.controlId + "_right");
            leftArrowButton.onclick = Function.createDelegate(this, this.ArrowClickLeft);
            rightArrowButton.onclick = Function.createDelegate(this, this.ArrowClickRight);

            this.clients = element.select("div.Client");

            if (this.clients.length > 0)
            {
                this.clientRange = $(this.controlId + "_ClientRange");
                this.clientRange2 = $(this.controlId + "_ClientRange2");

                this.vertPos = getAbsolutePosition($(this.clientRange.id));
                $(this.clientRange.id).style.top = this.vertPos.y + "px";
                $(this.clientRange2.id).style.top = this.vertPos.y + "px";

                $(this.clientRange.id).style.left = this.vertPos.x + "px";
                $(this.clientRange.id).style.top = this.vertPos.y + "px";


                this.vertActiveClientRange = 1;

                var isExistProject = false;
                for (var i = 0; i < this.clients.length; i++)
                {
                    this.clients[i].id = "ClientDataVert_" + i.toString();
                    var isselected = this.clients[i].getAttribute('isselected');
                    if (isselected == "True")
                    {
                        isExistProject = true;
                        this.currentVertClient = i;
                    }
                }
                if (!isExistProject) this.currentVertClient = Math.ceil(this.clients.length * Math.random()) - 1;
                this.clientRange.innerHTML = $(this.clients[this.currentVertClient].id).innerHTML;

                if (!isExistProject)
                this.ChangeVerticalTimer = window.setTimeout(Function.createDelegate(this, this.ChangeVertical), this.vertChangeTime);

            }
        }
    }


}

document.observe("dom:loaded", function()
{
    //    ClientHoriz = new ClientData("ClientRangeControl1", "ClientHoriz");
    //    ClientHoriz.InitHorizontal();

    //Collapsed Header Links initialize
    var hdss = $$(".CollapsedHeader");
    for (var i = 0; i < hdss.length; i++)
    {
        hdss[i].id = "CollapsedHeaderLink" + i.toString();

    }

    if (readCookie("OpenedAccordionId" + document.location))
    {
        var accordions = readCookie("OpenedAccordionId" + document.location).split("*");
        for (var i = 0; i < accordions.length; i++)
        {
            var accordion = document.getElementById(accordions[i]);
            if (accordion) OnClickCollapsedHeaderToggle(accordion);
        }
    }

    //Исправление ошибки bitrix. После апдейта февр 2010.
    var el = document.getElementById("ctl00_Requisite_UP_AJAX");
    if (el != null)
    {
        el.parentNode.style.display = "none";
    }

});

function getAbsolutePosition(element)
{
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.parent)
    {
        var tmp = getAbsolutePosition(element.parent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
};
