var _tsSITIO = escape("Grupo Nación GN S.A.");
var _tsROOT = "https://www.gruponacion.biz/tschat/";
var _tsURL = escape(window.location.href);
var _tsId = null;

function openChat() {
    _tsInvitado = false;
    this.chatWindow = window.open(_tsROOT + 'chat.main.jsp?id=' + _tsId, 'Chat', 'width=300,height=420,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
    this.chatWindow.focus();
    this.chatWindow.opener = window;
}

function cancelInvite() {
    _tsInvitado = false;
}

function tsTrackPage(inviteFlag) {
    _tsId = $.cookie("id");
    //$("#MSG0").html(_tsId);
    if (_tsId == null) _tsId = "";
    $.getJSON(
            _tsROOT + "is?url=" + _tsURL + "&web=" + _tsSITIO + "&id=" + _tsId + "&stamp=" + (new Date()).getTime() + "&cb=?",
            function (data) {
                if (_tsId != data.id) {
                    _tsId = data.id;
                    var date = new Date();
                    date.setTime(date.getTime() + (10 * 60 * 1000));
                    $.cookie("id", _tsId, { path: '/', expires: date });
                }
                //$("#MSG").html(_tsId);
                chkInvite(data.inv);
            });

    /*
    var getC = 0;
    _tsIMG.onload = chkInvite;
    if (inviteFlag) {
        function tsSend() {
            var rTimer = 6000;
            _tsIMG.src = _tsROOT + "us?url=" + _tsURL + "&web=" + _tsSITIO + "&stamp=" + (new Date()).getTime();
            if (_tsInvitado == false && getC < 185) {
                if (getC > 10) {
                    rTimer = 10000;
                }
                if (rTimer > 0) {
                    _tsInviteTimer = setTimeout(tsSend, rTimer);
                }
            }
        }
        setTimeout(tsSend, 1000);
    } else {
        _tsIMG.src = _tsROOT + "us?url=" + _tsURL + "&web=" + _tsSITIO + "&stamp=" + (new Date()).getTime();
    }*/
}

var _tsIMG = new Image();
var _tsInvitado = false;
var _tsXPos = 0;
var _tsYPos = 0;
var _tsW = 150;
var _tsH = 60;
var _tsStartPos = _tsEndPos = 0;
var _tsInviteTimer = 0;
var _tsMoveTimer = 0;
var _tsMoving = true;

function chkInvite(invite) {
    if (invite && _tsInvitado == false) {
        var div = document.getElementById('inviteWindow');
        if (div == null) {
            div = document.createElement("DIV");
            div.setAttribute('name', 'inviteWindow');
            div.setAttribute('id', 'inviteWindow');
            div.setAttribute('align', 'right');
            div.style.position = 'absolute';
            div.style.cursor = 'help';
            div.style.top = _tsYPos;
            div.style.left = _tsXPos;
            div.style.zIndex = 10000;
            div.style.width = _tsW + 'px';
            div.style.height = _tsH + 'px';
            div.style.font = 'normal normal bold 9pt arial';
            div.style.background = "#F5F5F5 url('" + _tsROOT + "images/normal/ask_expert_animated_icon.gif') no-repeat scroll 5px 5px";
            div.style.borderColor = '#EBEBEB';
            div.style.borderWidth = '1px';
            div.style.borderStyle = 'solid';
            div.style.padding = '40px 0 0 50px';
            div.style.textAlign = 'center';
            div.innerHTML = "Necesita ayuda?<br/>";
            var divSi = document.createElement("a");
            divSi.style.margin = '5px';
            divSi.style.cursor = 'pointer';
            divSi.innerHTML = 'Si';
            divSi.onclick = openChat;
            var divNo = document.createElement("a");
            divNo.style.margin = '5px';
            divNo.style.cursor = 'pointer';
            divNo.innerHTML = 'No';
            divNo.onclick = cancelInvite;
            div.appendChild(divSi);
            div.appendChild(divNo);
            div.onmouseover = function() {
                _tsMoving = false;
            };
            div.onmouseout = function() {
                _tsMoving = true;
            };
            document.body.appendChild(div);
        }
        div.style.display = 'inline';
        _tsInvitado = true;
        if (_tsInviteTimer > 0) clearTimeout(_tsInviteTimer);
        start();
    }
}

function pageWidth(ancho) {
    return window.innerWidth ? window.innerWidth : document.body ? document.body.clientWidth : ancho;
}
function pageHeight(alto) {
    return window.innerHeight ? window.innerHeight : document.body ? document.body.clientHeight : alto;
}
function posLeft() {
    return document.body.scrollLeft;
}
function posTop() {
    return document.body.scrollTop;
}
function start() {
    window.onresize = function () {
        moveinit();
    };
    moveinit();
    moveit();
    mover();
}
function moveinit() {
    _tsXPos = (pageWidth() - _tsW) / 2;
    _tsStartPos = _tsYPos = 10;
    _tsEndPos = (pageHeight() - _tsH) / 2;
    _tsEndPos -= (_tsEndPos - _tsStartPos) % 2;
}
function moveit() {
    var x = (posLeft() + _tsXPos) + 'px';
    var y = (posTop() + _tsYPos) + 'px';
    document.getElementById('inviteWindow').style.left = x;
    document.getElementById('inviteWindow').style.top = y;
}
function mover() {
    if (_tsMoving) {
        if (_tsYPos < _tsEndPos) {
            _tsYPos += 2;
        }
        else if (_tsYPos > _tsEndPos) {
            _tsYPos -= 2;
        }
        else {
            _tsEndPos = _tsStartPos;
            _tsStartPos = _tsYPos;
        }
        moveit();
    }
    if (_tsInvitado) {
        _tsMoveTimer = setTimeout('mover()', 25);
    } else if (_tsMoveTimer > 0) {
        clearTimeout(_tsMoveTimer);
        document.getElementById('inviteWindow').style.display = 'none';
    }
}
