
var active_signup = '';

// called when an account ajax call has returned with new contents to replace the account sign_in/up box
//     set proper state for sign in and join buttons, fill in drop down with sign or join
//     remap any internal anchors to use the account_submit ajax call
function refresh_sign_in(response, account_action) {

    if ((account_action == '/account/sign_up') || (account_action == '/account/connect_create')) {
        $('#sign_in_dropdown').css({'top': '40px', 'right': '0'});
        $('.top .join').css({'background-position': '0 -57px'});
        $('#header_signin').css({'background-position': '0 0'});
        active_signup = 'account_signup';
    }
    else {
        $('#sign_in_dropdown').css({'top': '20px', 'right': '102px'});
        $('.top .join').css({'background-position': '0 -57px'});
        $('#signup_slidedown #sign_in_username_email').focus();
        $('#header_signin').css({'background-position': '0 -20px' });
        active_signup = 'account_signin';
    }

    $('#sign_in_dropdown').html(response);
    $('[rel=ajax_account]').each(function() {
            $(this).attr('href', "javascript:account_submit('" + $(this).attr('account_url') + "', '')");
        });

    $('#signup_slidedown').show();
}

// all submit buttons in account module add an onsubmit to call this function with the form id
//     this turns the submit into an ajax call
function account_submit(url, form_id) {

    $.post(url, (form_id) ? $('#' + form_id).serialize() : '{}', function(response) {

            if (response.substr(0, 10) != '<!-- embed') {

                // if signing up , force them to fill out account settings
                if ((url == '/account/sign_up') || (connect_create))
                    window.location = '/account/account_settings';
                else
                    window.location.reload();
                
                return;
            }
            refresh_sign_in(response, url);
        });
}

// hide signup if already active 
//
function toggle_buttons(url) {
    if (((url == '/account/sign_up') && (active_signup == 'account_signup')) ||
        ((url != '/account/sign_up') && (active_signup == 'account_signin'))) {
        $('#signup_slidedown').hide();
        active_signup = '';
        return false;
    }
    return true;
}

// hook up the buttons to the account_submit ajax call
//
$(document).ready(function()
{
        $('#header_join').click(function(e) {
                e.preventDefault();
                if (toggle_buttons('/account/sign_up')){
                    account_submit('/account/sign_up', '');
 				}
            });

        $('#header_signin').click(function(e) {
                e.preventDefault();
                if (toggle_buttons('/account/sign_in'))
                    account_submit('/account/sign_in', ''); 
            });

        $('#footer_join').submit(function () {
                account_submit("/account/sign_up", 'footer_join');
                $("html,body").animate({ scrollTop: 0 }, "slow");
                return false;
            });

        $('#blog_sign_in').click(function(e) {
                e.preventDefault();
                location.hash = 'comment-block';
                account_submit('/account/sign_in', ''); 
            });

        $('.account_module #close').live('click', (function(e) {
                e.preventDefault();
                $('#signup_slidedown').hide();
                active_signup = '';
                })
        );

        if (sign_in_now){
            account_submit('/account/sign_in', ''); 
		}
		
        if (connect_create) {
            account_submit('/account/connect_create', ''); 
		}
 });


