﻿/*
* HeatMap functions.
* Authors: CHR, DFK
* Date: 08/09/2009
*/
// MSM. Extension de filtro jquery para detectar si un elemento es realmente visible
jQuery.extend(jQuery.expr[":"], {

    reallyvisible: function(a) {
        function getFirstParentNonInheritedVisibility(a) {
            var lParents = jQuery(a).parents();
            if (lParents.length == 0)
                return 'visible';
            var i = 0;
            while (i < lParents.length) {
                var lCurrentParent = lParents[i];
                if (jQuery(lCurrentParent).css('visibility') === 'visible')
                    return 'visible';
                if (jQuery(lCurrentParent).css('visibility') === 'hidden')
                    return 'hidden';
                i++;
            }
            return 'visible';
        }
        return (a.offsetWidth > 0 || a.offsetHeight > 0) &&
                        ((jQuery(a).css('visibility') === 'visible') ||
                         ((jQuery(a).css('visibility') === 'inherit') && (getFirstParentNonInheritedVisibility(a) === 'visible')));
    }
});
function HeatMap(aMainContentId) {
    this.MainContentId = aMainContentId;
    this.StartTime = new Date();
    this.Referrer = document.referrer;
    this.contentViewerContainer = null;
}

HeatMap.prototype.executeTests = function() {
    var lControlsToGenerate = $('form *[id!=]');
    var self = this;
    
    $.each(lControlsToGenerate, function(i, val) {
    
        var lControlTest = $get(val.id);
        
        var lControlX = self.getX(lControlTest);
        var lControlY = self.getY(lControlTest);
        
        var lControlWidth = lControlTest.offsetWidth;
        var lControlHeight = lControlTest.offsetHeight;

        if (lControlWidth > 0 &&
        lControlHeight > 0 &&
        val.innerHTML.trim().length > 0 &&
        lControlX > 0 &&
        lControlY > 0) {
            var lNumberPerControl = 10;
            for (i = 0; i < lNumberPerControl; i++) {
                var lFooXRel = Math.floor(Math.random() * lControlWidth);
                var lFooX = lFooXRel + lControlX;
                var lFooYRel = Math.floor(Math.random() * lControlHeight);
                var lFooY = lFooYRel + lControlY;
                
                var lFooEvent = {
                    target: lControlTest,
                    pageX: lFooX,
                    pageY: lFooY
                };
                self.registerClick(lFooEvent, 0, 0);
            }
        }
    });
    TLK.RadAlert('Clicks aleatorios generados con éxito.');
}


HeatMap.prototype.IsHeatMapActive = function() {
    var hmapIFrame = window.top.$('#__HeatMapIframe');
    return hmapIFrame.length == 1;
}

HeatMap.prototype.getContentViewerContainer = function() {
    /* Nota del window.top.$. Lo puse para que si activan HeatMap con Estadísticas activadas
     * no se encontraba el #__HeatMapIframe desde el ámbito actual, cualfuera sea!
     */
    var hmapIFrame = window.top.$('#__HeatMapIframe');
    if (hmapIFrame.length == 1) {
        return $(hmapIFrame.contents()[0].body);
    }

    if (this.contentViewerContainer == null) {
        this.contentViewerContainer = $('.EIPContentViewerContainer');
    }
    return this.contentViewerContainer;
}

HeatMap.prototype.registerClick = function(e, offsetLeft, offsetTop) {
    var el = e.target;
    while ($(el).attr('id') == '' && $(el) != $(document.body)) {
        el = $(el)[0].parentNode;
    }
    
    var relX = e.pageX - this.getX(el);
    var relY = e.pageY - this.getY(el);
    
    this.doRegisterClick(el, e.target.tagName, relX, relY);
}

