﻿var xOffset, yOffset;
var tempX = 0;
var tempY = 0;

//detect browser
var IE = document.all ? true : false
if (!IE) {
    document.captureEvents(Event.MOUSEMOVE)
}

function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");

    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

//find the position of the first item on screen and store offsets
//find the first item on screen (after body)
var firstElement = document.getElementsByTagName('body')[0].childNodes[0];
//find the offset coordinates
xOffset = findPosX(firstElement);
yOffset = findPosY(firstElement);
if (IE && getIEVersionNumber() < 8) { // In IE there's a default margin in the page body. If margin's not defined, use defaults
    var marginLeftExplorer = parseInt(document.getElementsByTagName('body')[0].style.marginLeft);
    var marginTopExplorer = parseInt(document.getElementsByTagName('body')[0].style.marginTop);
    /*assume default 10px/15px margin in explorer*/
    if (isNaN(marginLeftExplorer)) { marginLeftExplorer = 10; }
    if (isNaN(marginTopExplorer)) { marginTopExplorer = 15; }
    xOffset = xOffset + marginLeftExplorer;
    yOffset = yOffset + marginTopExplorer;
}
/*attach a handler to the onmousedown event that calls a function to store the values*/
document.onmousedown = getMouseXY;

/*Functions*/
/*Find positions*/
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
function getMouseXY(e) {
    if (IE) {
        tempX = event.clientX + document.body.scrollLeft
        tempY = event.clientY + document.body.scrollTop
    } else {
        tempX = e.pageX
        tempY = e.pageY
    }
    tempX -= xOffset + document.body.clientWidth / 2;
    tempY -= yOffset;
    YachtTravel.HeatMap.InsertClick(Math.round(tempX), Math.round(tempY), location.href);    
    return true;
}
