/**
 * JJBW Page object
 *
 * @changes 2008-04-23 / mb / created
 *
 * @copyright Copyright © 2005-2008 / CHILIBYTES Böhm & Welte GbR / www.chilibytes.com
 * @author mb
 * @version 1.0.0
 *
 * @package de.jj_bw
 * @subpackage www
 *
 */

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

/**
 * Requires windows events
 */
var CB_WindowEvents = com.chilibytes.ccl.controls.CB_WindowEvents;
var CB_Environment = com.chilibytes.ccl.html.CB_Environment;
var CB_CSS = com.chilibytes.ccl.html.CB_CSS;
var CB_Morphing = com.chilibytes.ccl.gfx.CB_Morphing;
var CB_GFXUtilities = com.chilibytes.ccl.gfx.CB_GFXUtilities;
var CB_News = com.chilibytes.ccl.cms.CB_News;
var CB_MailAddress = com.chilibytes.ccl.email.CB_MailAddress;
var JJBW_WSW = de.jj_bw.www.JJBW_WSW;
var JJBW_SSOController = de.jjbw.www.JJBW_SSOController;

/**
 * Constructor
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page = function() {
	// Constructor
	this.constructor = de.jj_bw.www.JJBW_Page;
	// Super class
	CB_WindowEvents.call( this );
	// Morphing
	this.fMorphNewsFadeP = null;
	this.fMorphFrameScaleP = null;
	this.fPageFrameSizeOrg = null;
	this.fInfoFrameSizeOrg = null;
	// Object variables
	this.fFrameHeaderObj = null;
	this.fFrameBodyObj = null;
	this.fFrameFooterObj = null;
	this.fFrameSubNavObj = null;
	this.fFrameContentObj = null;
	this.fFrameNewsinfoObj = null;
	this.fFrameLoaderObj = null;
	this.fIFrameContentObj = null;
	this.fNewsinfoObj = null;
	this.fIsWidescreen = false;
	this.fRefreshSubmenu = false;
	this.fCBNews = null;
	this.fWSWController = null;
	this.fSSOController = null;
	this.fIframeLoadedTimeoutObj = null;
	this.fManualControlLoader = false;
	this.fIFrameMenuPath = null;
	this.fPreviousMainMenuItem = null;
	this.fPreviousSubMenuItem = null;
}

/**
 * Inherit mouse events class
 */
var _jjbwPageInherit = function() {};
_jjbwPageInherit.prototype = CB_WindowEvents.prototype;
de.jj_bw.www.JJBW_Page.prototype = new _jjbwPageInherit();