HeatMap.prototype.doRegisterClick = function(el, tagName, relX, relY) {
    var lElapsedMilliseconds = new Date() - this.StartTime;
    
    var clickEntity = {
        SizeX: el.offsetWidth,
        SizeY: el.offsetHeight,
        // Text: el.innerText.substr(0, 500),
        ControlClientId: el.id,
        ControlX: relX,
        ControlY: relY,
        TagName: tagName,
        ContentId: this.MainContentId,
        ElapsedTime: lElapsedMilliseconds,
        Referrer: this.Referrer,
        // Parámetros imprescindibles para que vaya con los repeater paginados
        DataPagerQueryStrings: _dataPagerQueryStrings,
        QueryString: window.location.search
    };

    var arguments = $.compactJSON({
        "aClick": clickEntity
    });

    // Ponemos OnFailNull porque hay un error extraño con navegadores NO IE
    // Se registra el click correctamente pero da un error.
    this.executeHeatMapMethod("RegisterClick", arguments, null, this.OnFailNull);
}


HeatMap.prototype.generateQuery = function() {
    var query = {
        ContentId: this.MainContentId,
        DateFrom: '1990-01-01',
        DateTo: '2090-01-01',
        // Parámetros imprescindibles para que vaya con los repeater paginados
        DataPagerQueryStrings: _dataPagerQueryStrings,
        QueryString: window.location.search
    };
    
    var hMapFilters = window.top.hMapFilters;
    
    if (hMapFilters) {
        query.DateFrom = hMapFilters.DateFrom;
        query.DateTo = hMapFilters.DateTo;
    }
    
    return query;
}

HeatMap.prototype.getY = function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}
HeatMap.prototype.getX = function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

HeatMap.prototype.createHeatMapIFrame = function() {
    var iframe = document.createElement("iframe");
    iframe.id = "__HeatMapIframe";
    var qstringInit = '?';
    if (window.location.href.split('?') > 1) { // ya tiene query
        qstringInit = '&';
    }
    var layOutDiv = this.getContentViewerContainer();
    var scroll = layOutDiv.scroll()[0];

    var qstring = 'DisableEIP=true&HeatMap=true&Width=' + scroll.scrollWidth + '&Height=' + scroll.scrollHeight;
    // Poner el ? o el & para la queryString según si ya existía o no
    var prefix = window.location.href.indexOf('?') == -1 ? '?' : '&';
    var jframe = $(iframe);
    jframe.attr('src', window.location.href.replace('#', '') + prefix + qstring);

    // Ver el offset que introduce la barra del EIP
    var oEIPToolBarDiv = $get('__ContentViewerContainer');
    var lOffTop = 0;
    if (oEIPToolBarDiv) {
        lOffTop = oEIPToolBarDiv.offsetTop;
    }

    jframe.css('display', 'block');
    jframe.css('position', 'absolute');
    jframe.css('top', lOffTop + 'px');
    jframe.css('left', '0px');

    jframe.css('border', '0');
    jframe.css('border', 'none');
    jframe.css('z-index', '8000');

    jframe.css('height', document.body.offsetHeight);
    jframe.css('width', document.body.offsetWidth);
    jframe.css('overflow', 'hidden');
    
    document.body.appendChild(iframe);
   
    EIP.loadingWindow('Calculando mapa de calor...') 
    
    // creamos capa debajo iframe para ocultar web al redimensionar
    var lDiv = window.top.document.createElement("div");
    lDiv.id = "divFakeHeatMapBackground";
    lDiv.style.position = "absolute";
    lDiv.style.backgroundColor = "#C0C0C0";
    lDiv.style.left = "0px";
    lDiv.style.top = lOffTop + 'px';
    lDiv.style.width = "100%";
    lDiv.style.height = document.body.offsetHeight;
    lDiv.style.zIndex = 7000;
    window.top.document.body.appendChild(lDiv);

    return iframe;
}

HeatMap.prototype.generateHeatMap = function() {
    var query = this.generateQuery();
    var arguments = $.compactJSON({
        "aQuery": query
    });
    var self = this;
    
    this.executeHeatMapMethod('GetControlsToProcess', arguments, function(result) {
        var actualHeatMapPosition = new Array();
        $.each(result.d, function(i, val) {
            if (val !== null && val !== '') {
                var el = $get(val);
                // MSM: Determinamos si el elemento es realmente visible
                if (el && $(el).is(':reallyvisible')) {
                    var actualPos = {
                        ControlClientId: val,
                        PosX: self.getX(el),
                        PosY: self.getY(el),
                        ClientSizeX: el != undefined ? el.offsetWidth : 0,
                        ClientSizeY: el != undefined ? el.offsetHeight : 0
                    };

                    actualHeatMapPosition.push(actualPos);
                }
            }
        });
        self.calculateHeatMap(actualHeatMapPosition);
    });
}

