Balloon = function(elem)
{
    this.elem = elem;
    this.defaultX = parseInt(elem.style.left);
    this.defaultY = parseInt(elem.style.top);
    
    this.snap = false;
    this.shiftX = 0;
    this.shiftY = 0;

    this.newX = 0;
    this.newY = 0;
    
    this._x = this.defaultX;
    this._y = this.defaultY;
    
    this.dx = 0;
    this.dy = 0;

    this.timeout = false;
    this.duration = 60;
    this.time = 0;
    this.radius = 150;
    
    this.amplX = 0.85;
    this.amplY = 0.85;

    this.stick = function (event)
    {   
        if (this.elem.getElementsByTagName('A')) if (this.elem.getElementsByTagName('A')[0]) this.elem.getElementsByTagName('A')[0].className = 'hover';
        
        this.snap = true;
        if (!event)
        {
            event = window.event;
        }        
        if (event.layerX && event.layerY)
        {
            if (event.target == this.elem.parentNode)
            {
                this.newX = event.layerX - this.elem.offsetWidth / 2;
                this.newY = event.layerY - this.elem.offsetHeight / 2;
            }
            if (event.target == this.elem || event.target.parentNode == this.elem)
            {
                this.newX = this._x + event.layerX - this.elem.offsetWidth / 2;
                this.newY = this._y + event.layerY - this.elem.offsetHeight / 2;
            }
        }
        else if (event.offsetX && event.offsetY && event.srcElement)
        {            
            this.newX = event.offsetX - this.elem.offsetWidth / 2;
            this.newY = event.offsetY - this.elem.offsetHeight / 2;    
            
            if (event.srcElement == this.elem)
            {
                this.newX = this._x + event.offsetX - this.elem.offsetWidth / 2;
                this.newY = this._y + event.offsetY - this.elem.offsetHeight / 2;
            }
            else if (event.srcElement.parentNode == this.elem)                
            {
                this.newX = parseInt(this.elem.style.left) + event.offsetX + event.srcElement.offsetLeft - this.elem.offsetWidth / 2;
                this.newY = parseInt(this.elem.style.top) + event.offsetY + event.srcElement.offsetTop - this.elem.offsetHeight / 2;                
            }
        }
        else
        {            
            return false;
        }
        
        if (window.opera && event.srcElement.tagName == 'A')
        {
//            return false;
        }
        clearTimeout(this.timeout)

        var xx = this.defaultX - this.newX;
        var yy = this.defaultY - this.newY;
        
        if (this.newX + this.elem.offsetWidth + this.elem.parentNode.offsetLeft > this.elem.parentNode.parentNode.offsetWidth || this.newX + this.elem.parentNode.offsetLeft < 20)
        {
            this.drop(event);
            return false;        
        }
        
        
        if ((xx * xx) / (this.elem.offsetWidth * this.elem.offsetWidth) + (yy * yy) / (this.elem.offsetHeight * this.elem.offsetHeight) >= 1)
        {
            this.drop(event);
            return false;
        }
        else
        {
            if (this.newX - this._x > 100)
            {
                this.amplX = 0.85;
            }
            else
            {
                this.amplX = 0.8;
            }
            if (this.newY - this._y > 100)
            {
                this.amplX = 0.85;
            }
            else
            {
                this.amplX = 0.8;
            }
            
            this.time = 0;
            blockEvent(event);            
            this.go();            
        }
        return false;
    }
    
    this.drop = function (event)
    {
        if (!event)
        {
            event = window.event;
        }
        
        if (event.srcElement)
        {
            if (event.srcElement == this.elem || event.srcElement.parentNode == this.elem)
            {
                return false;
            }
        }
        
        if (this.snap)
        {
            if (this.elem.getElementsByTagName('A')) if (this.elem.getElementsByTagName('A')[0]) this.elem.getElementsByTagName('A')[0].className = '';
            this.snap = false;
            this.time = 0;
            this.newX = this.defaultX;
            this.newY = this.defaultY;
            this.amplX = 0.85;
            this.amplY = 0.85;            
            this.go();
        }
        return false;
    }

    this.go = function ()
    {
        if (this.time <= this.duration)
        {
            this.dx += ( this.newX - this._x ) * 0.25;// скорость (1-макс., 0-мин.)
            this.dx *= this.amplX; // затухание
            this._x += this.dx;
            //
            this.dy += ( this.newY - this._y ) * 0.25
            this.dy *= this.amplY;
            this._y += this.dy;
            
            if (this.snap == false) 
            {
                if ( Math.abs(this.dx) < 0.1 && Math.abs(this.dy) < 0.1 ) 
                {
			        this.elem.style.left = this.defaultX;
			        this.elem.style.top = this.defaultY;
                    clearTimeout(this.timeout);
                    return false;
                }
            }

            if (this._x + this.elem.offsetWidth + this.elem.parentNode.offsetLeft < this.elem.parentNode.parentNode.offsetWidth - 50 || this._x + this.elem.parentNode.offsetLeft > 0)
            {
                this.elem.style.left = this._x + 'px';
            }
            else 
            {
                this.elem.style.left = this.defaultX - Math.random() * 5;
            }
            this.elem.style.top = this._y + 'px';
            this.time++;
            switch(parseInt(this.elem.id.replace('balloon', '')))
            {
                case 1: this.timeout = setTimeout("baloonObject1.go()", 40); break;
                case 2: this.timeout = setTimeout("baloonObject2.go()", 40); break;
                case 3: this.timeout = setTimeout("baloonObject3.go()", 40); break;
                case 4: this.timeout = setTimeout("baloonObject4.go()", 40); break;
                case 5: this.timeout = setTimeout("baloonObject5.go()", 40); break;
                case 6: this.timeout = setTimeout("baloonObject6.go()", 40); break;
                case 7: this.timeout = setTimeout("baloonObject7.go()", 40); break;
            }
        }
        else
        {
            clearTimeout(this.timeout);
        }
    }
}

