$(function() {
    var login = $.profile("login");

    if (!login) {
        login = "webmail";
    }

    if (login == "webmail") {
        selectCustomerWebmail();
    }
    else {
        selectCustomerToolbox();
    }

    // Add tab control
    $('#customer_login_username').autotab({ target: $('#customer_login_password') });
    $('#customer_login_password').autotab({ target: $('#customer_login_username') });

    // Initialise username/password field background behaviour.
    $("#customer_login_username, #customer_login_password")
        .each(function() { if(!this.value) $(this).addClass("initial"); else $(this).removeClass("initial");})
        .blur(function() { this.value || $(this).addClass("initial"); })
        .focus(function() { $(this).removeClass("initial"); });

    // Set focus to the login form
    $('#customer_login_username')[0].focus();
});

$("#customer_login_toolbox").click(function() { selectCustomerToolbox(); submitLoginForm(); return false; });
$("#customer_login_webmail").click(function() { selectCustomerWebmail(); submitLoginForm(); return false; });

/** Fix for roll over not updating state correctly. */
$("#customer_login_webmail").bind('mouseenter', function() { 
    $("#customer_login_webmail").addClass('selected');
    $("#customer_login_toolbox").removeClass('selected');
});
$("#customer_login_toolbox").bind('mouseenter', function() { 
    $("#customer_login_toolbox").addClass('selected');
    $("#customer_login_webmail").removeClass('selected');
});

$("#customer_login_form").submit(function() {
    $.profile("login", $("#customer_login_webmail").hasClass("selected") ? "webmail" : "toolbox");
    return true;
});
        
function submitLoginForm() {
    if ($.browser.msie && $.browser.version == "6.0") {
        var a = $(document.createElement("input"));
        a.attr('type', 'submit');
        a.css('display', 'none');
        $(document.customer_login_form).append(a);
        a.trigger('click');
    }
    else
        document.getElementById("customer_login_form").submit(); 
}

function selectCustomerToolbox() {
    $("#customer_login_webmail").removeClass("selected");
    $("#customer_login_toolbox").addClass("selected");

    if ($.browser.msie) {
        try {
            document.customer_login_form.action = "https://toolbox.iinet.net.au/";
        }
        catch (e) {
        }
    }
    else {
        document.getElementById("customer_login_form").action = "https://toolbox.iinet.net.au/";
    }

    var a = $(document.createElement("input"));
    a.attr('name', 'action');
    a.attr('value', 'login');
    a.css('display', 'none');
    $(document.customer_login_form).append(a);

    return false;
};

function selectCustomerWebmail() {
    $("#customer_login_toolbox").removeClass("selected");
    $("#customer_login_webmail").addClass("selected");

    if ($.browser.msie) {
        try {
            document.customer_login_form.action = "http://mail.iinet.net.au/mail/atmail.pl";
        }
        catch (e) {
        }
    }
    else {
        document.getElementById("customer_login_form").action = "http://mail.iinet.net.au/mail/atmail.pl";
    }

    var a = $(document.createElement("input"));
    a.attr('name', 'action');
    a.attr('value', 'login');
    a.css('display', 'none');
    $(document.customer_login_form).append(a);

    return false;
};

// vim: set et ts=4 sw=4 cin nopaste:
