YAHOO.util.Event.onDOMReady(function(){
try {
    var Y = YAHOO, U = Y.util, E = U.Event, D = U.Dom, d = document, unmEdt, pwdEdt, unmEdp, pwdEdp, doLogin, txtUser, txtPass, frmLogin, showMsg, msgDiv = D.get("login_msg_dv"), int;
    txtUser = D.get("login_txt_usr");
    txtPass = D.get("login_txt_pwd");
    
    // Effects...
    unmEdt = function() {
        if(!D.hasClass(this,"fnt") && this.value == "") {
            this.value  = "Username";
            this.edited = false;
            D.addClass(this,"fnt");
        } else {
            //alert("Here");
        }
    };
    
    pwdEdt = function() {
        if(!D.hasClass(this,"fnt") && this.value == "") {
            this.type   = "text";
            this.value  = "Password";
            D.addClass(this,"fnt");
        }
    };
    
    unmEdp = function() {
        if(D.hasClass(this,"fnt")) {
            this.value = "";
            D.removeClass(this, "fnt");
            this.focus();
        }
    };
    
    pwdEdp = function() {
        if(D.hasClass(this,"fnt")) {
            this.value = "";
            this.type  = "password";
            D.removeClass(this, "fnt");
            this.focus();
        }
    };
    
    E.on(txtUser,"mouseover",unmEdp);
    E.on(txtPass,"mouseover",pwdEdp);
    E.on(txtUser,"focus",unmEdp);
    E.on(txtPass,"focus",pwdEdp);
    E.on(txtUser,"blur",unmEdt);
    E.on(txtPass,"blur",pwdEdt);
    unmEdt.call(txtUser);
    pwdEdt.call(txtPass);
    
    // Showing message
    showMsg = function(msg, type) {
        switch(type) {
            default:
            case "info":
                msgDiv.style.color = "blue";
                break;
            case "warning":
                msgDiv.style.color = "red";
                break;
        }
        
        msgDiv.style.display = "block";
        msgDiv.innerHTML     = msg;

        height               = 15;
        var time = 300, steps = 10, period = time / steps, red = height / steps, i = 0;
        
        if(int) {
            window.clearInterval(int);
        }
        
        int = window.setInterval(function(){
            hgt                 = (++i * red);
            msgDiv.style.height = (hgt) + "px";
        }, period / 1000);
        
        window.setTimeout(function(){window.clearInterval(int);}, time);
    };
    
    hideMsg = function(v) {
        if(v && E.getCharCode(v) == 13) {
            return;
        }
        
        height               = 15;
        var time = 300, steps = 10, period = time / steps, red = height / steps, i = 0;
        
        if(int) {
            window.clearInterval(int);
        }
        
        int = window.setInterval(function(){
            msgDiv.style.height = (height - ++i * red) + "px";
        }, period / 1000);
        
        window.setTimeout(function(){msgDiv.style.display = "none";window.clearInterval(int);}, time);
    };
    
    E.on(txtUser,"keypress",hideMsg);
    E.on(txtPass,"keypress",hideMsg);
    
    // Login proper...
    doLogin = function(e) {
        try {
            E.preventDefault(e);
        } catch(a){}
        
        var userName = txtUser.value,password = txtPass.value;
        showMsg("Loggin you in... please wait a while.", "info");
        
        if(D.hasClass(txtUser,"fnt") || D.hasClass(txtPass,"fnt") || userName == "" || password == "") {
            // One of the form fields have not been filled yet...
            showMsg("Please fill in the username and password.", "warning");
            return;
        }
        
        var data = "username=" + encodeURIComponent(userName) + "&password=" + encodeURIComponent(password),
        url = "/ajax/account/login",
        cb = {
            success: function(o) {
                try {
                    var r = Y.lang.JSON.parse(o.responseText);
                    
                    if(r.success) {
                        showMsg("Login succeeded... reloading page.", "info");
                        window.location = location.href;
                    } else {
                        showMsg("Login failed. Check username and password.", "warning");
                    }
                } catch(f) {
                    showMsg("Dynamic login failed, reverting to normal login.", "warning");
                    frmLogin.submit();
                    return;
                }
            },
            
            failure: function() {
                showMsg("Login failed. Please check network connection.", "warning");
            }
        };
        
        U.Connect.asyncRequest("POST", url, cb, data);
    };
    
    frmLogin = D.get("login");
    E.on(frmLogin, "submit", doLogin);
} catch(e){
//    alert(e.stack);
}
});