/**
 * Configures the menu
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._MenuInit = function() {
	if ( CBPage ) {
		if ( document.getElementById( JJBW_PAGE_ID_CONTAINER_WSW ) ) {
			fwswMenu = new CB_Menu( JJBW_PAGE_ID_MENU_WSW, CBPage.AJAXInterfaceURLGet() );
			fwswMenu.EventMenuItemOnClickAttach( this, this.WSWMenuItemClickedEvent );
			fwswMenu.EventMenuItemOnHoverAttach( this, this.WSWMenuItemHoverEvent );
			CBPage.MenuAdd( fwswMenu );
		}
		else if ( window.top == window.self ) {
			fMainMenu = new CB_Menu( JJBW_PAGE_ID_MENU_MAIN, CBPage.AJAXInterfaceURLGet() );
			fMainMenu.EventMenuItemOnClickAttach( this, this.MenuItemClickedEvent );
			fMainMenu.EventMenuItemOnHighlightedAttach( this, this.MainMenuItemHighlightedEvent );
			CBPage.MenuAdd( fMainMenu );

			fSubMenu = new CB_Menu( JJBW_PAGE_ID_MENU_SUB, CBPage.AJAXInterfaceURLGet() );
			fSubMenu.EventMenuItemOnClickAttach( this, this.MenuItemClickedEvent );
			fSubMenu.EventMenuItemOnHighlightedAttach( this, this.SubMenuItemHighlightedEvent );
			CBPage.MenuAdd( fSubMenu );
		}
	}
}

/**
 * Configures the wsw controller
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._WSWInit = function() {
	if ( CBPage && document.getElementById( JJBW_PAGE_ID_CONTAINER_WSW ) ) {
		this.fManualControlLoader = true;
		this.fWSWController = new JJBW_WSW( JJBW_PAGE_ID_CONTAINER_WSW, CBPage.AJAXInterfaceURLGet() );
		this.fWSWController.EventOnLoadedAttach( this, this.WSWOnLoadedEvent )
		this.fWSWController.Reload();
	}
}

/**
 * Configures the news controller
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._NewsInit = function() {
	if ( CBPage && window.top == window.self ) {
		this.fCBNews = new CB_News( JJBW_PAGE_ID_CONTAINER_NEWS, CBPage.AJAXInterfaceURLGet() );
		this.fCBNews.ShortViewLengthSet( JJBW_PAGE_NEWS_SHORT_VIEW_LENGTH );
		this.fCBNews.ShortViewSuffixSet( JJBW_PAGE_NEWS_SHORT_VIEW_SUFFIX );
		this.fCBNews.ShortViewCountSet( JJBW_PAGE_NEWS_SHORT_VIEW_COUNT );
		this.fCBNews.EventNewsItemOnClickAttach( this, this.NewsItemClickedEvent );
		this.fCBNews.Reload();
	}
}

/**
 * Configures the page object
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._PageInit = function( eventP ) {
	this.fFrameHeaderObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_HEADER );
	this.fFrameBodyObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_BODY );
	this.fFrameFooterObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_FOOTER );
	this.fFrameSubNavObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_SUBNAVIGATION );
	this.fFrameContentObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_CONTENT );
	this.fFrameNewsinfoObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_NEWS );
	this.fFrameLoaderObj = document.getElementById( JJBW_PAGE_ID_PAGE_LOADER );
	this.fIFrameContentObj = document.getElementById( JJBW_PAGE_ID_IFRAME_CONTENT );
	this.fNewsinfoObj = document.getElementById( JJBW_PAGE_ID_CONTAINER_NEWS_LOGO );
	if ( this.fFrameNewsinfoObj && this.fNewsinfoObj ) {
		this.fMorphNewsFadeP = new CB_Morphing();
		this.fMorphNewsFadeP.ObjectByReferenceAdd( this.fNewsinfoObj );
	}
	if ( this.fFrameContentObj ) {
		this.fMorphFrameScaleP = new CB_Morphing();
		this.fMorphFrameScaleP.EventOnOperationSuccessHandlerAttach( this, this.MorphFrameScaleOnOperationSuccessEvent );
		this.fMorphFrameScaleP.ObjectByReferenceAdd( this.fFrameNewsinfoObj, false, true , false, false );
		// SSO creation
		this.fSSOController = new JJBW_SSOController();
		this.fSSOController.IFrameContentSet( this.fIFrameContentObj );
		//this.fSSOController.CheckAuthorisation();
	}
}

/**
 * Frame scale succeeded event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.MorphFrameScaleOnOperationSuccessEvent = function() {
	if ( this.fIsWidescreen ) {
		this._NewsVisibilitySet( false );
	}
	else {
		this._NewsHeightFix();
	}
}

/**
 * Window on load event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.MenuItemClickedEvent = function( menuItemP ) {
	this.fRefreshSubmenu = false;
	if ( menuItemP.IDGet() == JJBW_PAGE_ID_LOGIN_MENU_ITEM && JJBW_SSOController ) {
		if ( this.fSSOController.IsLoggedIn() ) {
			this.fSSOController.Logout();
		}
		else {
			this.fSSOController.LoginMenuItemSet( menuItemP );
			this.fSSOController.PreviousMenuItemClickedSet( this.fPreviousMainMenuItem, this.fPreviousSubMenuItem );
			this.fSSOController.Show();
		}
	}
	else {
		if ( menuItemP.TargetGet() != null && menuItemP.TargetGet() != "_blank" ) {
			this._IFrame2Widescreen( menuItemP.UserDataGet( "isWidescreen" ) );
		}
		if ( !menuItemP.UserDataGet( "disableLoader" ) ) {
			this.LoadingStatusChanged( false );
		}
		this.fSSOController.Hide();
	}
}


/**
 * WSE Menu item clicked event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.WSWMenuItemClickedEvent = function( menuItemP ) {
	this.fWSWController.ShowItem( menuItemP.IDGet() );
}

/**
 * WSE Menu item clicked event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.WSWOnLoadedEvent = function() {
	var wswMenu = CBPage.MenuGet( JJBW_PAGE_ID_MENU_WSW );
	if ( wswMenu ) {
		wswMenu.MenuItemActivate( JJBW_PAGE_ID_DEFAULT_WSW_ITEM );
	}
	this.fWSWController.ShowName( JJBW_PAGE_ID_DEFAULT_WSW_ITEM );
	this.fWSWController.ShowItem( JJBW_PAGE_ID_DEFAULT_WSW_ITEM );
}

/**
 * WSW Menu item highlighting event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.WSWMenuItemHoverEvent  = function( menuItemP, hasFocus ) {
	this.fWSWController.ShowName( menuItemP.IDGet(), hasFocus );
}

/**
 * Window on load event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.NewsItemClickedEvent = function( newsID ) {
	this.MenuItemActivate( "navi14/navi141" );
}

/**
 * Activates an menu item
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.MenuItemActivate = function( menuItemPath ) {
	mainMenu = CBPage.MenuGet( JJBW_PAGE_ID_MENU_MAIN );
	subMenu = CBPage.MenuGet( JJBW_PAGE_ID_MENU_SUB );
	var menuPathA = menuItemPath.split( "/" );
	mainMenu.MenuItemActivate( menuPathA[0] );
	if ( menuPathA[1] )	subMenu.MenuItemActivate( menuPathA[1] );
}

/**
 * Window on load event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.MainMenuItemHighlightedEvent = function( menuItemP ) {
	if ( menuItemP.TargetGet() != "_blank" ) {
		this.fRefreshSubmenu = true;
		this._IFrame2Widescreen( menuItemP.UserDataGet( "isWidescreen" ) );
	}
}

/**
 * Window on load event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.SubMenuItemHighlightedEvent = function( menuItemP ) {
	if ( menuItemP.TargetGet() != "_blank" ) {
		this.fRefreshSubmenu = false;
		this._IFrame2Widescreen( menuItemP.UserDataGet( "isWidescreen" ) );
	}
}

/**
 * Window on resize event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.LoadingStatusChanged = function( ready ) {
	if ( window.top != window.self && parent && parent.JJBW_Page ) {
		parent.JJBW_Page.LoadingStatusChanged( ready );
	}
	else {
		if ( this.fFrameLoaderObj ) {
			CB_GFXUtilities.ObjectVisibilitySet( this.fFrameLoaderObj, !ready, true );
		}
		this._IFrameVisibilitySet( ready );
	}
}

/**
 * Property to SET the visibility state of content iframe
 *
 * @access private
 */
