// MLY_LOGIN_BASEURI = "lo-f.at/lfblog/";
// MLY_LOGIN_TAGID   = "login";


function MlyLoginApp() {
    this.initRequest();
    this.initStylesheets();
}

MlyLoginApp.prototype = {
    state: 0,
    xsltManager: null,
    reqManager: null,
    
    initRequest: function() {
        this.reqManager = new MlyRequestManager(MLY_LOGIN_BASEURI);
    },

    initStylesheets: function() {
        this.xsltManager = new MlyXSLTManager();

        this.xsltManager.addStyleURI( "login", 
                                      "http://" + MLY_LOGIN_STYLEURI + "login.xsl" );
        this.xsltManager.addStyleURI( "info", 
                                      "http://" + MLY_LOGIN_STYLEURI + "info.xsl" );
    
        this.xsltManager.addXSLParam( "servername", MLY_SERVER );
        this.xsltManager.addXSLParam( "authpath",   MLY_LOGIN_AUTHURI );
        this.xsltManager.addXSLParam( "imagepath",  MLY_BLOG_IMAGEURI );
    },   
    
    prepareStatus: function() {
        this.reqManager.prepareURI("login", false);
        this.reqManager.request(null, false);
    },

    handleStatus: function() {
        this.prepareStatus();
        this.display();
    },

    handleLogin: function() {
        this.reqManager.prepareURI("login", false);
        // fetch the login form information

        // we should ask the user, if he/she wants to
        // work in secure mode then we submit.

        var f = document.getElementById("loginform");
        
        var envelope = this.getFormData( f );        
        
        if ( envelope ) {
                f.submit();
                return false;
        }
        this.reqManager.request(envelope, false);
        this.display();
        return false;
    },

    handleLogout: function() {
        this.reqManager.prepareURI("logout", false);
        this.reqManager.request(null, false);  
        // now fetch the new information
        this.handleStatus();
        return false;
    },
 
    display: function() {
        var status = this.getRPCStatus();
        if ( status != null ) {
            if ( status == "OK" ) {
                this.xsltManager.display( "info", 
                                          MLY_LOGIN_TAGID, 
                                          this.reqManager.rpcresult);
            }
            else {
                this.xsltManager.display( "login", 
                                          MLY_LOGIN_TAGID, 
                                          this.reqManager.rpcresult);
            }
        }
    },
    
    getRPCStatus: function() {
        var res =  this.reqManager.rpcresult;
        var retval = "";
        if ( res != null ) {
            retval = res.documentElement.childNodes[1].childNodes[0].nodeValue;
        }
        return retval;
    },
    
    getFormData: function(form) {
        var aParams = new Array();

        var str;
        str = encodeURIComponent("email") + "=" + encodeURIComponent( form.email.value );
        aParams.push( str );
        str = encodeURIComponent("passwd") +  "=" + encodeURIComponent( form.passwd.value );
        aParams.push( str );

        return aParams.join( "&" );
    },
    
    switchPanel: function() {
	this.xsltManager.switchStyle( 'loginscreen', 
			              'userinfoblock',
                                      'invisibleblock' ); 
    },

    showLogin: function() {
	this.xsltManager.switchStyle( 'loginscreen', 
			              'rightfloatscreen',
                                      'invisibleblock' ); 
    },

    hideLogin: function() {
	this.xsltManager.changeStyle( 'loginscreen', 
			              'invisibleblock'); 
    }
    
};


var mlyLoginApp = new MlyLoginApp();

if ( window.attachEvent ) {
    window.attachEvent('onload', function(e) {
        mlyLoginApp.handleStatus();
    } ) ;
}
else if ( window.addEventListener ) {
    window.addEventListener( 'load', 
                             function(e) { mlyLoginApp.handleStatus(); },
                             false );
}
