/****************************************************************************************
 * LiveZilla ChatGeotrackingMapClass
 *
 ***************************************************************************************/
function ChatGeotrackingMapClass() {
    this.iframe = null;
    this.receiver = null;
    this.urlIsSet = false;
    this.visitorQueue = [];
    this.delayCounter = 0;
    this.delayAddIsInProgress = false;
    this.selectedVisitor = null;
}

ChatGeotrackingMapClass.HasAtlasNglLicense = function() {
    return (
        typeof ChatVisitorClass !== 'undefined' &&
        typeof ChatVisitorClass.HasAtlasNglLicense === 'function' &&
        ChatVisitorClass.HasAtlasNglLicense()
    );
};

ChatGeotrackingMapClass.prototype.setIframe = function(iframe) {
    if (iframe === null)
    {
        this.iframe = null;
        this.urlIsSet = false;
        return;
    }

    if (!ChatGeotrackingMapClass.HasAtlasNglLicense())
    {
        this.iframe = null;
        this.urlIsSet = false;
        return;
    }

    this.iframe = iframe;
};

ChatGeotrackingMapClass.prototype.setReceiver = function(receiver) {
    if (
        !ChatGeotrackingMapClass.HasAtlasNglLicense() ||
        typeof window === 'undefined' ||
        !window.location ||
        receiver !== window.location.origin
    )
    {
        this.receiver = null;
        this.urlIsSet = false;
        return;
    }

    this.receiver = window.location.origin;
};

ChatGeotrackingMapClass.prototype.move = function(_left, _up) {
    this.postMessage({action: 'move', left: _left, up: _up});
};

ChatGeotrackingMapClass.prototype.zoom = function(_in) {
    this.postMessage({action: 'zoom', in: _in});
};

ChatGeotrackingMapClass.prototype.addVisitor = function(_lat, _lon, _id) {
    this.postMessage({action: 'add-visitor', lat: _lat, lon: _lon, id: _id});
};

ChatGeotrackingMapClass.prototype.removeVisitor = function(_id) {
    this.postMessage({action: 'remove-visitor', id: _id});
};

ChatGeotrackingMapClass.prototype.clearAll = function() {
    this.postMessage({action: 'clear'});
    this.visitorQueue = [];
};

ChatGeotrackingMapClass.prototype.doZoomTo = function(_id) {
    this.postMessage({action: 'do-zoom-to', in: _id});
};

ChatGeotrackingMapClass.prototype.reset = function () {
    this.postMessage({ action: 'reset'});
};

ChatGeotrackingMapClass.prototype.getZoomLevel = function() {
    this.postMessage({action: 'get-zoomlevel'});
};

ChatGeotrackingMapClass.prototype.setMapType = function(_type) {
    this.postMessage({action: 'set-map-type', type: _type});
};

ChatGeotrackingMapClass.prototype.setChat = function(_id, _chat) {
    this.postMessage({action: 'set-chat', id: _id, chat: _chat});
};

ChatGeotrackingMapClass.prototype.setSelection = function(_id, _center) {

    this.postMessage({action: 'set-selection', id: _id, center: _center});
};

ChatGeotrackingMapClass.prototype.getSelection = function(_id, _center) {
    this.postMessage({action: 'get-selection', id: _id, center: _center});
};

ChatGeotrackingMapClass.prototype.sendUrl = function() {
    if (
        ChatGeotrackingMapClass.HasAtlasNglLicense() &&
        !this.urlIsSet &&
        this.iframe !== null &&
        this.receiver === window.location.origin
    )
    {
        setTimeout(function(){
            lzm_chatGeoTrackingMap.postMessage(
                {action: 'set-origin'},
                false
            );
        },2000);
    }
};

ChatGeotrackingMapClass.prototype.addOrQueueVisitor = function(_visitor) {

    if (!ChatGeotrackingMapClass.HasAtlasNglLicense())
    {
        this.visitorQueue = [];
        this.selectedVisitor = null;
        return;
    }

    if (this.urlIsSet)
    {
        if (typeof _visitor != 'undefined')
        {
            this.removeVisitor(String(_visitor.id));
            if(parseFloat(_visitor.lat) != -522)
                this.addVisitor(parseFloat(_visitor.lat), parseFloat(_visitor.long), String(_visitor.id));
        }
        var tmpVisitorArray = lzm_commonTools.clone(this.visitorQueue);
        this.visitorQueue = [];
        for (var i=0; i<tmpVisitorArray.length; i++) {
            if (tmpVisitorArray[i] != null) {
                this.addOrQueueVisitor(tmpVisitorArray[i]);
            }
        }
        if (this.selectedVisitor != null)
        {
            this.setSelection(this.selectedVisitor, '');
        }
    }
    else
    {
        this.move(0, 0);
        this.visitorQueue.push(_visitor);
        if (!this.delayAddIsInProgress)
            this.delayAddVisitor();
    }
};

ChatGeotrackingMapClass.prototype.delayAddVisitor = function() {
    var that = this;
    that.delayAddIsInProgress = true;
    setTimeout(function()
    {
        if (that.delayCounter < 20 && that.urlIsSet)
        {
            that.delayCounter = 0;
            that.delayAddIsInProgress = false;
            that.addOrQueueVisitor();
        }
        else if (that.delayCounter < 20)
        {
            that.delayCounter++;
            that.move(0,0);
            that.delayAddVisitor();
        }
        else
        {
            that.delayCounter = 0;
            that.delayAddIsInProgress = false;
        }
    }, 1500);
};

ChatGeotrackingMapClass.prototype.postMessage = function(_data, _sendUrl) {
    _sendUrl = (typeof _sendUrl != 'undefined') ? _sendUrl : true;

    if (
        !ChatGeotrackingMapClass.HasAtlasNglLicense() ||
        _data === null ||
        typeof _data !== 'object' ||
        typeof _data.action !== 'string' ||
        typeof window === 'undefined' ||
        !window.location ||
        this.receiver !== window.location.origin ||
        this.iframe === null ||
        !this.iframe.contentWindow
    )
        return;

    if (
        this.urlIsSet ||
        _data.action == 'set-origin'
    )
    {
        try
        {
            this.iframe.contentWindow.postMessage(
                _data,
                window.location.origin
            );
        }
        catch(e)
        {

        }
    }

    if (_sendUrl == true)
        this.sendUrl();
};