de.jj_bw.www.JJBW_Page.prototype._IFrameVisibilitySet = function( visible ) {
	if ( this.fIFrameContentObj ) {
		CB_GFXUtilities.ObjectVisibilitySet( this.fIFrameContentObj, visible, true );
	}
}

/**
 * Property to SET the visibility state of content iframe
 *
 * @access private
 */
de.jj_bw.www.JJBW_Page.prototype._NewsVisibilitySet = function( visible ) {
	if ( this.fFrameNewsinfoObj ) {
		CB_GFXUtilities.ObjectVisibilitySet( this.fFrameNewsinfoObj, visible, true );
	}
}

/**
 * Fixes the height of the newsinfo box on resize
 *
 * @access private
 */
de.jj_bw.www.JJBW_Page.prototype._NewsHeightFix = function() {
	if ( this.fFrameNewsinfoObj ) {
		this.fFrameNewsinfoObj.style.height = "96%";
	}
}

/**
 * Resizes the iframe
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._IFrame2Widescreen = function( enabled ) {
	this.fIsWidescreen = enabled;
	if ( enabled == true ) {
		if ( this.fPageFrameSizeOrg == null ) {
			this.fPageFrameSizeOrg = this.fFrameContentObj.offsetWidth;
			this.fInfoFrameSizeOrg = this.fFrameNewsinfoObj.offsetHeight;
			var newSize = this.fPageFrameSizeOrg + this.fFrameNewsinfoObj.offsetWidth;
			if ( this.fMorphFrameScaleP ) {
				this.LoadingStatusChanged( false );
				this._IFrameLoadTimeout();
				this.fFrameContentObj.style.width = newSize + "px";
				this.fMorphFrameScaleP.Scale( 0, 2, 1, 20 );
			}
			if ( this.fMorphNewsFadeP ) {
				this.fMorphNewsFadeP.Fade( 100, 0, 10, 2 );
			}
		}
	}
	else if ( this.fPageFrameSizeOrg ) {
		if ( this.fMorphFrameScaleP ) {
			this.LoadingStatusChanged( false );
			this._IFrameLoadTimeout();
			this.fFrameContentObj.style.width = this.fPageFrameSizeOrg + "px";
			this.fMorphFrameScaleP.Scale( 0, this.fInfoFrameSizeOrg, 1, 15 );
		}
		if ( this.fMorphNewsFadeP ) {
			this._NewsVisibilitySet( true );
			this.fMorphNewsFadeP.Fade( 0, 100, 10, 1 );
		}
		this.fPageFrameSizeOrg = null
	}
}

/**
 * Window on load event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.WindowOnLoad = function( eventP ) {
	this._MenuInit();
	// Page initialization
	this._PageInit();
	// News creation
	this._NewsInit();
	// Tweak layout (only IE6)
	this._LayoutFramesTweak();
	if ( window.top != window.self && parent && parent.JJBW_Page ) {
		// WSW init
		this._WSWInit();
		// Update top frame
		parent.JJBW_Page.IFrameLoaded( this.fManualControlLoader );
		this.fManualControlLoader = false;
	}
}

/**
 * Window on resize event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.WindowOnResize = function( eventP ) {
	this._LayoutFramesTweak();
}


/**
 * Tweaks the layout
 * only IE6 requires a little tweak
 *
 *
 * @access private
 */