HeatMap.prototype.calculateHeatMap = function(actualHeatMapPosition) {
    var lBody = document.body;
    var query = this.generateQuery();

    var arguments = $.compactJSON({
        "aQuery": query,
        "aWidth": lBody.offsetWidth,
        "aHeigth": $.query.get('Height'),
        "aPositions": actualHeatMapPosition
    });
    var self = this;
    this.executeHeatMapMethod('CalculateHeatMap', arguments, function(result) {
        // Crear el tag imagen con el mapade calor
        var oImg = $get('__HeatMapImg');
        if (!oImg) {
            oImg = document.createElement("img");
        }
        // Ver el offset que introduce la barra del EIP
        var oEIPToolBarDiv = $get('__ContentViewerContainer');
        var lOffTop = 0;
        if (oEIPToolBarDiv) {
            lOffTop = oEIPToolBarDiv.offsetTop;
        }

        oImg.setAttribute('src', "/HeatMapHandler.ashx?heatMapId=" + result.d);
        var jImage = $(oImg);
        jImage.attr('id', '__HeatMapImg');
        jImage.css('display', 'block');
        jImage.css('position', 'absolute');
        jImage.css('top', lOffTop + 'px');
        jImage.css('left', '0px');
        jImage.css('filter', 'alpha(opacity=80)');
        jImage.css('opacity', '0.8');
        jImage.css('z-index', '8001');
        jImage.css('overflow', 'hidden');
        jImage.attr('height', $.query.get('Height') - 5); // 5 mágico
        jImage.attr('width', lBody.offsetWidth);

        document.body.appendChild(oImg);
        
        top.EIP.unloadLoadingWindow();
        // Como ahora hay 2 capas de estadísticas, una en la página y otra en el heatmap,
        // hay que sincronizar el hecho de que las estadísticas estén activas
        jImage.load(function() {
            var hmapRadToolbar = window.top.heatMapBar._toolBar;
            var statsOn = hmapRadToolbar.findItemByValue('REPORTS_HEATMAP_STATS').get_checked();
            if (statsOn) {

                self.executeHeatMapStats();
            }
        });
    });
}

HeatMap.prototype.areStatsVisible = function() {
    var cvCont = this.getContentViewerContainer();

    var lTempBalls = cvCont.find(".HMBallInfo");
    return (lTempBalls.length > 0);
}

HeatMap.prototype.removeHeatMapStatsIfEnabled = function() {
    var cvCont = this.getContentViewerContainer();

    var lTempBalls = cvCont.find(".HMBallInfo");
    if (lTempBalls.length > 0) {
        // Es porque hay que quitarlas YA
        $.each(lTempBalls, function(i, val) {
            $(val).remove();
        });
        // Lo mismo con las capas que pudieran estar abiertas.
        var lBallLayers = $("*[class^='HMInfo']");
        $.each(lBallLayers, function(i, val) {
            $(val).remove();
        });
        return;
    }
}

