﻿//Obj pro monitorovani webu
function Monitor(klic)
{
    //Obj pro praci ze souradnicema
    var souradnice = new Souradnice();
    var uzivatelInfo = new UzivatelInfo(klic);
    var ulozitData = new UlozitData();

    AddEvtListener(document, 'mousemove', MouseMoveEvent);
    if (location.hash.indexOf("mrizka=kalibrace") > -1) //Ulozim data pro kalibraci
    {
        Kalibrace();
        AddEvtListener(document, 'click', KalibraceClickEvent);
    }
    else
        AddEvtListener(document, 'click', ClickEvent);
    AddEvtListener(window, 'resize', ResizeEvent);


    function MouseMoveEvent(e)
    {
        souradnice.GetMouseXY(e);
    }

    function ClickEvent()
    {
        uzivatelInfo.NastavitSouradnice(souradnice.SouradniceX(), souradnice.SouradniceY());
        var scroll = souradnice.GetScrollXY();
        uzivatelInfo.NastavitScroll(scroll[0], scroll[1]);

        ulozitData.Ulozit(uzivatelInfo.GetPostData());

    }

    function ResizeEvent()
    {
        uzivatelInfo = new UzivatelInfo(klic);
    }

    function KalibraceClickEvent()
    {
        uzivatelInfo.NastavitSouradnice(souradnice.SouradniceX(), souradnice.SouradniceY());
        var scroll = souradnice.GetScrollXY();
        uzivatelInfo.NastavitScroll(scroll[0], scroll[1]);

        ulozitData.Kalibrace(uzivatelInfo.GetPostData());
    }

    function Kalibrace()
    {
        document.getElementsByTagName("body")[0].style.cssText = 'cursor:crosshair !important';
    }

    function UlozitData()
    {
        var img = new Image();

        this.Kalibrace = function Kalirace(postData)
        {
            img.src = "http://s6.kaktus.cz/Monitor/Ajax/UlozitKalibrace.ashx?" + postData;
        }

        this.Ulozit = function Ulozit(postData)
        {
            img.src = "http://s6.kaktus.cz/Monitor/Ajax/UlozitData.ashx?" + postData;
        }
    }

    //Informace o uzivateli
    function UzivatelInfo(klic)
    {
        var IE = document.all ? true : false; //Zijistim si zda se jedna o IE
        var tempX = 0;
        var tempY = 0;
        var tempScrollX = 0;
        var tempScrollY = 0;
        var winRozliseni = GetRozliseniOkna();
        var winRozliseniScroll = GetRozliseniOknaSroll();

        this.SouradniceX = function SouradniceX() { return tempX; };
        this.SouradniceY = function SouradniceY() { return tempY; };
        this.ScrollX = function ScrollX() { return tempScrollX; };
        this.ScrollY = function ScrollY() { return tempScrollY; };
        this.NastavitSouradnice = function NastavitSouradnice(x, y) { tempX = x; tempY = y; };
        this.NastavitScroll = function NastavitScroll(x, y) { tempScrollX = x; tempScrollY = y; };
        this.WinWidth = winRozliseni[0];
        this.WinHeight = winRozliseni[1];
        this.WinScrollWidth = winRozliseniScroll[0];
        this.WinScrollHeight = winRozliseniScroll[1];
        this.UserAgent = navigator.userAgent;
        this.ScreenWidth = screen.width;
        this.ScreenHeight = screen.height;
        this.ScreenAvailWidth = screen.availWidth;
        this.ScreenAvailHeight = screen.availHeight;
        this.ColorDepth = screen.colorDepth;
        this.PixelDepth = screen.pixelDepth;
        this.AppCodeName = navigator.appCodeName;
        this.AppName = navigator.appName;
        this.AppVersion = navigator.appVersion;
        this.CookieEnabled = navigator.cookieEnabled;
        this.Platform = navigator.platform;
        this.Referrer = document.referrer;
        this.HostName = location.hostname;
        this.Port = location.port;
        this.QueryString = location.search;
        this.Href = location.href;
        this.PathName = location.pathname;
        this.Jazyk = GetJazyk();
        this.Browser = GetBrowser();
        this.Klic = klic;

        this.GetPostData = function GetPostData()
        {
            var data = "";
            data += "x=" + this.SouradniceX();
            data += "&y=" + this.SouradniceY();
            data += "&WinWidth=" + this.WinWidth;
            data += "&WinHeight=" + this.WinHeight;
            //data += "&UserAgent="+this.UserAgent;
            data += "&ScreenWidth=" + this.ScreenWidth;
            data += "&ScreenHeight=" + this.ScreenHeight;
            data += "&ScreenAvailWidth=" + this.ScreenAvailWidth;
            data += "&ScreenAvailHeight=" + this.ScreenAvailHeight;
            data += "&ColorDepth=" + this.ColorDepth;
            data += "&PixelDepth=" + this.PixelDepth;
            data += "&AppCodeName=" + this.AppCodeName;
            data += "&AppName=" + this.AppName;
            //data += "&AppVersion="+this.AppVersion;
            data += "&CookieEnabled=" + this.CookieEnabled;
            data += "&Platform=" + this.Platform;
            data += "&Referrer=" + this.Referrer;
            data += "&HostName=" + this.HostName;
            data += "&Port=" + this.Port;
            data += "&QueryString=" + this.QueryString;
            data += "&Href=" + this.Href;
            data += "&PathName=" + this.PathName;
            data += "&Jazyk=" + this.Jazyk;
            data += "&ScrollX=" + this.ScrollX();
            data += "&ScrollY=" + this.ScrollY();
            data += "&WinScrollWidth=" + this.WinScrollWidth;
            data += "&WinScrollHeight=" + this.WinScrollHeight;
            data += "&Browser=" + this.Browser;
            data += "&Klic=" + this.Klic;

            return data;
        }

        function GetRozliseniOkna()
        {
            var winW = 0, winH = 0;

            if (IE)
            {
                winW = document.body.offsetWidth;
                winH = document.body.offsetHeight;
            }
            else
            {
                winW = window.innerWidth;
                winH = window.innerHeight;
            }

            return [winW, winH];
        }

        function GetRozliseniOknaSroll()
        {
            var winW = 0, winH = 0;

            winW = document.getElementsByTagName('html')[0].scrollWidth;
            winH = document.getElementsByTagName('html')[0].scrollHeight;

            return [winW, winH];
        }

        function GetJazyk()
        {
            if (IE)
                return navigator.browserLanguage;
            else
                return navigator.language;
        }

        function GetBrowser()
        {
            var b = navigator.userAgent ? navigator.userAgent.toLowerCase().replace(/-/g, '') : '';
            var browsers = ['chrome', 'firefox', 'safari', 'msie', 'opera'];
            for (i = 0; i < browsers.length; i += 1)
            {
                if (b.indexOf(browsers[i]) !== -1)
                {
                    return browsers[i];
                    break;
                }
            }

            return 'unknown'
        }
    }

    //Obj pro praci ze souradnicema
    function Souradnice()
    {
        var tempX = 0;
        var tempY = 0;
        var IE = document.all ? true : false; //Zijistim si zda se jedna o IE

        //Navratove promene pro souradnice
        this.SouradniceX = function SouradniceX() { return tempX; }   //temp ulozena souradnice X
        this.SouradniceY = function SouradniceY() { return tempY; }   //temp ulozena souradncie Y

        //METODY PRO ZISKANI SOURADNIC
        //Ziska souradnice X Y
        this.GetMouseXY = function GetMouseXY(e)
        {
            var scroll = this.GetScrollXY();
            if (!e)
            {
                e = window.event;
            }

            tempX = e.clientX + scroll[0];
            tempY = e.clientY + scroll[1];

            if (tempX < 0) { tempX = 0; }
            if (tempY < 0) { tempY = 0; }

            return true;
        }

        //Ziska pozici scroll baru X Y
        this.GetScrollXY = function GetScrollXY()
        {
            var scrOfX = 0, scrOfY = 0;
            if (typeof (window.pageYOffset) == 'number')
            {
                //Netscape    
                scrOfY = window.pageYOffset;
                scrOfX = window.pageXOffset;
            }
            else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
            {
                //DOM   
                scrOfY = document.body.scrollTop;
                scrOfX = document.body.scrollLeft;
            }
            else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
            {
                //IE6  
                scrOfY = document.documentElement.scrollTop;
                scrOfX = document.documentElement.scrollLeft;
            }

            return [scrOfX, scrOfY];
        }
    }

    //Event listener
    function AddEvtListener(obj, evtName, f)
    {
        if (document.addEventListener) //FF
        {
            if (obj)
            {
                obj.addEventListener(evtName, f, false);
            }
            else
            {
                addEventListener(evtName, f, false);
            }
        }
        else if (attachEvent) //IE
        {
            if (obj)
            {
                obj.attachEvent('on' + evtName, f);
            }
            else
            {
                attachEvent('on' + evtName, f);
            }
        }
    }
}
