var ballsCanvas = null;
var balls = [];
var kin;
// physics globals
var collisionDamper = 0.3;

var mouseX = 99999;
var mouseY = 99999;
var balls = null;
var ballRadius = 10;
var images = [];
var googleMap = false;
function loadImages(callback) {
    var loadedImages = 0;
    var numImages = 10;


    for (i = 1; i <= 10; i++) {
        images[i] = new Image();
        images[i].onload = function() {
            if (++loadedImages >= numImages) {
                callback();
            }
        };
        images[i].src = '/images/balls/' + i + '.png';
    }
}
function initializeMap() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(43.238239, 76.914532), 15);
        map.setUIToDefault();
        var point = new GLatLng(43.238372, 76.913931);
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(65, 65);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(40, 50);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);

        var icon = new GIcon(baseIcon);
        icon.image = "/images/google.png";

        map.addOverlay(new GMarker(point,{icon:icon}));
        map.disableScrollWheelZoom();
    }
}
$(function() {
    // google map
    $('.map-0').toggle(function() {
        $('.googl-map').slideDown(function() {
            if (!googleMap) {
                initializeMap();
                googleMap = true;
            }
            document.location.href = '#map';
        });
    }, function() {
        $('.googl-map').slideUp();
    });


//    Анимация логотипа
    var canvas = document.getElementById('logoCanvas');
    if (canvas)
        if (canvas.getContext) {
            var ctx = canvas.getContext('2d');
            ctx.fillStyle = "White";
            ctx.strokeStyle = "White";
            ctx.translate(99, 70);
            ctx.scale(1, 1.03);
            var mouse = false;
            var animation = 0;
            var sensivity = 45;
            var r1 = 0.52 * Math.PI;
            var r2 = 0;
            var eye = false;

            function draw() {

                ctx.clearRect(-75, -75, 150, 150);
                ctx.beginPath();
                ctx.arc(0, 0, 40, r2, r1, false);
                ctx.lineTo(-3, 35);
                ctx.arc(0, 0, 35, r1, r2, true);
                ctx.fill();


            }

            function eyes(r) {
                if (mouse)
                    if (r) {
                        ctx.beginPath();
                        ctx.arc(9, 22, 3, 0, Math.PI * 2, true);
                        ctx.fill();
                        ctx.beginPath();
                        ctx.arc(22, 11, 3, 0, Math.PI * 2, true);
                        ctx.fill();
                    } else {
                        draw();
                        ctx.beginPath();
                        ctx.arc(9, 22, 3, 0, Math.PI * 2, true);
                        ctx.fill();
                        ctx.lineWidth = 2;
                        ctx.beginPath();
                        ctx.arc(22, 11, 2, Math.PI * 0.8, -Math.PI * 0.3, true);
                        ctx.stroke();
                        ctx.lineWidth = 1;
                        setTimeout(function() {
                            eyes(true);
                        }, 500);

                    }

            }

            function smile(r) {
                if (r === true) {
                    if (animation < sensivity - 5)
                        setTimeout(function() {
                            animation++;
                            ctx.rotate(Math.PI / 4 / sensivity);
                            draw();
                            smile(mouse);
                        }, 5);
                    else {
                        eye = true;

                        setTimeout(function() {
                            eyes(eye);
                            setTimeout(function() {
                                eyes(false);
                            }, 500);
                        }, 50);
                    }
                } else {
                    eye = false;
                    if (animation > 0)
                        setTimeout(function() {
                            animation--;
                            ctx.rotate(-Math.PI / 4 / sensivity);
                            draw();
                            smile(mouse);
                        }, 5);

                }
            }

            draw();
            $('#logoCanvas').hover(function() {
                mouse = true;
                smile(mouse)
            }, function() {
                mouse = false;
                smile(mouse)
            });
        }
    ballsCanvas = document.getElementById('vesnaBalls');
    if (ballsCanvas)
        if (ballsCanvas.getContext) {


            ballsCanvas.width = $('#canvas').width();
            ballsCanvas.height = $('footer').height() + 150;
            context = ballsCanvas.getContext("2d");
            ballsCanvas.onmouseout = function() {
                mouseX = 99999;
                mouseY = 99999;
            };

            loadImages(function() {


                kin = new Kinetic_2d("vesnaBalls", "2d");

                initBalls(60, (ballsCanvas.width));
                t = null;
                kin.setDrawStage(function() {
                    updateStageObjects();
                    kin.clear();
                    var mousePos = kin.getMousePos();
                    mouseX = mousePos === null ? 99999 : mousePos.x;
                    mouseY = mousePos === null ? 99999 : mousePos.y;

                    for (var n = 0; n < balls.length; n++) {
                        context.drawImage(images[balls[n].img], balls[n].x, balls[n].y);
                    }
                });
                kin.startAnimation();
            });


        }
});

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
function Ball(x, y, vx, vy, r) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    this.origX = x;
    this.origY = y;
    this.r = r;
    this.img = getRandomInt(1, 10);
}