HeatMap.prototype.executeHeatMapStats = function() {
    var lQuery = this.generateQuery();
    var lArguments = $.compactJSON({
        "aQuery": lQuery
    });
    var self = this;

    if (this.areStatsVisible()) {
        this.removeHeatMapStatsIfEnabled();
        return;
    }

    var lTempBalls = $(".HMBallInfo");
    if (lTempBalls.length > 0) {
        // Es porque hay que quitarlas YA
        $.each(lTempBalls, function(i, val) {
            $(val).remove();
        });
        // Lo mismo con las capas que pudieran estar abiertas.
        var lBallLayers = $("*[class^='HMInfo']");
        $.each(lBallLayers, function(i, val) {
            $(val).remove();
        });
        return;
    }

    this.executeHeatMapMethod("GetHeatMapLinkStats", lArguments, function(result) {
        if (result.d.length == 0) {
            TLK.RadAlert('No hay registros para esta página');
            return;
        }

        var n = 0;
        $.each(result.d, function(i, val) {
            var cvCont = self.getContentViewerContainer();

            var el = cvCont.find('#' + val.ControlClientId)[0];

            var lIcon;
            lIcon = $("<img id='" + val.ControlClientId + "_HM' class='HMBallInfo' src='/pb/img/HeatMap/Level" + val.Interval + ".png'/>");

            var lPosX = self.getX(el) - 22;
            var lPosY = self.getY(el) - 2;

            // Corrección para evitar que queden sobre la izquierda...
            if (lPosX < 0) {
                lPosX = 0;
            }

            // Calcular posiciones corrigiendo jqueryLayOut
            // NO hay que tener en cuenta el scroll porque ya se calcula arriba con los getX getY
            var cvCont = self.getContentViewerContainer();
            lPosX = self.ApplyLeftCorrection(lPosX);
            lPosY = self.ApplyTopCorrection(lPosY);
            lIcon.appendTo(cvCont);

            lIcon.css('position', 'absolute');
            lIcon.css('left', lPosX + 'px');
            lIcon.css('top', lPosY + 'px');
            lIcon.css('z-index', '8005');
            // Agregar mouseover a la bola
            lIcon.mouseover(function(e) {
                self.ControlMouseOver(el, 'HMHilite' + val.Interval);
            });
            lIcon.mouseout(function(e) {
                self.DownliteAll();
            });
            lIcon.click(function(e) {
                self.CreateHeatZoneInfoLayer(val.ControlClientId, lPosX, lPosY, val);
            });
        });
    });
}

HeatMap.prototype.CreateHeatZoneInfoLayer = function(sourceControl, posX, posY, val) {
    var lLayerId = sourceControl + "_HMInfo";
    var self = this;
    var cvCont = self.getContentViewerContainer();
    var lTempLayer = cvCont.find("#" + lLayerId);
    if (lTempLayer.length > 0) {
        lTempLayer.hide('slow', function() {
            lTempLayer.remove();
        });
        return;
    }

    var lMasInfoId = sourceControl + "_Info";
    var lLayer = $("<div id='" + lLayerId + "' class='HMInfo" + val.Interval + "'></div>");
    var lPar = $("<b>Total de Clicks</b>: " + val.Clicks + "<br />");
    var lPar2 = $('<b>Porcentaje</b>: ' + Math.round(val.Percentage) + '%<br /><a class="HMapMasInfo" id="' + lMasInfoId + '">M&aacute;s Informaci&oacute;n</a><br />');

    lPar.appendTo(lLayer);
    lPar2.appendTo(lLayer);

    var lPosXFinal = posX + 5;
    var lPosYFinal = posY + 20;

    // Control para evitar que quede sobre la derecha de la pantalla.
    if (lPosXFinal > (document.body.clientWidth - 220)) {
        lPosXFinal = document.body.clientWidth - 220;
    }

    lLayer.css('left', lPosXFinal);
    lLayer.css('top', lPosYFinal);

    //lLayer.appendTo(document.body);
    lLayer.appendTo(cvCont);
    lLayer.hide();
    lLayer.fadeIn("slow");

    var lMasInfo = cvCont.find('#' + lMasInfoId);
    lMasInfo.click(function(e) {
        lMasInfo.remove();
        self.MoreInfoLayer(lLayer, sourceControl);
        lLayer.css('width', '200px');
    });
}

