document.observe("dom:loaded", function() {
    try {
        var ua = navigator.userAgent.toLowerCase(),
            platform = navigator.platform.toLowerCase(),
            UA = ua.match(/(opera|ie|firefox|chrome|version)[\s\/:]([\w\d\.]+)?.*?(safari|version[\s\/:]([\w\d\.]+)|$)/) || [null, 'unknown', 0],
            mode = UA[1] == 'ie' && document.documentMode;
        
        var Browser = this.Browser = {
            name: (UA[1] == 'version') ? UA[3] : UA[1],
            version: mode || parseFloat((UA[1] == 'opera' && UA[4]) ? UA[4] : UA[2]),
            Platform: {
                name: ua.match(/ip(?:ad|od|hone)/) ? 'ios' : (ua.match(/(?:webos|android)/) || platform.match(/mac|win|linux/) || ['other'])[0]
            }		
        };

        var analyzer = function( element_to_inject_image ){
    
            // Prototype implementation of documentMode.getSize()
            var screen_size  = document.viewport.getDimensions();
            var old_cookie   = document.cookie || '';
            var cookie_match = old_cookie.match(/session_uuid=([^;]+)/);
            var session_uuid = cookie_match == undefined ? undefined : cookie_match[1];
            
            // this is Number.random() in Mootools 1.2
            var number_random = function( min, max ){
                return Math.floor( min + Math.random() * ( max - min + 1 ) )
            };
            
            if ( session_uuid == undefined ){
                var stupid_array = Array(10).join().split(',');
                session_uuid = stupid_array.map(function(){return number_random(65,90)}).map(function(n){return String.fromCharCode(n)}).join('');
                document.cookie = [
                    'session_uuid=' + session_uuid,
                    old_cookie,
                    'path=/'
                ].join('; ');
            }
            
            var data = {
                browser_name:       Browser.name,
                browser_platform:   Browser.Platform.name,
                browser_version:    Browser.version,
                screen_x:   screen_size.width,
                screen_y:   screen_size.height,
                url:        document.URL,
                referrer:   document.referrer,
                uuid: session_uuid,
                project_id: 5, // rsvp project
                r: Math.random()
            };
            
            // http and https addresses 
            var image_url = 'http://analyzer.zwoelf.net/pixel.gif';
            if( document.URL.match(/^https/) != null ){
                image_url = 'https://analyzer.zwoelf.net:4443/pixel.gif'; // 4443 is a nonstandard port, that's right.
            }

            var image_src =  image_url + '?' + $H( data ).toQueryString();
            element_to_inject_image.insert({ bottom: new Element( 'img', { src: image_src } ) });
            
        };
   
        analyzer( document.body );
    } catch(e){ console.log(e) }
});

