﻿

var mouseDown = false;
var mouseDownPos;
var mouseDownBackgroundPos;
var mousePos = 0;

function Globe() {

    thisObj = this;

    this.GlobeDots = [];
    this.MapX = 0;
    this.RotationStep = 2;
    this.MapWidth = 942;
    this.RestartPosition = -this.MapWidth;
    this.Paused = false;
    this.EventOpen = false;

    $(document).mousedown(function (e) {

        if (globeMove) {

            mouseDown = true;
            globe.Paused = true;
            mouseDownPos = e.pageX;

            var p = $('#globeMap').css('background-position');
            if (typeof (p) === 'undefined') {
                mouseDownBackgroundPos = parseInt($('#globeMap').css('background-position-x'));
            }
            else {
                mouseDownBackgroundPos = parseInt($('#globeMap').css("background-position").split(" ")[0]);
            }

            $(document).mousemove(function (e) {
                if (mouseDown) {

                    globe.MoveByMouse(e.pageX);
                }
            });

            $(document).mouseup(function () {

                if (mouseDown) {
                    //globe.Paused = false;
                    mouseDown = false;

                    $(document).unbind('mousemove');
                }
            });
        }
    });


    $('#panelsOverlay').mousemove(function (e) {
        if ((e.pageX - $(this).offset().left) > 200 && (e.pageX - $(this).offset().left) < 700 && (e.pageY - $(this).offset().top) > 0 && (e.pageY - $(this).offset().top) < 500) {
            globe.Paused = true;
        }
        else {
            globe.Paused = false;
        }
    });
}


Globe.prototype.AddGlobeDot = function (globeDot) {
    globeDot.Globe = this;
    this.GlobeDots.push(globeDot);
}

Globe.prototype.Update = function () {
    if (this.Paused) return;
    if (this.EventOpen) return;
    this.Rotate();
    for (var i in this.GlobeDots) { this.GlobeDots[i].Update(); }
}

Globe.prototype.Rotate = function() {
    this.MapX += this.RotationStep;
    if (this.MapX <= this.RestartPosition) this.MapX = 0;
    else if (this.MapX >= 0) this.MapX = this.RestartPosition;

    $('#globeMap').css("background-position", this.MapX + "px 0px");
}

Globe.prototype.MoveByMouse = function (mouseMove) {
    var diff = mouseMove - mouseDownPos;
    this.MapX = mouseDownBackgroundPos + diff;
    if (this.MapX <= this.RestartPosition) this.MapX = 0;
    else if (this.MapX >= 0) this.MapX = this.RestartPosition;

    //if (this.MapX >= this.MapWidth)  this.MapX = 0;
    //else if (this.MapX < 0) this.MapX = this.MapWidth;

    $('#globeMap').css("background-position", this.MapX + "px 0px");

    for (var i in this.GlobeDots) { this.GlobeDots[i].Update(); }
}

