﻿var st_mouseX = 0, st_mouseY = 0;
var st_recordFrame_intID = 0;
var st_sendUpdate_intID = 0;
var st_pageID = 0;
var st_data_x = "";
var st_data_y = "";
var st_timeIndex = 0;

document.onmousemove = function(e) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        st_mouseX = window.event.clientX;
        st_mouseY = window.event.clientY;
    }
    else {
        st_mouseX = e.pageX;
        st_mouseY = e.pageY;
    }
}
/*document.onclick = function(e) {
    if (!e) {var e = window.event;}
    
    if (document.attachEvent) {
 	    var elm = e.srcElement;
    }
    else {
 	    var elm = e.target;
    }
    
    if (elm.tagName == "A") {
        elm.style.background = "#CC0000";
        elm.style.color = "white";
        //elm.href = "http://google.com";
        //alert(elm.href);
    }

    
    //document.getElementById("").tagName
}*/

function st_recordFrame() {
    st_data_x += st_mouseX + ",";
    st_data_y += st_mouseY + ",";
    
    st_timeIndex += st_recordRate;
    
    if (st_timeIndex >= st_maxRecordTime)
    {
        clearInterval(st_recordFrame_intID);
        clearInterval(st_sendUpdate_intID);
    }    
}

function st_sendUpdate() {
    if (st_pageID == 0)
        return;

    var xData = st_data_x;
    var yData = st_data_y;
    
    st_data_x = "";
    st_data_y = "";

    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        //if (xmlHttp.readyState == 4)
        //    widgetFormLoaded(xmlHttp.responseText);    
    }

    xmlHttp.open("post", "/site/SessionTracker/SessionTrackerAPI.aspx?action=log&pid=" + st_pageID + "&x=" + xData + "&y=" + yData, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send(""); 
}

function st_initiateTrackerSession() {
    if (window.location.search.substring(1).indexOf("streplaysid") != -1)
        return;

    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            st_pageID = xmlHttp.responseText;    
        }
    }
    
    window.location.search.substring(1)
    
    var page = window.location.pathname;
    if (window.location.search.substring(1) != "")
        page += escape(window.location.search);
    
    xmlHttp.open("post", "/site/SessionTracker/SessionTrackerAPI.aspx?action=initiate&recordrate=" + st_recordRate + "&page=" + page + "&clientwidth=" + st_getPageWidthWithScroll(), true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send(""); 
    
    st_recordFrame_intID = setInterval("st_recordFrame()", st_recordRate);
    st_sendUpdate_intID = setInterval("st_sendUpdate()", st_updateRate);    
}

function st_getPageWidthWithScroll() {
    var xWithScroll = 0;

    if (window.innerHeight && window.scrollMaxY) {
        // Firefox        
        xWithScroll = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight){
        // all but Explorer Mac
        xWithScroll = document.body.scrollWidth;
    } 
    else {
        // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        xWithScroll = document.body.offsetWidth;
    }
    
    return xWithScroll;
} 

//setTimeout("st_initiateTrackerSession()", 100);
