/**
 * This function creates the toolbars and the layouts and then
 * assembles the panels ion the correct order. It's called from init_js.jsp.
 */

function layItOut(config) {

	var fshHolder = new FSHHint();
	var formHolder = new FormHolder(fshHolder, formName, loadingFormText);
	var navigationHolder = new NavigationHolder(formHolder);
	
	var fshListener = new FSHUpdateListener(fshEvent, fshHolder);
	var prevNextListener = new PrevNextListener(navigationButtonStatusEvent);
	var pageListChangedListener = new PageListChangedListener(pageListChangedEvent, navigationHolder);
	var infoTabListener = new InfoTabListener(infoTabEvent, navigationHolder);
	var formNameListener = new FormNameListener(formNameEvent, config.appName);
	var modalDialogListener = new ModalDialogListener(config.animateDialogs, modalDialogEvent);
	var sessionTimeoutListener = new SessionTimeoutListener(sessionTimeoutEvent);

	var keeperAliver = new KeepAlive(120000, 'keep_alive.jsp'); // 2 mins
	
	var formClearer = new FormClearer(config.animateDialogs,
	                                  config.newCancelButton,
	                                  config.newOKButton);
	var fileSaver = new FileSaver(formHolder);
	var fileOpener = new FileOpener(config.animateDialogs,
	                                config.loadCancelButton,
	                                config.loadImportButton,
	                                config.convertCancelButton,
	                                config.convertOKButton);
	var documentGenerator = new DocumentGenerator(formHolder);

	// Toolbars:
	var inputToolbar = new YAHOO.ext.Toolbar('input_toolbar');
    inputToolbar.addButton({text: config.newButton, tooltip: config.newButtonTip, className: 'newButton', click: formClearer.newFile, scope: formClearer, id: 'inputNewButton'});
//JC    inputToolbar.addButton({text: config.saveButton, tooltip: config.saveButtonTip, className: 'saveButton', click: fileSaver.saveFile, scope: fileSaver, id: 'inputSaveButton'});
//JC    inputToolbar.addButton({text: config.importButton, tooltip: config.importButtonTip, className: 'importButton', click: fileOpener.openFile, scope: fileOpener, id: 'inputImportButton'});
    inputToolbar.addSeparator();
    inputToolbar.addButton({text: config.documentButton, tooltip: config.documentButtonTip, className: 'documentButton', click: documentGenerator.generate, scope: documentGenerator, id: 'inputDocumentButton'});
    // inputToolbar.addButton({text: config.printButton, tooltip: config.printButtonTip, className: 'printButton', click: printReport, id: 'inputPrintButton'});
    inputToolbar.addSeparator();
    prevButton = inputToolbar.addButton({text: config.prevButton, tooltip: config.prevButtonTip, className: 'prevButton', click: formHolder.gotoPrevPage, scope: formHolder, id: 'inputPrevButton'});
    nextButton = inputToolbar.addButton({text: config.nextButton, tooltip: config.nextButtonTip, className: 'nextButton', click: formHolder.gotoNextPage, scope: formHolder, id: 'inputNextButton'});
    inputToolbar.addSeparator();
    inputToolbar.addButton({text: config.infoButton, tooltip: config.infoButtonTip, className: 'infoButton', click: infoTabListener.showInfoForCurrentPage, scope: infoTabListener, id: 'inputInfoButton'});

    var infoToolbar = new YAHOO.ext.Toolbar('info_toolbar');
    infoToolbar.addButton({text: config.printButton, tooltip: config.printButtonTip, className: 'printButton', click: infoTabListener.printInfoTab, scope: infoTabListener, id: 'infoPrintButton'});
    //infoToolbar.addButton({text: config.saveButton, tooltip: config.saveButtonTip, className: 'saveButton', click: saveInfo});

	// Fix: Try to make the info-frame display its document:
	document.getElementById('info-frame').src = document.getElementById('info-frame').src;

	// Layouts:
    var mainLayout = new YAHOO.ext.BorderLayout(document.body, {
        hideOnLayout: true,
        north: {
            split:false,
            initialSize: 75,
            minSize: 0,
            maxSize: 75,
            titlebar: false
        },
        center: {
            tabPosition: 'top',
            resizeTabs: false,
            autoScroll:false,
            alwaysShowTabs: true,
            titlebar: false
        }
    });

   	var topLayout = new YAHOO.ext.BorderLayout('header', {
        hideOnLayout: true,
        west:   { titlebar: false },
        center: { titlebar: false },
        east:   { titlebar: false }
    });

   	var inputLayout = new YAHOO.ext.BorderLayout('input', {
        hideOnLayout: true,
        west:   { 
	        split:true,
        	titlebar: false 
        },
        center: { 
            autoScroll:false,
        	titlebar: false
        }
    });

   	var inputSidebarLayout = new YAHOO.ext.BorderLayout('input_sidebar', {
        hideOnLayout: true,
        center: { titlebar: false},
        south:  {
            split:true,
            initialSize: 250,
            minSize: 50,
            maxSize: 400,
            autoScroll:false,
            collapsible:true,
            animate: true,
            cmargins: { top:2, bottom:0, right:0, left:0 },
            titlebar: true
        }
    });

	// Structure:
    mainLayout.beginUpdate();
	    mainLayout.add('north', new YAHOO.ext.NestedLayoutPanel(topLayout, {}));
	    topLayout.beginUpdate();
		    topLayout.add('west',   new YAHOO.ext.ContentPanel('top_left', {fitToFrame: true}));
		    topLayout.add('center', new YAHOO.ext.ContentPanel('top_center', {fitToFrame: true}));
		    topLayout.add('east',   new YAHOO.ext.ContentPanel('top_right', {fitToFrame: true}));
	    topLayout.endUpdate();
	    
	    mainLayout.add('center', new YAHOO.ext.NestedLayoutPanel(inputLayout, config.inputPageTab));
	    inputLayout.beginUpdate();
		    inputLayout.add('west', new YAHOO.ext.NestedLayoutPanel(inputSidebarLayout, 'input_sidebar_layout'));
		    inputSidebarLayout.beginUpdate();
			    inputSidebarLayout.add('center', new YAHOO.ext.ContentPanel('nav', {fitToFrame: true}));
			    inputSidebarLayout.add('south',  new YAHOO.ext.ContentPanel('fsh', {title: config.fshTitle, fitToFrame: true}));
		    inputSidebarLayout.endUpdate();

    		inputLayout.add('center', new YAHOO.ext.ContentPanel('vif', {toolbar: inputToolbar, fitToFrame: true}));
	    inputLayout.endUpdate();

		// mainLayout.add('center', new YAHOO.ext.ContentPanel('central_storage', {title: config.centralStorageTab, fitToFrame: true}));
		mainLayout.add('center', new YAHOO.ext.ContentPanel('information', {title: config.informationTab, toolbar: infoToolbar, fitToFrame: true}));
		mainLayout.showPanel('input');
		
		// Fix for FF1.5 not loading the info page until the panel has been displayed once.
		// Note: IE7 will fail if we simply show the 'information' panel and set timeout to show 'input' (can't set focus to elements that are not displayed).
		delayedCall(mainLayout, ".showPanel('information')", 1);
		delayedCall(mainLayout, ".showPanel('input')", 10);
		
	mainLayout.endUpdate();

	var keyHandler = new KeyHandler(formHolder, documentGenerator);
	infoTabListener.setMainLayout(mainLayout);
	
	YAHOO.util.Event.addListener(document.getElementById('the_body'), 'keydown', keyHandler.handleKey, keyHandler, true);
	//YAHOO.ext.Element.get('the_body').addKeyListener({key: 37, ctrl: true}, gotoPrevPage);
	//YAHOO.ext.Element.get('the_body').addKeyListener({key: 39, ctrl: true}, gotoNextPage);


}

/** Print button on input toolbar.
function printReport() {
	var printWin = open('print.jsp','','width=500,height=600');
}
*/