function initBalls(count, width) {
    var step = Math.round(width / count);
    if (balls) return;
    balls = [];
    for (i = 6; i < count - 6; i++) {
        balls.push(new Ball(i * step - 35 - getRandomInt(-5, 5), getRandomInt(40, 60), 0, 0, getRandomInt(25, 40)));
    }
}

function updateStageObjects() {
    var timeInterval = 15;
    var floorFriction = 0.0005 * timeInterval;
    var mouseForceMultiplier = 1 * timeInterval;
    var restoreForce = 0.002 * timeInterval;
    for (var n = 0; n < balls.length; n++) {
        balls[n].y = balls[n].y + balls[n].vy;
        balls[n].x = balls[n].x + balls[n].vx;
        if (balls[n].x > balls[n].origX) {
            balls[n].vx -= restoreForce;
        }
        else {
            balls[n].vx += restoreForce;
        }
        if (balls[n].y > balls[n].origY) {
            balls[n].vy -= restoreForce;
        }
        else {
            balls[n].vy += restoreForce;
        }

        var distX = balls[n].x - mouseX;
        var distY = balls[n].y - mouseY;

        var radius = Math.sqrt(Math.pow(distX, 2) +
            Math.pow(distY, 2));

        var totalDist = Math.abs(distX) + Math.abs(distY);

        var forceX = (Math.abs(distX) / totalDist) *
            (1 / radius) *
            mouseForceMultiplier;
        var forceY = (Math.abs(distY) / totalDist) *
            (1 / radius) *
            mouseForceMultiplier;
        if (distX > 0) {
            balls[n].vx += forceX;
        }
        else {
            balls[n].vx -= forceX;
        }
        if (distY > 0) {
            balls[n].vy += forceY;
        }
        else {
            balls[n].vy -= forceY;
        }
        if (balls[n].vx > 0) {
            balls[n].vx -= floorFriction;
        }
        else
        if (balls[n].vx < 0) {
            balls[n].vx += floorFriction;
        }
        if (balls[n].vy > 0) {
            balls[n].vy -= floorFriction;
        }
        else
        if (balls[n].vy < 0) {
            balls[n].vy += floorFriction;
        }

        if (balls[n].y > (ballsCanvas.height - ballRadius)) {
            balls[n].y = ballsCanvas.height - ballRadius - 2;
            balls[n].vy *= -1;
            balls[n].vy *= (1 - collisionDamper);
        }
        if (balls[n].y < (ballRadius)) {
            balls[n].y = ballRadius + 2;
            balls[n].vy *= -1;
            balls[n].vy *= (1 - collisionDamper);
        }
        if (balls[n].x > (ballsCanvas.width - ballRadius)) {
            balls[n].x = ballsCanvas.width - ballRadius - 2;
            balls[n].vx *= -1;
            balls[n].vx *= (1 - collisionDamper);
        }
        if (balls[n].x < (ballRadius)) {
            balls[n].x = ballRadius + 2;
            balls[n].vx *= -1;
            balls[n].vx *= (1 - collisionDamper);
        }
    }

}

