/**********************************************************************
* /js/component/authentication.js
* Copyright (C) 2007-2008 Kyoto University
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-
* 1301  USA
***********************************************************************/

//* -*- encoding: utf-8; -*- */
//* $Id: authentication.js 604 2008-02-28 04:36:36Z morimoto $
//* $Date:: 2008-02-28 13:36:36 +0900#$
//*/

Event.observe( window, 'load', function(){
	var args = {
		'dir' : '.'
	};

	$A( document.getElementsByTagName( 'script' ) ).findAll( function(s) {
		return ( s.src && s.src.match( /authentication\.js\?(.*)?/ ) );
	} ).each( function(s) {
		s.src.match( /\.js\?(.*)/ );
		$A( RegExp.$1.split( '&' ) ).each( function(e) {
			var a = e.split( '=', 2 );
			args[ a[0] ] = a[1];
		} );
	} );
	
	new Ajax.Request(
		args['dir'] + '/php/ajax/authentication.php',
		{
			method : 'post',
			onSuccess : function ( httpObj ) {
				var res = eval( '(' + httpObj.responseText + ')' );
				
				if($( 'auth' )){
				
					var oAuth = $( 'auth' );

					if ( res['status'] == 'ERROR' ) {
						var oDiv = document.createElement( 'div' );
						var oName = document.createElement( 'span' );
						oName.id = 'auth-user-name';
						oName.appendChild( document.createTextNode( 'Authentication Failed : ' ) );
						oDiv.appendChild( oName );
						oDiv.appendChild( document.createTextNode( res['contents'] ) );
						Element.update( oAuth, oDiv );

						var oLink = $('auth-link');
						oLink.href = args['dir'] + '/auth/login';
						Element.update( oLink, 'Login' );
						return;
					}

					res = res['contents'];

					var oLink = document.createElement( 'a' );
					oLink.href = args['dir'] + '/auth/profile_change';
					oLink.title = 'Update My Account Information';

					var oName = document.createElement( 'span' );
					oName.id = 'auth-user-name';
					oName.appendChild( document.createTextNode( res['user_name'] ) );
					if ( parseInt( res['logged_in'] ) ) {
						oLink.appendChild( oName );
						oAuth.appendChild( oLink );
					} else {
						oAuth.appendChild( oName );
					}

					oAuth.appendChild( document.createTextNode( '.' ) );

					var oLink = $('auth-link');
					if ( parseInt( res['logged_in'] ) ) {
						oLink.href = args['dir'] + '/auth/logout';
						Element.update( oLink, 'Logout' );
					} else {
						oLink.href = args['dir'] + '/auth/login';
						Element.update( oLink, 'Login' );
					}
				
				}else{
				
					res = res['contents'];
					var oLink = $('auth-link');
					if ( parseInt( res['logged_in'] ) ) {
						oLink.href = args['dir'] + '/auth/logout';
						Element.update( oLink, 'Logout' );
					} else {
						oLink.href = args['dir'] + '/auth/login';
						Element.update( oLink, 'Login' );
					}
					
				}
				
			},
			
			onFailure: function(){
				alert('Server Error.');
			}
		} );
} );