de.jj_bw.www.JJBW_Page.prototype._LayoutFramesTweak = function() {
    if ( CB_Environment.CurrentClientBrowser() & CB_ENVIRONMENT_BROWSER_IDENT_IE6x || CB_Environment.CurrentClientBrowser() & CB_ENVIRONMENT_BROWSER_IDENT_SAFARI ) {
	    if ( this.fFrameHeaderObj && this.fFrameBodyObj && this.fFrameFooterObj ) {
	        // Available height: inner window height - header height - footer height
	        var availableHeight = this.fFrameBodyObj.offsetParent.offsetHeight - this.fFrameHeaderObj.offsetHeight - this.fFrameFooterObj.offsetHeight;
	        this.fFrameBodyObj.style.height = availableHeight + "px";
	    }
    }
    if ( CB_Environment.CurrentClientBrowser() & CB_ENVIRONMENT_BROWSER_IDENT_IE6x ) {
	    if ( this.fPageFrameSizeOrg == null && this.fFrameBodyObj && this.fFrameSubNavObj && this.fFrameContentObj && this.fFrameNewsinfoObj ) {
	    	// Determined the margin values of the content object
	    	var marginOffset = parseInt( CB_CSS.CSSAttributeGet( this.fFrameContentObj, "margin-left", "px" ) ) + parseInt( CB_CSS.CSSAttributeGet( this.fFrameContentObj, "margin-right", "px" ) );
	    	var subnavRightEnd = this.fFrameSubNavObj.offsetLeft + this.fFrameSubNavObj.offsetWidth;
	    	var availableWidth = this.fFrameNewsinfoObj.offsetLeft -  subnavRightEnd - marginOffset;
	    	this.fFrameContentObj.style.width = availableWidth + "px";
	    }
	}
}

/**
 * Window on resize event
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.IFrameLoaded = function( manualControlLoader ) {
	var subMenu = CBPage.MenuGet( JJBW_PAGE_ID_MENU_SUB );
	var mainMenu = CBPage.MenuGet( JJBW_PAGE_ID_MENU_MAIN );
	this._IFrameLoadTimeout( true );
	if ( subMenu && this.fRefreshSubmenu ) {
		subMenu.MenuItemRefresh( true );
	}
	else {
		this.fRefreshSubmenu = true;
	}
	if ( mainMenu != null && subMenu != null ) {
		this.fPreviousMainMenuItem = mainMenu.ClickedItemGet();
		this.fPreviousSubMenuItem = subMenu.ClickedItemGet();
	}
	if ( !manualControlLoader ) this.LoadingStatusChanged( true );
}

/**
 *
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype.IFrameMenuPathSet = function( menuPath ) {
	this.fIFrameMenuPath = menuPath;
}

/**
*
*
* @access public
*/
de.jj_bw.www.JJBW_Page.prototype.IFrameMenuPathGet = function() {
	return this.fIFrameMenuPath;
}


/**
 * Time out control
 *
 * @access public
 */
de.jj_bw.www.JJBW_Page.prototype._IFrameLoadTimeout = function( resetTimeout ) {
	var self = this;
	if ( resetTimeout ) {
		if ( this.fIframeLoadedTimeoutObj ) window.clearTimeout( this.fIframeLoadedTimeoutObj );
	}
	else {
		this.fIframeLoadedTimeoutObj = window.setTimeout( function() { self.IFrameLoaded(); }, JJBW_PAGE_IFRAME_LOAD_TIMEOUT );
	}
}


de.jj_bw.www.JJBW_Page.prototype.SubmitContactForm = function() {
	var success = false;
	var fNameObj = document.getElementById( "fName" );
	var fMailObj = document.getElementById( "fEmail" );
	var fMsgObj = document.getElementById( "fMessage" );
	if ( fNameObj && fMailObj && fMsgObj ) {
		success = true;
		if ( !CB_MailAddress.AddressSyntaxValid( fMailObj.value ) ) {
			fMailObj.className = "contact-error";
			success = false;
		}
		if ( fNameObj.value == "" ) {
			fNameObj.className = "contact-error";
			success = false;
		}
		if ( fMsgObj.value == "" ) {
			fMsgObj.className = "contact-error";
			success = false;
		}
	}
	if ( !success ) {
		var fErrorMsgObj = document.getElementById( "contact-error-message" );
		if ( fErrorMsgObj ) CB_GFXUtilities.ObjectVisibilitySet( fErrorMsgObj, true, true );
	}
	else {
		var fContactForm = document.getElementById( "FContact" );
		if ( fContactForm ) fContactForm.submit();
	}
}


/**
 * Create instance
 */
var JJBW_Page = new de.jj_bw.www.JJBW_Page();
