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;
				case 8: this.timeout = setTimeout("baloonObject8.go()", 40); break;
				case 9: this.timeout = setTimeout("baloonObject9.go()", 40); break;
				case 10: this.timeout = setTimeout("baloonObject10.go()", 40); break;
				case 11: this.timeout = setTimeout("baloonObject11.go()", 40); break;
				case 12: this.timeout = setTimeout("baloonObject12.go()", 40); break;
				case 13: this.timeout = setTimeout("baloonObject13.go()", 40); break;
				case 14: this.timeout = setTimeout("baloonObject14.go()", 40); break;
				case 15: this.timeout = setTimeout("baloonObject15.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;
var baloonObject8;
var baloonObject9;
var baloonObject10;
var baloonObject11;
var baloonObject12;
var baloonObject13;
var baloonObject14;
var baloonObject15;

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');
    var balloon8 = get('balloon8');
    var balloon9 = get('balloon9');
    var balloon10 = get('balloon10');
    var balloon11 = get('balloon11');
    var balloon12 = get('balloon12');
    var balloon13 = get('balloon13');
    var balloon14 = get('balloon14');
    var balloon15 = get('balloon15');
  
    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;    
    }

    if (balloon8)
    {
        baloonObject8 = new Balloon(balloon8);
        balloon8.parentNode.onmousemove = function(event) {baloonObject8.stick(event);};
        balloon8.onmouseout = blockEvent;
        balloon8.onmousemove = function(event) {baloonObject8.stick(event);};
        balloon8.onmouseover = blockEvent;    
    }

    if (balloon9)
    {
        baloonObject9 = new Balloon(balloon9);
        balloon9.parentNode.onmousemove = function(event) {baloonObject9.stick(event);};
        balloon9.onmouseout = blockEvent;
        balloon9.onmousemove = function(event) {baloonObject9.stick(event);};
        balloon9.onmouseover = blockEvent;    
    }

    if (balloon10)
    {
        baloonObject10 = new Balloon(balloon10);
        balloon10.parentNode.onmousemove = function(event) {baloonObject10.stick(event);};
        balloon10.onmouseout = blockEvent;
        balloon10.onmousemove = function(event) {baloonObject10.stick(event);};
        balloon10.onmouseover = blockEvent;    
    }
    
    if (balloon11)
    {
        baloonObject11 = new Balloon(balloon11);
        balloon11.parentNode.onmousemove = function(event) {baloonObject11.stick(event);};
        balloon11.onmouseout = blockEvent;
        balloon11.onmousemove = function(event) {baloonObject11.stick(event);};
        balloon11.onmouseover = blockEvent;    
    }
    
    if (balloon12)
    {
        baloonObject12 = new Balloon(balloon12);
        balloon12.parentNode.onmousemove = function(event) {baloonObject12.stick(event);};
        balloon12.onmouseout = blockEvent;
        balloon12.onmousemove = function(event) {baloonObject12.stick(event);};
        balloon12.onmouseover = blockEvent;    
    }
    
    if (balloon13)
    {
        baloonObject13 = new Balloon(balloon13);
        balloon13.parentNode.onmousemove = function(event) {baloonObject13.stick(event);};
        balloon13.onmouseout = blockEvent;
        balloon13.onmousemove = function(event) {baloonObject13.stick(event);};
        balloon13.onmouseover = blockEvent;    
    }
    
    if (balloon14)
    {
        baloonObject14 = new Balloon(balloon14);
        balloon14.parentNode.onmousemove = function(event) {baloonObject14.stick(event);};
        balloon14.onmouseout = blockEvent;
        balloon14.onmousemove = function(event) {baloonObject14.stick(event);};
        balloon14.onmouseover = blockEvent;    
    }
    
    if (balloon15)
    {
        baloonObject15 = new Balloon(balloon15);
        balloon15.parentNode.onmousemove = function(event) {baloonObject15.stick(event);};
        balloon15.onmouseout = blockEvent;
        balloon15.onmousemove = function(event) {baloonObject15.stick(event);};
        balloon15.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);
        }		
        if (balloon8)
        {        
            baloonObject8.drop(event);
        }		
        if (balloon9)
        {        
            baloonObject9.drop(event);
        }		
        if (balloon10)
        {        
            baloonObject10.drop(event);
        }		
        if (balloon11)
        {        
            baloonObject11.drop(event);
        }		
        if (balloon12)
        {        
            baloonObject12.drop(event);
        }		
        if (balloon13)
        {        
            baloonObject13.drop(event);
        }		
        if (balloon14)
        {        
            baloonObject14.drop(event);
        }		
        if (balloon15)
        {        
            baloonObject15.drop(event);
        }
    };
}

window.onload = init;