HeatMap.prototype.MoreInfoLayer = function(aInfoLayer, aSourceControlId) {

    var masInfoDivContainer = $('<div></div>').appendTo(aInfoLayer);
    
    var masInfoDiv = $('<div class="hMapMoreInfoDiv"></div>').appendTo(masInfoDivContainer);
    var ulLowerTabs = $('<ul class="hMapLowerTabs"></ul>').appendTo(masInfoDivContainer);
    var lInfoText = $('<div class="hTabsToolTip"></div>').appendTo(masInfoDivContainer).hide();
    
    var browsers = this.creareMoreInfoLayerLowerTab("/pb/img/HeatMap/Navegadores.png", "Informaci&oacute;n de navegadores", lInfoText).appendTo(ulLowerTabs);
    var referrer = this.creareMoreInfoLayerLowerTab("/pb/img/HeatMap/Referer.png", "Informaci&oacute;n de Referrer", lInfoText).appendTo(ulLowerTabs);
    var timeToClick = this.creareMoreInfoLayerLowerTab("/pb/img/HeatMap/TimeToClick.png", "Informaci&oacute;n de tiempo hasta clicar", lInfoText).appendTo(ulLowerTabs);
    var os = this.creareMoreInfoLayerLowerTab("/pb/img/HeatMap/SistemaOperativo.png", "Informaci&oacute;n de Sistemas operativos", lInfoText).appendTo(ulLowerTabs);
    
    var showInfoTabFunction = Function.createDelegate(this, function(result) {
        this.showInfoTab(masInfoDiv, result);
    });
    
    var self = this;

    self.QueryBrowsers(aSourceControlId, function(result) {
        showInfoTabFunction(result);
    });
    browsers.click(function() {
        self.QueryBrowsers(aSourceControlId, function(result) {
            showInfoTabFunction(result);
        });
    });

    timeToClick.click(function() {
        self.QueryTimeToClick(aSourceControlId, function(result) {
            showInfoTabFunction(result);
        });
    });

    os.click(function() {
        self.QueryOperatingSystem(aSourceControlId, function(result) {
            showInfoTabFunction(result);
        });
    });

    referrer.click(function() {
        self.QueryReferrer(aSourceControlId, function(result) {
            showInfoTabFunction(result);
        });
    });
}

HeatMap.prototype.showInfoTab = function(masInfoDiv, result) {
    masInfoDiv.empty();
    $.each(result.d, function(i, val) {
        var divInfo = $('<div class="barDiv"><div style="margin-bottom: 5px">' + val.Description + ':</div></div>').appendTo(masInfoDiv);
        var spanBar = $('<span class="barSpan"><span class="percentageBar">' + val.Percentage + '%</span></span>').appendTo(divInfo);
        $(spanBar).progressBar(val.Percentage, {
            showText: true,
            increment: 5,
            speed: 10
        });
    });
}

HeatMap.prototype.QueryBrowsers = function(aSourceControlId, aCallBack) {
    this.ExecuteMoreInfoQuery('GetHeatMapControlStatsAgent', aSourceControlId, aCallBack);
}

HeatMap.prototype.QueryTimeToClick = function(aSourceControlId, aCallBack) {
    this.ExecuteMoreInfoQuery('GetHeatMapControlStatsTimeToClick', aSourceControlId, aCallBack);
}

HeatMap.prototype.QueryOperatingSystem = function(aSourceControlId, aCallBack) {
    this.ExecuteMoreInfoQuery('GetHeatMapControlStatsOS', aSourceControlId, aCallBack);
}


HeatMap.prototype.QueryReferrer = function(aSourceControlId, aCallBack) {
    this.ExecuteMoreInfoQuery('GetHeatMapControlStatsReferrer', aSourceControlId, aCallBack);
}

HeatMap.prototype.ExecuteMoreInfoQuery = function(aMethod, aSourceControlId, aCallBack) {
    var query = this.generateQuery();
    var arguments = $.compactJSON({
        "aQuery": query,
        "aClientControlId": aSourceControlId
    });
    this.executeHeatMapMethod(aMethod, arguments, aCallBack);
}

HeatMap.prototype.creareMoreInfoLayerLowerTab = function(src, title, infoLayer) {
    var lTab = $('<li><img src="' + src + '" title="' + title + '" alt="' + title + '"/></li>');
    //    lTab.mouseover(function(e) {
    //        infoLayer[0].innerHTML = title;
    //        infoLayer.fadeIn(300);
    //    });
    //    lTab.mouseout(function(e) {
    //        infoLayer[0].innerHTML = "";
    //        infoLayer.hide();
    //    });
    return lTab;
}


