

var UserTracking = new Class({


    str_track_url: null,


    /**
     * Constructor
     *
     * Determines if the UserTracking cookie is already set and/or whether
     * the client itself supports cookies before showing the prompt.
     *
     * @param str str_popup_url           URL of the prompt to show
     * @param str str_track_url           URL to send the results to
     * @return void
     */
    initialize: function(str_popup_url, str_track_url) {
    
        this.str_track_url = str_track_url;
    
        // does the client support cookies?
        if(!this._has_cookies()) {
            return false;
        }
    
        // cookie support is available
        this._show_prompt(str_popup_url);
    },
    
    
    
    /**
     * Determines whether the client supports cookies by creating a temporary one
     * and attempting to read it back.
     *
     * @return boolean
     */
    _has_cookies: function() {
    
        // set a test cookie
        Cookie.write('cookie_test', $time());
        
        // test we can read the cookie back
        if(!Cookie.read('cookie_test')) {
            return false;
        }
        
        // cookie set OK. dispose of it before returning true
        Cookie.dispose('cookie_test');
        
        return true;
    },
    
    
    
    /**
     * Displays the prompt as an iframe (creates this and the "fade out" shim
     * behind it.
     *
     * @param str str_url  URL of the prompt to show
     * @return void
     */
    _show_prompt: function(str_url) {
    
        var obj_window_size = window.getSize();
        var obj_window_scroll = window.getScrollSize();
        
        // create shim
        var elm_shim = new Element('div', {
            id: 'user_tracking_shim',
            
            styles: {
                'width': obj_window_scroll.x,
                'height': obj_window_scroll.y
            }
        }).inject(document.body, 'bottom');
        
        // create prompt
        var elm_iframe = new Element('iframe', {
            id: 'user_tracking',
            frameborder: '0',
            allowTransparency: 'true',
            src: str_url
        }).inject(document.body, 'bottom');

        // position it in the center of the screen
        var obj_iframe_size = elm_iframe.getSize();
        
        elm_iframe.setStyles({
            'top': (obj_window_size.y / 2) - (obj_iframe_size.y / 2),
            'left': (obj_window_size.x / 2) - (obj_iframe_size.x / 2)
        });
    },
    
    
    
    /**
     * Usually called externally (from the iframe prompt) in order to store
     * the user's selection. Sets the cookie with passed value and returns.
     *
     * @param str str_type_id  Type ID retrieve from the iframe prompt
     * @return void
     */
    store_type: function(str_type_id) {
        
        // send the selected item to the server
        new Request({
            url: this.str_track_url,
            method: 'post',
            data: 'type=' + str_type_id
        }).send();
        
        // hide the shim and iframe elements
        $(document.body).getElement('#user_tracking').fade('hide');
        $(document.body).getElement('#user_tracking_shim').fade('out');
    }
});

