﻿$(document).ready(function () {

    /* This code is run on page load */

    var deg = 0;
    var dif = -3;

    /* Assigning the buttons to a variable for speed: */
    var arr = $('.btn');

    /* Caching the length of the array in a vаriable: */
    var len = arr.length;

    /* Finding the centers of the animation container: */
    var centerX = $('#stage').width() / 2 - 40;
    var centerY = $('#stage').height() / 2 - 60;

    /* Applying relative positioning to the buttons: */
    arr.css('position', 'absolute');

    /* The function inside the interval is run 25 times per second */
    setInterval(function () {

        /* This forms an area with no activity on mouse move in the middle of the stage */
        if (Math.abs(dif) < 0.5) return false;

        /* Increment the angle: */
        deg += dif;

        /* Loop through all the buttons: */
        $.each(arr, function (i) {

            /* Calculate the sine and cosine with the new angle */
            var eSin = Math.sin(((360 / len) * i + deg) * Math.PI / 180);
            var eCos = Math.cos(((360 / len) * i + deg) * Math.PI / 180);

            /* Setting the css properties */
            $(this).css({
                top: centerY + 25 * eSin,
                left: centerX + 90 * eCos,
                opacity: 0.8 + eSin * 0.2,
                zIndex: Math.round(80 + eSin * 20)
            });
        })
    }, 40);

    /* Detecting the movements of the mouse and speeding up or reversing the rotation accordingly: */
    var over = false;
    $("#stage").mousemove(function (e) {

        if (!this.leftOffset) {
            /* This if section is only run the first time the function is executed. */
            this.leftOffset = $(this).offset().left;
            this.width = $(this).width();
        }

        /* If the mouse is over a button, set dif to 0, which halts the animation */
        if (over) dif = 0;
        else
            dif = -5 + (10 * ((e.pageX - this.leftOffset) / this.width));

        /* In the other case calculate the speed according to the X position of the mouse */
    });

    /* Detecting whether the mouse is positioned above a share button: */
    $(".bcontent").hover(
		function () { over = true; dif = 0; },
		function () { over = false; }
	);
});