function get(id)
{
    return document.getElementById(id) || false;
}

function blockEvent(event)
{    
    if (!event)
    {
        event = window.event;
    }
    if (event.stopPropagation) 
    {
        event.stopPropagation();
    }
    else 
    {
        event.cancelBubble = true;
    }
    if (event.preventDefault) 
    {
        event.preventDefault();
    }
    else 
    {
        event.returnValue = false;
    }
}

Math.easeOutElastic = function (t, b, c, d, a, p) 
{
    // t: current time, 
    // b: beginning value, 
    // c: change in value, 
    // d: duration, 
    // a: amplitude (optional),
    // p: period (optional)
    // t and d can be in frames or seconds/milliseconds 
    
    if (t == 0) 
    {
        return b;
    }
    if ((t /= d) == 1) 
    {
        return b + c;
    }
    if (!p) 
    {
        p = d*.3;
    }
    if (a < Math.abs(c))
    {
        a = c;
        var s = p / 4;
    }
    else 
    {
        var s = p / (2 * Math.PI) * Math.asin(c / a);
    }
    return Math.round(a * Math.pow(2,-10*t) * Math.sin( (t * d - s) * (2 * Math.PI) / p ) + c + b);
};

Math.easeInOutBack = function (t, b, c, d, s) 
{
    // t: current time, 
    // b: beginning value, 
    // c: change in value, 
    // d: duration, 
    // s: overshoot amount (optional)
    // t and d can be in frames or seconds/milliseconds
    // s controls the amount of overshoot: higher s means greater overshoot
    // s has a default value of 1.70158, which produces an overshoot of 10 percent
    // s==0 produces cubic easing with no overshoot 
    if (s == undefined) 
    {
        s = 1.70158;
    }
    if ((t/=d/2) < 1) 
    {
        return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    }
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
};

var baloonObject1;
var baloonObject2;
var baloonObject3;
var baloonObject4;
var baloonObject5;
var baloonObject6;
var baloonObject7;