HeatMap.prototype.ControlMouseOver = function(sourceControl, borderStyle) {
    this.CrearMarcoGenerico(sourceControl, borderStyle);
}

HeatMap.prototype.DownliteAll = function(domElementToIgnore) {
    var self = this;
    var cvCont = self.getContentViewerContainer();

    removeIfNotCurrent(cvCont.find("*[class^='HMHilite']"), domElementToIgnore);

    function removeIfNotCurrent(elemArray, domElementToIgnore) {

        for (var i = 0; i < elemArray.length; i++) {
            var jCurrentElement = $(elemArray[i]);
            var lSourcElement = jCurrentElement.data("sourceElement");
            if (lSourcElement !== domElementToIgnore) {
                jCurrentElement.remove();
                if ($(lSourcElement).data("highlighted")) {
                    $(lSourcElement).data("highlighted", false);
                }
            }
        }
    }
};

//Aplicar corrección de posicion de debido a jquery layout
HeatMap.prototype.ApplyLeftCorrection = function(left) {
    if (this.IsHeatMapActive()) return left;
    var cvCont = this.getContentViewerContainer();
    var parentOffset = cvCont.offset();
    return left -= parentOffset.left;
}

HeatMap.prototype.ApplyLeftCorrectionWithScroll = function(left) {
    if (this.IsHeatMapActive()) return left;
    var cvCont = this.getContentViewerContainer();
    return this.ApplyLeftCorrection(left) + cvCont.scrollLeft();
}

//Aplicar corrección de posicion de debido a jquery layout
HeatMap.prototype.ApplyTopCorrection = function(top) {
    if (this.IsHeatMapActive()) return top;
    var cvCont = this.getContentViewerContainer();
    var parentOffset = cvCont.offset();
    return top -= parentOffset.top;
}

HeatMap.prototype.ApplyTopCorrectionWithScroll = function(top) {
    if (this.IsHeatMapActive()) return top;
    var cvCont = this.getContentViewerContainer();
    return this.ApplyTopCorrection(top) + cvCont.scrollTop();
}

HeatMap.prototype.CrearMarcoGenerico = function(source, classname) {
    var padding = 3;
    var cvCont = this.getContentViewerContainer();
    var jSource = cvCont.find('#' + $(source).attr('id'));
    var ofs = jSource.offset();
    var h = jSource.outerHeight();
    var w = jSource.outerWidth();

    var leftLeft = this.ApplyLeftCorrectionWithScroll(ofs.left - padding);
    var leftTop = this.ApplyTopCorrectionWithScroll(ofs.top - padding);

    var lDivHightliteLeft = $("<div class='" + classname + "'></div>").appendTo(cvCont);
    lDivHightliteLeft.css("left", leftLeft);
    lDivHightliteLeft.css("top", leftTop);
    lDivHightliteLeft.css("width", 1);
    lDivHightliteLeft.css("height", h + 2 * padding);
    lDivHightliteLeft.data("sourceElement", source);

    var rightLeft = this.ApplyLeftCorrectionWithScroll(ofs.left + w + padding);
    var rightTop = this.ApplyTopCorrectionWithScroll(ofs.top - padding);

    var lDivHightliteRight = $("<div class='" + classname + "'></div>").appendTo(cvCont);
    lDivHightliteRight.css("left", rightLeft);
    lDivHightliteRight.css("top", rightTop);
    lDivHightliteRight.css("width", 1);
    lDivHightliteRight.css("height", h + 2 * padding);
    lDivHightliteRight.data("sourceElement", source);

    var topLeft = this.ApplyLeftCorrectionWithScroll(ofs.left - padding);
    var topTop = this.ApplyTopCorrectionWithScroll(ofs.top - padding);

    var lDivHightliteTop = $("<div class='" + classname + "'></div>").appendTo(cvCont);
    lDivHightliteTop.css("left", topLeft);
    lDivHightliteTop.css("top", topTop);
    lDivHightliteTop.css("width", w + 2 * padding);
    lDivHightliteTop.css("height", 1);
    lDivHightliteTop.data("sourceElement", source);

    var bottomLeft = this.ApplyLeftCorrectionWithScroll(ofs.left - padding);
    var bottomTop = this.ApplyTopCorrectionWithScroll(ofs.top + h + padding);

    var lDivHightliteBottom = $("<div class='" + classname + "'></div>").appendTo(cvCont);
    lDivHightliteBottom.css("left", bottomLeft);
    lDivHightliteBottom.css("top", bottomTop);
    lDivHightliteBottom.css("width", w + 2 * padding);
    lDivHightliteBottom.css("height", 1);
    lDivHightliteBottom.data("sourceElement", source);

    cvCont.find("." + classname).fadeIn(300);

};

