/**
 * auth.js
 * Fall 2005
 *
 * JavaScript support for the HB_Auth module. Login form submissions are re-
 * routed through XMLHttpRequest in order to minimize page refreshes for
 * unsuccessful logins. There is also a method that can be used to
 * identify a logged in user from other JavaScripts.
 *
 * @author  Dan Phiffer <dan@phiffer.org>
 * @version 0.03
 */


function HB_Auth() {
    
    if (!document.getElementById) {
        return;
    }
    
    var login_form = document.getElementById('auth');
    if (login_form) {
        //login_form.onsubmit = this.submit_credentials;
        this.setup_login_form();
    }
    
    var user_div = document.getElementById('user');
    var options_link = user_div.getElementsByTagName('a');
    if (options_link.length > 0) {
        options_link = options_link[0];
        options_link.onclick = this.toggle_options;
    }
    
}

HB_Auth.prototype.setup_login_form = function() {
    
    var form = document.getElementById('auth');
    
    form.username.empty = true;
    form.username.default_value = form.username.value;
    form.username.onfocus = function() {
        if (this.empty) {
            this.value = '';
            this.empty = false;
            this.className = 'text';
        }
    }
    
    form.username.onblur = function() {
        if (this.value == '') {
            this.empty = true;
            this.value = this.default_value;
            this.className = 'text blank';
        }
    }
    form.password.empty = true;
    form.password.onfocus = function() {
        if (this.empty) {
            this.value = '';
            this.empty = false;
            this.className = 'text';
        }
    }
    
}

HB_Auth.prototype.submit_credentials = function() {
    var content = 'username=' + escape(this.username.value) + '&' +
                  'password=' + escape(this.password.value) + '&' +
                  'async=1';
    var url = this.getAttribute('action');
    remote(url, hb_auth.response_handler, content);
    
    var button = this.getElementsByTagName('input')[2];
    button.setAttribute('disabled', 'disabled');
    
    return false;
}

HB_Auth.prototype.response_handler = function(response) {
    if (response.responseText == 1) {
        window.location.reload();
    } else {
        hb_auth.login_response('Sorry, your login failed. Please try again.');
        var form = document.getElementById('auth');
        var button = form.getElementsByTagName('input')[2];
        button.removeAttribute('disabled');
    }
}

HB_Auth.prototype.login_user = function() {
    var login = document.getElementById('login');
    var header = document.getElementById('header');
    login.style.display = 'none';
    var div = header.insertBefore(document.createElement('div'), login);
    div.className = 'user';
    div.innerHTML = 'Logged in as <strong>' + login.username.value +
                    '</strong> &middot; <a href="?logout">Logout</a>';
}

HB_Auth.prototype.logout_user = function() {
    // TODO: build a logout clean-up mechanism
}

HB_Auth.prototype.toggle_options = function(refresh) {
    
    var body = document.getElementsByTagName('body')[0];
    
    if (refresh == true && hb_auth.options) {
        var options = hb_auth.options;
        hb_auth.options = null;
        body.removeChild(options);
    } else if (!hb_auth.options) {
        hb_auth.options = body.appendChild(document.createElement('div'));
        hb_auth.options.setAttribute('id', 'user_options');
        remote('./', hb_auth.setup_options_markup, 'hb_auth=show_options');
    } else if (hb_auth.options.style.display == 'none') {
        document.getElementById('header_right_corner').style.visibility = 'hidden';
        hb_auth.options.style.display = 'block';
    } else {
        hb_auth.options.style.display = 'none';
        document.getElementById('header_right_corner').style.visibility = 'visible';
    }
    return false;
}

HB_Auth.prototype.setup_options_markup = function(resp) {
    
    document.getElementById('header_right_corner').style.visibility = 'hidden';
    
    hb_auth.options.innerHTML = resp.responseText;
    
    var user_div = document.getElementById('user');
    hb_auth.options.style.top = (user_div.offsetTop + 
                                 user_div.offsetHeight) + 'px';
    hb_auth.options.style.left = find_left(user_div) + 'px';
    hb_auth.options.style.width = user_div.offsetWidth + 'px';
    
    hb_auth.options.getElementsByTagName('form')[0].onsubmit = hb_auth.options_submit;
}

HB_Auth.prototype.options_submit = function() {
    var content = 'name=' + escape(this.name.value) + '&' +
                  'email=' + escape(this.email.value) + '&' +
                  'website=' + escape(this.website.value) + '&' +
                  'hb_auth=save_options';
    remote('./', hb_auth.options_response, content);
    return false;
}

HB_Auth.prototype.options_response = function(resp) {
    
    hb_auth.toggle_options();
}

HB_Auth.prototype.login_response = function(message) {
    var form = document.getElementById('auth');
    if (!form.response) {
        form.response = form.appendChild(document.createElement('div'));
    }
    form.response.className = 'response';
    form.response.innerHTML = message;
}

HB_Auth.prototype.get_username = function() {
    var user_div = document.getElementById('user');
    
    var a_list = user_div.getElementsByTagName('a');
    if (a_list.length == 0) {
        return false;
    }
    return a_list[0].innerHTML;
}

HB_Auth.prototype.check_access = function(action) {
    var content = 'hb_auth=check_access&action=' + escape(action);
    remote('./', hb_auth.check_access_response, content);
}

HB_Auth.prototype.check_access_response = function(response) {
    //alert(response);
}

var hb_auth;
add_event(window, 'onload', function() {
    hb_auth = new HB_Auth();
});