function init()
{
    
    var balloon1 = get('balloon1');
    var balloon2 = get('balloon2');
    var balloon3 = get('balloon3');
    var balloon4 = get('balloon4');
    var balloon5 = get('balloon5');
    var balloon6 = get('balloon6');
    var balloon7 = get('balloon7');
  
    if (balloon1)
    {
        baloonObject1 = new Balloon(balloon1);
        balloon1.parentNode.onmousemove = function(event) {baloonObject1.stick(event);};
        balloon1.onmouseout = blockEvent;
        balloon1.onmousemove = function(event) {baloonObject1.stick(event);};
        balloon1.onmouseover = blockEvent;    
    }
    
    if (balloon2)
    {
        baloonObject2 = new Balloon(balloon2);
        balloon2.parentNode.onmousemove = function(event) {baloonObject2.stick(event);};
        balloon2.onmouseout = blockEvent;
        balloon2.onmousemove = function(event) {baloonObject2.stick(event);};
        balloon2.onmouseover = blockEvent;    
    }

    if (balloon3)
    {
        baloonObject3 = new Balloon(balloon3);
        balloon3.parentNode.onmousemove = function(event) {baloonObject3.stick(event);};
        balloon3.onmouseout = blockEvent;
        balloon3.onmousemove = function(event) {baloonObject3.stick(event);};
        balloon3.onmouseover = blockEvent;    
    }

    if (balloon4)
    {
        baloonObject4 = new Balloon(balloon4);
        balloon4.parentNode.onmousemove = function(event) {baloonObject4.stick(event);};
        balloon4.onmouseout = blockEvent;
        balloon4.onmousemove = function(event) {baloonObject4.stick(event);};
        balloon4.onmouseover = blockEvent;    
    }

    if (balloon5)
    {
        baloonObject5 = new Balloon(balloon5);
        balloon5.parentNode.onmousemove = function(event) {baloonObject5.stick(event);};
        balloon5.onmouseout = blockEvent;
        balloon5.onmousemove = function(event) {baloonObject5.stick(event);};
        balloon5.onmouseover = blockEvent;    
    }

    if (balloon6)
    {
        baloonObject6 = new Balloon(balloon6);
        balloon6.parentNode.onmousemove = function(event) {baloonObject6.stick(event);};
        balloon6.onmouseout = blockEvent;
        balloon6.onmousemove = function(event) {baloonObject6.stick(event);};
        balloon6.onmouseover = blockEvent;    
    }

    if (balloon7)
    {
        baloonObject7 = new Balloon(balloon7);
        balloon7.parentNode.onmousemove = function(event) {baloonObject7.stick(event);};
        balloon7.onmouseout = blockEvent;
        balloon7.onmousemove = function(event) {baloonObject7.stick(event);};
        balloon7.onmouseover = blockEvent;    
    }
    
    document.body.onmousemove = function(event) 
    {
        if (balloon1)
        {
            baloonObject1.drop(event);
        }
        if (balloon2)
        {        
            baloonObject2.drop(event);
        }        
        if (balloon3)
        {        
            baloonObject3.drop(event);
        }
        if (balloon4)
        {        
            baloonObject4.drop(event);
        }
        if (balloon5)
        {        
            baloonObject5.drop(event);
        }
        if (balloon6)
        {        
            baloonObject6.drop(event);
        }
        if (balloon7)
        {        
            baloonObject7.drop(event);
        }
    };
    window.a1 = false;
    window.a2 = false;
    window.a2 = false;
    document.onkeydown = keyPress;
    if (document.cookie.search('hell') > -1) hell();
}

function hell()
{
    var divs = document.body.getElementsByTagName('DIV');
    for (i in divs)
    {
        if (divs[i].className == 'container' && document.body.className == 'travelling_motion')
        {
            divs[i].style.cssText = 'background:transparent url(/images/__backend/common/hell.jpg) no-repeat scroll 50% 0;';                        
            document.cookie = "hell=1";
        }
    }    
}

function keyPress(event)
{
    if (window.event) 
    {    
        event = window.event;
    }
    //alert(event.keyCode ? event.keyCode : event.which ? event.which : null);
    switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
    {
        case 72:
            window.a3 = false;    
            window.a2 = false;
            window.a1 = true;            
            //h
        break;
        case 69:
            window.a3 = false;        
            window.a2 = window.a1 ? true : false;            
            window.a1 = false;
            //e
        break;
        case 76:
            if (window.a3)
            {
                hell();
                document.onkeydown = function () {return false;};
            }
            window.a3 = window.a2 ? true : false;
            window.a2 = false;
            window.a1 = false;
            //l
        break;
        default:
            window.a3 = false;
            window.a2 = false;
            window.a1 = false;
        break;
    }
}
window.onload = init;
