/**
 * Menu controller
 *
 * @changes 2007-11-02 / mw / created
 *
 * @copyright Copyright © 2005-2007 by www.CHILIBYTES.com
 * @author mw/mb
 * @version 1.0.0
 *
 * @package de.jj-bw
 * @subpackage www
 */

/**
 * Namespaces
 */
if ( !window.de ) window.de = {};
if ( !de.jjbw ) de.jjbw  = {};
if ( !de.jjbw.www ) de.jjbw.www = {};


var CB_Environment = com.chilibytes.ccl.html.CB_Environment;
var CB_WindowEvents = com.chilibytes.ccl.controls.CB_WindowEvents;
var CB_SSOAuthForm = com.chilibytes.ccl.security.CB_SSOAuthForm;

/**
 * Constructor
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController = function() {
	// Constructor
	this.constructor = de.jjbw.www.JJBW_SSOController;
	// Super class
	CB_WindowEvents.call( this );
	// Objects
	this.fSSOAuthForm = null;
	this.fMenuItemLogin = null;
	this.fPreviousMainMenuItem = null;
	this.fPreviousSubMenuItem = null;
	this.fIframeContent = null;
	this.fLoggedIn = false;
	this._InitJJBW_SSOController();
}

/**
 * Inherit from window events
 */
var _JJBW_SSOControllerInheritP = function() {};
_JJBW_SSOControllerInheritP.prototype = CB_WindowEvents.prototype;
de.jjbw.www.JJBW_SSOController.prototype = new _JJBW_SSOControllerInheritP();

/**
 * Property to set the login menu item
 *
 * @access private
 */
de.jjbw.www.JJBW_SSOController.prototype.LoginMenuItemSet = function( value ) {
	this.fMenuItemLogin = value;
}

/**
 * Property to set the previous menu items
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.PreviousMenuItemClickedSet = function( mainMenuItem, subMenuItem ) {
	this.fPreviousMainMenuItem = mainMenuItem;
	this.fPreviousSubMenuItem = subMenuItem;
}

/**
 * Property to set the content iframe
 *
 * @access private
 */
de.jjbw.www.JJBW_SSOController.prototype.IFrameContentSet = function( value ) {
	this.fIframeContent = value;
}

/**
 * Property to get login status
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.IsLoggedIn = function() {
	return this.fLoggedIn;
}

/**
 * Property to get login status
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.Logout = function() {
	this.fSSOAuthForm.Logout();
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.Show = function() {
	if ( this.fSSOAuthForm != null ) {
		this.fSSOAuthForm.VisibleSet( true );
	}
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.Hide = function() {
	if ( this.fSSOAuthForm != null ) {
		this.fSSOAuthForm.VisibleSet( false );
	}
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.CheckAuthorisation = function() {
	if ( this.fSSOAuthForm != null ) {
		this.fSSOAuthForm.IsAuthorizedCheck();
	}
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.EventLoginSuccess = function() {
	this.fLoggedIn = true;
	if ( this.fMenuItemLogin ) {
		this.fMenuItemLogin.AlternativeLabelSwitch();
	}
	var menuPath = null;
	if ( this.fPreviousMainMenuItem != null ) menuPath = this.fPreviousMainMenuItem.IDGet();
	if ( this.fPreviousSubMenuItem != null ) menuPath += "/" + this.fPreviousSubMenuItem.IDGet();
	if ( menuPath != null ) JJBW_Page.MenuItemActivate( menuPath );
	if ( this.fIframeContent != null ) this.fIframeContent.src = this.fIframeContent.contentDocument.location.href;

}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.EventIsAuthorized = function( authorized ) {
	if ( authorized && !this.fLoggedIn ) {
		if ( this.fMenuItemLogin ) {
			this.fMenuItemLogin.AlternativeLabelSwitch();
		}
	}
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.EventLogoutSuccess = function() {
	this.fLoggedIn = false;
	if ( this.fMenuItemLogin ) {
		this.fMenuItemLogin.AlternativeLabelSwitch();
	}
}

/**
 * Overridden event raised on window load
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.WindowOnLoad = function( eventP ) {

}

/**
 * Overridden event raised on window unload
 *
 * @access public
 */
de.jjbw.www.JJBW_SSOController.prototype.WindowOnUnload = function( eventP ) {

}

/**
 * Initialize function
 */
de.jjbw.www.JJBW_SSOController.prototype._InitJJBW_SSOController = function() {
	this.fSSOAuthForm = new CB_SSOAuthForm( "ajaxinterface.php" );
	this.fSSOAuthForm.EventLoginSuccessAttach( this, this.EventLoginSuccess );
	this.fSSOAuthForm.EventLogoutSuccessAttach( this, this.EventLogoutSuccess );
	this.fSSOAuthForm.EventIsAuthorizedAttach( this, this.EventIsAuthorized );
	this.fSSOAuthForm.LoginFailureMessageSet( "Benutzername und/oder Passwort konnten nicht erkannt werden..." );
}

/**
 * Initialize function
 */
de.jjbw.www.JJBW_SSOController.prototype.Submit = function() {
	if( this.fSSOAuthForm != null ) {
		this.fSSOAuthForm.Login();
	}
}

/**
 * Property to get the current ajax sso form object
 *
 * @access private
 */
de.jjbw.www.JJBW_SSOController.prototype.SSOFormGet = function() {
	return this.fSSOAuthForm;
}
