/*

<script src="/js/audience.js"></script>

Audience.pagevue(); // et hop une page vue

<button id="button-1" onclick="Audience.clic(event, this)">button-1</button>

<form onsubmit="return false">
    <input type="button"    clic="input-button-1"   onclick="Audience.clic(event, this)" value="input-button-1">
    <input type="button"    id="input-button-2"     onclick="Audience.clic(event, this)" value="input-button-2">
    <input type="submit"    clic="input-submit-1"   onclick="Audience.clic(event, this)" value="input-submit-1">
    <input type="image"     id="input-image-1"      onclick="Audience.clic(event, this)" src="/i/site/logo-hiver.gif" align="absmiddle">
</form>

<a id="lien-google" href="http://www.google.com"    onclick="return Audience.clic(event, this)">lien-google</a>
<a id="lien-yahoo"  href="http://www.yahoo.com"     onclick="return Audience.clic(event, this)">lien-yahoo</a>

*/

var Audience = {

    service: {
        // 'clic'      : '/W/audience/clic.php',
        // 'pagevue'   : '/W/audience/pagevue.php'
        'clic'      : window.location.protocol+'//'+window.location.host+'/W/audience/clic.php',
        'pagevue'   : window.location.protocol+'//'+window.location.host+'/W/audience/pagevue.php'
    },

    xhr: null,
    lastURL: null,
    lastBody: null,

    getXHR: function() {
        if (this.xhr == null) {
            /*
            if (this.xhr == null) try { this.xhr = new ActiveXObject("Msxml2.XMLHTTP") ; } catch(e) { this.xhr = null ; }
            if (this.xhr == null) try { this.xhr = new ActiveXObject("Microsoft.XMLHTTP") ; } catch(e) { this.xhr = null ; }
            if (this.xhr == null) try { this.xhr = new XMLHttpRequest() ; } catch(e) { this.xhr = null ; }
            */
            this.xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ie ? new ActiveXObject('Microsoft.XMLHTTP') : null);
            if (this.xhr == null) {
                return null;
            }
        }
        return this.xhr;
    },

    getAffilFromCookie: function() {
        var affil = { affil_src_id: null, affil_code_id: null };
        var pair = document.cookie.match(/_affil=([^;]+);/);
        if (pair != null) {
            var fields     = pair[1].split(/%3A/i);
            affil.affil_src_id  = fields[0];
            affil.affil_code_id = fields[1];
        }
        return affil;
    },

    getDocumentReferer: function() {
        return document.referrer;
    },

    pagevue: function() {
        var body = "document_referer="+escape(this.getDocumentReferer());
        // alert(body); return;
        // console.log(body);

        var xhr = this.getXHR();
        if (xhr == null) {
            // console.log("no XHR");
            return false;
        }
        // normalement le true ci-dessous devrait éviter de bloquer l'affichage de la page
        // même si l'appel Audience.pagevue se fait en haut du body

        xhr.open('POST', this.service.pagevue, true); // ici il faut un true (false == synchrone, true == asynchrone)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(body);
        this.lastURL    = this.service.pagevue;
        this.lastBody   = body;
        return false;
    },

    getScreenGeometry: function() {
        var width = 0;
        var height = 0;

        // dimensions de l'écran
        if (typeof(screen.width) == "number") {
            width = screen.width;
            height = screen.height;
        } else {
        }
        var geometry = width+"x"+height;
        // console.log("getScreenGeometry "+geometry);
		return geometry;
    },
    getInnerGeometry: function() {
        var width = 0;
        var height = 0;

        // dimensions de la zone visible = dimensions externes fenêtre - épaisseurs scrollbar
        if (typeof(document.body.clientWidth) == "number") {
            width = document.body.clientWidth;
            height = document.body.clientHeight;
        } else {
        }
        var geometry = width+"x"+height;
        // console.log("getInnerGeometry "+geometry);
		return geometry;
    },
    getClicPosition: function(event) {
        event = event || window.event;
        if (event == null) {
            // console.log("getClicPosition : window.event is null");
            return "undefined";
        }

        var x = 0;
        var y = 0;

        if (typeof(event.clientX) == "number") {
            // console.log("event.clientX");
            x += event.clientX;
            y += event.clientY;
        } else if (typeof(event.pageX) == "number") {
            // console.log("event.pageX");
            x += event.pageX;
            y += event.pageX;
        }
        // console.log("getClicPosition : "+x+" "+y);
        if (typeof(document.body.scrollLeft) == "number") { // en premier pour webkit
            // console.log("document.body.scrollLeft");
            x += document.body.scrollLeft;
            y += document.body.scrollTop;
        } else if (typeof(document.documentElement.scrollLeft) == "number") {
            // console.log("document.documentElement.scrollLeft");
            x += document.documentElement.scrollLeft;
            y += document.documentElement.scrollTop;
        }
        // console.log("getClicPosition : "+x+" "+y);
		var position = "";
		if (x<0) {
		    position += x;
		} else {
		    position += "%2B"+x;
		}
		if (y<0) {
		    position += y;
		} else {
		    position += "%2B"+y;
		}
		return position;
    },
    writeClicImgTag: function(clic_humanID) {
        // console.log(clic_humanID);
        var qs = ""
        +"nocache="+escape(Math.random())
        +"&mode=image"
        +"&clic_humanID="+escape(clic_humanID)
        +"&screen_geometry="+escape(this.getScreenGeometry())
        +"&inner_geometry="+escape(this.getInnerGeometry())
        +"&document_referer="+escape(this.getDocumentReferer());
        // console.log(qs);
        var url = this.service.clic+"?"+qs;
        // console.log(url);
        var html = "<img src=\""+url+"\" border=\"0\"/>";
        // console.log(html);
        document.writeln(html);
    },
    clic: function(event, element, clicName, clicUrl) {
        //console.log(clicName);
        var clic_humanID = null;
        var redirect_uri = null;

        if (element != null) {
            if (typeof(element) == "object") {
                var tagName = element.tagName;
                tagName = tagName.toLowerCase();
                // console.log('tagName = '+tagName);
                if (tagName == "a") {
                    if (typeof(element.clic_href) == "undefined") {
                        element.clic_href = element.getAttribute('href');
                    }
                    redirect_uri = element.clic_href;
                    clic_humanID = element.getAttribute('clic') || element.getAttribute('id');
                }
                // else if (tagName == "select") { // on ne sait pas le gérer proprement
                //     clic_humanID = element.getAttribute('clic') || element.getAttribute('id') || element.getAttribute('name');
                // }
                else if (tagName == "input") {
                    clic_humanID = element.getAttribute('clic') || element.getAttribute('id') || element.getAttribute('name');
                }
                else if (tagName == "button") {
                    clic_humanID = element.getAttribute('clic') || element.getAttribute('id');
                }
            }
        }
        // console.log('clic_humanID = '+clic_humanID);
        // console.log('redirect_uri = '+redirect_uri);

        if (clicName != null && typeof(clicName) == "string") {
            clic_humanID = clicName;
        }
        if (clicUrl != null && typeof(clicUrl) == "string") {
            redirect_uri = clicUrl;
        }
        // console.log('clic_humanID = '+clic_humanID);
        // console.log('redirect_uri = '+redirect_uri);

        /////////////////////////////////////////////////////////////////////////////////////
        var body = ""
        +"clic_humanID="+escape(clic_humanID)
        +"&redirect_uri="+escape(redirect_uri)
        +"&screen_geometry="+escape(this.getScreenGeometry())
        +"&inner_geometry="+escape(this.getInnerGeometry())
        +"&clic_position="+escape(this.getClicPosition(event))
        +"&document_referer="+escape(this.getDocumentReferer());
        // console.log(body);

        if (element != null && typeof(element) == "object" && redirect_uri != null && typeof(redirect_uri) == "string") {
            element.href = this.service.clic+'?'+body;
            this.lastURL    = this.service.clic;
            this.lastBody   = body;
            return true;
        } else {
            var xhr = this.getXHR();
            if (xhr == null) {
                // console.log("no XHR");
                return false;
            }
            xhr.open('POST', this.service.clic, false); // ici il faut un false (false == synchrone, true == asynchrone)
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send(body);
            this.lastURL    = this.service.clic;
            this.lastBody   = body;
            return false;
        }
    }

};
