﻿var Position = null;
var PaddingX = null;
var PaddingY = null;
var Speed = null;
var SetPaddingX = 50;
var SetPaddingY = 100;
var SetSpeed = 0; // 0.001;
var ObjectList = null;
var Interval = 1;
var Sector = null;

function Animate()
{
    for (i = 0; i < ObjectList.length; i++)
    {
        var RX = (document.body.clientWidth - ObjectList[i].offsetWidth) / 2 - PaddingX;
        var RY = (document.body.clientHeight - ObjectList[i].offsetHeight) / 2 - PaddingY;
        var top = PaddingY + RY + (RY * Math.sin(Position + (i * Sector)));
        var left = PaddingX + RX + (RX * Math.cos(Position + (i * Sector)));
        if (top < 0) return;
        if (left < 0) return;
        ObjectList[i].style.top = parseInt(top) + 'px';
        ObjectList[i].style.left = parseInt(left) + 'px';
        ObjectList[i].style.visibility = 'visible';
        Position = Position + Speed / (Math.PI * 2);
        if (Position >= Math.PI * 2) Position = 0;
    }
    
    if (PaddingX > SetPaddingX) PaddingX = PaddingX - 0.05 * (PaddingX - SetPaddingX);
    if (PaddingY > SetPaddingY) PaddingY = PaddingY - 0.05 * (PaddingY - SetPaddingY);
    if (Speed > SetSpeed) Speed = Speed - 0.05 * (Speed - SetSpeed);
    if (Speed == 0) return;
    Interval = 0.1 / Speed;
    setTimeout('Animate()', Interval);
}

function InitAnimation()
{
    if (PaddingX == null) PaddingX = parseInt(document.body.clientWidth / 3);
    if (PaddingY == null) PaddingY = parseInt(document.body.clientHeight / 3);
    if (Speed == null) Speed = 0.1;

    var Logo = document.getElementById("Logo");
    Logo.style.top = ((document.body.clientHeight - Logo.offsetHeight) / 2) + 'px';
    Logo.style.left = ((document.body.clientWidth - Logo.offsetWidth) / 2) + 'px';
    Logo.style.visibility = 'visible';
    //var Season = document.getElementById("Season");
    //Season.style.top = (document.body.clientHeight - Season.offsetHeight - 28) + 'px';
    //Season.style.left = (document.body.clientWidth - Season.offsetWidth) + 'px';
    //Season.style.visibility = 'visible';

    if (ObjectList == null)
    {
        ObjectList = new Array();
        var Layer = document.getElementsByTagName('div');
        for (var i = 0; i < Layer.length; i++)
            if (Layer[i].className == 'AnimatingBox')
                ObjectList[ObjectList.length] = Layer[i];
        Sector = Math.PI * 2 / ObjectList.length;
        Position = Math.random() * 360;
    }
    
    self.setTimeout('Animate()', Interval);
}

function DelayedInitAnimation()
{
    self.setTimeout('InitAnimation()', 1);
}

if (window.addEventListener)
{
    window.addEventListener("load", DelayedInitAnimation, false);
    window.addEventListener("resize", DelayedInitAnimation, false);
}
else if (window.attachEvent) // ie
{
    window.attachEvent('onload', DelayedInitAnimation);
    window.attachEvent('onresize', DelayedInitAnimation);
}