HeatMap.prototype.createDeleteHeatMapPopUp = function() {
    var url = '/' + Pb.Utils.Url.getPageLang() + '/pb/forms/backoffice/HeatMap/HeatMapRemovePopUp.aspx';
    var wndMgr = TLK.GetRadWindowManager();
    var wnd = wndMgr.open(url);
    wnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
    wnd.set_width(400);
    wnd.set_height(250);
    wnd.center();
    wnd.set_modal(true);
    wnd.set_visibleStatusbar(false);
};

HeatMap.prototype.closeHeatMapOptionsPupUp = function() {    
    
    var wndMgr = TLK.GetRadWindowManager();
    var wnd = null;
    wnd = wndMgr.getWindowByName('HeatMapOptions');
    if (wnd != null && wnd != undefined) {
        wnd.close();
        return;
    }
};

HeatMap.prototype.isHeatMapOptionsVisible = function() {
    var wndMgr = TLK.GetRadWindowManager();
    var wnd = null;
    wnd = wndMgr.getWindowByName('HeatMapOptions');
    return (wnd != null && wnd != undefined);
};

HeatMap.prototype.createHeatMapOptionsPopUp = function() {

    var wndMgr = TLK.GetRadWindowManager();
    var url = '/' + Pb.Utils.Url.getPageLang() + '/pb/forms/backoffice/HeatMap/HeatMapOptionsPopUp.aspx?ContentId=' + this.MainContentId;

    var wnd = wndMgr.open(url, 'HeatMapOptions');
    wnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Pin);
    wnd.set_width(400);
    wnd.set_height(265);
    // Centrar para luego poder mover abajo a la derecha
    wnd.center();
    var bounds = wnd.getWindowBounds();
    
    wnd.moveTo(document.body.offsetWidth - 405, bounds.y + 150);
    
    wnd.set_visibleStatusbar(false);
    wnd.set_modal(false);
    wnd.togglePin();

    return wnd;
}



HeatMap.prototype.executeHeatMapMethod = function(methodName, methodArguments, onSuccess, onFail) {
    this.executeMethod("HeatMapService.asmx", methodName, methodArguments, onSuccess, onFail ? onFail : this.OnFail);
}

HeatMap.prototype.OnFailNull = function(XMLHttpRequest, textStatus, errorThrown) {
    //NOOP XD
}

HeatMap.prototype.OnFail = function(XMLHttpRequest, textStatus, errorThrown) {

    try {
        TLK.RadAlert($.evalJSON(XMLHttpRequest.responseText).Message);
    } 
    catch (e) {
        TLK.RadAlert('Ha ocurrido un error');
    }
}
HeatMap.prototype.executeMethod = function(serviceName, methodName, methodArguments, onSuccess, onFail) {
    // IMPORTANTE: En navegadores no IE (FireFox, Chrome) el registro de click sobre un enlace da un error
    // si ejecutamos la llamada de manera asíncrona. En Chrome ni siquiera hace traza.
    // Decidimos hacer síncrona la llamada para navegadores no IE para solucionar este problema.
    var isIE = navigator.userAgent.search('MSIE') != -1;
    $.ajax({
        async: isIE,
        type: "POST",
        url: "/pb/services/" + serviceName + "/" + methodName + "?pbl=" + Pb.Utils.Url.getPageLang(),
        data: methodArguments,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        error: onFail
    });
}
